Skip to content

Commit 2a6cc83

Browse files
author
Ruchira Sasanka
committed
updated suggesting/coloring of call & return args & implicit operands.
Changed added instr to a deque (from a vector) llvm-svn: 831
1 parent 086bf0f commit 2a6cc83

File tree

2 files changed

+147
-70
lines changed

2 files changed

+147
-70
lines changed

llvm/lib/CodeGen/RegAlloc/LiveRangeInfo.cpp

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,27 @@ void LiveRangeInfo::constructLiveRanges()
108108

109109
const MachineInstr * MInst = *MInstIterator;
110110

111-
// Now if the machine instruction has special operands that must be
112-
// set with a "suggested color", do it here.
113-
// This will be true for call/return instructions
111+
// Now if the machine instruction is a call/return instruction,
112+
// add it to CallRetInstrList for processing its implicit operands
114113

115-
116-
if( MRI.handleSpecialMInstr(MInst, *this, RegClassList) )
117-
continue;
118-
119-
114+
if( (TM.getInstrInfo()).isReturn( MInst->getOpCode()) ||
115+
(TM.getInstrInfo()).isCall( MInst->getOpCode()) )
116+
CallRetInstrList.push_back( MInst );
117+
118+
120119
// iterate over MI operands to find defs
121120
for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
122121

123-
124-
// delete later from here ************
125-
MachineOperand::MachineOperandType OpTyp =
126-
OpI.getMachineOperand().getOperandType();
127-
128-
if (DEBUG_RA && OpTyp == MachineOperand::MO_CCRegister) {
129-
cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
130-
printValue( OpI.getMachineOperand().getVRegValue() );
131-
cout << endl;
122+
if( DEBUG_RA) {
123+
MachineOperand::MachineOperandType OpTyp =
124+
OpI.getMachineOperand().getOperandType();
125+
126+
if ( OpTyp == MachineOperand::MO_CCRegister) {
127+
cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
128+
printValue( OpI.getMachineOperand().getVRegValue() );
129+
cout << endl;
130+
}
132131
}
133-
// ************* to here
134-
135132

136133
// create a new LR iff this operand is a def
137134
if( OpI.isDef() ) {
@@ -193,60 +190,56 @@ void LiveRangeInfo::constructLiveRanges()
193190
}
194191
}
195192

196-
197-
198-
199193
} // if isDef()
200194

201195
} // for all opereands in machine instructions
202196

203197
} // for all machine instructions in the BB
204198

205-
206199
} // for all BBs in method
207200

208-
// go thru LLVM instructions in the basic block and suggest colors
209-
// for their args. Also record all CALL
210-
// instructions and Return instructions in the CallRetInstrList
211-
// This is done because since there are no reverse pointers in machine
212-
// instructions to find the llvm instruction, when we encounter a call
213-
// or a return whose args must be specailly colored (e.g., %o's for args)
214-
// We have to makes sure that all LRs of call/ret args are added before
215-
// doing this. But return value of call will not have a LR.
216201

217-
BBI = Meth->begin(); // random iterator for BBs
202+
// Now we have to suggest clors for call and return arg live ranges.
203+
// Also, if there are implicit defs (e.g., retun value of a call inst)
204+
// they must be added to the live range list
218205

219-
for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
206+
suggestRegs4CallRets();
220207

221-
BasicBlock::const_iterator InstIt = (*BBI)->begin();
208+
if( DEBUG_RA)
209+
cout << "Initial Live Ranges constructed!" << endl;
222210

223-
for( ; InstIt != (*BBI)->end() ; ++InstIt) {
211+
}
224212

225-
const Instruction *const CallRetI = *InstIt;
226-
unsigned OpCode = (CallRetI)->getOpcode();
227-
228-
if( (OpCode == Instruction::Call) ) {
229-
CallRetInstrList.push_back(CallRetI );
230-
MRI.suggestRegs4CallArgs( (CallInst *) CallRetI, *this, RegClassList );
231-
}
232213

