[PATCH] D154307: [InstructionSimplify] Avoid simplifying ICmp without parent

Anshil Gandhi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 2 13:38:27 PDT 2023


gandhi21299 created this revision.
gandhi21299 added reviewers: arsenm, nikic.
Herald added subscribers: kmitropoulou, StephenFan, hiraditya.
Herald added a project: All.
gandhi21299 requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Prior to simplifying ICmp dominated by an assume intrinsic,
the ICmp may be removed from it's parent basic block. This patch
fixes https://github.com/llvm/llvm-project/issues/63337.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154307

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/NewGVN/assume-dominating-icmp.ll


Index: llvm/test/Transforms/NewGVN/assume-dominating-icmp.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/NewGVN/assume-dominating-icmp.ll
@@ -0,0 +1,62 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -passes=newgvn -S < %s | FileCheck %s
+
+ at c = dso_local local_unnamed_addr global i32 0, align 4
+
+; Function Attrs: nounwind optsize uwtable
+define dso_local i32 @main(i1 %cond, i32 %0, i32 %1) {
+; CHECK-LABEL: define dso_local i32 @main
+; CHECK-SAME: (i1 [[COND:%.*]], i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK:       if.then:
+; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[TMP0]], 1
+; CHECK-NEXT:    [[TOBOOL1_NOT:%.*]] = icmp eq i32 [[TMP1]], 0
+; CHECK-NEXT:    br i1 [[TOBOOL1_NOT]], label [[IF_END6:%.*]], label [[IF_THEN2:%.*]]
+; CHECK:       if.then2:
+; CHECK-NEXT:    [[TOBOOL3_NOT:%.*]] = icmp ne i32 [[XOR]], 0
+; CHECK-NEXT:    tail call void @llvm.assume(i1 [[TOBOOL3_NOT]])
+; CHECK-NEXT:    br label [[IF_END6]]
+; CHECK:       if.end6:
+; CHECK-NEXT:    [[F_0:%.*]] = phi i32 [ undef, [[ENTRY:%.*]] ], [ [[XOR]], [[IF_THEN:%.*]] ], [ [[XOR]], [[IF_THEN2]] ]
+; CHECK-NEXT:    [[NOT:%.*]] = xor i32 [[F_0]], -1
+; CHECK-NEXT:    [[TMP2:%.*]] = load i32, ptr @c, align 4
+; CHECK-NEXT:    [[OR:%.*]] = or i32 [[TMP2]], [[NOT]]
+; CHECK-NEXT:    [[TOBOOL7_NOT:%.*]] = icmp eq i32 [[OR]], 0
+; CHECK-NEXT:    [[TOBOOL9_NOT:%.*]] = icmp eq i32 [[F_0]], 0
+; CHECK-NEXT:    [[OR_COND:%.*]] = or i1 [[TOBOOL7_NOT]], [[TOBOOL9_NOT]]
+; CHECK-NEXT:    br i1 [[OR_COND]], label [[IF_END10:%.*]], label [[WHILE_COND_PREHEADER:%.*]]
+; CHECK:       while.cond.preheader:
+; CHECK-NEXT:    ret i32 1
+; CHECK:       if.end10:
+; CHECK-NEXT:    ret i32 0
+;
+entry:
+  br i1 %cond, label %if.then, label %if.end6
+
+if.then:                                          ; preds = %entry
+  %xor = xor i32 %0, 1
+  %tobool1.not = icmp eq i32 %1, 0
+  br i1 %tobool1.not, label %if.end6, label %if.then2
+
+if.then2:                                         ; preds = %if.then
+  %tobool3.not = icmp ne i32 %xor, 0
+  tail call void @llvm.assume(i1 %tobool3.not)
+  br label %if.end6
+
+if.end6:                                          ; preds = %if.then2, %if.then, %entry
+  %f.0 = phi i32 [ undef, %entry ], [ %xor, %if.then ], [ %xor, %if.then2 ]
+  %not = xor i32 %f.0, -1
+  %2 = load i32, ptr @c, align 4
+  %or = or i32 %2, %not
+  %tobool7.not = icmp eq i32 %or, 0
+  %tobool9.not = icmp eq i32 %f.0, 0
+  %or.cond = or i1 %tobool7.not, %tobool9.not
+  br i1 %or.cond, label %if.end10, label %while.cond.preheader
+
+while.cond.preheader:                             ; preds = %if.end6
+  ret i32 1
+
+if.end10:                                         ; preds = %if.end6
+  ret i32 0
+}
+
+declare void @llvm.assume(i1 noundef)
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3662,7 +3662,7 @@
                                                Value *LHS, Value *RHS,
                                                const SimplifyQuery &Q) {
   // Gracefully handle instructions that have not been inserted yet.
-  if (!Q.AC || !Q.CxtI)
+  if (!Q.AC || !Q.CxtI || !Q.CxtI->getParent())
     return nullptr;
 
   for (Value *AssumeBaseOp : {LHS, RHS}) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154307.536624.patch
Type: text/x-patch
Size: 3456 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230702/a23cefc7/attachment.bin>


More information about the llvm-commits mailing list