[PATCH] D109054: Small improvement in SimplifyCFG.cpp

Dmitry Bakunevich via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 1 04:35:38 PDT 2021


dbakunevich created this revision.
dbakunevich added a reviewer: mkazantsev.
Herald added a subscriber: hiraditya.
dbakunevich requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This improvement aims to add "assume" after removing "undef" from "phinode" to improve optimization.

The piece of code that I changed initially just removed the edge from the parent block that came in phinode with undef. Since the code simply removed the edge, there was no information left in the parent block under what condition the transition from the previous block to the current one was made. Let me give you an example. We have a variable that is always non-negative, and if it is negative, then it is undef. Therefore, this function will remove the edge on phinode with undef and make the jump unconditional. And this, in turn, will interfere with optimization, which calculates the range of values ​​of the variable. Therefore, it is necessary to add assume, which will clearly show that the variable is non-negative.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109054

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll


Index: llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll
+++ llvm/test/Transforms/SimplifyCFG/tautological-conditional-branch.ll
@@ -14,8 +14,12 @@
 ; CHECK-NEXT:    unreachable
 ; CHECK:       bb21:
 ; CHECK-NEXT:    [[I:%.*]] = icmp eq %0* [[ARG17:%.*]], null
+; CHECK-NEXT:    [[TMP0:%.*]] = xor i1 [[I]], true
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP0]])
 ; CHECK-NEXT:    call void @hoge()
-; CHECK-NEXT:    [[I27:%.*]] = getelementptr inbounds [[TMP0:%.*]], %0* [[ARG19:%.*]], i64 0, i32 0
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[ARG16:%.*]], true
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP1]])
+; CHECK-NEXT:    [[I27:%.*]] = getelementptr inbounds [[TMP0]], %0* [[ARG19:%.*]], i64 0, i32 0
 ; CHECK-NEXT:    [[I28:%.*]] = load %1*, %1** [[I27]], align 8
 ; CHECK-NEXT:    call void @pluto.1(%1* [[I28]])
 ; CHECK-NEXT:    call void @pluto()
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6598,9 +6598,15 @@
           // destination from conditional branches.
           if (BI->isUnconditional())
             Builder.CreateUnreachable();
-          else
+          else {
+            Value* Cond = BI->getCondition();
+            if (BI->getSuccessor(0) == BB)
+              Builder.CreateAssumption(Builder.CreateNot(Cond));
+            else
+              Builder.CreateAssumption(Cond);
             Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
                                                        : BI->getSuccessor(0));
+          }
           BI->eraseFromParent();
           if (DTU)
             DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}});


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109054.369897.patch
Type: text/x-patch
Size: 1947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210901/5749dc51/attachment.bin>


More information about the llvm-commits mailing list