10b57cec5SDimitry Andric //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass transforms loops by placing phi nodes at the end of the loops for
100b57cec5SDimitry Andric // all values that are live across the loop boundary. For example, it turns
110b57cec5SDimitry Andric // the left into the right code:
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // for (...) for (...)
140b57cec5SDimitry Andric // if (c) if (c)
150b57cec5SDimitry Andric // X1 = ... X1 = ...
160b57cec5SDimitry Andric // else else
170b57cec5SDimitry Andric // X2 = ... X2 = ...
180b57cec5SDimitry Andric // X3 = phi(X1, X2) X3 = phi(X1, X2)
190b57cec5SDimitry Andric // ... = X3 + 4 X4 = phi(X3)
200b57cec5SDimitry Andric // ... = X4 + 4
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric // This is still valid LLVM; the extra phi nodes are purely redundant, and will
230b57cec5SDimitry Andric // be trivially eliminated by InstCombine. The major benefit of this
240b57cec5SDimitry Andric // transformation is that it makes many other loop optimizations, such as
250b57cec5SDimitry Andric // LoopUnswitching, simpler.
260b57cec5SDimitry Andric //
270b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LCSSA.h"
300b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
310b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
320b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
330b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
340b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
350b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
3681ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
370b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h"
380b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
390b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
400b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
41fe6060f1SDimitry Andric #include "llvm/IR/DebugInfo.h"
420b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
430b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
440b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
450b57cec5SDimitry Andric #include "llvm/IR/PredIteratorCache.h"
46480093f4SDimitry Andric #include "llvm/InitializePasses.h"
470b57cec5SDimitry Andric #include "llvm/Pass.h"
48480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
490b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h"
500b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
510b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdater.h"
520b57cec5SDimitry Andric using namespace llvm;
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric #define DEBUG_TYPE "lcssa"
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric STATISTIC(NumLCSSA, "Number of live out of a loop variables");
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS
590b57cec5SDimitry Andric static bool VerifyLoopLCSSA = true;
600b57cec5SDimitry Andric #else
610b57cec5SDimitry Andric static bool VerifyLoopLCSSA = false;
620b57cec5SDimitry Andric #endif
630b57cec5SDimitry Andric static cl::opt<bool, true>
640b57cec5SDimitry Andric VerifyLoopLCSSAFlag("verify-loop-lcssa", cl::location(VerifyLoopLCSSA),
650b57cec5SDimitry Andric cl::Hidden,
660b57cec5SDimitry Andric cl::desc("Verify loop lcssa form (time consuming)"));
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric /// Return true if the specified block is in the list.
isExitBlock(BasicBlock * BB,const SmallVectorImpl<BasicBlock * > & ExitBlocks)690b57cec5SDimitry Andric static bool isExitBlock(BasicBlock *BB,
700b57cec5SDimitry Andric const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
710b57cec5SDimitry Andric return is_contained(ExitBlocks, BB);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric /// For every instruction from the worklist, check to see if it has any uses
750b57cec5SDimitry Andric /// that are outside the current loop. If so, insert LCSSA PHI nodes and
760b57cec5SDimitry Andric /// rewrite the uses.
formLCSSAForInstructions(SmallVectorImpl<Instruction * > & Worklist,const DominatorTree & DT,const LoopInfo & LI,ScalarEvolution * SE,SmallVectorImpl<PHINode * > * PHIsToRemove,SmallVectorImpl<PHINode * > * InsertedPHIs)770b57cec5SDimitry Andric bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
785ffd83dbSDimitry Andric const DominatorTree &DT, const LoopInfo &LI,
7906c3fb27SDimitry Andric ScalarEvolution *SE,
8006c3fb27SDimitry Andric SmallVectorImpl<PHINode *> *PHIsToRemove,
8106c3fb27SDimitry Andric SmallVectorImpl<PHINode *> *InsertedPHIs) {
820b57cec5SDimitry Andric SmallVector<Use *, 16> UsesToRewrite;
83e8d8bef9SDimitry Andric SmallSetVector<PHINode *, 16> LocalPHIsToRemove;
840b57cec5SDimitry Andric PredIteratorCache PredCache;
850b57cec5SDimitry Andric bool Changed = false;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric // Cache the Loop ExitBlocks across this loop. We expect to get a lot of
880b57cec5SDimitry Andric // instructions within the same loops, computing the exit blocks is
890b57cec5SDimitry Andric // expensive, and we're not mutating the loop structure.
900b57cec5SDimitry Andric SmallDenseMap<Loop*, SmallVector<BasicBlock *,1>> LoopExitBlocks;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric while (!Worklist.empty()) {
930b57cec5SDimitry Andric UsesToRewrite.clear();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric Instruction *I = Worklist.pop_back_val();
960b57cec5SDimitry Andric assert(!I->getType()->isTokenTy() && "Tokens shouldn't be in the worklist");
970b57cec5SDimitry Andric BasicBlock *InstBB = I->getParent();
980b57cec5SDimitry Andric Loop *L = LI.getLoopFor(InstBB);
990b57cec5SDimitry Andric assert(L && "Instruction belongs to a BB that's not part of a loop");
1000b57cec5SDimitry Andric if (!LoopExitBlocks.count(L))
1010b57cec5SDimitry Andric L->getExitBlocks(LoopExitBlocks[L]);
1020b57cec5SDimitry Andric assert(LoopExitBlocks.count(L));
1030b57cec5SDimitry Andric const SmallVectorImpl<BasicBlock *> &ExitBlocks = LoopExitBlocks[L];
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric if (ExitBlocks.empty())
1060b57cec5SDimitry Andric continue;
1070b57cec5SDimitry Andric
108bdd1243dSDimitry Andric for (Use &U : make_early_inc_range(I->uses())) {
1090b57cec5SDimitry Andric Instruction *User = cast<Instruction>(U.getUser());
1100b57cec5SDimitry Andric BasicBlock *UserBB = User->getParent();
111e8d8bef9SDimitry Andric
112bdd1243dSDimitry Andric // Skip uses in unreachable blocks.
113bdd1243dSDimitry Andric if (!DT.isReachableFromEntry(UserBB)) {
114bdd1243dSDimitry Andric U.set(PoisonValue::get(I->getType()));
115bdd1243dSDimitry Andric continue;
116bdd1243dSDimitry Andric }
117bdd1243dSDimitry Andric
118e8d8bef9SDimitry Andric // For practical purposes, we consider that the use in a PHI
119e8d8bef9SDimitry Andric // occurs in the respective predecessor block. For more info,
120e8d8bef9SDimitry Andric // see the `phi` doc in LangRef and the LCSSA doc.
1210b57cec5SDimitry Andric if (auto *PN = dyn_cast<PHINode>(User))
1220b57cec5SDimitry Andric UserBB = PN->getIncomingBlock(U);
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric if (InstBB != UserBB && !L->contains(UserBB))
1250b57cec5SDimitry Andric UsesToRewrite.push_back(&U);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric // If there are no uses outside the loop, exit with no change.
1290b57cec5SDimitry Andric if (UsesToRewrite.empty())
1300b57cec5SDimitry Andric continue;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric ++NumLCSSA; // We are applying the transformation
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric // Invoke instructions are special in that their result value is not
1350b57cec5SDimitry Andric // available along their unwind edge. The code below tests to see whether
1360b57cec5SDimitry Andric // DomBB dominates the value, so adjust DomBB to the normal destination
1370b57cec5SDimitry Andric // block, which is effectively where the value is first usable.
1380b57cec5SDimitry Andric BasicBlock *DomBB = InstBB;
1390b57cec5SDimitry Andric if (auto *Inv = dyn_cast<InvokeInst>(I))
1400b57cec5SDimitry Andric DomBB = Inv->getNormalDest();
1410b57cec5SDimitry Andric
1425ffd83dbSDimitry Andric const DomTreeNode *DomNode = DT.getNode(DomBB);
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric SmallVector<PHINode *, 16> AddedPHIs;
1450b57cec5SDimitry Andric SmallVector<PHINode *, 8> PostProcessPHIs;
1460b57cec5SDimitry Andric
14706c3fb27SDimitry Andric SmallVector<PHINode *, 4> LocalInsertedPHIs;
14806c3fb27SDimitry Andric SSAUpdater SSAUpdate(&LocalInsertedPHIs);
1490b57cec5SDimitry Andric SSAUpdate.Initialize(I->getType(), I->getName());
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric // Insert the LCSSA phi's into all of the exit blocks dominated by the
1520b57cec5SDimitry Andric // value, and add them to the Phi's map.
15306c3fb27SDimitry Andric bool HasSCEV = SE && SE->isSCEVable(I->getType()) &&
15406c3fb27SDimitry Andric SE->getExistingSCEV(I) != nullptr;
1550b57cec5SDimitry Andric for (BasicBlock *ExitBB : ExitBlocks) {
1560b57cec5SDimitry Andric if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
1570b57cec5SDimitry Andric continue;
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric // If we already inserted something for this BB, don't reprocess it.
1600b57cec5SDimitry Andric if (SSAUpdate.HasValueForBlock(ExitBB))
1610b57cec5SDimitry Andric continue;
16206c3fb27SDimitry Andric PHINode *PN = PHINode::Create(I->getType(), PredCache.size(ExitBB),
1635f757f3fSDimitry Andric I->getName() + ".lcssa");
1645f757f3fSDimitry Andric PN->insertBefore(ExitBB->begin());
16506c3fb27SDimitry Andric if (InsertedPHIs)
16606c3fb27SDimitry Andric InsertedPHIs->push_back(PN);
1670b57cec5SDimitry Andric // Get the debug location from the original instruction.
1680b57cec5SDimitry Andric PN->setDebugLoc(I->getDebugLoc());
169e8d8bef9SDimitry Andric
170e8d8bef9SDimitry Andric // Add inputs from inside the loop for this PHI. This is valid
171e8d8bef9SDimitry Andric // because `I` dominates `ExitBB` (checked above). This implies
172e8d8bef9SDimitry Andric // that every incoming block/edge is dominated by `I` as well,
173e8d8bef9SDimitry Andric // i.e. we can add uses of `I` to those incoming edges/append to the incoming
174e8d8bef9SDimitry Andric // blocks without violating the SSA dominance property.
1750b57cec5SDimitry Andric for (BasicBlock *Pred : PredCache.get(ExitBB)) {
1760b57cec5SDimitry Andric PN->addIncoming(I, Pred);
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric // If the exit block has a predecessor not within the loop, arrange for
1790b57cec5SDimitry Andric // the incoming value use corresponding to that predecessor to be
1800b57cec5SDimitry Andric // rewritten in terms of a different LCSSA PHI.
1810b57cec5SDimitry Andric if (!L->contains(Pred))
1820b57cec5SDimitry Andric UsesToRewrite.push_back(
1830b57cec5SDimitry Andric &PN->getOperandUse(PN->getOperandNumForIncomingValue(
1840b57cec5SDimitry Andric PN->getNumIncomingValues() - 1)));
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric AddedPHIs.push_back(PN);
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric // Remember that this phi makes the value alive in this block.
1900b57cec5SDimitry Andric SSAUpdate.AddAvailableValue(ExitBB, PN);
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric // LoopSimplify might fail to simplify some loops (e.g. when indirect
1930b57cec5SDimitry Andric // branches are involved). In such situations, it might happen that an
1940b57cec5SDimitry Andric // exit for Loop L1 is the header of a disjoint Loop L2. Thus, when we
1950b57cec5SDimitry Andric // create PHIs in such an exit block, we are also inserting PHIs into L2's
1960b57cec5SDimitry Andric // header. This could break LCSSA form for L2 because these inserted PHIs
1970b57cec5SDimitry Andric // can also have uses outside of L2. Remember all PHIs in such situation
1980b57cec5SDimitry Andric // as to revisit than later on. FIXME: Remove this if indirectbr support
1990b57cec5SDimitry Andric // into LoopSimplify gets improved.
2000b57cec5SDimitry Andric if (auto *OtherLoop = LI.getLoopFor(ExitBB))
2010b57cec5SDimitry Andric if (!L->contains(OtherLoop))
2020b57cec5SDimitry Andric PostProcessPHIs.push_back(PN);
20306c3fb27SDimitry Andric
20406c3fb27SDimitry Andric // If we have a cached SCEV for the original instruction, make sure the
20506c3fb27SDimitry Andric // new LCSSA phi node is also cached. This makes sures that BECounts
20606c3fb27SDimitry Andric // based on it will be invalidated when the LCSSA phi node is invalidated,
20706c3fb27SDimitry Andric // which some passes rely on.
20806c3fb27SDimitry Andric if (HasSCEV)
20906c3fb27SDimitry Andric SE->getSCEV(PN);
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric
2120b57cec5SDimitry Andric // Rewrite all uses outside the loop in terms of the new PHIs we just
2130b57cec5SDimitry Andric // inserted.
2140b57cec5SDimitry Andric for (Use *UseToRewrite : UsesToRewrite) {
215e8d8bef9SDimitry Andric Instruction *User = cast<Instruction>(UseToRewrite->getUser());
216e8d8bef9SDimitry Andric BasicBlock *UserBB = User->getParent();
217e8d8bef9SDimitry Andric
218e8d8bef9SDimitry Andric // For practical purposes, we consider that the use in a PHI
219e8d8bef9SDimitry Andric // occurs in the respective predecessor block. For more info,
220e8d8bef9SDimitry Andric // see the `phi` doc in LangRef and the LCSSA doc.
221e8d8bef9SDimitry Andric if (auto *PN = dyn_cast<PHINode>(User))
222e8d8bef9SDimitry Andric UserBB = PN->getIncomingBlock(*UseToRewrite);
223e8d8bef9SDimitry Andric
2240b57cec5SDimitry Andric // If this use is in an exit block, rewrite to use the newly inserted PHI.
2250b57cec5SDimitry Andric // This is required for correctness because SSAUpdate doesn't handle uses
2260b57cec5SDimitry Andric // in the same block. It assumes the PHI we inserted is at the end of the
2270b57cec5SDimitry Andric // block.
2280b57cec5SDimitry Andric if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
2290b57cec5SDimitry Andric UseToRewrite->set(&UserBB->front());
2300b57cec5SDimitry Andric continue;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric // If we added a single PHI, it must dominate all uses and we can directly
2340b57cec5SDimitry Andric // rename it.
2350b57cec5SDimitry Andric if (AddedPHIs.size() == 1) {
2360b57cec5SDimitry Andric UseToRewrite->set(AddedPHIs[0]);
2370b57cec5SDimitry Andric continue;
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric // Otherwise, do full PHI insertion.
2410b57cec5SDimitry Andric SSAUpdate.RewriteUse(*UseToRewrite);
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andric SmallVector<DbgValueInst *, 4> DbgValues;
245*0fca6ea1SDimitry Andric SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
246*0fca6ea1SDimitry Andric llvm::findDbgValues(DbgValues, I, &DbgVariableRecords);
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric // Update pre-existing debug value uses that reside outside the loop.
249bdd1243dSDimitry Andric for (auto *DVI : DbgValues) {
2500b57cec5SDimitry Andric BasicBlock *UserBB = DVI->getParent();
2510b57cec5SDimitry Andric if (InstBB == UserBB || L->contains(UserBB))
2520b57cec5SDimitry Andric continue;
2530b57cec5SDimitry Andric // We currently only handle debug values residing in blocks that were
2540b57cec5SDimitry Andric // traversed while rewriting the uses. If we inserted just a single PHI,
2550b57cec5SDimitry Andric // we will handle all relevant debug values.
2560b57cec5SDimitry Andric Value *V = AddedPHIs.size() == 1 ? AddedPHIs[0]
2570b57cec5SDimitry Andric : SSAUpdate.FindValueForBlock(UserBB);
2580b57cec5SDimitry Andric if (V)
259fe6060f1SDimitry Andric DVI->replaceVariableLocationOp(I, V);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric
2625f757f3fSDimitry Andric // RemoveDIs: copy-paste of block above, using non-instruction debug-info
2635f757f3fSDimitry Andric // records.
264*0fca6ea1SDimitry Andric for (DbgVariableRecord *DVR : DbgVariableRecords) {
265*0fca6ea1SDimitry Andric BasicBlock *UserBB = DVR->getMarker()->getParent();
2665f757f3fSDimitry Andric if (InstBB == UserBB || L->contains(UserBB))
2675f757f3fSDimitry Andric continue;
2685f757f3fSDimitry Andric // We currently only handle debug values residing in blocks that were
2695f757f3fSDimitry Andric // traversed while rewriting the uses. If we inserted just a single PHI,
2705f757f3fSDimitry Andric // we will handle all relevant debug values.
2715f757f3fSDimitry Andric Value *V = AddedPHIs.size() == 1 ? AddedPHIs[0]
2725f757f3fSDimitry Andric : SSAUpdate.FindValueForBlock(UserBB);
2735f757f3fSDimitry Andric if (V)
274*0fca6ea1SDimitry Andric DVR->replaceVariableLocationOp(I, V);
2755f757f3fSDimitry Andric }
2765f757f3fSDimitry Andric
2770b57cec5SDimitry Andric // SSAUpdater might have inserted phi-nodes inside other loops. We'll need
2780b57cec5SDimitry Andric // to post-process them to keep LCSSA form.
27906c3fb27SDimitry Andric for (PHINode *InsertedPN : LocalInsertedPHIs) {
2800b57cec5SDimitry Andric if (auto *OtherLoop = LI.getLoopFor(InsertedPN->getParent()))
2810b57cec5SDimitry Andric if (!L->contains(OtherLoop))
2820b57cec5SDimitry Andric PostProcessPHIs.push_back(InsertedPN);
28306c3fb27SDimitry Andric if (InsertedPHIs)
28406c3fb27SDimitry Andric InsertedPHIs->push_back(InsertedPN);
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
2870b57cec5SDimitry Andric // Post process PHI instructions that were inserted into another disjoint
2880b57cec5SDimitry Andric // loop and update their exits properly.
2890b57cec5SDimitry Andric for (auto *PostProcessPN : PostProcessPHIs)
2900b57cec5SDimitry Andric if (!PostProcessPN->use_empty())
2910b57cec5SDimitry Andric Worklist.push_back(PostProcessPN);
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // Keep track of PHI nodes that we want to remove because they did not have
294e8d8bef9SDimitry Andric // any uses rewritten.
2950b57cec5SDimitry Andric for (PHINode *PN : AddedPHIs)
2960b57cec5SDimitry Andric if (PN->use_empty())
297e8d8bef9SDimitry Andric LocalPHIsToRemove.insert(PN);
298e8d8bef9SDimitry Andric
2990b57cec5SDimitry Andric Changed = true;
3000b57cec5SDimitry Andric }
301e8d8bef9SDimitry Andric
302e8d8bef9SDimitry Andric // Remove PHI nodes that did not have any uses rewritten or add them to
303e8d8bef9SDimitry Andric // PHIsToRemove, so the caller can remove them after some additional cleanup.
304e8d8bef9SDimitry Andric // We need to redo the use_empty() check here, because even if the PHI node
305e8d8bef9SDimitry Andric // wasn't used when added to LocalPHIsToRemove, later added PHI nodes can be
306e8d8bef9SDimitry Andric // using it. This cleanup is not guaranteed to handle trees/cycles of PHI
307e8d8bef9SDimitry Andric // nodes that only are used by each other. Such situations has only been
308e8d8bef9SDimitry Andric // noticed when the input IR contains unreachable code, and leaving some extra
309e8d8bef9SDimitry Andric // redundant PHI nodes in such situations is considered a minor problem.
310e8d8bef9SDimitry Andric if (PHIsToRemove) {
311e8d8bef9SDimitry Andric PHIsToRemove->append(LocalPHIsToRemove.begin(), LocalPHIsToRemove.end());
312e8d8bef9SDimitry Andric } else {
313e8d8bef9SDimitry Andric for (PHINode *PN : LocalPHIsToRemove)
3140b57cec5SDimitry Andric if (PN->use_empty())
3150b57cec5SDimitry Andric PN->eraseFromParent();
316e8d8bef9SDimitry Andric }
3170b57cec5SDimitry Andric return Changed;
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric // Compute the set of BasicBlocks in the loop `L` dominating at least one exit.
computeBlocksDominatingExits(Loop & L,const DominatorTree & DT,SmallVector<BasicBlock *,8> & ExitBlocks,SmallSetVector<BasicBlock *,8> & BlocksDominatingExits)3210b57cec5SDimitry Andric static void computeBlocksDominatingExits(
3225ffd83dbSDimitry Andric Loop &L, const DominatorTree &DT, SmallVector<BasicBlock *, 8> &ExitBlocks,
3230b57cec5SDimitry Andric SmallSetVector<BasicBlock *, 8> &BlocksDominatingExits) {
3240b57cec5SDimitry Andric // We start from the exit blocks, as every block trivially dominates itself
3250b57cec5SDimitry Andric // (not strictly).
326e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 8> BBWorklist(ExitBlocks);
3270b57cec5SDimitry Andric
3280b57cec5SDimitry Andric while (!BBWorklist.empty()) {
3290b57cec5SDimitry Andric BasicBlock *BB = BBWorklist.pop_back_val();
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric // Check if this is a loop header. If this is the case, we're done.
3320b57cec5SDimitry Andric if (L.getHeader() == BB)
3330b57cec5SDimitry Andric continue;
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric // Otherwise, add its immediate predecessor in the dominator tree to the
3360b57cec5SDimitry Andric // worklist, unless we visited it already.
3370b57cec5SDimitry Andric BasicBlock *IDomBB = DT.getNode(BB)->getIDom()->getBlock();
3380b57cec5SDimitry Andric
339349cc55cSDimitry Andric // Exit blocks can have an immediate dominator not belonging to the
3400b57cec5SDimitry Andric // loop. For an exit block to be immediately dominated by another block
3410b57cec5SDimitry Andric // outside the loop, it implies not all paths from that dominator, to the
3420b57cec5SDimitry Andric // exit block, go through the loop.
3430b57cec5SDimitry Andric // Example:
3440b57cec5SDimitry Andric //
3450b57cec5SDimitry Andric // |---- A
3460b57cec5SDimitry Andric // | |
3470b57cec5SDimitry Andric // | B<--
3480b57cec5SDimitry Andric // | | |
3490b57cec5SDimitry Andric // |---> C --
3500b57cec5SDimitry Andric // |
3510b57cec5SDimitry Andric // D
3520b57cec5SDimitry Andric //
3530b57cec5SDimitry Andric // C is the exit block of the loop and it's immediately dominated by A,
3540b57cec5SDimitry Andric // which doesn't belong to the loop.
3550b57cec5SDimitry Andric if (!L.contains(IDomBB))
3560b57cec5SDimitry Andric continue;
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric if (BlocksDominatingExits.insert(IDomBB))
3590b57cec5SDimitry Andric BBWorklist.push_back(IDomBB);
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric
formLCSSA(Loop & L,const DominatorTree & DT,const LoopInfo * LI,ScalarEvolution * SE)3635ffd83dbSDimitry Andric bool llvm::formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
3640b57cec5SDimitry Andric ScalarEvolution *SE) {
3650b57cec5SDimitry Andric bool Changed = false;
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS
3680b57cec5SDimitry Andric // Verify all sub-loops are in LCSSA form already.
36904eeddc0SDimitry Andric for (Loop *SubLoop: L) {
37004eeddc0SDimitry Andric (void)SubLoop; // Silence unused variable warning.
3710b57cec5SDimitry Andric assert(SubLoop->isRecursivelyLCSSAForm(DT, *LI) && "Subloop not in LCSSA!");
37204eeddc0SDimitry Andric }
3730b57cec5SDimitry Andric #endif
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> ExitBlocks;
3760b57cec5SDimitry Andric L.getExitBlocks(ExitBlocks);
3770b57cec5SDimitry Andric if (ExitBlocks.empty())
3780b57cec5SDimitry Andric return false;
3790b57cec5SDimitry Andric
3800b57cec5SDimitry Andric SmallSetVector<BasicBlock *, 8> BlocksDominatingExits;
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric // We want to avoid use-scanning leveraging dominance informations.
3830b57cec5SDimitry Andric // If a block doesn't dominate any of the loop exits, the none of the values
3840b57cec5SDimitry Andric // defined in the loop can be used outside.
3850b57cec5SDimitry Andric // We compute the set of blocks fullfilling the conditions in advance
3860b57cec5SDimitry Andric // walking the dominator tree upwards until we hit a loop header.
3870b57cec5SDimitry Andric computeBlocksDominatingExits(L, DT, ExitBlocks, BlocksDominatingExits);
3880b57cec5SDimitry Andric
3890b57cec5SDimitry Andric SmallVector<Instruction *, 8> Worklist;
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric // Look at all the instructions in the loop, checking to see if they have uses
3920b57cec5SDimitry Andric // outside the loop. If so, put them into the worklist to rewrite those uses.
3930b57cec5SDimitry Andric for (BasicBlock *BB : BlocksDominatingExits) {
3940b57cec5SDimitry Andric // Skip blocks that are part of any sub-loops, they must be in LCSSA
3950b57cec5SDimitry Andric // already.
3960b57cec5SDimitry Andric if (LI->getLoopFor(BB) != &L)
3970b57cec5SDimitry Andric continue;
3980b57cec5SDimitry Andric for (Instruction &I : *BB) {
3990b57cec5SDimitry Andric // Reject two common cases fast: instructions with no uses (like stores)
4000b57cec5SDimitry Andric // and instructions with one use that is in the same block as this.
4010b57cec5SDimitry Andric if (I.use_empty() ||
4020b57cec5SDimitry Andric (I.hasOneUse() && I.user_back()->getParent() == BB &&
4030b57cec5SDimitry Andric !isa<PHINode>(I.user_back())))
4040b57cec5SDimitry Andric continue;
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric // Tokens cannot be used in PHI nodes, so we skip over them.
4070b57cec5SDimitry Andric // We can run into tokens which are live out of a loop with catchswitch
4080b57cec5SDimitry Andric // instructions in Windows EH if the catchswitch has one catchpad which
4090b57cec5SDimitry Andric // is inside the loop and another which is not.
4100b57cec5SDimitry Andric if (I.getType()->isTokenTy())
4110b57cec5SDimitry Andric continue;
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric Worklist.push_back(&I);
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric }
416e8d8bef9SDimitry Andric
41706c3fb27SDimitry Andric Changed = formLCSSAForInstructions(Worklist, DT, *LI, SE);
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric assert(L.isLCSSAForm(DT));
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric return Changed;
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric /// Process a loop nest depth first.
formLCSSARecursively(Loop & L,const DominatorTree & DT,const LoopInfo * LI,ScalarEvolution * SE)4255ffd83dbSDimitry Andric bool llvm::formLCSSARecursively(Loop &L, const DominatorTree &DT,
4265ffd83dbSDimitry Andric const LoopInfo *LI, ScalarEvolution *SE) {
4270b57cec5SDimitry Andric bool Changed = false;
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andric // Recurse depth-first through inner loops.
4300b57cec5SDimitry Andric for (Loop *SubLoop : L.getSubLoops())
4310b57cec5SDimitry Andric Changed |= formLCSSARecursively(*SubLoop, DT, LI, SE);
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andric Changed |= formLCSSA(L, DT, LI, SE);
4340b57cec5SDimitry Andric return Changed;
4350b57cec5SDimitry Andric }
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andric /// Process all loops in the function, inner-most out.
formLCSSAOnAllLoops(const LoopInfo * LI,const DominatorTree & DT,ScalarEvolution * SE)4385ffd83dbSDimitry Andric static bool formLCSSAOnAllLoops(const LoopInfo *LI, const DominatorTree &DT,
4390b57cec5SDimitry Andric ScalarEvolution *SE) {
4400b57cec5SDimitry Andric bool Changed = false;
441bdd1243dSDimitry Andric for (const auto &L : *LI)
4420b57cec5SDimitry Andric Changed |= formLCSSARecursively(*L, DT, LI, SE);
4430b57cec5SDimitry Andric return Changed;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric namespace {
4470b57cec5SDimitry Andric struct LCSSAWrapperPass : public FunctionPass {
4480b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
LCSSAWrapperPass__anona62c7f040111::LCSSAWrapperPass4490b57cec5SDimitry Andric LCSSAWrapperPass() : FunctionPass(ID) {
4500b57cec5SDimitry Andric initializeLCSSAWrapperPassPass(*PassRegistry::getPassRegistry());
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric // Cached analysis information for the current function.
4540b57cec5SDimitry Andric DominatorTree *DT;
4550b57cec5SDimitry Andric LoopInfo *LI;
4560b57cec5SDimitry Andric ScalarEvolution *SE;
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric bool runOnFunction(Function &F) override;
verifyAnalysis__anona62c7f040111::LCSSAWrapperPass4590b57cec5SDimitry Andric void verifyAnalysis() const override {
4600b57cec5SDimitry Andric // This check is very expensive. On the loop intensive compiles it may cause
4610b57cec5SDimitry Andric // up to 10x slowdown. Currently it's disabled by default. LPPassManager
4620b57cec5SDimitry Andric // always does limited form of the LCSSA verification. Similar reasoning
4630b57cec5SDimitry Andric // was used for the LoopInfo verifier.
4640b57cec5SDimitry Andric if (VerifyLoopLCSSA) {
4650b57cec5SDimitry Andric assert(all_of(*LI,
4660b57cec5SDimitry Andric [&](Loop *L) {
4670b57cec5SDimitry Andric return L->isRecursivelyLCSSAForm(*DT, *LI);
4680b57cec5SDimitry Andric }) &&
4690b57cec5SDimitry Andric "LCSSA form is broken!");
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric };
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric /// This transformation requires natural loop information & requires that
4740b57cec5SDimitry Andric /// loop preheaders be inserted into the CFG. It maintains both of these,
4750b57cec5SDimitry Andric /// as well as the CFG. It also requires dominator information.
getAnalysisUsage__anona62c7f040111::LCSSAWrapperPass4760b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
4770b57cec5SDimitry Andric AU.setPreservesCFG();
4780b57cec5SDimitry Andric
4790b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
4800b57cec5SDimitry Andric AU.addRequired<LoopInfoWrapperPass>();
4810b57cec5SDimitry Andric AU.addPreservedID(LoopSimplifyID);
4820b57cec5SDimitry Andric AU.addPreserved<AAResultsWrapperPass>();
4830b57cec5SDimitry Andric AU.addPreserved<BasicAAWrapperPass>();
4840b57cec5SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>();
4850b57cec5SDimitry Andric AU.addPreserved<ScalarEvolutionWrapperPass>();
4860b57cec5SDimitry Andric AU.addPreserved<SCEVAAWrapperPass>();
4870b57cec5SDimitry Andric AU.addPreserved<BranchProbabilityInfoWrapperPass>();
4880b57cec5SDimitry Andric AU.addPreserved<MemorySSAWrapperPass>();
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric // This is needed to perform LCSSA verification inside LPPassManager
4910b57cec5SDimitry Andric AU.addRequired<LCSSAVerificationPass>();
4920b57cec5SDimitry Andric AU.addPreserved<LCSSAVerificationPass>();
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric };
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric char LCSSAWrapperPass::ID = 0;
4980b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LCSSAWrapperPass, "lcssa", "Loop-Closed SSA Form Pass",
4990b57cec5SDimitry Andric false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)5000b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
5010b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
5020b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LCSSAVerificationPass)
5030b57cec5SDimitry Andric INITIALIZE_PASS_END(LCSSAWrapperPass, "lcssa", "Loop-Closed SSA Form Pass",
5040b57cec5SDimitry Andric false, false)
5050b57cec5SDimitry Andric
5060b57cec5SDimitry Andric Pass *llvm::createLCSSAPass() { return new LCSSAWrapperPass(); }
5070b57cec5SDimitry Andric char &llvm::LCSSAID = LCSSAWrapperPass::ID;
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric /// Transform \p F into loop-closed SSA form.
runOnFunction(Function & F)5100b57cec5SDimitry Andric bool LCSSAWrapperPass::runOnFunction(Function &F) {
5110b57cec5SDimitry Andric LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
5120b57cec5SDimitry Andric DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
5130b57cec5SDimitry Andric auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
5140b57cec5SDimitry Andric SE = SEWP ? &SEWP->getSE() : nullptr;
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric return formLCSSAOnAllLoops(LI, *DT, SE);
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)5190b57cec5SDimitry Andric PreservedAnalyses LCSSAPass::run(Function &F, FunctionAnalysisManager &AM) {
5200b57cec5SDimitry Andric auto &LI = AM.getResult<LoopAnalysis>(F);
5210b57cec5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
5220b57cec5SDimitry Andric auto *SE = AM.getCachedResult<ScalarEvolutionAnalysis>(F);
5230b57cec5SDimitry Andric if (!formLCSSAOnAllLoops(&LI, DT, SE))
5240b57cec5SDimitry Andric return PreservedAnalyses::all();
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric PreservedAnalyses PA;
5270b57cec5SDimitry Andric PA.preserveSet<CFGAnalyses>();
5280b57cec5SDimitry Andric PA.preserve<ScalarEvolutionAnalysis>();
5290b57cec5SDimitry Andric // BPI maps terminators to probabilities, since we don't modify the CFG, no
5300b57cec5SDimitry Andric // updates are needed to preserve it.
5310b57cec5SDimitry Andric PA.preserve<BranchProbabilityAnalysis>();
5320b57cec5SDimitry Andric PA.preserve<MemorySSAAnalysis>();
5330b57cec5SDimitry Andric return PA;
5340b57cec5SDimitry Andric }
535