[llvm-commits] CVS: llvm/lib/Transforms/Scalar/Reassociate.cpp

Chris Lattner lattner at cs.uiuc.edu
Thu Sep 1 22:23:34 PDT 2005



Changes in directory llvm/lib/Transforms/Scalar:

Reassociate.cpp updated: 1.52 -> 1.53
---
Log message:

add some assertions and fix problems where reassociate could access the 
Ops vector out of range


---
Diffs of the changes:  (+11 -2)

 Reassociate.cpp |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)


Index: llvm/lib/Transforms/Scalar/Reassociate.cpp
diff -u llvm/lib/Transforms/Scalar/Reassociate.cpp:1.52 llvm/lib/Transforms/Scalar/Reassociate.cpp:1.53
--- llvm/lib/Transforms/Scalar/Reassociate.cpp:1.52	Wed Aug 24 12:55:32 2005
+++ llvm/lib/Transforms/Scalar/Reassociate.cpp	Fri Sep  2 00:23:22 2005
@@ -455,6 +455,7 @@
         Ops.pop_back();
       break;
     }
+  if (Ops.size() == 1) return;
 
   // Handle destructive annihilation do to identities between elements in the
   // argument list here.
@@ -467,6 +468,7 @@
     // If we find any, we can simplify the expression. X&~X == 0, X|~X == -1.
     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
       // First, check for X and ~X in the operand list.
+      assert(i < Ops.size());
       if (BinaryOperator::isNot(Ops[i].Op)) {    // Cannot occur for ^.
         Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
         unsigned FoundX = FindInOperandList(Ops, i, X);
@@ -487,6 +489,7 @@
 
       // Next, check for duplicate pairs of values, which we assume are next to
       // each other, due to our sorting criteria.
+      assert(i < Ops.size());
       if (i+1 != Ops.size() && Ops[i+1].Op == Ops[i].Op) {
         if (Opcode == Instruction::And || Opcode == Instruction::Or) {
           // Drop duplicate values.
@@ -516,6 +519,7 @@
     // Scan the operand lists looking for X and -X pairs.  If we find any, we
     // can simplify the expression. X+-X == 0
     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
+      assert(i < Ops.size());
       // Check for X and -X in the operand list.
       if (BinaryOperator::isNeg(Ops[i].Op)) {
         Value *X = BinaryOperator::getNegArgument(Ops[i].Op);
@@ -524,15 +528,20 @@
           // Remove X and -X from the operand list.
           if (Ops.size() == 2) {
             Ops[0].Op = Constant::getNullValue(X->getType());
-            Ops.erase(Ops.begin()+1);
+            Ops.pop_back();
             ++NumAnnihil;
             return;
           } else {
             Ops.erase(Ops.begin()+i);
-            if (i < FoundX) --FoundX;
+            if (i < FoundX)
+              --FoundX;
+            else
+              --i;   // Need to back up an extra one.
             Ops.erase(Ops.begin()+FoundX);
             IterateOptimization = true;
             ++NumAnnihil;
+            --i;     // Revisit element.
+            e -= 2;  // Removed two elements.
           }
         }
       }






More information about the llvm-commits mailing list