Skip to content

Commit

Permalink
[InstCombine] Don't check uses of constant exprs (llvm#113684)
Browse files Browse the repository at this point in the history
This patch skips constant expressions to avoid iterating over uses on
other functions.

Fix crash reported in
llvm#105510 (comment).
  • Loading branch information
dtcxzyw authored Oct 28, 2024
1 parent 819abe4 commit 5155c38
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3753,7 +3753,9 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
}

// Replace all dominated uses of the condition with true/false
if (BI.getSuccessor(0) != BI.getSuccessor(1)) {
// Ignore constant expressions to avoid iterating over uses on other
// functions.
if (!isa<Constant>(Cond) && BI.getSuccessor(0) != BI.getSuccessor(1)) {
for (auto &U : make_early_inc_range(Cond->uses())) {
BasicBlockEdge Edge0(BI.getParent(), BI.getSuccessor(0));
if (DT.dominates(Edge0, U)) {
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/Transforms/InstCombine/pr105510.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instcombine < %s | FileCheck %s

; Make sure we don't crash in this case.
@g = global i32 0

define i1 @foo() {
; CHECK-LABEL: define i1 @foo() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br i1 ptrtoint (ptr @g to i1), label %[[IF_THEN:.*]], label %[[IF_ELSE:.*]]
; CHECK: [[IF_THEN]]:
; CHECK-NEXT: ret i1 true
; CHECK: [[IF_ELSE]]:
; CHECK-NEXT: ret i1 false
;
entry:
br i1 ptrtoint (ptr @g to i1), label %if.then, label %if.else

if.then:
ret i1 true

if.else:
ret i1 false
}

define i1 @bar() {
; CHECK-LABEL: define i1 @bar() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br i1 ptrtoint (ptr @g to i1), label %[[IF_THEN:.*]], label %[[IF_ELSE:.*]]
; CHECK: [[IF_THEN]]:
; CHECK-NEXT: ret i1 true
; CHECK: [[IF_ELSE]]:
; CHECK-NEXT: ret i1 false
;
entry:
br i1 ptrtoint (ptr @g to i1), label %if.then, label %if.else

if.then:
ret i1 true

if.else:
ret i1 false
}

0 comments on commit 5155c38

Please sign in to comment.