Skip to content

[InstCombine] Fix null pointer dereference in foldCmpLoadFromIndexedGlobal #93050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
// Find out if the comparison would be true or false for the i'th element.
Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
CompareRHS, DL, &TLI);

if (!C)
return nullptr;

// If the result is undef for this element, ignore it.
if (isa<UndefValue>(C)) {
// Extend range state machines to cover this element in case there is an
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/Transforms/InstCombine/pr93029.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instcombine < %s | FileCheck %s

@global = external global i32
@global_arr = constant [2 x ptr] [ptr @global, ptr @global]

define i1 @pr93029(i64 %idx) {
; CHECK-LABEL: define i1 @pr93029(
; CHECK-SAME: i64 [[IDX:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr [2 x ptr], ptr @global_arr, i64 0, i64 [[IDX]]
; CHECK-NEXT: [[PTR:%.*]] = load ptr, ptr [[ARRAYIDX]], align 8
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[PTR]], inttoptr (i64 10001 to ptr)
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
%arrayidx = getelementptr [2 x ptr], ptr @global_arr, i64 0, i64 %idx
%ptr = load ptr, ptr %arrayidx, align 8
%cmp = icmp ult ptr %ptr, inttoptr (i64 10001 to ptr)
ret i1 %cmp
}
Loading