RegAllocSimple.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a simple register allocator. *Very* simple: It immediate
  11. // spills every value right after it is computed, and it reloads all used
  12. // operands from the spill area to temporary registers before each instruction.
  13. // It does not keep values in registers across instructions.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #define DEBUG_TYPE "regalloc"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/RegAllocRegistry.h"
  23. #include "llvm/Target/TargetInstrInfo.h"
  24. #include "llvm/Target/TargetMachine.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/Compiler.h"
  27. #include "llvm/ADT/Statistic.h"
  28. #include "llvm/ADT/STLExtras.h"
  29. #include <map>
  30. using namespace llvm;
  31. STATISTIC(NumStores, "Number of stores added");
  32. STATISTIC(NumLoads , "Number of loads added");
  33. namespace {
  34. static RegisterRegAlloc
  35. simpleRegAlloc("simple", " simple register allocator",
  36. createSimpleRegisterAllocator);
  37. class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass {
  38. public:
  39. static char ID;
  40. RegAllocSimple() : MachineFunctionPass(&ID) {}
  41. private:
  42. MachineFunction *MF;
  43. const TargetMachine *TM;
  44. const TargetRegisterInfo *TRI;
  45. const TargetInstrInfo *TII;
  46. // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
  47. // these values are spilled
  48. std::map<unsigned, int> StackSlotForVirtReg;
  49. // RegsUsed - Keep track of what registers are currently in use. This is a
  50. // bitset.
  51. std::vector<bool> RegsUsed;
  52. // RegClassIdx - Maps RegClass => which index we can take a register
  53. // from. Since this is a simple register allocator, when we need a register
  54. // of a certain class, we just take the next available one.
  55. std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
  56. public:
  57. virtual const char *getPassName() const {
  58. return "Simple Register Allocator";
  59. }
  60. /// runOnMachineFunction - Register allocate the whole function
  61. bool runOnMachineFunction(MachineFunction &Fn);
  62. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  63. AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
  64. MachineFunctionPass::getAnalysisUsage(AU);
  65. }
  66. private:
  67. /// AllocateBasicBlock - Register allocate the specified basic block.
  68. void AllocateBasicBlock(MachineBasicBlock &MBB);
  69. /// getStackSpaceFor - This returns the offset of the specified virtual
  70. /// register on the stack, allocating space if necessary.
  71. int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
  72. /// Given a virtual register, return a compatible physical register that is
  73. /// currently unused.
  74. ///
  75. /// Side effect: marks that register as being used until manually cleared
  76. ///
  77. unsigned getFreeReg(unsigned virtualReg);
  78. /// Moves value from memory into that register
  79. unsigned reloadVirtReg(MachineBasicBlock &MBB,
  80. MachineBasicBlock::iterator I, unsigned VirtReg);
  81. /// Saves reg value on the stack (maps virtual register to stack value)
  82. void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  83. unsigned VirtReg, unsigned PhysReg);
  84. };
  85. char RegAllocSimple::ID = 0;
  86. }
  87. /// getStackSpaceFor - This allocates space for the specified virtual
  88. /// register to be held on the stack.
  89. int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
  90. const TargetRegisterClass *RC) {
  91. // Find the location VirtReg would belong...
  92. std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg);
  93. if (I != StackSlotForVirtReg.end())
  94. return I->second; // Already has space allocated?
  95. // Allocate a new stack object for this spill location...
  96. int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
  97. RC->getAlignment());
  98. // Assign the slot...
  99. StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
  100. return FrameIdx;
  101. }
  102. unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
  103. const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg);
  104. TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
  105. TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
  106. while (1) {
  107. unsigned regIdx = RegClassIdx[RC]++;
  108. assert(RI+regIdx != RE && "Not enough registers!");
  109. unsigned PhysReg = *(RI+regIdx);
  110. if (!RegsUsed[PhysReg]) {
  111. MF->getRegInfo().setPhysRegUsed(PhysReg);
  112. return PhysReg;
  113. }
  114. }
  115. }
  116. unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
  117. MachineBasicBlock::iterator I,
  118. unsigned VirtReg) {
  119. const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
  120. int FrameIdx = getStackSpaceFor(VirtReg, RC);
  121. unsigned PhysReg = getFreeReg(VirtReg);
  122. // Add move instruction(s)
  123. ++NumLoads;
  124. TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
  125. return PhysReg;
  126. }
  127. void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
  128. MachineBasicBlock::iterator I,
  129. unsigned VirtReg, unsigned PhysReg) {
  130. const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
  131. int FrameIdx = getStackSpaceFor(VirtReg, RC);
  132. // Add move instruction(s)
  133. ++NumStores;
  134. TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC);
  135. }
  136. void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
  137. // loop over each instruction
  138. for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
  139. // Made to combat the incorrect allocation of r2 = add r1, r1
  140. std::map<unsigned, unsigned> Virt2PhysRegMap;
  141. RegsUsed.resize(TRI->getNumRegs());
  142. // This is a preliminary pass that will invalidate any registers that are
  143. // used by the instruction (including implicit uses).
  144. const TargetInstrDesc &Desc = MI->getDesc();
  145. const unsigned *Regs;
  146. if (Desc.ImplicitUses) {
  147. for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
  148. RegsUsed[*Regs] = true;
  149. }
  150. if (Desc.ImplicitDefs) {
  151. for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
  152. RegsUsed[*Regs] = true;
  153. MF->getRegInfo().setPhysRegUsed(*Regs);
  154. }
  155. }
  156. // Loop over uses, move from memory into registers.
  157. for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
  158. MachineOperand &MO = MI->getOperand(i);
  159. if (MO.isRegister() && MO.getReg() &&
  160. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  161. unsigned virtualReg = (unsigned) MO.getReg();
  162. DOUT << "op: " << MO << "\n";
  163. DOUT << "\t inst[" << i << "]: ";
  164. DEBUG(MI->print(*cerr.stream(), TM));
  165. // make sure the same virtual register maps to the same physical
  166. // register in any given instruction
  167. unsigned physReg = Virt2PhysRegMap[virtualReg];
  168. if (physReg == 0) {
  169. if (MO.isDef()) {
  170. int TiedOp = Desc.findTiedToSrcOperand(i);
  171. if (TiedOp == -1) {
  172. physReg = getFreeReg(virtualReg);
  173. } else {
  174. // must be same register number as the source operand that is
  175. // tied to. This maps a = b + c into b = b + c, and saves b into
  176. // a's spot.
  177. assert(MI->getOperand(TiedOp).isRegister() &&
  178. MI->getOperand(TiedOp).getReg() &&
  179. MI->getOperand(TiedOp).isUse() &&
  180. "Two address instruction invalid!");
  181. physReg = MI->getOperand(TiedOp).getReg();
  182. }
  183. spillVirtReg(MBB, next(MI), virtualReg, physReg);
  184. } else {
  185. physReg = reloadVirtReg(MBB, MI, virtualReg);
  186. Virt2PhysRegMap[virtualReg] = physReg;
  187. }
  188. }
  189. MO.setReg(physReg);
  190. DOUT << "virt: " << virtualReg << ", phys: " << MO.getReg() << "\n";
  191. }
  192. }
  193. RegClassIdx.clear();
  194. RegsUsed.clear();
  195. }
  196. }
  197. /// runOnMachineFunction - Register allocate the whole function
  198. ///
  199. bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
  200. DOUT << "Machine Function\n";
  201. MF = &Fn;
  202. TM = &MF->getTarget();
  203. TRI = TM->getRegisterInfo();
  204. TII = TM->getInstrInfo();
  205. // Loop over all of the basic blocks, eliminating virtual register references
  206. for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
  207. MBB != MBBe; ++MBB)
  208. AllocateBasicBlock(*MBB);
  209. StackSlotForVirtReg.clear();
  210. return true;
  211. }
  212. FunctionPass *llvm::createSimpleRegisterAllocator() {
  213. return new RegAllocSimple();
  214. }