Skip to content

Commit 75db002

Browse files
committed
[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.
1 parent aefab6f commit 75db002

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,17 +1539,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
15391539
// so the result is greater-than
15401540
if (!GV->hasExternalWeakLinkage())
15411541
return ICmpInst::ICMP_UGT;
1542-
} else if (isa<ConstantPointerNull>(CE1Op0)) {
1543-
// If we are indexing from a null pointer, check to see if we have any
1544-
// non-zero indices.
1545-
for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1546-
if (!CE1->getOperand(i)->isNullValue())
1547-
// Offsetting from null, must not be equal.
1548-
return ICmpInst::ICMP_UGT;
1549-
// Only zero indexes from null, must still be zero.
1550-
return ICmpInst::ICMP_EQ;
15511542
}
1552-
// Otherwise, we can't really say if the first operand is null or not.
15531543
} else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
15541544
if (isa<ConstantPointerNull>(CE1Op0)) {
15551545
// If its not weak linkage, the GVal must have a non-zero address

llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ define i1 @global_gep_sgt_null() {
118118
ret i1 %cmp
119119
}
120120

121+
; @g2_weak may be null, in which case this is a zero-index GEP and the pointers
122+
; are equal.
121123
define i1 @null_gep_ne_null() {
122124
; CHECK-LABEL: @null_gep_ne_null(
123-
; CHECK-NEXT: ret i1 true
125+
; CHECK-NEXT: ret i1 icmp ne (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
124126
;
125127
%gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
126128
%cmp = icmp ne i8* %gep, null
@@ -129,7 +131,7 @@ define i1 @null_gep_ne_null() {
129131

130132
define i1 @null_gep_ugt_null() {
131133
; CHECK-LABEL: @null_gep_ugt_null(
132-
; CHECK-NEXT: ret i1 true
134+
; CHECK-NEXT: ret i1 icmp ugt (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
133135
;
134136
%gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
135137
%cmp = icmp ugt i8* %gep, null

0 commit comments

Comments
 (0)