Teach frame lowering to ignore debug values after the terminators.
authorJakob Stoklund Olesen <stoklund@2pi.dk>
Thu, 13 Jan 2011 21:28:52 +0000 (21:28 +0000)
committerJakob Stoklund Olesen <stoklund@2pi.dk>
Thu, 13 Jan 2011 21:28:52 +0000 (21:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123399 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
include/llvm/CodeGen/MachineBasicBlock.h
lib/CodeGen/MachineBasicBlock.cpp
lib/Target/ARM/ARMFrameLowering.cpp
lib/Target/ARM/Thumb1FrameLowering.cpp
lib/Target/Alpha/AlphaFrameLowering.cpp
lib/Target/Blackfin/BlackfinFrameLowering.cpp
lib/Target/CellSPU/SPUFrameLowering.cpp
lib/Target/MBlaze/MBlazeFrameLowering.cpp
lib/Target/MSP430/MSP430FrameLowering.cpp
lib/Target/Mips/MipsFrameLowering.cpp
lib/Target/PowerPC/PPCFrameLowering.cpp
lib/Target/Sparc/SparcFrameLowering.cpp
lib/Target/SystemZ/SystemZFrameLowering.cpp
lib/Target/X86/X86FrameLowering.cpp

index 49daf5f4d34cc9be3179b8a5a1255228d6ff0300..16cb5c9ce85da5087c6a3765f828747e5ed828b7 100644 (file)
@@ -300,6 +300,10 @@ public:
   /// it returns end()
   iterator getFirstTerminator();
 
+  /// getLastNonDebugInstr - returns an iterator to the last non-debug
+  /// instruction in the basic block, or end()
+  iterator getLastNonDebugInstr();
+
   /// SplitCriticalEdge - Split the critical edge from this block to the
   /// given successor block, and return the newly created block, or null
   /// if splitting is not possible.
index 813fad288e8fa461eb5128af1dc52de9af5dc665..ad1ab287e345634e3f3f0d113b2fd0eea7bca111 100644 (file)
@@ -162,6 +162,18 @@ MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
   return I;
 }
 
+MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
+  iterator B = begin(), I = end();
+  while (I != B) {
+    --I;
+    if (I->isDebugValue())
+      continue;
+    return I;
+  }
+  // The block is all debug values.
+  return end();
+}
+
 void MachineBasicBlock::dump() const {
   print(dbgs());
 }
index 4a64867d3a21f669f811d624da0e19edc12e4b25..b8cbf5d2db30f52222a6fdf1f536227d31f0a05e 100644 (file)
@@ -297,7 +297,7 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
                                     MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   assert(MBBI->getDesc().isReturn() &&
          "Can only insert epilog into returning blocks");
   unsigned RetOpcode = MBBI->getOpcode();
@@ -378,7 +378,7 @@ void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
   if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
       RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
     // Tail call return: adjust the stack pointer and jump to callee.
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
 
     // Jump to label or value in register.
index 8a6dda8214849fe923126b2e971190e0f9ce6755..233e165387719d303a593bb06b5f6225a67b2e3f 100644 (file)
@@ -189,7 +189,7 @@ static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
 
 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
                                    MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   assert((MBBI->getOpcode() == ARM::tBX_RET ||
           MBBI->getOpcode() == ARM::tPOP_RET) &&
          "Can only insert epilog into returning blocks");
