[llvm-commits] CVS: llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp

Nate Begeman natebegeman at mac.com
Sun Jun 12 16:50:44 PDT 2005



Changes in directory llvm/lib/Target/PowerPC:

PowerPCBranchSelector.cpp updated: 1.8 -> 1.9
---
Log message:

Fix a memory smasher caught by Mac OS X's debug malloc library.  We were
incorrectly using an iterator after it was invalid.


---
Diffs of the changes:  (+19 -8)

 PowerPCBranchSelector.cpp |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)


Index: llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp
diff -u llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp:1.8 llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp:1.9
--- llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp:1.8	Thu Apr 21 18:20:02 2005
+++ llvm/lib/Target/PowerPC/PowerPCBranchSelector.cpp	Sun Jun 12 18:50:33 2005
@@ -86,7 +86,14 @@
 
         for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
              MBBI != EE; ++MBBI) {
-          if (MBBI->getOpcode() == PPC::COND_BRANCH) {
+          // We may end up deleting the MachineInstr that MBBI points to, so
+          // remember its opcode now so we can refer to it after calling erase()
+          unsigned OpcodeToReplace = MBBI->getOpcode();
+
+          if (OpcodeToReplace == PPC::COND_BRANCH) {
+            MachineBasicBlock::iterator MBBJ = MBBI;
+            ++MBBJ;
+            
             // condbranch operands:
             // 0. CR0 register
             // 1. bc opcode
@@ -101,17 +108,21 @@
             unsigned Opcode = MBBI->getOperand(1).getImmedValue();
             unsigned Inverted = PPC32InstrInfo::invertPPCBranchOpcode(Opcode);
 
-            MachineInstr *MI = MBBI;
             if (Displacement >= -32768 && Displacement <= 32767) {
-              BuildMI(*MBB, MBBI, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
+              BuildMI(*MBB, MBBJ, Opcode, 2).addReg(PPC::CR0).addMBB(trueMBB);
             } else {
-              BuildMI(*MBB, MBBI, Inverted, 2).addReg(PPC::CR0).addSImm(8);
-              BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(trueMBB);
-              BuildMI(*MBB, MBBI, PPC::B, 1).addMBB(falseMBB);
+              BuildMI(*MBB, MBBJ, Inverted, 2).addReg(PPC::CR0).addSImm(8);
+              BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(trueMBB);
+              BuildMI(*MBB, MBBJ, PPC::B, 1).addMBB(falseMBB);
             }
-            MBB->erase(MI);
+
+            // Erase the psuedo COND_BRANCH instruction, and then back up the
+            // iterator so that when the for loop increments it, we end up in
+            // the correct place rather than iterating off the end.
+            MBB->erase(MBBI);
+            MBBI = --MBBJ;
           }
-          ByteCount += bytesForOpcode(MBBI->getOpcode());
+          ByteCount += bytesForOpcode(OpcodeToReplace);
         }
       }
 






More information about the llvm-commits mailing list