233-
else if (OpCode == Instruction::Ret ) {
234-
CallRetInstrList.push_back( CallRetI );
235-
MRI.suggestReg4RetValue( (ReturnInst *) CallRetI, *this);
236-
}
237214

215+
// Suggest colors for call and return args.
216+
// Also create new LRs for implicit defs
238217

239-
} // for each llvm instr in BB
218+
void LiveRangeInfo::suggestRegs4CallRets()
219+
{
240220

241-
} // for all BBs in method
221+
CallRetInstrListType::const_iterator It = CallRetInstrList.begin();
242222

243-
if( DEBUG_RA)
244-
cout << "Initial Live Ranges constructed!" << endl;
223+
for( ; It != CallRetInstrList.end(); ++It ) {
224+
225+
const MachineInstr *MInst = *It;
226+
MachineOpCode OpCode = MInst->getOpCode();
227+
228+
if( (TM.getInstrInfo()).isReturn(OpCode) )
229+
MRI.suggestReg4RetValue( MInst, *this);
230+
231+
else if( (TM.getInstrInfo()).isCall( OpCode ) )
232+
MRI.suggestRegs4CallArgs( MInst, *this, RegClassList );
233+
234+
else
235+
assert( 0 && "Non call/ret instr in CallRetInstrList" );
236+
}
245237

246238
}
247239

248240

249241

