52 #define DEBUG_TYPE "lcssa" 54 STATISTIC(NumLCSSA,
"Number of live out of a loop variables");
56 #ifdef EXPENSIVE_CHECKS 59 static bool VerifyLoopLCSSA =
false;
64 cl::desc(
"Verify loop lcssa form (time consuming)"));
87 while (!Worklist.
empty()) {
88 UsesToRewrite.
clear();
94 assert(L &&
"Instruction belongs to a BB that's not part of a loop");
95 if (!LoopExitBlocks.
count(L))
100 if (ExitBlocks.empty())
106 if (
auto *PN = dyn_cast<PHINode>(User))
107 UserBB = PN->getIncomingBlock(U);
109 if (InstBB != UserBB && !L->contains(UserBB))
114 if (UsesToRewrite.
empty())
124 if (
auto *Inv = dyn_cast<InvokeInst>(I))
125 DomBB = Inv->getNormalDest();
157 if (!L->contains(Pred))
177 if (!L->contains(OtherLoop))
183 for (
Use *UseToRewrite : UsesToRewrite) {
190 if (
auto *PN = dyn_cast<PHINode>(User))
191 UserBB = PN->getIncomingBlock(*UseToRewrite);
193 if (isa<PHINode>(UserBB->begin()) &&
isExitBlock(UserBB, ExitBlocks)) {
195 if (UseToRewrite->get()->hasValueHandle())
197 UseToRewrite->set(&UserBB->front());
210 for (
auto DVI : DbgValues) {
212 if (InstBB == UserBB || L->contains(UserBB))
222 for (
PHINode *InsertedPN : InsertedPHIs) {
223 if (
auto *OtherLoop = LI.
getLoopFor(InsertedPN->getParent()))
224 if (!L->contains(OtherLoop))
230 for (
auto *PostProcessPN : PostProcessPHIs)
231 if (!PostProcessPN->use_empty())
253 for (
PHINode *PN : PHIsToRemove)
255 PN->eraseFromParent();
270 while (!BBWorklist.
empty()) {
300 if (BlocksDominatingExits.
insert(IDomBB))
307 bool Changed =
false;
311 if (ExitBlocks.
empty())
327 for (
BasicBlock *BB : BlocksDominatingExits) {
332 (
I.hasOneUse() &&
I.user_back()->getParent() == BB &&
333 !isa<PHINode>(
I.user_back())))
340 if (
I.getType()->isTokenTy())
362 bool Changed =
false;
375 bool Changed =
false;
394 void verifyAnalysis()
const override {
399 if (VerifyLoopLCSSA) {
404 "LCSSA form is broken!");
444 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
445 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
446 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
447 SE = SEWP ? &SEWP->getSE() :
nullptr;
Legacy wrapper pass to provide the GlobalsAAResult object.
Pass interface - Implemented by all 'passes'.
void initializeLCSSAWrapperPassPass(PassRegistry &)
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.
This class represents lattice values for constants.
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 push_back(const T &Elt)
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.
LLVMContext & getContext() const
All values hold a context through their type.
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.
void findDbgValues(SmallVectorImpl< DbgValueInst *> &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
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...
Value * FindValueForBlock(BasicBlock *BB) const
Return the value for the specified block if the SSAUpdater has one, otherwise return nullptr...
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.
void setOperand(unsigned i, Value *Val)
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.
const Function * getParent() const
Return the enclosing method, or null if none.
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.
LLVM Value Representation.
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.