[PATCH] D52362: [CloneFunction] Simplify previously unsimplifiable instructions

Anna Thomas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 21 08:24:38 PDT 2018


anna created this revision.
anna added reviewers: majnemer, philip, apilipenko, eraman.

Sometimes during clone function, we may not be able to simplify during
the first phase of cloning: CloneBlock. This is because the instruction
may not be tied to any basic block yet (as the case when cloning is
called during inlining).

This patch adds all instructions from the cloned function into the
second pass of simplification, which is done after the instructions are
tied to basic block. We reuse the second pass which was previously handling phi
nodes and their uses.

The benefit of this patch is to simplify the instructions that are
inlined from the callee to the caller, so that later passes after
inlining have fewer instructions to churn on (see test case for example).


Repository:
  rL LLVM

https://reviews.llvm.org/D52362

Files:
  lib/Transforms/Utils/CloneFunction.cpp
  test/Transforms/Inline/inline_inv_group.ll


Index: test/Transforms/Inline/inline_inv_group.ll
===================================================================
--- test/Transforms/Inline/inline_inv_group.ll
+++ test/Transforms/Inline/inline_inv_group.ll
@@ -4,14 +4,13 @@
 target triple = "x86_64-unknown-linux-gnu"
 
 define i8* @callee() alwaysinline {
-; CHECK-LABEL: define i8* @callee()
     %1 = call i8* @llvm.strip.invariant.group.p0i8(i8* null)
     ret i8* %1
 }
 
 define i8* @caller() {
 ; CHECK-LABEL: define i8* @caller()
-; CHECK-NEXT: call i8* @llvm.strip.invariant.group.p0i8(i8* null)
+; CHECK-NEXT: ret i8* null
     %1 = call i8* @callee()
     ret i8* %1
 }
Index: lib/Transforms/Utils/CloneFunction.cpp
===================================================================
--- lib/Transforms/Utils/CloneFunction.cpp
+++ lib/Transforms/Utils/CloneFunction.cpp
@@ -475,11 +475,14 @@
   //
   // Defer PHI resolution until rest of function is resolved.
   SmallVector<const PHINode*, 16> PHIToResolve;
+  std::vector<const BasicBlock*> ReachableBBsInOldFunc;
   for (const BasicBlock &BI : *OldFunc) {
     Value *V = VMap.lookup(&BI);
     BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
     if (!NewBB) continue;  // Dead block.
 
+    // This was a reachable BB from the StartingBB.
+    ReachableBBsInOldFunc.push_back(&BI);
     // Add the new block to the new function.
     NewFunc->getBasicBlockList().push_back(NewBB);
 
@@ -595,6 +598,18 @@
     if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
       Worklist.insert(PHIToResolve[Idx]);
 
+  // In this second pass, we also add all instructions reachable from
+  // StartingInst.  This allows simplification of instructions that previously
+  // could not be simplified because they were not tied to any basic block.
+  for (auto &I: make_range(StartingInst->getIterator(), StartingBB->end()))
+     Worklist.insert(&I);
+
+  for (auto *BB: ReachableBBsInOldFunc) {
+    if (BB == StartingBB)
+      continue;
+    for (auto &I: *BB)
+      Worklist.insert(&I);
+  }
   // Note that we must test the size on each iteration, the worklist can grow.
   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
     const Value *OrigV = Worklist[Idx];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52362.166487.patch
Type: text/x-patch
Size: 2179 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180921/49a1e59e/attachment.bin>


More information about the llvm-commits mailing list