242+
250243
void LiveRangeInfo::coalesceLRs()
251244
{
252245

@@ -318,8 +311,6 @@ void LiveRangeInfo::coalesceLRs()
318311

319312
if( RCOfDef == RCOfUse ) { // if the reg classes are the same
320313

321-
// if( LROfUse->getTypeID() == LROfDef->getTypeID() ) {
322-
323314
if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
324315

325316
unsigned CombinedDegree =

llvm/lib/CodeGen/RegAlloc/PhyRegAlloc.cpp

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,100 @@ void PhyRegAlloc::addInterferencesForArgs()
248248
}
249249

250250

251+
#if 0
252+
//----------------------------------------------------------------------------
253+
254+
//----------------------------------------------------------------------------
255+
256+
257+
void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst,
258+
const BasicBlock *BB )
259+
{
260+
assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) );
261+
262+
int StackOff = 10; // ****TODO : Change
263+
set<unsigned> PushedRegSet();
264+
265+
// Now find the LR of the return value of the call
266+
// The last *implicit operand* is the return value of a call
267+
// Insert it to to he PushedRegSet since we must not save that register
268+
// and restore it after the call.
269+
// We do this because, we look at the LV set *after* the instruction
270+
// to determine, which LRs must be saved across calls. The return value
271+
// of the call is live in this set - but we must not save/restore it.
272+
273+
unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
274+
if( NumOfImpRefs > 0 ) {
275+
276+
if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) {
277+
278+
const Value *RetVal = CallMI->getImplicitRef(NumOfImpRefs-1);
279+
LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
280+
assert( RetValLR && "No LR for RetValue of call");
281+
282+
PushedRegSet.insert(
283+
MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(),
284+
RetValLR->getColor() ) );
285+
}
286+
287+
}
288+
289+
290+
LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB);
291+
292+
LiveVarSet::const_iterator LIt = LVSetAft->begin();
293+
294+
// for each live var in live variable set after machine inst
295+
for( ; LIt != LVSetAft->end(); ++LIt) {
296+
297+
// get the live range corresponding to live var
298+
LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
299+
300+
// LROfVar can be null if it is a const since a const
301+
// doesn't have a dominating def - see Assumptions above
302+
if( LR ) {
303+
304+
if( LR->hasColor() ) {
305+
306+
unsigned RCID = (LR->getRegClass())->getID();
307+
unsigned Color = LR->getColor();
308+
309+
if ( MRI.isRegVolatile(RCID, Color) ) {
310+
311+
// if the value is in both LV sets (i.e., live before and after
312+
// the call machine instruction)
313+
314+
unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
315+
316+
if( PuhsedRegSet.find(Reg) == PhusedRegSet.end() ) {
317+
318+
// if we haven't already pushed that register
319+
320+
MachineInstr *AdI =
321+
MRI.saveRegOnStackMI(Reg, MRI.getFPReg(), StackOff );
322+
323+
((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdI);
324+
((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdI);
325+
326+
327+
PushedRegSet.insert( Reg );
328+
StackOff += 4; // ****TODO: Correct ??????
329+
cout << "Inserted caller saving instr");
330+
331+
} // if not already pushed
332+
333+
} // if LR has a volatile color
334+
335+
} // if LR has color
336+
337+
} // if there is a LR for Var
338+
339+
} // for each value in the LV set after instruction
340+
341+
}
342+
343+
#endif
344+
251345
//----------------------------------------------------------------------------
252346
// This method is called after register allocation is complete to set the
253347
// allocated reisters in the machine code. This code will add register numbers
@@ -275,12 +369,12 @@ void PhyRegAlloc::updateMachineCode()
275369
// ***TODO: Add InstrnsAfter as well
276370
if( AddedInstrMap[ MInst ] ) {
277371

278-
vector<MachineInstr *> &IBef =
372+
deque<MachineInstr *> &IBef =
279373
(AddedInstrMap[MInst])->InstrnsBefore;
280374

281375
if( ! IBef.empty() ) {
282376

283-
vector<MachineInstr *>::iterator AdIt;
377+
deque<MachineInstr *>::iterator AdIt;
284378

285379
for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
286380

@@ -331,7 +425,8 @@ void PhyRegAlloc::updateMachineCode()
331425
cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
332426
}
333427

334-
Op.setRegForValue( 1000 ); // mark register as invalid
428+
if( Op.getAllocatedRegNum() == -1)
429+
Op.setRegForValue( 1000 ); // mark register as invalid
335430

336431
#if 0
337432
if( ((Val->getType())->isLabelType()) ||
@@ -475,31 +570,22 @@ void PhyRegAlloc::colorCallRetArgs()
475570

476571
for( ; It != CallRetInstList.end(); ++It ) {
477572

478-
const Instruction *const CallRetI = *It;
479-
unsigned OpCode = (CallRetI)->getOpcode();
573+
const MachineInstr *const CRMI = *It;
574+
unsigned OpCode = CRMI->getOpCode();
480575

481-
const MachineInstr *CRMI = *((CallRetI->getMachineInstrVec()).begin());
482-
483-
484-
assert( (TM.getInstrInfo().isReturn(CRMI->getOpCode()) ||
485-
TM.getInstrInfo().isCall(CRMI->getOpCode()) )
486-
&& "First Machine Instruction is not a Call/Retrunr" );
487-
488576
// get the added instructions for this Call/Ret instruciton
489577
AddedInstrns *AI = AddedInstrMap[ CRMI ];
490578
if ( !AI ) {
491579
AI = new AddedInstrns();
492580
AddedInstrMap[ CRMI ] = AI;
493581
}
494582

495-
if( (OpCode == Instruction::Call) )
496-
MRI.colorCallArgs( (CallInst *) CallRetI, LRI, AI );
583+
if( (TM.getInstrInfo()).isCall( OpCode ) )
584+
MRI.colorCallArgs( CRMI, LRI, AI );
497585

498-
499-
else if (OpCode == Instruction::Ret )
500-
MRI.colorRetValue( (ReturnInst *) CallRetI, LRI, AI );
586+
else if ( (TM.getInstrInfo()).isReturn(OpCode) )
587+
MRI.colorRetValue( CRMI, LRI, AI );
501588

502-
503589
else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
504590

505591
}

0 commit comments

Comments
 (0)