[PATCH] D33801: [InstSimplify][ConstantFolding] Teach constant folding how to handle icmp null, (inttoptr x) as well as it handles icmp (inttoptr x), null

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 1 14:33:19 PDT 2017


craig.topper created this revision.

The constant folding code currently assumes that the constant expression will always be on the left and the simple null will be on the right. But that's not true at least on the path from InstSimplify.

This patch adds support to ConstantFolding to detect the reversed case.


https://reviews.llvm.org/D33801

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstSimplify/compare.ll


Index: test/Transforms/InstSimplify/compare.ll
===================================================================
--- test/Transforms/InstSimplify/compare.ll
+++ test/Transforms/InstSimplify/compare.ll
@@ -1289,7 +1289,7 @@
 
 define i1 @constant_fold_null_inttoptr() {
 ; CHECK-LABEL: @constant_fold_null_inttoptr(
-; CHECK-NEXT:    ret i1 icmp eq (i32* inttoptr (i64 32 to i32*), i32* null)
+; CHECK-NEXT:    ret i1 false
 ;
   %x = icmp eq i32* null, inttoptr (i64 32 to i32*)
   ret i1 %x
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1170,7 +1170,9 @@
                                                 const DataLayout &DL,
                                                 const TargetLibraryInfo *TLI) {
   // fold: icmp (inttoptr x), null         -> icmp x, 0
+  // fold: icmp null, (inttoptr x)         -> icmp 0, x
   // fold: icmp (ptrtoint x), 0            -> icmp x, null
+  // fold: icmp 0, (ptrtoint x)            -> icmp null, x
   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
   //
@@ -1240,6 +1242,29 @@
         Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
       return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
     }
+  } else if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
+    if (Ops0->isNullValue()) {
+      if (CE1->getOpcode() == Instruction::IntToPtr) {
+        Type *IntPtrTy = DL.getIntPtrType(CE1->getType());
+        // Convert the integer value to the right size to ensure we get the
+        // proper extension or truncation.
+        Constant *C = ConstantExpr::getIntegerCast(CE1->getOperand(0),
+                                                   IntPtrTy, false);
+        Constant *Null = Constant::getNullValue(C->getType());
+        return ConstantFoldCompareInstOperands(Predicate, Null, C, DL, TLI);
+      }
+
+      // Only do this transformation if the int is intptrty in size, otherwise
+      // there is a truncation or extension that we aren't modeling.
+      if (CE1->getOpcode() == Instruction::PtrToInt) {
+        Type *IntPtrTy = DL.getIntPtrType(CE1->getOperand(0)->getType());
+        if (CE1->getType() == IntPtrTy) {
+          Constant *C = CE1->getOperand(0);
+          Constant *Null = Constant::getNullValue(C->getType());
+          return ConstantFoldCompareInstOperands(Predicate, Null, C, DL, TLI);
+        }
+      }
+    }
   }
 
   return ConstantExpr::getCompare(Predicate, Ops0, Ops1);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33801.101113.patch
Type: text/x-patch
Size: 2627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170601/ae34bfea/attachment.bin>


More information about the llvm-commits mailing list