[PATCH] D50247: [GuardWidening] Ignore guards with trivial conditions

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 3 05:28:42 PDT 2018


mkazantsev created this revision.
mkazantsev added reviewers: fedor.sergeev, apilipenko, sanjoy, reames.

Guard widening should not spend efforts on dealing with guards with trivial true/false conditions.
Such guards can easily be eliminated by any further cleanup pass like instcombine. However we
should not unconditionally delete them because it may be profitable to widen other conditions
into such guards.


https://reviews.llvm.org/D50247

Files:
  lib/Transforms/Scalar/GuardWidening.cpp
  test/Transforms/GuardWidening/basic.ll


Index: test/Transforms/GuardWidening/basic.ll
===================================================================
--- test/Transforms/GuardWidening/basic.ll
+++ test/Transforms/GuardWidening/basic.ll
@@ -379,3 +379,29 @@
 right:
   ret void
 }
+
+; Make sure we do not widen guard by trivial true conditions into something.
+define void @f_15(i1 %cond_0, i1 %cond_1) {
+; CHECK-LABEL: @f_15(
+entry:
+; CHECK:  call void (i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"() ]
+; CHECK:  call void (i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ]
+; CHECK:  ret void
+
+  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"() ]
+  call void(i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ]
+  ret void
+}
+
+; Make sure we do not widen guard by trivial false conditions into something.
+define void @f_16(i1 %cond_0, i1 %cond_1) {
+; CHECK-LABEL: @f_16(
+entry:
+; CHECK:  call void (i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"() ]
+; CHECK:  call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
+; CHECK:  ret void
+
+  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"() ]
+  call void(i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
+  ret void
+}
Index: lib/Transforms/Scalar/GuardWidening.cpp
===================================================================
--- lib/Transforms/Scalar/GuardWidening.cpp
+++ lib/Transforms/Scalar/GuardWidening.cpp
@@ -344,6 +344,15 @@
     Instruction *GuardInst, const df_iterator<DomTreeNode *> &DFSI,
     const DenseMap<BasicBlock *, SmallVector<Instruction *, 8>> &
         GuardsInBlock, bool InvertCondition) {
+  auto *Cond = getCondition(GuardInst);
+  auto *True = ConstantInt::getTrue(GuardInst->getContext());
+  auto *False = ConstantInt::getFalse(GuardInst->getContext());
+  // Ignore trivial true or false conditions. These instructions will be
+  // trivially eliminated by any cleanup pass. Do not erase them because other
+  // guards can possibly be widened into them.
+  if (Cond == True || Cond == False)
+    return false;
+
   Instruction *BestSoFar = nullptr;
   auto BestScoreSoFar = WS_IllegalOrNegative;
   auto *GuardInstLoop = LI.getLoopFor(GuardInst->getParent());
@@ -388,7 +397,7 @@
     for (auto *Candidate : make_range(I, E)) {
       auto Score =
           computeWideningScore(GuardInst, GuardInstLoop, Candidate, CurLoop);
-      LLVM_DEBUG(dbgs() << "Score between " << *getCondition(GuardInst)
+      LLVM_DEBUG(dbgs() << "Score between " << *Cond
                         << " and " << *getCondition(Candidate) << " is "
                         << scoreTypeToString(Score) << "\n");
       if (Score > BestScoreSoFar) {
@@ -410,9 +419,7 @@
                     << " with score " << scoreTypeToString(BestScoreSoFar)
                     << "\n");
   widenGuard(BestSoFar, getCondition(GuardInst), InvertCondition);
-  auto NewGuardCondition = InvertCondition
-                               ? ConstantInt::getFalse(GuardInst->getContext())
-                               : ConstantInt::getTrue(GuardInst->getContext());
+  auto *NewGuardCondition = InvertCondition ? False : True;
   setCondition(GuardInst, NewGuardCondition);
   EliminatedGuardsAndBranches.push_back(GuardInst);
   WidenedGuards.insert(BestSoFar);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50247.158986.patch
Type: text/x-patch
Size: 3293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180803/24e8f1c3/attachment.bin>


More information about the llvm-commits mailing list