[PATCH] D104063: [SimplifyCFG] avoid crash on degenerate loop

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 10 14:30:21 PDT 2021


spatel created this revision.
spatel added reviewers: lebedev.ri, reames, RKSimon.
Herald added subscribers: hiraditya, mcrosier.
spatel requested review of this revision.
Herald added a project: LLVM.

I'm not sure how to describe the code pattern in the test based on:
https://llvm.org/PR50638
...but if the `IfCond` is itself the phi that we are trying to remove, then the loop at line 2835 can end up with something like:

  %cmp = select i1 %cmp, i1 false, i1 true

And that can then lead to an assert somewhere else (although I'm still not seeing that locally in my release + asserts build). I think this can only happen with unreachable code.


https://reviews.llvm.org/D104063

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll


Index: llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
+++ llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
@@ -23,4 +23,33 @@
 
 }
 
+ at a = external dso_local global i32, align 4
+
+define i32 @PR50638() {
+; CHECK-LABEL: @PR50638(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    store i32 0, i32* @a, align 4
+; CHECK-NEXT:    ret i32 0
+;
+entry:
+  store i32 0, i32* @a, align 4
+  br label %pre.for
+
+pre.for:
+  %tobool.not = phi i1 [ false, %for ], [ true, %entry ]
+  br i1 %tobool.not, label %end, label %for
+
+for:
+  %cmp = phi i1 [ true, %pre.for ], [ false, %post.for ]
+  %storemerge = phi i32 [ 0, %pre.for ], [ 1, %post.for ]
+  store i32 %storemerge, i32* @a, align 4
+  br i1 %cmp, label %post.for, label %pre.for
+
+post.for:
+  br label %for
+
+end:
+  ret i32 0
+}
+
 !0 = !{!"branch_weights", i32 4, i32 64}
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2710,7 +2710,7 @@
   Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
   if (!IfCond ||
       // Don't bother if the branch will be constant folded trivially.
-      isa<ConstantInt>(IfCond))
+      isa<ConstantInt>(IfCond) || IfCond == PN)
     return false;
 
   // Okay, we found that we can merge this two-entry phi node into a select.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104063.351260.patch
Type: text/x-patch
Size: 1528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210610/5e732457/attachment.bin>


More information about the llvm-commits mailing list