[PATCH] D124603: [SimpleLoopUnswitch] Add freeze if branch execs for partial unswitching.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 28 03:03:10 PDT 2022


fhahn created this revision.
fhahn added reviewers: asbirlea, reames, nlopes, aqjune, nikic.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

We cannot skip the freezing the condition if the unswitched branch
executes, if the condition is a chain of ANDs/ORs. For example, if if we
have an AND %c1, %c2 with  %c1 == undef and %c2 == 0, there would be no
branch on undef in the original code, but a branch on undef if we
unswitch %c1.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124603

Files:
  llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
  llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll


Index: llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
===================================================================
--- llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
+++ llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
@@ -2717,7 +2717,8 @@
 entry:
   br label %loop_begin
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    br i1 %cond, label %entry.split.us, label %entry.split
+; CHECK-NEXT:    [[FROZEN:%.+]] = freeze i1 %cond
+; CHECK-NEXT:    br i1 [[FROZEN]], label %entry.split.us, label %entry.split
 
 loop_begin:
   %v1 = load i1, i1* %ptr
@@ -2790,7 +2791,9 @@
 entry:
   br label %loop_begin
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    %[[INV_AND:.*]] = and i1 %cond3, %cond1
+; CHECK-NEXT:    [[C3_FR:%.+]] = freeze i1 %cond3
+; CHECK-NEXT:    [[C1_FR:%.+]] = freeze i1 %cond1
+; CHECK-NEXT:    %[[INV_AND:.*]] = and i1 [[C3_FR]], [[C1_FR]]
 ; CHECK-NEXT:    br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
 
 loop_begin:
@@ -2871,7 +2874,9 @@
 entry:
   br label %loop_begin
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    %[[INV_OR:.*]] = or i1 %cond3, %cond1
+; CHECK-NEXT:    [[C3_FR:%.+]] = freeze i1 %cond3
+; CHECK-NEXT:    [[C1_FR:%.+]] = freeze i1 %cond1
+; CHECK-NEXT:    %[[INV_OR:.*]] = or i1 [[C3_FR]], [[C1_FR]]
 ; CHECK-NEXT:    br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
 
 loop_begin:
@@ -4231,7 +4236,9 @@
 entry:
   br label %loop_begin
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    %[[INV_AND:.*]] = and i1 %cond2, %cond1
+; CHECK-NEXT:    [[C2_FR:%.+]] = freeze i1 %cond2
+; CHECK-NEXT:    [[C1_FR:%.+]] = freeze i1 %cond1
+; CHECK-NEXT:    %[[INV_AND:.*]] = and i1 [[C2_FR]], [[C1_FR]]
 ; CHECK-NEXT:    br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
 
 loop_begin:
@@ -4309,7 +4316,9 @@
 entry:
   br label %loop_begin
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    %[[INV_OR:.*]] = or i1 %cond2, %cond1
+; CHECK-NEXT:    [[C2_FR:%.+]] = freeze i1 %cond2
+; CHECK-NEXT:    [[C1_FR:%.+]] = freeze i1 %cond1
+; CHECK-NEXT:    %[[INV_OR:.*]] = or i1 [[C2_FR]], [[C1_FR]]
 ; CHECK-NEXT:    br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
 
 loop_begin:
Index: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -2330,9 +2330,9 @@
       buildPartialInvariantUnswitchConditionalBranch(
           *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU);
     else {
-      buildPartialUnswitchConditionalBranch(*SplitBB, Invariants, Direction,
-                                            *ClonedPH, *LoopPH, InsertFreeze,
-                                            BI, &AC, DT);
+      buildPartialUnswitchConditionalBranch(
+          *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH,
+          FreezeLoopUnswitchCond, BI, &AC, DT);
     }
     DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH});
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124603.425736.patch
Type: text/x-patch
Size: 3018 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220428/e3ec402d/attachment.bin>


More information about the llvm-commits mailing list