[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 15:26:11 PDT 2017
craig.topper updated this revision to Diff 101124.
craig.topper added a comment.
Implement with no code duplication. Just detect that LHS is not a constant expr, but RHS is. If that's the case swap the predicate and the operands and recurse back into the same function.
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,11 @@
Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
}
+ } else if (isa<ConstantExpr>(Ops1)) {
+ // If RHS is a constant expression, but the left side isn't, swap the
+ // operands and try again.
+ Predicate = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)Predicate);
+ return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
}
return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33801.101124.patch
Type: text/x-patch
Size: 1768 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170601/64f35bf6/attachment.bin>
More information about the llvm-commits
mailing list