Skip to content

Commit 05a4770

Browse files
committed
[ConstantFold] Fix incorrect type assumptions
If a pointer isn't a constant expression, global or block address, it's not guaranteed to be a null pointer. It can also be a no_cfi or dso_local_equivalent constant.
1 parent 236197a commit 05a4770

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,30 +1172,24 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
11721172
}
11731173

11741174
if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1175-
// Now we know that the RHS is a BlockAddress or simple
1176-
// constant (which, since the types must match, means that it is a
1177-
// ConstantPointerNull).
1175+
// Now we know that the RHS is a BlockAddress or simple constant.
11781176
if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
11791177
// Block address in another function can't equal this one, but block
11801178
// addresses in the current function might be the same if blocks are
11811179
// empty.
11821180
if (BA2->getFunction() != BA->getFunction())
11831181
return ICmpInst::ICMP_NE;
1184-
} else {
1185-
// Block addresses aren't null.
1186-
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1182+
} else if (isa<ConstantPointerNull>(V2)) {
11871183
return ICmpInst::ICMP_NE;
11881184
}
11891185
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
11901186
// Now we know that the RHS is a GlobalValue, BlockAddress or simple
1191-
// constant (which, since the types must match, means that it's a
1192-
// ConstantPointerNull).
1187+
// constant.
11931188
if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
11941189
return areGlobalsPotentiallyEqual(GV, GV2);
11951190
} else if (isa<BlockAddress>(V2)) {
11961191
return ICmpInst::ICMP_NE; // Globals never equal labels.
1197-
} else {
1198-
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1192+
} else if (isa<ConstantPointerNull>(V2)) {
11991193
// GlobalVals can never be null unless they have external weak linkage.
12001194
// We don't try to evaluate aliases here.
12011195
// NOTE: We should not be doing this constant folding if null pointer

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,26 @@ define i1 @global_gep_ugt_global_gep_complex() {
275275
%cmp = icmp ugt ptr %gep3, @g
276276
ret i1 %cmp
277277
}
278+
279+
declare void @func()
280+
281+
define i1 @global_no_cfi() {
282+
; CHECK-LABEL: @global_no_cfi(
283+
; CHECK-NEXT: ret i1 icmp eq (ptr @func, ptr no_cfi @func)
284+
;
285+
%cmp = icmp eq ptr @func, no_cfi @func
286+
ret i1 %cmp
287+
}
288+
289+
define i1 @blockaddr_no_cfi() {
290+
; CHECK-LABEL: @blockaddr_no_cfi(
291+
; CHECK-NEXT: br label [[BB:%.*]]
292+
; CHECK: bb:
293+
; CHECK-NEXT: ret i1 icmp eq (ptr blockaddress(@blockaddr_no_cfi, [[BB]]), ptr no_cfi @func)
294+
;
295+
br label %bb
296+
297+
bb:
298+
%cmp = icmp eq ptr blockaddress(@blockaddr_no_cfi, %bb), no_cfi @func
299+
ret i1 %cmp
300+
}

0 commit comments

Comments
 (0)