51 #define DEBUG_TYPE "lcssa" 53 STATISTIC(NumLCSSA,
"Number of live out of a loop variables");
55 #ifdef EXPENSIVE_CHECKS 58 static bool VerifyLoopLCSSA =
false;
63 cl::desc(
"Verify loop lcssa form (time consuming)"));
86 while (!Worklist.
empty()) {
87 UsesToRewrite.
clear();
93 assert(L &&
"Instruction belongs to a BB that's not part of a loop");
94 if (!LoopExitBlocks.
count(L))
99 if (ExitBlocks.empty())
105 if (
auto *PN = dyn_cast<PHINode>(User))
106 UserBB = PN->getIncomingBlock(U);
108 if (InstBB != UserBB && !L->contains(UserBB))
113 if (UsesToRewrite.
empty())
123 if (
auto *Inv = dyn_cast<InvokeInst>(I))
124 DomBB = Inv->getNormalDest();
156 if (!L->contains(Pred))
176 if (!L->contains(OtherLoop))
182 for (
Use *UseToRewrite : UsesToRewrite) {
189 if (
auto *PN = dyn_cast<PHINode>(User))
190 UserBB = PN->getIncomingBlock(*UseToRewrite);
192 if (isa<PHINode>(UserBB->begin()) &&
isExitBlock(UserBB, ExitBlocks)) {
194 if (UseToRewrite->get()->hasValueHandle())
196 UseToRewrite->set(&UserBB->front());
206 for (
PHINode *InsertedPN : InsertedPHIs) {
207 if (
auto *OtherLoop = LI.
getLoopFor(InsertedPN->getParent()))
208 if (!L->contains(OtherLoop))
214 for (
auto *PostProcessPN : PostProcessPHIs)
215 if (!PostProcessPN->use_empty())
237 for (
PHINode *PN : PHIsToRemove)
239 PN->eraseFromParent();
254 while (!BBWorklist.
empty()) {
284 if (BlocksDominatingExits.
insert(IDomBB))
291 bool Changed =
false;
295 if (ExitBlocks.
empty())
311 for (
BasicBlock *BB : BlocksDominatingExits) {
316 (
I.hasOneUse() &&
I.user_back()->getParent() == BB &&
317 !isa<PHINode>(
I.user_back())))
324 if (
I.getType()->isTokenTy())
346 bool Changed =
false;
359 bool Changed =
false;
378 void verifyAnalysis()
const override {
383 if (VerifyLoopLCSSA) {
388 "LCSSA form is broken!");
428 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
429 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
430 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
431 SE = SEWP ? &SEWP->getSE() :
nullptr;
Legacy wrapper pass to provide the GlobalsAAResult object.
Pass interface - Implemented by all 'passes'.
void initializeLCSSAWrapperPassPass(PassRegistry &)
void push_back(const T &Elt)
iterator_range< use_iterator > uses()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Helper class for SSA formation on a set of values defined in multiple blocks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Compute iterated dominance frontiers using a linear time algorithm.
This is the interface for a simple mod/ref and alias analysis over globals.
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
The main scalar evolution driver.
const Use & getOperandUse(unsigned i) const
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
static bool isExitBlock(BasicBlock *BB, const SmallVectorImpl< BasicBlock *> &ExitBlocks)
Return true if the specified block is in the list.
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
static unsigned getOperandNumForIncomingValue(unsigned i)
AnalysisUsage & addRequired()
ArrayRef< BasicBlock * > get(BasicBlock *BB)
#define INITIALIZE_PASS_DEPENDENCY(depName)
This is the interface for a SCEV-based alias analysis.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
A Use represents the edge between a Value definition and its users.
Analysis pass that exposes the LoopInfo for a function.
BlockT * getHeader() const
void insertDebugValuesForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode *> &InsertedPHIs)
Propagate dbg.value intrinsics through the newly inserted PHIs.
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Type * getType() const
All values are typed, get the type of this value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries...
AnalysisUsage & addPreservedID(const void *ID)
static void ValueIsRAUWd(Value *Old, Value *New)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static bool runOnFunction(Function &F, bool PostInlining)
bool formLCSSAForInstructions(SmallVectorImpl< Instruction *> &Worklist, DominatorTree &DT, LoopInfo &LI)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop...
A set of analyses that are preserved following a run of a transformation pass.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM Basic Block Representation.
size_t size(BasicBlock *BB) const
DomTreeNodeBase * getIDom() const
static cl::opt< bool, true > VerifyLoopLCSSAFlag("verify-loop-lcssa", cl::location(VerifyLoopLCSSA), cl::Hidden, cl::desc("Verify loop lcssa form (time consuming)"))
static bool VerifyLoopLCSSA
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
Analysis pass providing a never-invalidated alias analysis result.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static bool formLCSSAOnAllLoops(LoopInfo *LI, DominatorTree &DT, ScalarEvolution *SE)
Process all loops in the function, inner-most out.
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
A SetVector that performs no allocations if smaller than a certain size.
Legacy wrapper pass to provide the SCEVAAResult object.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
LLVM_NODISCARD T pop_back_val()
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
unsigned getNumIncomingValues() const
Return the number of incoming edges.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Represents analyses that only rely on functions' control flow.
Analysis pass that exposes the ScalarEvolution for a function.
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
Analysis pass providing a never-invalidated alias analysis result.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
static void computeBlocksDominatingExits(Loop &L, DominatorTree &DT, SmallVector< BasicBlock *, 8 > &ExitBlocks, SmallSetVector< BasicBlock *, 8 > &BlocksDominatingExits)
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_NODISCARD bool empty() const
Represents a single loop in the control flow graph.
bool isTokenTy() const
Return true if this is 'token'.
void preserveSet()
Mark an analysis set as preserved.
StringRef getName() const
Return a constant reference to the value's name.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
void preserve()
Mark an analysis as preserved.
INITIALIZE_PASS_BEGIN(LCSSAWrapperPass, "lcssa", "Loop-Closed SSA Form Pass", false, false) INITIALIZE_PASS_END(LCSSAWrapperPass
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
The legacy pass manager's analysis pass to compute loop information.
This is the interface for LLVM's primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
LocationClass< Ty > location(Ty &L)
const BasicBlock * getParent() const
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Legacy wrapper pass to provide the BasicAAResult object.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.