[llvm-commits] [llvm] r46687 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2008-01-29-AddICmp.ll

Nick Lewycky nicholas at mxc.ca
Sun Feb 3 08:33:10 PST 2008


Author: nicholas
Date: Sun Feb  3 10:33:09 2008
New Revision: 46687

URL: http://llvm.org/viewvc/llvm-project?rev=46687&view=rev
Log:
There are some cases where icmp(add) can be folded into a new icmp. Handle them.

Added:
    llvm/trunk/test/Transforms/InstCombine/2008-01-29-AddICmp.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=46687&r1=46686&r2=46687&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sun Feb  3 10:33:09 2008
@@ -45,6 +45,7 @@
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/CallSite.h"
+#include "llvm/Support/ConstantRange.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/InstVisitor.h"
@@ -5704,6 +5705,37 @@
                                           DivRHS))
         return R;
     break;
+
+  case Instruction::Add:
+    // Fold: icmp pred (add, X, C1), C2
+
+    if (!ICI.isEquality()) {
+      ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
+      if (!LHSC) break;
+      const APInt &LHSV = LHSC->getValue();
+
+      ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
+                            .subtract(LHSV);
+
+      if (ICI.isSignedPredicate()) {
+        if (CR.getLower().isSignBit()) {
+          return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
+                              ConstantInt::get(CR.getUpper()));
+        } else if (CR.getUpper().isSignBit()) {
+          return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
+                              ConstantInt::get(CR.getLower()));
+        }
+      } else {
+        if (CR.getLower().isMinValue()) {
+          return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
+                              ConstantInt::get(CR.getUpper()));
+        } else if (CR.getUpper().isMinValue()) {
+          return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
+                              ConstantInt::get(CR.getLower()));
+        }
+      }
+    }
+    break;
   }
   
   // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.

Added: llvm/trunk/test/Transforms/InstCombine/2008-01-29-AddICmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2008-01-29-AddICmp.ll?rev=46687&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2008-01-29-AddICmp.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/2008-01-29-AddICmp.ll Sun Feb  3 10:33:09 2008
@@ -0,0 +1,19 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {a.off}
+
+define i1 @test1(i32 %a) {
+  %a.off = add i32 %a, 4          ; <i32> [#uses=1]
+  %C = icmp ult i32 %a.off, 4             ; <i1> [#uses=1]
+  ret i1 %C
+}
+
+define i1 @test2(i32 %a) {
+  %a.off = sub i32 %a, 4          ; <i32> [#uses=1]
+  %C = icmp ugt i32 %a.off, -5             ; <i1> [#uses=1]
+  ret i1 %C
+}
+
+define i1 @test3(i32 %a) {
+  %a.off = add i32 %a, 4          ; <i32> [#uses=1]
+  %C = icmp slt i32 %a.off, 2147483652             ; <i1> [#uses=1]
+  ret i1 %C
+}





More information about the llvm-commits mailing list