index 949bd4fb2c6765f4d29fa3938b923d805fc2a847..690cd1da9c1dcd4d19bf6a3dc348b674ba22a604 100644 (file)
@@ -104,7 +104,7 @@ void AlphaFrameLowering::emitPrologue(MachineFunction &MF) const {
 void AlphaFrameLowering::emitEpilogue(MachineFunction &MF,
                                   MachineBasicBlock &MBB) const {
   const MachineFrameInfo *MFI = MF.getFrameInfo();
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
 
   assert((MBBI->getOpcode() == Alpha::RETDAG ||
index 594325f95c2a6682227806201a125c983988bb91..08bb9522b7c393e66d8ae4c38da58e67b8304788 100644 (file)
@@ -90,7 +90,7 @@ void BlackfinFrameLowering::emitEpilogue(MachineFunction &MF,
     static_cast<const BlackfinRegisterInfo*>(MF.getTarget().getRegisterInfo());
   const BlackfinInstrInfo &TII =
     *static_cast<const BlackfinInstrInfo*>(MF.getTarget().getInstrInfo());
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   DebugLoc dl = MBBI->getDebugLoc();
 
   int FrameSize = MFI->getStackSize();
index 596d39c4417a08b686a5faf343c8419830a88541..432f4a1b59e247e414eb41fecd5635480a2e08d9 100644 (file)
@@ -187,7 +187,7 @@ void SPUFrameLowering::emitPrologue(MachineFunction &MF) const {
     // sufficient number instructions in the basic block. Note that
     // this is just a best guess based on the basic block's size.
     if (MBB.size() >= (unsigned) SPUFrameLowering::branchHintPenalty()) {
-      MachineBasicBlock::iterator MBBI = prior(MBB.end());
+      MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
       dl = MBBI->getDebugLoc();
 
       // Insert terminator label
@@ -199,7 +199,7 @@ void SPUFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void SPUFrameLowering::emitEpilogue(MachineFunction &MF,
                                 MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   const SPUInstrInfo &TII =
     *static_cast<const SPUInstrInfo*>(MF.getTarget().getInstrInfo());
   const MachineFrameInfo *MFI = MF.getFrameInfo();
index e501621154d9af50cbb70c76fa5ab16970e35676..e7639025cf1a9c7169fce6311f9c97b3a42c4746 100644 (file)
@@ -386,7 +386,7 @@ void MBlazeFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void MBlazeFrameLowering::emitEpilogue(MachineFunction &MF,
                                    MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   MachineFrameInfo *MFI            = MF.getFrameInfo();
   MBlazeFunctionInfo *MBlazeFI     = MF.getInfo<MBlazeFunctionInfo>();
   const MBlazeInstrInfo &TII =
index 9d3735314fd94b834c02d55c76c6c47b30f21fef..c99f4ab6c2f9c430dd07b7c45392187a90b4418b 100644 (file)
@@ -110,7 +110,7 @@ void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
   const MSP430InstrInfo &TII =
     *static_cast<const MSP430InstrInfo*>(MF.getTarget().getInstrInfo());
 
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   unsigned RetOpcode = MBBI->getOpcode();
   DebugLoc DL = MBBI->getDebugLoc();
 
index a81f947b02e75be141cabd7381dccbc986874233..711887abec8a888cbc111bd3ac88fcc3d63202d9 100644 (file)
@@ -266,7 +266,7 @@ void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
                                  MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   MachineFrameInfo *MFI            = MF.getFrameInfo();
   MipsFunctionInfo *MipsFI         = MF.getInfo<MipsFunctionInfo>();
   const MipsInstrInfo &TII =
index 4c5b94dc91cea4c9fde77d0d4f6579d9c50a5d9a..6aca6b00a06c7abdda8063de9c7a7219d37d2c04 100644 (file)
@@ -497,7 +497,8 @@ void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
                                 MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
+  assert(MBBI != MBB.end() && "Returning block has no terminator");
   const PPCInstrInfo &TII =
     *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
 
@@ -676,29 +677,29 @@ void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
           .addReg(TmpReg);
      }
   } else if (RetOpcode == PPC::TCRETURNdi) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
   } else if (RetOpcode == PPC::TCRETURNri) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
   } else if (RetOpcode == PPC::TCRETURNai) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
   } else if (RetOpcode == PPC::TCRETURNdi8) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
   } else if (RetOpcode == PPC::TCRETURNri8) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
   } else if (RetOpcode == PPC::TCRETURNai8) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
   }
index 1efd707d7fddf048522b84245fa1c7b97d121953..320c8ca26d7e7471951c739edfc62e950c35f69c 100644 (file)
@@ -69,7 +69,7 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF) const {
 
 void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
                                   MachineBasicBlock &MBB) const {
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   const SparcInstrInfo &TII =
     *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
   DebugLoc dl = MBBI->getDebugLoc();
index 05c2b89fe4729796b2245c31937a7610789bc8f5..2ad84a2d052e4ca7340bb5dd8dc56ff7fbd3a0dc 100644 (file)
@@ -141,7 +141,7 @@ void SystemZFrameLowering::emitPrologue(MachineFunction &MF) const {
 void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
                                     MachineBasicBlock &MBB) const {
   const MachineFrameInfo *MFI = MF.getFrameInfo();
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
   const SystemZInstrInfo &TII =
     *static_cast<const SystemZInstrInfo*>(MF.getTarget().getInstrInfo());
   SystemZMachineFunctionInfo *SystemZMFI =
index 7c7b4f3f8a7822d951cc0c7c9ab906567c6b1662..02010f87e468280e8ea08064d106d81216f72989 100644 (file)
@@ -646,7 +646,8 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
   X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
   const X86RegisterInfo *RegInfo = TM.getRegisterInfo();
   const X86InstrInfo &TII = *TM.getInstrInfo();
-  MachineBasicBlock::iterator MBBI = prior(MBB.end());
+  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
+  assert(MBBI != MBB.end() && "Returning block has no instructions");
   unsigned RetOpcode = MBBI->getOpcode();
   DebugLoc DL = MBBI->getDebugLoc();
   bool Is64Bit = STI.is64Bit();
@@ -709,7 +710,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
     MachineBasicBlock::iterator PI = prior(MBBI);
     unsigned Opc = PI->getOpcode();
 
-    if (Opc != X86::POP32r && Opc != X86::POP64r &&
+    if (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::DBG_VALUE &&
         !PI->getDesc().isTerminator())
       break;
 
@@ -756,7 +757,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
 
   // We're returning from function via eh_return.
   if (RetOpcode == X86::EH_RETURN || RetOpcode == X86::EH_RETURN64) {
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
     MachineOperand &DestAddr  = MBBI->getOperand(0);
     assert(DestAddr.isReg() && "Offset should be in register!");
     BuildMI(MBB, MBBI, DL,
@@ -768,7 +769,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
              RetOpcode == X86::TCRETURNmi64) {
     bool isMem = RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64;
     // Tail call return: adjust the stack pointer and jump to callee.
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getFirstTerminator();
     MachineOperand &JumpTarget = MBBI->getOperand(0);
     MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
     assert(StackAdjust.isImm() && "Expecting immediate value.");
@@ -826,7 +827,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
              (X86FI->getTCReturnAddrDelta() < 0)) {
     // Add the return addr area delta back since we are not tail calling.
     int delta = -1*X86FI->getTCReturnAddrDelta();
-    MBBI = prior(MBB.end());
+    MBBI = MBB.getLastNonDebugInstr();
 
     // Check for possible merge with preceeding ADD instruction.
     delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);