[llvm] 5155c38 - [InstCombine] Don't check uses of constant exprs (#113684)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 00:09:23 PDT 2024


Author: Yingwei Zheng
Date: 2024-10-28T15:09:20+08:00
New Revision: 5155c38ceef019f3225c5eaeba6b986e2430752f

URL: https://github.com/llvm/llvm-project/commit/5155c38ceef019f3225c5eaeba6b986e2430752f
DIFF: https://github.com/llvm/llvm-project/commit/5155c38ceef019f3225c5eaeba6b986e2430752f.diff

LOG: [InstCombine] Don't check uses of constant exprs (#113684)

This patch skips constant expressions to avoid iterating over uses on
other functions.

Fix crash reported in
https://github.com/llvm/llvm-project/pull/105510#issuecomment-2437521147.

Added: 
    llvm/test/Transforms/InstCombine/pr105510.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 971ace2a4f4716..73a4705531781a 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -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)) {

diff  --git a/llvm/test/Transforms/InstCombine/pr105510.ll b/llvm/test/Transforms/InstCombine/pr105510.ll
new file mode 100644
index 00000000000000..844fa14ad991ee
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr105510.ll
@@ -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.
+ at 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
+}


        


More information about the llvm-commits mailing list