[PATCH] D88014: [LoopUnswitch] Trivial simplification: remove trivial dead condition after unswitch

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 04:05:00 PDT 2020


skatkov created this revision.
skatkov added reviewers: asbirlea, aqjune, fhahn, DaniilSuchkov.
Herald added subscribers: dantrushin, hiraditya.
Herald added a project: LLVM.
skatkov requested review of this revision.

Non trivial loop unswitch can keep the dead condition instruction.
CL adds trivial dead code elimination for unused condition.


https://reviews.llvm.org/D88014

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
@@ -2720,8 +2720,6 @@
 ; CHECK-NEXT:    br label %loop_begin.us
 ;
 ; CHECK:       loop_begin.us:
-; CHECK-NEXT:    %[[V1_US:.*]] = load i1, i1* %ptr
-; CHECK-NEXT:    %[[OR_US:.*]] = or i1 %[[V1_US]], true
 ; CHECK-NEXT:    br label %loop_a.us
 ;
 ; CHECK:       loop_a.us:
@@ -2796,12 +2794,6 @@
 ; CHECK-NEXT:    br label %loop_begin.us
 ;
 ; CHECK:       loop_begin.us:
-; CHECK-NEXT:    %[[V1_US:.*]] = load i1, i1* %ptr1
-; CHECK-NEXT:    %[[V2_US:.*]] = load i1, i1* %ptr2
-; CHECK-NEXT:    %[[AND1_US:.*]] = and i1 %[[V1_US]], %cond1
-; CHECK-NEXT:    %[[OR1_US:.*]] = or i1 %[[V2_US]], %cond2
-; CHECK-NEXT:    %[[AND2_US:.*]] = and i1 %[[AND1_US]], %[[OR1_US]]
-; CHECK-NEXT:    %[[AND3_US:.*]] = and i1 %[[AND2_US]], %cond3
 ; CHECK-NEXT:    br label %loop_b.us
 ;
 ; CHECK:       loop_b.us:
@@ -2883,12 +2875,6 @@
 ; CHECK-NEXT:    br label %loop_begin.us
 ;
 ; CHECK:       loop_begin.us:
-; CHECK-NEXT:    %[[V1_US:.*]] = load i1, i1* %ptr1
-; CHECK-NEXT:    %[[V2_US:.*]] = load i1, i1* %ptr2
-; CHECK-NEXT:    %[[OR1_US:.*]] = or i1 %[[V1_US]], %cond1
-; CHECK-NEXT:    %[[AND1_US:.*]] = and i1 %[[V2_US]], %cond2
-; CHECK-NEXT:    %[[OR2_US:.*]] = or i1 %[[OR1_US]], %[[AND1_US]]
-; CHECK-NEXT:    %[[OR3_US:.*]] = or i1 %[[OR2_US]], %cond3
 ; CHECK-NEXT:    br label %loop_b.us
 ;
 ; CHECK:       loop_b.us:
Index: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -50,6 +50,7 @@
 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/LoopUtils.h"
 #include "llvm/Transforms/Utils/ValueMapper.h"
 #include <algorithm>
@@ -1139,9 +1140,21 @@
   // Replace the cloned branch with an unconditional branch to the cloned
   // unswitched successor.
   auto *ClonedSuccBB = cast<BasicBlock>(VMap.lookup(UnswitchedSuccBB));
-  ClonedParentBB->getTerminator()->eraseFromParent();
+  Instruction *ClonedTerminator = ClonedParentBB->getTerminator();
+  // Trivial Simplification. If Terminator is a conditional branch and
+  // condition becomes dead - erase it.
+  Value *ClonedConditionToErase = nullptr;
+  if (auto *BI = dyn_cast<BranchInst>(ClonedTerminator))
+    ClonedConditionToErase = BI->getCondition();
+  else if (auto *SI = dyn_cast<SwitchInst>(ClonedTerminator))
+    ClonedConditionToErase = SI->getCondition();
+
+  ClonedTerminator->eraseFromParent();
   BranchInst::Create(ClonedSuccBB, ClonedParentBB);
 
+  if (ClonedConditionToErase)
+    RecursivelyDeleteTriviallyDeadInstructions(ClonedConditionToErase);
+
   // If there are duplicate entries in the PHI nodes because of multiple edges
   // to the unswitched successor, we need to nuke all but one as we replaced it
   // with a direct branch.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88014.293127.patch
Type: text/x-patch
Size: 3251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200921/76acb700/attachment.bin>


More information about the llvm-commits mailing list