1 //===-- PhyRegAlloc.cpp ---------------------------------------------------===//
3 // Register allocation for LLVM.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/CodeGen/RegisterAllocation.h"
8 #include "llvm/CodeGen/RegAllocCommon.h"
9 #include "llvm/CodeGen/IGNode.h"
10 #include "llvm/CodeGen/PhyRegAlloc.h"
11 #include "llvm/CodeGen/MachineInstr.h"
12 #include "llvm/CodeGen/MachineInstrAnnot.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Target/MachineFrameInfo.h"
18 #include "llvm/Target/MachineInstrInfo.h"
19 #include "llvm/Function.h"
20 #include "llvm/Type.h"
21 #include "llvm/iOther.h"
22 #include "Support/STLExtras.h"
23 #include "Support/CommandLine.h"
28 RegAllocDebugLevel_t DEBUG_RA;
30 static cl::opt<RegAllocDebugLevel_t, true>
31 DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
32 cl::desc("enable register allocation debugging information"),
34 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
35 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
36 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
37 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
38 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
39 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
42 //----------------------------------------------------------------------------
43 // RegisterAllocation pass front end...
44 //----------------------------------------------------------------------------
46 class RegisterAllocator : public FunctionPass {
47 TargetMachine &Target;
49 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
51 const char *getPassName() const { return "Register Allocation"; }
53 bool runOnFunction(Function &F) {
55 cerr << "\n********* Function "<< F.getName() << " ***********\n";
57 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
58 &getAnalysis<LoopInfo>());
59 PRA.allocateRegisters();
61 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.addRequired<LoopInfo>();
67 AU.addRequired<FunctionLiveVarInfo>();
72 Pass *getRegisterAllocator(TargetMachine &T) {
73 return new RegisterAllocator(T);
76 //----------------------------------------------------------------------------
77 // Constructor: Init local composite objects and create register classes.
78 //----------------------------------------------------------------------------
79 PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
80 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
81 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
82 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
83 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
85 // create each RegisterClass and put in RegClassList
87 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
88 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
93 //----------------------------------------------------------------------------
94 // Destructor: Deletes register classes
95 //----------------------------------------------------------------------------
96 PhyRegAlloc::~PhyRegAlloc() {
97 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
98 delete RegClassList[rc];
100 AddedInstrMap.clear();
103 //----------------------------------------------------------------------------
104 // This method initally creates interference graphs (one in each reg class)
105 // and IGNodeList (one in each IG). The actual nodes will be pushed later.
106 //----------------------------------------------------------------------------
107 void PhyRegAlloc::createIGNodeListsAndIGs() {
108 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
111 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
114 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
116 for (; HMI != HMIEnd ; ++HMI ) {
118 LiveRange *L = HMI->second; // get the LiveRange
121 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
122 << RAV(HMI->first) << "****\n";
126 // if the Value * is not null, and LR is not yet written to the IGNodeList
127 if (!(L->getUserIGNode()) ) {
128 RegClass *const RC = // RegClass of first value in the LR
129 RegClassList[ L->getRegClass()->getID() ];
130 RC->addLRToIG(L); // add this LR to an IG
136 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
137 RegClassList[rc]->createInterferenceGraph();
139 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
143 //----------------------------------------------------------------------------
144 // This method will add all interferences at for a given instruction.
145 // Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
146 // class as that of live var. The live var passed to this function is the
147 // LVset AFTER the instruction
148 //----------------------------------------------------------------------------
150 void PhyRegAlloc::addInterference(const Value *Def,
151 const ValueSet *LVSet,
154 ValueSet::const_iterator LIt = LVSet->begin();
156 // get the live range of instruction
158 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
160 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
161 assert( IGNodeOfDef );
163 RegClass *const RCOfDef = LROfDef->getRegClass();
165 // for each live var in live variable set
167 for ( ; LIt != LVSet->end(); ++LIt) {
169 if (DEBUG_RA >= RA_DEBUG_Verbose)
170 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
172 // get the live range corresponding to live var
174 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
176 // LROfVar can be null if it is a const since a const
177 // doesn't have a dominating def - see Assumptions above
180 if (LROfDef != LROfVar) // do not set interf for same LR
181 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
182 RCOfDef->setInterference( LROfDef, LROfVar);
188 //----------------------------------------------------------------------------
189 // For a call instruction, this method sets the CallInterference flag in
190 // the LR of each variable live int the Live Variable Set live after the
191 // call instruction (except the return value of the call instruction - since
192 // the return value does not interfere with that call itself).
193 //----------------------------------------------------------------------------
195 void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
196 const ValueSet *LVSetAft) {
198 if (DEBUG_RA >= RA_DEBUG_Interference)
199 cerr << "\n For call inst: " << *MInst;
201 ValueSet::const_iterator LIt = LVSetAft->begin();
203 // for each live var in live variable set after machine inst
205 for ( ; LIt != LVSetAft->end(); ++LIt) {
207 // get the live range corresponding to live var
209 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
211 // LR can be null if it is a const since a const
212 // doesn't have a dominating def - see Assumptions above
215 if (DEBUG_RA >= RA_DEBUG_Interference) {
216 cerr << "\n\tLR after Call: ";
219 LR->setCallInterference();
220 if (DEBUG_RA >= RA_DEBUG_Interference) {
221 cerr << "\n ++After adding call interference for LR: " ;
228 // Now find the LR of the return value of the call
229 // We do this because, we look at the LV set *after* the instruction
230 // to determine, which LRs must be saved across calls. The return value
231 // of the call is live in this set - but it does not interfere with call
232 // (i.e., we can allocate a volatile register to the return value)
234 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
236 if (const Value *RetVal = argDesc->getReturnValue()) {
237 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
238 assert( RetValLR && "No LR for RetValue of call");
239 RetValLR->clearCallInterference();
242 // If the CALL is an indirect call, find the LR of the function pointer.
243 // That has a call interference because it conflicts with outgoing args.
244 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
245 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
246 assert( AddrValLR && "No LR for indirect addr val of call");
247 AddrValLR->setCallInterference();
255 //----------------------------------------------------------------------------
256 // This method will walk thru code and create interferences in the IG of
257 // each RegClass. Also, this method calculates the spill cost of each
258 // Live Range (it is done in this method to save another pass over the code).
259 //----------------------------------------------------------------------------
260 void PhyRegAlloc::buildInterferenceGraphs()
263 if (DEBUG_RA >= RA_DEBUG_Interference)
264 cerr << "Creating interference graphs ...\n";
266 unsigned BBLoopDepthCost;
267 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
269 const MachineBasicBlock &MBB = *BBI;
270 const BasicBlock *BB = MBB.getBasicBlock();
272 // find the 10^(loop_depth) of this BB
274 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
276 // get the iterator for machine instructions
278 MachineBasicBlock::const_iterator MII = MBB.begin();
280 // iterate over all the machine instructions in BB
282 for ( ; MII != MBB.end(); ++MII) {
283 const MachineInstr *MInst = *MII;
285 // get the LV set after the instruction
287 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
288 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
291 // set the isCallInterference flag of each live range wich extends
292 // accross this call instruction. This information is used by graph
293 // coloring algo to avoid allocating volatile colors to live ranges
294 // that span across calls (since they have to be saved/restored)
296 setCallInterferences(MInst, &LVSetAI);
299 // iterate over all MI operands to find defs
301 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
302 OpE = MInst->end(); OpI != OpE; ++OpI) {
303 if (OpI.isDef()) // create a new LR iff this operand is a def
304 addInterference(*OpI, &LVSetAI, isCallInst);
306 // Calculate the spill cost of each live range
308 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
309 if (LR) LR->addSpillCost(BBLoopDepthCost);
313 // if there are multiple defs in this instruction e.g. in SETX
315 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
316 addInterf4PseudoInstr(MInst);
319 // Also add interference for any implicit definitions in a machine
320 // instr (currently, only calls have this).
322 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
323 if ( NumOfImpRefs > 0 ) {
324 for (unsigned z=0; z < NumOfImpRefs; z++)
325 if (MInst->implicitRefIsDefined(z) )
326 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
330 } // for all machine instructions in BB
331 } // for all BBs in function
334 // add interferences for function arguments. Since there are no explict
335 // defs in the function for args, we have to add them manually
337 addInterferencesForArgs();
339 if (DEBUG_RA >= RA_DEBUG_Interference)
340 cerr << "Interference graphs calculated!\n";
345 //--------------------------------------------------------------------------
346 // Pseudo instructions will be exapnded to multiple instructions by the
347 // assembler. Consequently, all the opernds must get distinct registers.
348 // Therefore, we mark all operands of a pseudo instruction as they interfere
350 //--------------------------------------------------------------------------
351 void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
353 bool setInterf = false;
355 // iterate over MI operands to find defs
357 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
358 ItE = MInst->end(); It1 != ItE; ++It1) {
359 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
360 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
362 MachineInstr::const_val_op_iterator It2 = It1;
363 for (++It2; It2 != ItE; ++It2) {
364 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
367 RegClass *RCOfOp1 = LROfOp1->getRegClass();
368 RegClass *RCOfOp2 = LROfOp2->getRegClass();
370 if (RCOfOp1 == RCOfOp2 ){
371 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
375 } // for all other defs in machine instr
376 } // for all operands in an instruction
378 if (!setInterf && MInst->getNumOperands() > 2) {
379 cerr << "\nInterf not set for any operand in pseudo instr:\n";
381 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
387 //----------------------------------------------------------------------------
388 // This method will add interferences for incoming arguments to a function.
389 //----------------------------------------------------------------------------
391 void PhyRegAlloc::addInterferencesForArgs() {
392 // get the InSet of root BB
393 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
395 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
396 // add interferences between args and LVars at start
397 addInterference(AI, &InSet, false);
399 if (DEBUG_RA >= RA_DEBUG_Interference)
400 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
405 //----------------------------------------------------------------------------
406 // This method is called after register allocation is complete to set the
407 // allocated reisters in the machine code. This code will add register numbers
408 // to MachineOperands that contain a Value. Also it calls target specific
409 // methods to produce caller saving instructions. At the end, it adds all
410 // additional instructions produced by the register allocator to the
411 // instruction stream.
412 //----------------------------------------------------------------------------
414 //-----------------------------
415 // Utility functions used below
416 //-----------------------------
418 InsertBefore(MachineInstr* newMI,
419 MachineBasicBlock& MBB,
420 MachineBasicBlock::iterator& MII)
422 MII = MBB.insert(MII, newMI);
427 InsertAfter(MachineInstr* newMI,
428 MachineBasicBlock& MBB,
429 MachineBasicBlock::iterator& MII)
431 ++MII; // insert before the next instruction
432 MII = MBB.insert(MII, newMI);
436 SubstituteInPlace(MachineInstr* newMI,
437 MachineBasicBlock& MBB,
438 MachineBasicBlock::iterator MII)
444 PrependInstructions(vector<MachineInstr *> &IBef,
445 MachineBasicBlock& MBB,
446 MachineBasicBlock::iterator& MII,
447 const std::string& msg)
451 MachineInstr* OrigMI = *MII;
452 std::vector<MachineInstr *>::iterator AdIt;
453 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
456 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
457 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
459 InsertBefore(*AdIt, MBB, MII);
465 AppendInstructions(std::vector<MachineInstr *> &IAft,
466 MachineBasicBlock& MBB,
467 MachineBasicBlock::iterator& MII,
468 const std::string& msg)
472 MachineInstr* OrigMI = *MII;
473 std::vector<MachineInstr *>::iterator AdIt;
474 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
477 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
478 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
480 InsertAfter(*AdIt, MBB, MII);
486 void PhyRegAlloc::updateMachineCode() {
487 // Insert any instructions needed at method entry
488 MachineBasicBlock::iterator MII = MF.front().begin();
489 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
490 "At function entry: \n");
491 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
492 "InstrsAfter should be unnecessary since we are just inserting at "
493 "the function entry point here.");
495 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
498 // iterate over all the machine instructions in BB
499 MachineBasicBlock &MBB = *BBI;
500 for (MachineBasicBlock::iterator MII = MBB.begin();
501 MII != MBB.end(); ++MII) {
503 MachineInstr *MInst = *MII;
504 unsigned Opcode = MInst->getOpCode();
506 // do not process Phis
507 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
510 // Reset tmp stack positions so they can be reused for each machine instr.
511 MF.popAllTempValues(TM);
513 // Now insert speical instructions (if necessary) for call/return
516 if (TM.getInstrInfo().isCall(Opcode) ||
517 TM.getInstrInfo().isReturn(Opcode)) {
518 AddedInstrns &AI = AddedInstrMap[MInst];
520 if (TM.getInstrInfo().isCall(Opcode))
521 MRI.colorCallArgs(MInst, LRI, &AI, *this, MBB.getBasicBlock());
522 else if (TM.getInstrInfo().isReturn(Opcode))
523 MRI.colorRetValue(MInst, LRI, &AI);
526 // Set the registers for operands in the machine instruction
527 // if a register was successfully allocated. If not, insert
528 // code to spill the register value.
530 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
532 MachineOperand& Op = MInst->getOperand(OpNum);
533 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
534 Op.getType() == MachineOperand::MO_CCRegister)
536 const Value *const Val = Op.getVRegValue();
538 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
539 if (!LR) // consts or labels will have no live range
541 // if register is not allocated, mark register as invalid
542 if (Op.getAllocatedRegNum() == -1)
543 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
548 MInst->SetRegForOperand(OpNum,
549 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
552 // LR did NOT receive a color (register). Insert spill code.
553 insertCode4SpilledLR(LR, MInst, MBB.getBasicBlock(), OpNum);
555 } // for each operand
557 // Now add instructions that the register allocator inserts before/after
558 // this machine instructions (done only for calls/rets/incoming args)
559 // We do this here, to ensure that spill for an instruction is inserted
560 // closest as possible to an instruction (see above insertCode4Spill...)
562 // First, if the instruction in the delay slot of a branch needs
563 // instructions inserted, move it out of the delay slot and before the
564 // branch because putting code before or after it would be VERY BAD!
566 unsigned bumpIteratorBy = 0;
567 if (MII != MBB.begin())
568 if (unsigned predDelaySlots =
569 TM.getInstrInfo().getNumDelaySlots((*(MII-1))->getOpCode()))
571 assert(predDelaySlots==1 && "Not handling multiple delay slots!");
572 if (TM.getInstrInfo().isBranch((*(MII-1))->getOpCode())
573 && (AddedInstrMap.count(MInst) ||
574 AddedInstrMap[MInst].InstrnsAfter.size() > 0))
576 // Current instruction is in the delay slot of a branch and it
577 // needs spill code inserted before or after it.
578 // Move it before the preceding branch.
579 InsertBefore(MInst, MBB, --MII);
581 new MachineInstr(TM.getInstrInfo().getNOPOpCode());
582 SubstituteInPlace(nopI, MBB, MII+1); // replace orig with NOP
583 --MII; // point to MInst in new location
584 bumpIteratorBy = 2; // later skip the branch and the NOP!
588 // If there are instructions to be added, *before* this machine
589 // instruction, add them now.
591 if (AddedInstrMap.count(MInst)) {
592 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MBB, MII,"");
595 // If there are instructions to be added *after* this machine
596 // instruction, add them now
598 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
600 // if there are delay slots for this instruction, the instructions
601 // added after it must really go after the delayed instruction(s)
602 // So, we move the InstrAfter of the current instruction to the
603 // corresponding delayed instruction
605 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) {
607 // Delayed instructions are typically branches or calls. Let's make
608 // sure this is not a branch, otherwise "insert-after" is meaningless,
609 // and should never happen for any reason (spill code, register
611 assert(! TM.getInstrInfo().isBranch(MInst->getOpCode()) &&
612 ! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
613 "INTERNAL ERROR: Register allocator should not be inserting "
614 "any code after a branch or return!");
616 move2DelayedInstr(MInst, *(MII+delay) );
619 // Here we can add the "instructions after" to the current
620 // instruction since there are no delay slots for this instruction
621 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MBB, MII,"");
625 // If we mucked with the instruction order above, adjust the loop iterator
627 MII = MII + bumpIteratorBy;
629 } // for each machine instruction
635 //----------------------------------------------------------------------------
636 // This method inserts spill code for AN operand whose LR was spilled.
637 // This method may be called several times for a single machine instruction
638 // if it contains many spilled operands. Each time it is called, it finds
639 // a register which is not live at that instruction and also which is not
640 // used by other spilled operands of the same instruction. Then it uses
641 // this register temporarily to accomodate the spilled value.
642 //----------------------------------------------------------------------------
643 void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
645 const BasicBlock *BB,
646 const unsigned OpNum) {
648 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
649 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
650 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
651 "Return value of a ret must be handled elsewhere");
653 MachineOperand& Op = MInst->getOperand(OpNum);
654 bool isDef = MInst->operandIsDefined(OpNum);
655 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
656 unsigned RegType = MRI.getRegType( LR );
657 int SpillOff = LR->getSpillOffFromFP();
658 RegClass *RC = LR->getRegClass();
659 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
661 MF.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
663 vector<MachineInstr*> MIBef, MIAft;
664 vector<MachineInstr*> AdIMid;
666 // Choose a register to hold the spilled value. This may insert code
667 // before and after MInst to free up the value. If so, this code should
668 // be first and last in the spill sequence before/after MInst.
669 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
671 // Set the operand first so that it this register does not get used
672 // as a scratch register for later calls to getUsableUniRegAtMI below
673 MInst->SetRegForOperand(OpNum, TmpRegU);
675 // get the added instructions for this instruction
676 AddedInstrns &AI = AddedInstrMap[MInst];
678 // We may need a scratch register to copy the spilled value to/from memory.
679 // This may itself have to insert code to free up a scratch register.
680 // Any such code should go before (after) the spill code for a load (store).
681 int scratchRegType = -1;
683 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
685 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
686 MInst, MIBef, MIAft);
687 assert(scratchReg != MRI.getInvalidRegNum());
688 MInst->insertUsedReg(scratchReg);
691 if (!isDef || isDefAndUse) {
692 // for a USE, we have to load the value of LR from stack to a TmpReg
693 // and use the TmpReg as one operand of instruction
695 // actual loading instruction(s)
696 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
699 // the actual load should be after the instructions to free up TmpRegU
700 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
704 if (isDef) { // if this is a Def
705 // for a DEF, we have to store the value produced by this instruction
706 // on the stack position allocated for this LR
708 // actual storing instruction(s)
709 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
712 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
715 // Finally, insert the entire spill code sequences before/after MInst
716 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
717 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
720 cerr << "\nFor Inst:\n " << *MInst;
721 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
722 cerr << "; added Instructions:";
723 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
724 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
729 //----------------------------------------------------------------------------
730 // We can use the following method to get a temporary register to be used
731 // BEFORE any given machine instruction. If there is a register available,
732 // this method will simply return that register and set MIBef = MIAft = NULL.
733 // Otherwise, it will return a register and MIAft and MIBef will contain
734 // two instructions used to free up this returned register.
735 // Returned register number is the UNIFIED register number
736 //----------------------------------------------------------------------------
738 int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
739 const ValueSet *LVSetBef,
741 std::vector<MachineInstr*>& MIBef,
742 std::vector<MachineInstr*>& MIAft) {
744 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
746 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
749 // we couldn't find an unused register. Generate code to free up a reg by
750 // saving it on stack and restoring after the instruction
752 int TmpOff = MF.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
754 RegU = getUniRegNotUsedByThisInst(RC, MInst);
756 // Check if we need a scratch register to copy this register to memory.
757 int scratchRegType = -1;
758 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
760 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
761 MInst, MIBef, MIAft);
762 assert(scratchReg != MRI.getInvalidRegNum());
764 // We may as well hold the value in the scratch register instead
765 // of copying it to memory and back. But we have to mark the
766 // register as used by this instruction, so it does not get used
767 // as a scratch reg. by another operand or anyone else.
768 MInst->insertUsedReg(scratchReg);
769 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
770 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
773 { // the register can be copied directly to/from memory so do it.
774 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
775 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
782 //----------------------------------------------------------------------------
783 // This method is called to get a new unused register that can be used to
784 // accomodate a spilled value.
785 // This method may be called several times for a single machine instruction
786 // if it contains many spilled operands. Each time it is called, it finds
787 // a register which is not live at that instruction and also which is not
788 // used by other spilled operands of the same instruction.
789 // Return register number is relative to the register class. NOT
791 //----------------------------------------------------------------------------
792 int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
793 const MachineInstr *MInst,
794 const ValueSet *LVSetBef) {
796 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
798 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
800 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
801 IsColorUsedArr[i] = false;
803 ValueSet::const_iterator LIt = LVSetBef->begin();
805 // for each live var in live variable set after machine inst
806 for ( ; LIt != LVSetBef->end(); ++LIt) {
808 // get the live range corresponding to live var
809 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
811 // LR can be null if it is a const since a const
812 // doesn't have a dominating def - see Assumptions above
813 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
814 IsColorUsedArr[ LRofLV->getColor() ] = true;
817 // It is possible that one operand of this MInst was already spilled
818 // and it received some register temporarily. If that's the case,
819 // it is recorded in machine operand. We must skip such registers.
821 setRelRegsUsedByThisInst(RC, MInst);
823 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
824 if (!IsColorUsedArr[c])
825 return MRI.getUnifiedRegNum(RC->getID(), c);
831 //----------------------------------------------------------------------------
832 // Get any other register in a register class, other than what is used
833 // by operands of a machine instruction. Returns the unified reg number.
834 //----------------------------------------------------------------------------
835 int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
836 const MachineInstr *MInst) {
838 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
839 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
841 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
842 IsColorUsedArr[i] = false;
844 setRelRegsUsedByThisInst(RC, MInst);
846 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
847 if (!IsColorUsedArr[c])
848 return MRI.getUnifiedRegNum(RC->getID(), c);
850 assert(0 && "FATAL: No free register could be found in reg class!!");
855 //----------------------------------------------------------------------------
856 // This method modifies the IsColorUsedArr of the register class passed to it.
857 // It sets the bits corresponding to the registers used by this machine
858 // instructions. Both explicit and implicit operands are set.
859 //----------------------------------------------------------------------------
860 void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
861 const MachineInstr *MInst ) {
863 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
865 // Add the registers already marked as used by the instruction.
866 // This should include any scratch registers that are used to save
867 // values across the instruction (e.g., for saving state register values).
868 const vector<bool> ®sUsed = MInst->getRegsUsed();
869 for (unsigned i = 0, e = regsUsed.size(); i != e; ++i)
871 unsigned classId = 0;
872 int classRegNum = MRI.getClassRegNum(i, classId);
873 if (RC->getID() == classId)
875 assert(classRegNum < (int) IsColorUsedArr.size() &&
876 "Illegal register number for this reg class?");
877 IsColorUsedArr[classRegNum] = true;
881 // Now add registers allocated to the live ranges of values used in
882 // the instruction. These are not yet recorded in the instruction.
883 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
885 const MachineOperand& Op = MInst->getOperand(OpNum);
887 if (MInst->getOperandType(OpNum) == MachineOperand::MO_VirtualRegister ||
888 MInst->getOperandType(OpNum) == MachineOperand::MO_CCRegister)
889 if (const Value* Val = Op.getVRegValue())
890 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
891 if (Op.getAllocatedRegNum() == -1)
892 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
893 if (LROfVal->hasColor() )
894 // this operand is in a LR that received a color
895 IsColorUsedArr[LROfVal->getColor()] = true;
898 // If there are implicit references, mark their allocated regs as well
900 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
902 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
903 if (LRofImpRef->hasColor())
904 // this implicit reference is in a LR that received a color
905 IsColorUsedArr[LRofImpRef->getColor()] = true;
909 //----------------------------------------------------------------------------
910 // If there are delay slots for an instruction, the instructions
911 // added after it must really go after the delayed instruction(s).
912 // So, we move the InstrAfter of that instruction to the
913 // corresponding delayed instruction using the following method.
915 //----------------------------------------------------------------------------
916 void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
917 const MachineInstr *DelayedMI) {
919 // "added after" instructions of the original instr
920 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
922 // "added instructions" of the delayed instr
923 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
925 // "added after" instructions of the delayed instr
926 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
928 // go thru all the "added after instructions" of the original instruction
929 // and append them to the "addded after instructions" of the delayed
931 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
933 // empty the "added after instructions" of the original instruction
937 //----------------------------------------------------------------------------
938 // This method prints the code with registers after register allocation is
940 //----------------------------------------------------------------------------
941 void PhyRegAlloc::printMachineCode()
944 cerr << "\n;************** Function " << Fn->getName()
945 << " *****************\n";
947 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
949 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
951 // get the iterator for machine instructions
952 MachineBasicBlock& MBB = *BBI;
953 MachineBasicBlock::iterator MII = MBB.begin();
955 // iterate over all the machine instructions in BB
956 for ( ; MII != MBB.end(); ++MII) {
957 MachineInstr *MInst = *MII;
960 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
962 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
963 MachineOperand& Op = MInst->getOperand(OpNum);
965 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
966 Op.getType() == MachineOperand::MO_CCRegister /*||
967 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
969 const Value *const Val = Op.getVRegValue () ;
970 // ****this code is temporary till NULL Values are fixed
972 cerr << "\t<*NULL*>";
976 // if a label or a constant
977 if (isa<BasicBlock>(Val)) {
978 cerr << "\t"; printLabel( Op.getVRegValue () );
980 // else it must be a register value
981 const int RegNum = Op.getAllocatedRegNum();
983 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
985 cerr << "(" << Val->getName() << ")";
987 cerr << "(" << Val << ")";
992 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
994 if (LROfVal->hasSpillOffset() )
999 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
1000 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
1004 cerr << "\t" << Op; // use dump field
1009 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1010 if (NumOfImpRefs > 0) {
1011 cerr << "\tImplicit:";
1013 for (unsigned z=0; z < NumOfImpRefs; z++)
1014 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
1017 } // for all machine instructions
1027 //----------------------------------------------------------------------------
1029 //----------------------------------------------------------------------------
1030 void PhyRegAlloc::colorIncomingArgs()
1032 MRI.colorMethodArgs(Fn, LRI, &AddedInstrAtEntry);
1036 //----------------------------------------------------------------------------
1037 // Used to generate a label for a basic block
1038 //----------------------------------------------------------------------------
1039 void PhyRegAlloc::printLabel(const Value *Val) {
1041 cerr << Val->getName();
1043 cerr << "Label" << Val;
1047 //----------------------------------------------------------------------------
1048 // This method calls setSugColorUsable method of each live range. This
1049 // will determine whether the suggested color of LR is really usable.
1050 // A suggested color is not usable when the suggested color is volatile
1051 // AND when there are call interferences
1052 //----------------------------------------------------------------------------
1054 void PhyRegAlloc::markUnusableSugColors()
1056 // hash map iterator
1057 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1058 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1060 for (; HMI != HMIEnd ; ++HMI ) {
1062 LiveRange *L = HMI->second; // get the LiveRange
1064 if (L->hasSuggestedColor()) {
1065 int RCID = L->getRegClass()->getID();
1066 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1067 L->isCallInterference() )
1068 L->setSuggestedColorUsable( false );
1070 L->setSuggestedColorUsable( true );
1072 } // if L->hasSuggestedColor()
1074 } // for all LR's in hash map
1079 //----------------------------------------------------------------------------
1080 // The following method will set the stack offsets of the live ranges that
1081 // are decided to be spillled. This must be called just after coloring the
1082 // LRs using the graph coloring algo. For each live range that is spilled,
1083 // this method allocate a new spill position on the stack.
1084 //----------------------------------------------------------------------------
1086 void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1087 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
1089 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1090 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
1092 for ( ; HMI != HMIEnd ; ++HMI) {
1093 if (HMI->first && HMI->second) {
1094 LiveRange *L = HMI->second; // get the LiveRange
1095 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
1096 int stackOffset = MF.allocateSpilledValue(TM, Type::LongTy);
1097 L->setSpillOffFromFP(stackOffset);
1099 cerr << " LR# " << L->getUserIGNode()->getIndex()
1100 << ": stack-offset = " << stackOffset << "\n";
1103 } // for all LR's in hash map
1108 //----------------------------------------------------------------------------
1109 // The entry pont to Register Allocation
1110 //----------------------------------------------------------------------------
1112 void PhyRegAlloc::allocateRegisters()
1115 // make sure that we put all register classes into the RegClassList
1116 // before we call constructLiveRanges (now done in the constructor of
1117 // PhyRegAlloc class).
1119 LRI.constructLiveRanges(); // create LR info
1121 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
1122 LRI.printLiveRanges();
1124 createIGNodeListsAndIGs(); // create IGNode list and IGs
1126 buildInterferenceGraphs(); // build IGs in all reg classes
1129 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
1130 // print all LRs in all reg classes
1131 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1132 RegClassList[rc]->printIGNodeList();
1134 // print IGs in all register classes
1135 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1136 RegClassList[rc]->printIG();
1139 LRI.coalesceLRs(); // coalesce all live ranges
1141 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
1142 // print all LRs in all reg classes
1143 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1144 RegClassList[rc]->printIGNodeList();
1146 // print IGs in all register classes
1147 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1148 RegClassList[rc]->printIG();
1152 // mark un-usable suggested color before graph coloring algorithm.
1153 // When this is done, the graph coloring algo will not reserve
1154 // suggested color unnecessarily - they can be used by another LR
1156 markUnusableSugColors();
1158 // color all register classes using the graph coloring algo
1159 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
1160 RegClassList[rc]->colorAllRegs();
1162 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1163 // a poistion for such spilled LRs
1165 allocateStackSpace4SpilledLRs();
1167 MF.popAllTempValues(TM); // TODO **Check
1169 // color incoming args - if the correct color was not received
1170 // insert code to copy to the correct register
1172 colorIncomingArgs();
1174 // Now update the machine code with register names and add any
1175 // additional code inserted by the register allocator to the instruction
1178 updateMachineCode();
1181 cerr << "\n**** Machine Code After Register Allocation:\n\n";