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

Chris Lattner lattner at cs.uiuc.edu
Thu Jan 13 16:20:20 PST 2005



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.302 -> 1.303
---
Log message:

if two gep comparisons only differ by one index, compare that index directly.
This allows us to better optimize begin() -> end() comparisons in common cases.


---
Diffs of the changes:  (+28 -0)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.302 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.303
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.302	Thu Jan 13 17:26:48 2005
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Thu Jan 13 18:20:05 2005
@@ -2219,6 +2219,8 @@
     if (AllZeros)
       return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0),
                           SetCondInst::getSwappedCondition(Cond), I);
+
+    // If the other GEP has all zero indices, recurse.
     AllZeros = true;
     for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
       if (!isa<Constant>(GEPRHS->getOperand(i)) ||
@@ -2229,6 +2231,32 @@
     if (AllZeros)
       return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I);
 
+    if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
+      // If the GEPs only differ by one index, compare it.
+      unsigned NumDifferences = 0;  // Keep track of # differences.
+      unsigned DiffOperand = 0;     // The operand that differs.
+      for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
+        if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
+          if (GEPLHS->getOperand(i)->getType() != 
+                     GEPRHS->getOperand(i)->getType()) {
+            // Irreconsilable differences.
+            NumDifferences = 2;
+            break;
+          } else {
+            if (NumDifferences++) break;
+            DiffOperand = i;
+          }
+        }
+
+      if (NumDifferences == 0)   // SAME GEP?
+        return ReplaceInstUsesWith(I, // No comparison is needed here.
+                                 ConstantBool::get(Cond == Instruction::SetEQ));
+      else if (NumDifferences == 1) {
+        return new SetCondInst(Cond, GEPLHS->getOperand(DiffOperand),
+                               GEPRHS->getOperand(DiffOperand));
+      }
+    }
+
     // Only lower this if the setcc is the only user of the GEP or if we expect
     // the result to fold to a constant!
     if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&






More information about the llvm-commits mailing list