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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#include "llvm/CodeGen/LiveRangeInfo.h"
LiveRangeInfo::LiveRangeInfo(const Method *const M,
const TargetMachine& tm,
vector<RegClass *> &RCL)
: Meth(M), LiveRangeMap(),
TM(tm), RegClassList(RCL),
MRI( tm.getRegInfo()),
CallRetInstrList()
{ }
// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
// Note: the caller must make sure that L1 and L2 are distinct and both
// LRs don't have suggested colors
void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2)
{
assert( L1 != L2);
L1->setUnion( L2 ); // add elements of L2 to L1
ValueSet::iterator L2It;
for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) {
//assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
L1->add( *L2It ); // add the var in L2 to L1
LiveRangeMap[ *L2It ] = L1; // now the elements in L2 should map to L1
}
// Now if LROfDef(L1) has a suggested color, it will remain.
// But, if LROfUse(L2) has a suggested color, the new range
// must have the same color.
if(L2->hasSuggestedColor())
L1->setSuggestedColor( L2->getSuggestedColor() );
delete ( L2 ); // delete L2 as it is no longer needed
}
void LiveRangeInfo::constructLiveRanges()
{
if( DEBUG_RA)
cout << "Consturcting Live Ranges ..." << endl;
// first find the live ranges for all incoming args of the method since
// those LRs start from the start of the method
// get the argument list
const Method::ArgumentListType& ArgList = Meth->getArgumentList();
// get an iterator to arg list
Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
LiveRange * ArgRange = new LiveRange(); // creates a new LR and
const Value *const Val = (const Value *) *ArgIt;
assert( Val);
ArgRange->add( Val ); // add the arg (def) to it
LiveRangeMap[ Val ] = ArgRange;
// create a temp machine op to find the register class of value
//const MachineOperand Op(MachineOperand::MO_VirtualRegister);
unsigned rcid = MRI.getRegClassIDOfValue( Val );
ArgRange->setRegClass(RegClassList[ rcid ] );
if( DEBUG_RA > 1) {
cout << " adding LiveRange for argument ";
printValue( (const Value *) *ArgIt); cout << endl;
}
}
// Now suggest hardware registers for these method args
MRI.suggestRegs4MethodArgs(Meth, *this);
// Now find speical LLVM instructions (CALL, RET) and LRs in machine
// instructions.
Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
// Now find all LRs for machine the instructions. A new LR will be created
// only for defs in the machine instr since, we assume that all Values are
// defined before they are used. However, there can be multiple defs for
// the same Value in machine instructions.
// get the iterator for machine instructions
const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
MachineCodeForBasicBlock::const_iterator
MInstIterator = MIVec.begin();
// iterate over all the machine instructions in BB
for( ; MInstIterator != MIVec.end(); MInstIterator++) {
const MachineInstr * MInst = *MInstIterator;
// Now if the machine instruction has special operands that must be
// set with a "suggested color", do it here.
if( MRI.handleSpecialMInstr(MInst, *this, RegClassList) )
continue;
// iterate over MI operands to find defs
for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
// delete later from here ************
MachineOperand::MachineOperandType OpTyp =
OpI.getMachineOperand().getOperandType();
if (DEBUG_RA && OpTyp == MachineOperand::MO_CCRegister) {
cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
printValue( OpI.getMachineOperand().getVRegValue() );
cout << endl;
}
// ************* to here
// create a new LR iff this operand is a def
if( OpI.isDef() ) {
const Value *const Def = *OpI;
// Only instruction values are accepted for live ranges here
if( Def->getValueType() != Value::InstructionVal ) {
cout << "\n**%%Error: Def is not an instruction val. Def=";
printValue( Def ); cout << endl;
continue;
}
LiveRange *DefRange = LiveRangeMap[Def];
// see LR already there (because of multiple defs)
if( !DefRange) { // if it is not in LiveRangeMap
DefRange = new LiveRange(); // creates a new live range and
DefRange->add( Def ); // add the instruction (def) to it
LiveRangeMap[ Def ] = DefRange; // update the map
if( DEBUG_RA > 1) {
cout << " creating a LR for def: ";
printValue(Def); cout << endl;
}
// set the register class of the new live range
//assert( RegClassList.size() );
MachineOperand::MachineOperandType OpTy =
OpI.getMachineOperand().getOperandType();
bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
unsigned rcid = MRI.getRegClassIDOfValue(
OpI.getMachineOperand().getVRegValue(), isCC );
if(isCC && DEBUG_RA) {
cout << "\a**created a LR for a CC reg:";
printValue( OpI.getMachineOperand().getVRegValue() );
}
DefRange->setRegClass( RegClassList[ rcid ] );
}
else {
DefRange->add( Def ); // add the opearand to def range
// update the map - Operand points
// to the merged set
LiveRangeMap[ Def ] = DefRange;
if( DEBUG_RA > 1) {
cout << " added to an existing LR for def: ";
printValue( Def ); cout << endl;
}
}
} // if isDef()
} // for all opereands in machine instructions
} // for all machine instructions in the BB
} // for all BBs in method
// go thru LLVM instructions in the basic block and suggest colors
// for their args. Also record all CALL
// instructions and Return instructions in the CallRetInstrList
// This is done because since there are no reverse pointers in machine
// instructions to find the llvm instruction, when we encounter a call
// or a return whose args must be specailly colored (e.g., %o's for args)
// We have to makes sure that all LRs of call/ret args are added before
// doing this. But return value of call will not have a LR.
BBI = Meth->begin(); // random iterator for BBs
for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
BasicBlock::const_iterator InstIt = (*BBI)->begin();
for( ; InstIt != (*BBI)->end() ; ++InstIt) {
const Instruction *const CallRetI = *InstIt;
unsigned OpCode = (CallRetI)->getOpcode();
if( (OpCode == Instruction::Call) ) {
CallRetInstrList.push_back(CallRetI );
MRI.suggestRegs4CallArgs( (CallInst *) CallRetI, *this, RegClassList );
}
else if (OpCode == Instruction::Ret ) {
CallRetInstrList.push_back( CallRetI );
MRI.suggestReg4RetValue( (ReturnInst *) CallRetI, *this);
}
} // for each llvm instr in BB
} // for all BBs in method
if( DEBUG_RA)
cout << "Initial Live Ranges constructed!" << endl;
}
void LiveRangeInfo::coalesceLRs()
{
/* Algorithm:
for each BB in method
for each machine instruction (inst)
for each definition (def) in inst
for each operand (op) of inst that is a use
if the def and op are of the same type
if the def and op do not interfere //i.e., not simultaneously live
if (degree(LR of def) + degree(LR of op)) <= # avail regs
if both LRs do not have suggested colors
merge2IGNodes(def, op) // i.e., merge 2 LRs
*/
if( DEBUG_RA)
cout << endl << "Coalscing LRs ..." << endl;
Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
// get the iterator for machine instructions
const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
MachineCodeForBasicBlock::const_iterator
MInstIterator = MIVec.begin();
// iterate over all the machine instructions in BB
for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
const MachineInstr * MInst = *MInstIterator;
if( DEBUG_RA > 1) {
cout << " *Iterating over machine instr ";
MInst->dump();
cout << endl;
}
// iterate over MI operands to find defs
for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
if( DefI.isDef() ) { // iff this operand is a def
LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
assert( LROfDef );
RegClass *const RCOfDef = LROfDef->getRegClass();
MachineInstr::val_op_const_iterator UseI(MInst);
for( ; !UseI.done(); ++UseI){ // for all uses
LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
if( ! LROfUse ) { // if LR of use is not found
//don't warn about labels
if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
cout<<" !! Warning: No LR for use "; printValue(*UseI);
cout << endl;
}
continue; // ignore and continue
}
if( LROfUse == LROfDef) // nothing to merge if they are same
continue;
// RegClass *const RCOfUse = LROfUse->getRegClass();
//if( RCOfDef == RCOfUse ) { // if the reg classes are the same
if( LROfUse->getTypeID() == LROfDef->getTypeID() ) {
if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
unsigned CombinedDegree =
LROfDef->getUserIGNode()->getNumOfNeighbors() +
LROfUse->getUserIGNode()->getNumOfNeighbors();
if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
// if both LRs do not have suggested colors
if( ! (LROfDef->hasSuggestedColor() &&
LROfUse->hasSuggestedColor() ) ) {
RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
unionAndUpdateLRs(LROfDef, LROfUse);
}
} // if combined degree is less than # of regs
} // if def and use do not interfere
} // if reg classes are the same
} // for all uses
} // if def
} // for all defs
} // for all machine instructions
} // for all BBs
if( DEBUG_RA)
cout << endl << "Coalscing Done!" << endl;
}
/*--------------------------- Debug code for printing ---------------*/
void LiveRangeInfo::printLiveRanges()
{
LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
cout << endl << "Printing Live Ranges from Hash Map:" << endl;
for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
if( (*HMI).first && (*HMI).second ) {
cout <<" "; printValue((*HMI).first); cout << "\t: ";
((*HMI).second)->printSet(); cout << endl;
}
}
}
|