diff options
author | Chris Lattner <sabre@nondot.org> | 2005-09-02 05:23:22 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2005-09-02 05:23:22 +0000 |
commit | 368a3aabb2b48ad6e39d23d1c6cbdfe0c5857483 (patch) | |
tree | d9669b27816fef9c08a044f10a25748b15f3d289 /lib/Transforms/Scalar/Reassociate.cpp | |
parent | 7cd57f4c452c39ee595de19633787ec37933eb34 (diff) |
add some assertions and fix problems where reassociate could access the
Ops vector out of range
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23211 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/Reassociate.cpp')
-rw-r--r-- | lib/Transforms/Scalar/Reassociate.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index 60722ef68f..a9b0ea6cbb 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -455,6 +455,7 @@ void Reassociate::OptimizeExpression(unsigned Opcode, 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 @@ void Reassociate::OptimizeExpression(unsigned Opcode, // 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 @@ void Reassociate::OptimizeExpression(unsigned Opcode, // 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 @@ void Reassociate::OptimizeExpression(unsigned Opcode, // 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 @@ void Reassociate::OptimizeExpression(unsigned Opcode, // 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. } } } |