Skip to content

Commit

Permalink
[ConstantFold] Fix incorrect type assumptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Nov 3, 2023
1 parent 236197a commit 05a4770
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
14 changes: 4 additions & 10 deletions llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,30 +1172,24 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
}

if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
// Now we know that the RHS is a BlockAddress or simple
// constant (which, since the types must match, means that it is a
// ConstantPointerNull).
// Now we know that the RHS is a BlockAddress or simple constant.
if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
// Block address in another function can't equal this one, but block
// addresses in the current function might be the same if blocks are
// empty.
if (BA2->getFunction() != BA->getFunction())
return ICmpInst::ICMP_NE;
} else {
// Block addresses aren't null.
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
} else if (isa<ConstantPointerNull>(V2)) {
return ICmpInst::ICMP_NE;
}
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
// Now we know that the RHS is a GlobalValue, BlockAddress or simple
// constant (which, since the types must match, means that it's a
// ConstantPointerNull).
// constant.
if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
return areGlobalsPotentiallyEqual(GV, GV2);
} else if (isa<BlockAddress>(V2)) {
return ICmpInst::ICMP_NE; // Globals never equal labels.
} else {
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
} else if (isa<ConstantPointerNull>(V2)) {
// GlobalVals can never be null unless they have external weak linkage.
// We don't try to evaluate aliases here.
// NOTE: We should not be doing this constant folding if null pointer
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,26 @@ define i1 @global_gep_ugt_global_gep_complex() {
%cmp = icmp ugt ptr %gep3, @g
ret i1 %cmp
}

declare void @func()

define i1 @global_no_cfi() {
; CHECK-LABEL: @global_no_cfi(
; CHECK-NEXT: ret i1 icmp eq (ptr @func, ptr no_cfi @func)
;
%cmp = icmp eq ptr @func, no_cfi @func
ret i1 %cmp
}

define i1 @blockaddr_no_cfi() {
; CHECK-LABEL: @blockaddr_no_cfi(
; CHECK-NEXT: br label [[BB:%.*]]
; CHECK: bb:
; CHECK-NEXT: ret i1 icmp eq (ptr blockaddress(@blockaddr_no_cfi, [[BB]]), ptr no_cfi @func)
;
br label %bb

bb:
%cmp = icmp eq ptr blockaddress(@blockaddr_no_cfi, %bb), no_cfi @func
ret i1 %cmp
}

0 comments on commit 05a4770

Please sign in to comment.