Constant fold inttoptr and ptrtoint.
authorNick Lewycky <nicholas@mxc.ca>
Sat, 17 May 2008 09:03:26 +0000 (09:03 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sat, 17 May 2008 09:03:26 +0000 (09:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51216 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantFold.cpp
test/Transforms/InstCombine/2008-05-17-FoldIntToPtr.ll [new file with mode: 0644]

index 0913c481ad80dbb5d3846290a66ed9ed91a2c616..e381f3b102887f189587fcea27979567b4a62014 100644 (file)
@@ -1029,6 +1029,29 @@ static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
         }
       break;
 
+    case Instruction::PtrToInt:
+    case Instruction::IntToPtr:
+      // inttoptr(x1) != inttoptr(x2) iff x1 != x2
+      if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
+        if (CE1->getOpcode() == CE2->getOpcode()) {
+          Constant *Op1 = const_cast<Constant*>(CE1Op0);
+          Constant *Op2 = CE2->getOperand(0);
+          if (CE1Op->getType() == CE2Op->getType()) {
+            ConstantInt *R = 0;
+
+            ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
+            R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, Op1, Op2));
+            if (R && !R->isZero()) 
+              return pred;
+
+            pred = ICmpInst::ICMP_NE;
+            R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, Op1, Op2));
+            if (R && !R->isZero()) 
+              return pred;
+          }
+        }
+      break;
+
     case Instruction::GetElementPtr:
       // Ok, since this is a getelementptr, we know that the constant has a
       // pointer type.  Check the various cases.
diff --git a/test/Transforms/InstCombine/2008-05-17-FoldIntToPtr.ll b/test/Transforms/InstCombine/2008-05-17-FoldIntToPtr.ll
new file mode 100644 (file)
index 0000000..1d93294
--- /dev/null
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {ret i1 false}
+; PR2329
+
+define i1 @f() {
+  %x = icmp eq i8* inttoptr (i32 1 to i8*), inttoptr (i32 2 to i8*)
+  ret i1 %x
+}
+