summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-01-04 12:23:06 +0100
committerNikita Popov <npopov@redhat.com>2022-01-04 12:27:40 +0100
commit75db002725156fba9e9c38b7cefe57b7ed713734 (patch)
tree7a6f3e56ec7e75b345dcee3a4281835587b85753
parentaefab6f8d5b13c41d000feaa8f0e567d4b6a4681 (diff)
downloadllvm-75db002725156fba9e9c38b7cefe57b7ed713734.tar.gz
[ConstantFold] Remove another incorrect icmp of GEP fold
This fold is not correct, because indices might evaluate to zero even if they are not a literal zero integer. Additionally, this fold would be wrong (in the general case) for non-i8 types as well, due to index overflow. Drop this fold and instead let the target-dependent constant folder compute the actual offset and fold the comparison based on that.
-rw-r--r--llvm/lib/IR/ConstantFold.cpp10
-rw-r--r--llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll6
2 files changed, 4 insertions, 12 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index c3f3d3c4b4c1..f32d26ba0978 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1539,17 +1539,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
// so the result is greater-than
if (!GV->hasExternalWeakLinkage())
return ICmpInst::ICMP_UGT;
- } else if (isa<ConstantPointerNull>(CE1Op0)) {
- // If we are indexing from a null pointer, check to see if we have any
- // non-zero indices.
- for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
- if (!CE1->getOperand(i)->isNullValue())
- // Offsetting from null, must not be equal.
- return ICmpInst::ICMP_UGT;
- // Only zero indexes from null, must still be zero.
- return ICmpInst::ICMP_EQ;
}
- // Otherwise, we can't really say if the first operand is null or not.
} else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
if (isa<ConstantPointerNull>(CE1Op0)) {
// If its not weak linkage, the GVal must have a non-zero address
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
index b2d6b3f1c7fb..99f1a16c5427 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
@@ -118,9 +118,11 @@ define i1 @global_gep_sgt_null() {
ret i1 %cmp
}
+; @g2_weak may be null, in which case this is a zero-index GEP and the pointers
+; are equal.
define i1 @null_gep_ne_null() {
; CHECK-LABEL: @null_gep_ne_null(
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: ret i1 icmp ne (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
;
%gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
%cmp = icmp ne i8* %gep, null
@@ -129,7 +131,7 @@ define i1 @null_gep_ne_null() {
define i1 @null_gep_ugt_null() {
; CHECK-LABEL: @null_gep_ugt_null(
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: ret i1 icmp ugt (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
;
%gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
%cmp = icmp ugt i8* %gep, null