45 #define DEBUG_TYPE "loop-interchange"
49 cl::desc(
"Interchange if you gain more than this number"));
56 typedef std::vector<std::vector<char>> CharMatrix;
59 static const unsigned MaxMemInstrCount = 100;
62 static const unsigned MaxLoopNestDepth = 10;
64 struct LoopInterchange;
66 #ifdef DUMP_DEP_MATRICIES
67 void printDepMatrix(CharMatrix &DepMatrix) {
68 for (
auto I = DepMatrix.begin(),
E = DepMatrix.end();
I !=
E; ++
I) {
69 std::vector<char> Vec = *
I;
70 for (
auto II = Vec.begin(), EE = Vec.end(); II != EE; ++II)
77 static bool populateDependencyMatrix(CharMatrix &DepMatrix,
unsigned Level,
88 if (!isa<Instruction>(
I))
90 if (
LoadInst *Ld = dyn_cast<LoadInst>(
I)) {
94 }
else if (
StoreInst *St = dyn_cast<StoreInst>(
I)) {
97 MemInstr.push_back(&*
I);
102 DEBUG(
dbgs() <<
"Found " << MemInstr.size()
103 <<
" Loads and Stores to analyze\n");
105 ValueVector::iterator
I,
IE, J, JE;
107 for (I = MemInstr.begin(), IE = MemInstr.end(); I !=
IE; ++
I) {
108 for (J = I, JE = MemInstr.end(); J != JE; ++J) {
109 std::vector<char> Dep;
115 if (isa<LoadInst>(Src) && isa<LoadInst>(Dst))
118 if (
auto D = DI->
depends(Src, Dst,
true)) {
119 assert(
D->isOrdered() &&
"Expected an output, flow or anti dep.");
121 D->isFlow() ?
"flow" :
D->isAnti() ?
"anti" :
"output";
122 dbgs() <<
"Found " << DepType
123 <<
" dependency between Src and Dst\n"
124 <<
" Src:" << *Src <<
"\n Dst:" << *Dst <<
'\n');
125 unsigned Levels =
D->getLevels();
127 for (
unsigned II = 1; II <= Levels; ++II) {
128 const SCEV *Distance =
D->getDistance(II);
130 dyn_cast_or_null<SCEVConstant>(Distance);
139 Dep.push_back(Direction);
140 }
else if (
D->isScalar(II)) {
142 Dep.push_back(Direction);
144 unsigned Dir =
D->getDirection(II);
155 Dep.push_back(Direction);
158 while (Dep.size() !=
Level) {
162 DepMatrix.push_back(Dep);
163 if (DepMatrix.size() > MaxMemInstrCount) {
164 DEBUG(
dbgs() <<
"Cannot handle more than " << MaxMemInstrCount
165 <<
" dependencies inside loop\n");
173 if (DepMatrix.size() == 0)
180 static void interChangeDependencies(CharMatrix &DepMatrix,
unsigned FromIndx,
182 unsigned numRows = DepMatrix.size();
183 for (
unsigned i = 0;
i < numRows; ++
i) {
184 char TmpVal = DepMatrix[
i][ToIndx];
185 DepMatrix[
i][ToIndx] = DepMatrix[
i][FromIndx];
186 DepMatrix[
i][FromIndx] = TmpVal;
192 static bool isOuterMostDepPositive(CharMatrix &DepMatrix,
unsigned Row,
194 for (
unsigned i = 0;
i <= Column; ++
i) {
195 if (DepMatrix[Row][
i] ==
'<')
197 if (DepMatrix[Row][
i] ==
'>')
205 static bool containsNoDependence(CharMatrix &DepMatrix,
unsigned Row,
207 for (
unsigned i = 0;
i < Column; ++
i) {
208 if (DepMatrix[Row][
i] !=
'=' && DepMatrix[Row][
i] !=
'S' &&
209 DepMatrix[Row][
i] !=
'I')
215 static bool validDepInterchange(CharMatrix &DepMatrix,
unsigned Row,
216 unsigned OuterLoopId,
char InnerDep,
219 if (isOuterMostDepPositive(DepMatrix, Row, OuterLoopId))
222 if (InnerDep == OuterDep)
228 if (InnerDep ==
'=' || InnerDep ==
'S' || InnerDep ==
'I')
234 if (InnerDep ==
'>') {
237 if (OuterLoopId == 0)
243 if (!containsNoDependence(DepMatrix, Row, OuterLoopId))
254 static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix,
255 unsigned InnerLoopId,
256 unsigned OuterLoopId) {
258 unsigned NumRows = DepMatrix.size();
260 for (
unsigned Row = 0; Row < NumRows; ++Row) {
261 char InnerDep = DepMatrix[Row][InnerLoopId];
262 char OuterDep = DepMatrix[Row][OuterLoopId];
263 if (InnerDep ==
'*' || OuterDep ==
'*')
265 if (!validDepInterchange(DepMatrix, Row, OuterLoopId, InnerDep, OuterDep))
273 DEBUG(
dbgs() <<
"Calling populateWorklist on Func: "
274 << L.
getHeader()->getParent()->getName() <<
" Loop: %"
277 Loop *CurrentLoop = &
L;
278 const std::vector<Loop *> *Vec = &CurrentLoop->
getSubLoops();
279 while (!Vec->empty()) {
283 if (Vec->size() != 1) {
287 LoopList.push_back(CurrentLoop);
288 CurrentLoop = Vec->front();
291 LoopList.push_back(CurrentLoop);
298 return InnerIndexVar;
304 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
305 !PhiTy->isPointerTy())
312 if (!isa<SCEVConstant>(Step))
323 class LoopInterchangeLegality {
327 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
328 PreserveLCSSA(PreserveLCSSA), InnerLoopHasReduction(
false) {}
331 bool canInterchangeLoops(
unsigned InnerLoopId,
unsigned OuterLoopId,
332 CharMatrix &DepMatrix);
335 bool isLoopStructureUnderstood(
PHINode *InnerInductionVar);
337 bool currentLimitations();
339 bool hasInnerLoopReduction() {
return InnerLoopHasReduction; }
342 bool tightlyNested(
Loop *Outer,
Loop *Inner);
343 bool containsUnsafeInstructionsInHeader(
BasicBlock *BB);
345 bool containsUnsafeInstructionsInLatch(
BasicBlock *BB);
346 bool findInductionAndReductions(
Loop *L,
357 bool InnerLoopHasReduction;
362 class LoopInterchangeProfitability {
365 : OuterLoop(Outer), InnerLoop(Inner), SE(SE) {}
368 bool isProfitable(
unsigned InnerLoopId,
unsigned OuterLoopId,
369 CharMatrix &DepMatrix);
372 int getInstrOrderCost();
382 class LoopInterchangeTransform {
387 bool InnerLoopContainsReductions)
388 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
389 LoopExit(LoopNestExit),
390 InnerLoopHasReduction(InnerLoopContainsReductions) {}
394 void restructureLoops(
Loop *InnerLoop,
Loop *OuterLoop);
395 void removeChildLoop(
Loop *OuterLoop,
Loop *InnerLoop);
399 void splitInnerLoopHeader();
400 bool adjustLoopLinks();
401 void adjustLoopPreheaders();
402 bool adjustLoopBranches();
414 bool InnerLoopHasReduction;
426 :
FunctionPass(
ID), SE(nullptr), LI(nullptr), DI(nullptr), DT(nullptr) {
440 bool runOnFunction(
Function &
F)
override {
444 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
445 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
446 DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI();
447 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
448 DT = DTWP ? &DTWP->getDomTree() :
nullptr;
449 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
455 populateWorklist(*L, Worklist);
457 DEBUG(
dbgs() <<
"Worklist size = " << Worklist.
size() <<
"\n");
459 while (!Worklist.
empty()) {
461 Changed = processLoopList(LoopList, F);
466 bool isComputableLoopNest(LoopVector LoopList) {
467 for (
Loop *L : LoopList) {
470 DEBUG(
dbgs() <<
"Couldn't compute backedge count\n");
474 DEBUG(
dbgs() <<
"NumBackEdges is not equal to 1\n");
478 DEBUG(
dbgs() <<
"Loop doesn't have unique exit block\n");
485 unsigned selectLoopForInterchange(
const LoopVector &LoopList) {
488 return LoopList.size() - 1;
491 bool processLoopList(LoopVector LoopList,
Function &F) {
493 bool Changed =
false;
494 unsigned LoopNestDepth = LoopList.size();
495 if (LoopNestDepth < 2) {
496 DEBUG(
dbgs() <<
"Loop doesn't contain minimum nesting level.\n");
499 if (LoopNestDepth > MaxLoopNestDepth) {
500 DEBUG(
dbgs() <<
"Cannot handle loops of depth greater than "
501 << MaxLoopNestDepth <<
"\n");
504 if (!isComputableLoopNest(LoopList)) {
505 DEBUG(
dbgs() <<
"Not valid loop candidate for interchange\n");
509 DEBUG(
dbgs() <<
"Processing LoopList of size = " << LoopNestDepth <<
"\n");
511 CharMatrix DependencyMatrix;
512 Loop *OuterMostLoop = *(LoopList.begin());
513 if (!populateDependencyMatrix(DependencyMatrix, LoopNestDepth,
514 OuterMostLoop, DI)) {
515 DEBUG(
dbgs() <<
"Populating dependency matrix failed\n");
518 #ifdef DUMP_DEP_MATRICIES
519 DEBUG(
dbgs() <<
"Dependence before interchange\n");
520 printDepMatrix(DependencyMatrix);
526 if (!OuterMostLoopLatchBI)
540 if (isa<PHINode>(LoopNestExit->
begin())) {
541 DEBUG(
dbgs() <<
"PHI Nodes in loop nest exit is not handled for now "
542 "since on failure all loops branch to loop nest exit.\n");
546 unsigned SelecLoopId = selectLoopForInterchange(LoopList);
548 for (
unsigned i = SelecLoopId;
i > 0;
i--) {
550 processLoop(LoopList,
i,
i - 1, LoopNestExit, DependencyMatrix);
557 interChangeDependencies(DependencyMatrix,
i,
i - 1);
559 #ifdef DUMP_DEP_MATRICIES
560 DEBUG(
dbgs() <<
"Dependence after interchange\n");
561 printDepMatrix(DependencyMatrix);
563 Changed |= Interchanged;
568 bool processLoop(LoopVector LoopList,
unsigned InnerLoopId,
569 unsigned OuterLoopId,
BasicBlock *LoopNestExit,
570 std::vector<std::vector<char>> &DependencyMatrix) {
572 DEBUG(
dbgs() <<
"Processing Inner Loop Id = " << InnerLoopId
573 <<
" and OuterLoopId = " << OuterLoopId <<
"\n");
574 Loop *InnerLoop = LoopList[InnerLoopId];
575 Loop *OuterLoop = LoopList[OuterLoopId];
577 LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, LI, DT,
579 if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
580 DEBUG(
dbgs() <<
"Not interchanging Loops. Cannot prove legality\n");
583 DEBUG(
dbgs() <<
"Loops are legal to interchange\n");
584 LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE);
585 if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) {
586 DEBUG(
dbgs() <<
"Interchanging loops not profitable\n");
590 LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT,
591 LoopNestExit, LIL.hasInnerLoopReduction());
607 bool LoopInterchangeLegality::containsUnsafeInstructionsInHeader(
609 for (
auto I = BB->
begin(),
E = BB->
end(); I !=
E; ++
I) {
612 if (
LoadInst *L = dyn_cast<LoadInst>(I)) {
613 if (!areAllUsesReductions(L, InnerLoop))
615 }
else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
621 bool LoopInterchangeLegality::containsUnsafeInstructionsInLatch(
623 for (
auto I = BB->
begin(),
E = BB->
end(); I !=
E; ++
I) {
626 if (
StoreInst *L = dyn_cast<StoreInst>(I)) {
627 if (!isa<PHINode>(L->getOperand(0)))
629 }
else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
635 bool LoopInterchangeLegality::tightlyNested(
Loop *OuterLoop,
Loop *InnerLoop) {
640 DEBUG(
dbgs() <<
"Checking if loops are tightly nested\n");
647 if (!OuterLoopHeaderBI)
651 if (OuterLoopHeaderBI->
getSuccessor(
i) != InnerLoopPreHeader &&
656 DEBUG(
dbgs() <<
"Checking instructions in Loop header and Loop latch\n");
659 if (containsUnsafeInstructionsInHeader(OuterLoopHeader) ||
660 containsUnsafeInstructionsInLatch(OuterLoopLatch))
663 DEBUG(
dbgs() <<
"Loops are perfectly nested\n");
669 bool LoopInterchangeLegality::isLoopStructureUnderstood(
674 for (
unsigned i = 0;
i < Num; ++
i) {
676 if (isa<Constant>(Val))
686 InnerLoopPreheader &&
694 bool LoopInterchangeLegality::findInductionAndReductions(
709 dbgs() <<
"Failed to recognize PHI as an induction or reduction.\n");
717 for (
auto I = Block->
begin(); isa<PHINode>(
I); ++
I) {
727 if (!isa<PHINode>(Ins) && isOuterLoopExitBlock)
736 unsigned Num = BI->getNumSuccessors();
738 for (
unsigned i = 0;
i < Num; ++
i) {
739 if (BI->getSuccessor(
i) == LoopHeader)
741 return BI->getSuccessor(
i);
749 bool LoopInterchangeLegality::currentLimitations() {
760 if (!findInductionAndReductions(InnerLoop, Inductions, Reductions))
764 if (Inductions.
size() != 1) {
765 DEBUG(
dbgs() <<
"We currently only support loops with 1 induction variable."
766 <<
"Failed to interchange due to current limitation\n");
769 if (Reductions.
size() > 0)
770 InnerLoopHasReduction =
true;
774 if (!findInductionAndReductions(OuterLoop, Inductions, Reductions))
779 if (!Reductions.
empty())
782 if (Inductions.
size() != 1)
786 if (!isLoopStructureUnderstood(InnerInductionVar)) {
787 DEBUG(
dbgs() <<
"Loop structure not understood by pass\n");
819 if (!InnerIndexVarInc)
826 bool FoundInduction =
false;
828 if (isa<BranchInst>(I) || isa<CmpInst>(
I) || isa<TruncInst>(I))
835 FoundInduction =
true;
846 bool LoopInterchangeLegality::canInterchangeLoops(
unsigned InnerLoopId,
847 unsigned OuterLoopId,
848 CharMatrix &DepMatrix) {
850 if (!isLegalToInterChangeLoops(DepMatrix, InnerLoopId, OuterLoopId)) {
851 DEBUG(
dbgs() <<
"Failed interchange InnerLoopId = " << InnerLoopId
852 <<
" and OuterLoopId = " << OuterLoopId
853 <<
" due to dependence\n");
866 if (!OuterLoopPreHeader || OuterLoopPreHeader == OuterLoop->
getHeader() ||
867 isa<PHINode>(OuterLoopPreHeader->
begin()) ||
873 if (!InnerLoopPreHeader || InnerLoopPreHeader == InnerLoop->
getHeader() ||
874 InnerLoopPreHeader == OuterLoop->
getHeader()) {
881 if (currentLimitations()) {
882 DEBUG(
dbgs() <<
"Not legal because of current transform limitation\n");
887 if (!tightlyNested(OuterLoop, InnerLoop)) {
888 DEBUG(
dbgs() <<
"Loops not tightly nested\n");
895 int LoopInterchangeProfitability::getInstrOrderCost() {
896 unsigned GoodOrder, BadOrder;
897 BadOrder = GoodOrder = 0;
902 unsigned NumOp =
GEP->getNumOperands();
903 bool FoundInnerInduction =
false;
904 bool FoundOuterInduction =
false;
905 for (
unsigned i = 0;
i < NumOp; ++
i) {
916 if (AR->
getLoop() == InnerLoop) {
919 FoundInnerInduction =
true;
920 if (FoundOuterInduction) {
930 if (AR->
getLoop() == OuterLoop) {
933 FoundOuterInduction =
true;
934 if (FoundInnerInduction) {
943 return GoodOrder - BadOrder;
947 unsigned OuterLoopId,
948 CharMatrix &DepMatrix) {
952 unsigned Row = DepMatrix.size();
953 for (
unsigned i = 0;
i < Row; ++
i) {
954 if (DepMatrix[
i][InnerLoopId] !=
'S' && DepMatrix[
i][InnerLoopId] !=
'I')
957 if (DepMatrix[
i][OuterLoopId] !=
'=')
965 bool LoopInterchangeProfitability::isProfitable(
unsigned InnerLoopId,
966 unsigned OuterLoopId,
967 CharMatrix &DepMatrix) {
977 int Cost = getInstrOrderCost();
978 DEBUG(
dbgs() <<
"Cost = " << Cost <<
"\n");
989 void LoopInterchangeTransform::removeChildLoop(
Loop *OuterLoop,
993 if (*I == InnerLoop) {
1001 void LoopInterchangeTransform::restructureLoops(
Loop *InnerLoop,
1004 if (OuterLoopParent) {
1006 removeChildLoop(OuterLoopParent, OuterLoop);
1007 removeChildLoop(OuterLoop, InnerLoop);
1010 removeChildLoop(OuterLoop, InnerLoop);
1011 LI->changeTopLevelLoop(OuterLoop, InnerLoop);
1014 while (!InnerLoop->
empty())
1021 bool Transformed =
false;
1026 DEBUG(
dbgs() <<
"Calling Split Inner Loop\n");
1027 PHINode *InductionPHI = getInductionVariable(InnerLoop, SE);
1028 if (!InductionPHI) {
1029 DEBUG(
dbgs() <<
"Failed to find the point to split loop latch \n");
1042 splitInnerLoopLatch(InnerIndexVar);
1043 DEBUG(
dbgs() <<
"splitInnerLoopLatch done\n");
1046 splitInnerLoopHeader();
1047 DEBUG(
dbgs() <<
"splitInnerLoopHeader done\n");
1050 Transformed |= adjustLoopLinks();
1052 DEBUG(
dbgs() <<
"adjustLoopLinks failed\n");
1056 restructureLoops(InnerLoop, OuterLoop);
1060 void LoopInterchangeTransform::splitInnerLoopLatch(
Instruction *Inc) {
1062 BasicBlock *InnerLoopLatchPred = InnerLoopLatch;
1063 InnerLoopLatch =
SplitBlock(InnerLoopLatchPred, Inc, DT, LI);
1066 void LoopInterchangeTransform::splitInnerLoopHeader() {
1072 if (InnerLoopHasReduction) {
1075 ++(InnerLoopHeader->
begin()), InnerLoopHeader->
getName() +
".split");
1077 if (
Loop *L = LI->getLoopFor(InnerLoopHeader))
1082 for (
auto I = New->
begin(); isa<PHINode>(
I); ++
I) {
1089 P->eraseFromParent();
1095 DEBUG(
dbgs() <<
"Output of splitInnerLoopHeader InnerLoopHeaderSucc & "
1096 "InnerLoopHeader\n");
1109 void LoopInterchangeTransform::updateIncomingBlock(
BasicBlock *CurrBlock,
1112 for (
auto I = CurrBlock->
begin(); isa<PHINode>(
I); ++
I) {
1115 for (
unsigned i = 0;
i < Num; ++
i) {
1122 bool LoopInterchangeTransform::adjustLoopBranches() {
1124 DEBUG(
dbgs() <<
"adjustLoopBranches called\n");
1147 if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor ||
1148 !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI ||
1157 if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI)
1160 if (!InnerLoopHeaderSuccessor)
1165 unsigned NumSucc = OuterLoopPredecessorBI->getNumSuccessors();
1166 for (
unsigned i = 0;
i < NumSucc; ++
i) {
1167 if (OuterLoopPredecessorBI->getSuccessor(
i) == OuterLoopPreHeader)
1168 OuterLoopPredecessorBI->setSuccessor(
i, InnerLoopPreHeader);
1171 NumSucc = OuterLoopHeaderBI->getNumSuccessors();
1172 for (
unsigned i = 0;
i < NumSucc; ++
i) {
1173 if (OuterLoopHeaderBI->getSuccessor(
i) == OuterLoopLatch)
1174 OuterLoopHeaderBI->setSuccessor(
i, LoopExit);
1175 else if (OuterLoopHeaderBI->getSuccessor(
i) == InnerLoopPreHeader)
1176 OuterLoopHeaderBI->setSuccessor(
i, InnerLoopHeaderSuccessor);
1180 updateIncomingBlock(InnerLoopHeaderSuccessor, InnerLoopHeader,
1184 InnerLoopHeaderBI->eraseFromParent();
1187 if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader)
1188 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1);
1190 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0);
1193 for (
unsigned i = 0;
i < NumSucc; ++
i) {
1194 if (InnerLoopLatchPredecessorBI->
getSuccessor(
i) == InnerLoopLatch)
1195 InnerLoopLatchPredecessorBI->
setSuccessor(
i, InnerLoopLatchSuccessor);
1201 for (
auto I = InnerLoopLatchSuccessor->
begin(); isa<PHINode>(
I); ++
I) {
1202 PHINode *LcssaPhi = cast<PHINode>(
I);
1206 Value *Incoming =
P->getIncomingValueForBlock(InnerLoopLatch);
1207 P->replaceAllUsesWith(Incoming);
1208 P->eraseFromParent();
1211 if (OuterLoopLatchBI->
getSuccessor(0) == OuterLoopHeader)
1212 OuterLoopLatchSuccessor = OuterLoopLatchBI->
getSuccessor(1);
1214 OuterLoopLatchSuccessor = OuterLoopLatchBI->
getSuccessor(0);
1216 if (InnerLoopLatchBI->getSuccessor(1) == InnerLoopLatchSuccessor)
1217 InnerLoopLatchBI->setSuccessor(1, OuterLoopLatchSuccessor);
1219 InnerLoopLatchBI->setSuccessor(0, OuterLoopLatchSuccessor);
1221 updateIncomingBlock(OuterLoopLatchSuccessor, OuterLoopLatch, InnerLoopLatch);
1223 if (OuterLoopLatchBI->
getSuccessor(0) == OuterLoopLatchSuccessor) {
1231 void LoopInterchangeTransform::adjustLoopPreheaders() {
1251 bool LoopInterchangeTransform::adjustLoopLinks() {
1254 bool Changed = adjustLoopBranches();
1256 adjustLoopPreheaders();
1262 "Interchanges loops for cache reuse",
false,
false)
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
Pass interface - Implemented by all 'passes'.
loop Interchanges loops for cache false
void push_back(const T &Elt)
BasicBlock * getUniqueSuccessor()
Return the successor of this block if it has a unique successor.
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
Legacy pass manager pass to access dependence information.
unsigned getNumOperands() const
static bool isProfitableForVectorization(unsigned InnerLoopId, unsigned OuterLoopId, CharMatrix &DepMatrix)
The main scalar evolution driver.
static bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes)
Returns true if Phi is a reduction in TheLoop.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
LoopT * getParentLoop() const
An instruction for reading from memory.
DependenceInfo - This class is the main dependence-analysis driver.
BlockT * getHeader() const
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
StringRef getName() const
Return a constant reference to the value's name.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
LLVM_NODISCARD bool empty() const
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
static cl::opt< int > LoopInterchangeCostThreshold("loop-interchange-threshold", cl::init(0), cl::Hidden, cl::desc("Interchange if you gain more than this number"))
static BasicBlock * getLoopLatchExitBlock(BasicBlock *LatchBlock, BasicBlock *LoopHeader)
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
This node represents a polynomial recurrence on the trip count of the specified loop.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
BasicBlock * getSuccessor(unsigned i) const
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
const SCEV * getCouldNotCompute()
initializer< Ty > init(const Ty &Val)
std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool PossiblyLoopIndependent)
depends - Tests for a dependence between the Src and Dst instructions.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Conditional or Unconditional Branch instruction.
loop Interchanges loops for cache reuse
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
void splice(iterator where, iplist_impl &L2)
const InstListType & getInstList() const
Return the underlying instruction list container.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
FunctionPass class - This class is used to implement most global optimizations.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Value * getOperand(unsigned i) const
self_iterator getIterator()
static unsigned getIncomingValueNumForOperand(unsigned i)
void initializeLoopInterchangePass(PassRegistry &)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
INITIALIZE_PASS_BEGIN(LoopInterchange,"loop-interchange","Interchanges loops for cache reuse", false, false) INITIALIZE_PASS_END(LoopInterchange
Iterator for intrusive lists based on ilist_node.
static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock)
This is the shared class of boolean and integer constants.
A struct for saving information about induction variables.
void setIncomingBlock(unsigned i, BasicBlock *BB)
Pass * createLoopInterchangePass()
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
AnalysisUsage & addRequiredID(const void *ID)
static void moveBBContents(BasicBlock *FromBB, Instruction *InsertBefore)
Move all instructions except the terminator from FromBB right before InsertBefore.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
LLVM_NODISCARD T pop_back_val()
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
ConstantInt * getValue() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
iterator_range< user_iterator > users()
std::vector< BlockT * >::const_iterator block_iterator
block_iterator block_end() const
This class represents an analyzed expression in the program.
unsigned getNumSuccessors() const
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Loop * getLoop() const
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere...
const SCEV * getBackedgeTakenCount(const Loop *L)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
Legacy analysis pass which computes a DominatorTree.
std::vector< LoopT * >::const_iterator iterator
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const BasicBlock * getParent() const
static bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr)
Returns true if Phi is an induction in the loop L.
This class represents a constant integer value.