Take advantage of our knowledge of 2-address X86 instructions and
authorMisha Brukman <brukman+llvm@gmail.com>
Thu, 12 Dec 2002 23:20:31 +0000 (23:20 +0000)
committerMisha Brukman <brukman+llvm@gmail.com>
Thu, 12 Dec 2002 23:20:31 +0000 (23:20 +0000)
register-allocated them appropriately.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4976 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/RegAllocSimple.cpp

index af9b6716888826a9e1d0b989860a553ce6e23aa1..d78a75c92e3afe408bd42e71b6d02f41e70567c4 100644 (file)
@@ -38,6 +38,9 @@ namespace {
 
     // Maps physical register to their register classes
     std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
+
+    // Made to combat the incorrect allocation of r2 = add r1, r1
+    std::map<unsigned, unsigned> VirtReg2PhysRegMap;
     
     // Maps RegClass => which index we can take a register from. Since this is a
     // simple register allocator, when we need a register of a certain class, we
@@ -225,17 +228,30 @@ bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
           DEBUG(std::cerr << "const\n");
         } else if (op.isVirtualRegister()) {
           virtualReg = (unsigned) op.getAllocatedRegNum();
-          // save register to stack if it's a def
           DEBUG(std::cerr << "op: " << op << "\n");
           DEBUG(std::cerr << "\t inst[" << i << "]: ";
                 MI->print(std::cerr, TM));
-          if (op.opIsDef()) {
-            physReg = getFreeReg(virtualReg);
-            MachineBasicBlock::iterator J = I;
-            J = saveVirtRegToStack(++J, virtualReg, physReg);
-            I = --J;
+
+          // make sure the same virtual register maps to the same physical
+          // register in any given instruction
+          if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
+            physReg = VirtReg2PhysRegMap[virtualReg];
           } else {
-            I = moveUseToReg(I, virtualReg, physReg);
+            if (op.opIsDef()) {
+              if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
+                // must be same register number as the first operand
+                // This maps a = b + c into b += c, and saves b into a's spot
+                physReg = (unsigned) MI->getOperand(1).getAllocatedRegNum();
+              } else {
+                physReg = getFreeReg(virtualReg);
+              }
+              MachineBasicBlock::iterator J = I;
+              J = saveVirtRegToStack(++J, virtualReg, physReg);
+              I = --J;
+            } else {
+              I = moveUseToReg(I, virtualReg, physReg);
+            }
+            VirtReg2PhysRegMap[virtualReg] = physReg;
           }
           MI->SetMachineOperandReg(i, physReg);
           DEBUG(std::cerr << "virt: " << virtualReg << 
@@ -244,6 +260,7 @@ bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
       }
 
       clearAllRegs();
+      VirtReg2PhysRegMap.clear();
     }
 
   }