-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleave.cc
More file actions
327 lines (274 loc) · 6.74 KB
/
Copy pathcleave.cc
File metadata and controls
327 lines (274 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*====================================================================
* Cleave limits the size of a basic block based on a predicate
* function. The defualt predicate limits the size based on the number
* of instructions. Blocks are split from the bottom up until it is
* decided that they do not need any more splitting.
*
* Variable type meanings:
* arn - Arena
* in - Inst*
* blk - Block*
* ex - Expr
* lbl - Label
* edg - Edge*
*
*====================================================================
* $Id: cleave.c 159 2006-07-27 16:04:36Z dmp $
* $URL: http://dmpots.com/svn/research/compilers/regalloc/src/cleave.c $
********************************************************************/
#include <Shared.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <list>
#include "cleave.h"
#include "cfg_tools.h"
//file local variables
static Unsigned_Int cinLast = 0;
static Inst* (*fnCleavePred)(Block*) = NULL;
static Arena arnCleave = NULL;
//file local functions
static Inst* In_CleavePred(Block* blk);
static Block* Blk_CleaveBlock(Block* blk, Inst* in);
static BlockTuple Blk2_CleaveBlockAt(Inst* in, Block* blk);
static Inst* MoveInstTo(Inst* in, Block* blk);
static void RemoveInstFromBlock(Inst* in);
static void InsertInstInsertAfter(Inst* inInsert, Inst* inAfter);
static void FixControlFlow(Block* blkTop, Block* blkBot);
static Inst* In_FirstInst(Block* blk);
/*
*===============
* InitCleaver()
*===============
*
***/
void InitCleaver(Arena arn, Unsigned_Int cinLastT)
{
fnCleavePred = &In_CleavePred;
cinLast = cinLastT;
arnCleave = arn;
InitCFGTools(arn);
assert(cinLast != 1);//min 2 inst: 1 inst + 1 control flow
}
/*
*===============
* In_CleavePred()
*===============
*
***/
Inst* In_CleavePred(Block* blk)
{
Inst* in = NULL;
Inst* inCleaveHere = NULL;
Unsigned_Int cin = 0;
Block_ForAllInstsReverse(in, blk)
{
cin++;
if(cin == cinLast)
{
if(in != In_FirstInst(blk))
{
inCleaveHere = in;
break;
}
}
}
return inCleaveHere;
}
/*
*===============
* CleaveBlocks()
*===============
*
***/
void CleaveBlocks()
{
//do not split if 0 is passed as max number of insts
if(cinLast == 0) return;
CleaveBlocksWithPred(NULL);
}
/*
*========================
* CleaveBlocksWithPred()
*========================
*
***/
void CleaveBlocksWithPred(Inst* (*fnPred)(Block*))
{
if(fnPred != NULL)
{
fnCleavePred = fnPred;
}
assert(fnCleavePred);
Block* blk;
Inst* in;
std::list<Block*> lstblk;
ForAllBlocks(blk)
{
if((in = fnCleavePred(blk)))
{
lstblk.push_back(blk);
//blkT = Blk_CleaveBlock(blk, in);
//fix the loop variable
//blk = blkT;
}
}
//work through list
std::list<Block*>::iterator itblk;
for(itblk = lstblk.begin(); itblk != lstblk.end(); itblk++)
{
blk = *itblk;
debug("processing block: %s", bname(blk));
in = fnCleavePred(blk);
(void) Blk_CleaveBlock(blk, in);
}
Block_Order(); //fix control flow graph
}
/*
*========================
* Blk_CleaveBlk()
*========================
* cleaves a block repeatedly starting at Inst in unitl the block no
* longer needs to be split as determined by the predicate function
***/
Block* Blk_CleaveBlock(Block* blk, Inst* in)
{
Block* blkExit;
BlockTuple blk2;
blk2 = Blk2_CleaveBlockAt(in, blk);
blkExit = blk2.bottom;
blk = blk2.top;
while((in = fnCleavePred(blk)))
{
//cleave until the block should be clove no more
blk2 = Blk2_CleaveBlockAt(in, blk);
blk = blk2.top;
}
return blkExit;
}
/*
*========================
* Blk_CleaveBlkAt()
*========================
* cleaves the block from the bottom up to inLast
***/
BlockTuple Blk2_CleaveBlockAt(Inst* inLast, Block* blk)
{
Inst* in;
Inst* inIterFix;
Block* blkNew = CreateEmptyBlock();
debug("cleaving block: %s(%d)", bname(blk), bid(blk));
//walk up the insts backward until arriving at the clip point
Block_ForAllInstsReverse(in, blk)
{
debug("Move %s from %s to %s",
opcode_specs[in->operations[0]->opcode].opcode,
bname(blk), bname(blkNew));
inIterFix = MoveInstTo(in, blkNew);
if(in == inLast)
{
//insert control flow to new block
FixControlFlow(blk, blkNew);
break;
}
in = inIterFix;
}
BlockTuple blk2;
blk2.top = blk;
blk2.bottom = blkNew;
return blk2;
}
/*
*========================
* MoveInstFromTo()
*========================
*
***/
Inst* MoveInstTo(Inst* in, Block* blk)
{
//save so can continue iteration from the previous inst
//since we are going bottom up we need to grab the next inst
Inst* inNext = in->next_inst;
RemoveInstFromBlock(in);
//insert after the first instruction since we are moving the
//instructions over from the bottom up
InsertInstInsertAfter(in, blk->inst);
return inNext;
}
/*
*========================
* RemoveInstFromBlock()
*========================
*
***/
void RemoveInstFromBlock(Inst* in)
{
in->next_inst->prev_inst = in->prev_inst;
in->prev_inst->next_inst = in->next_inst;
}
/*
*========================
* FixControlFlow()
*========================
*
* 1)
***/
void FixControlFlow(Block* blkTop, Block* blkBot)
{
debug("fixing control flow: %s ==> %s", bname(blkTop), bname(blkBot));
//bottom block edges
Edge* edgBotPred = (Edge*)Arena_GetMem(arnCleave, sizeof(Edge));
edgBotPred->pred = blkTop;
edgBotPred->succ = blkBot;
edgBotPred->next_pred = NULL;
edgBotPred->next_succ = NULL;
edgBotPred->back_edge = FALSE;
edgBotPred->edge_extension = NULL;
blkBot->pred = edgBotPred;
blkBot->succ = blkTop->succ; //previous whole block succ list
//fix up the succ edges of the top block by moving them to the
//bottom block. then fix up the pred edges for all succsors of the
//top to point to the new bottom
Edge* edg;
Edge* edgT;
Block_ForAllSuccs(edg, blkTop)
{
edg->pred = blkBot; //new predecessor is the bottom block
Block_ForAllPreds(edgT, edg->succ)
{
if(edgT->pred == blkTop)
{
edgT->pred = blkBot;
}
}
}
//top block edges
InsertJumpFromTo(blkTop, blkBot);
Edge* edgTopSucc = (Edge*)Arena_GetMem(arnCleave, sizeof(Edge));
edgTopSucc->pred = blkTop;
edgTopSucc->succ = blkBot;
edgTopSucc->next_pred = NULL;
edgTopSucc->next_succ = NULL;
edgTopSucc->back_edge = FALSE;
edgTopSucc->edge_extension = NULL;
//blkTop->pred does not change
blkTop->succ = edgTopSucc;
}
/*
*========================
* InsertInstInsertAfter()
*========================
*
***/
void InsertInstInsertAfter(Inst* inInsert, Inst* inAfter)
{
Block_Insert_Instruction(inInsert, inAfter->next_inst);
}
Expr Ex_ExprFromLabel(Label* plbl)
{
return plbl->label;
}
Inst* In_FirstInst(Block* blk)
{
return blk->inst->next_inst;
}