53 #define DEBUG_TYPE "loop-interchange" 55 STATISTIC(LoopsInterchanged,
"Number of loops interchanged");
59 cl::desc(
"Interchange if you gain more than this number"));
66 using CharMatrix = std::vector<std::vector<char>>;
76 #ifdef DUMP_DEP_MATRICIES 77 static void printDepMatrix(CharMatrix &DepMatrix) {
78 for (
auto &Row : DepMatrix) {
96 if (!isa<Instruction>(
I))
98 if (
auto *Ld = dyn_cast<LoadInst>(&
I)) {
102 }
else if (
auto *St = dyn_cast<StoreInst>(&
I)) {
105 MemInstr.push_back(&
I);
111 <<
" Loads and Stores to analyze\n");
113 ValueVector::iterator
I,
IE, J, JE;
115 for (I = MemInstr.begin(), IE = MemInstr.end(); I !=
IE; ++
I) {
116 for (J = I, JE = MemInstr.end(); J != JE; ++J) {
117 std::vector<char> Dep;
123 if (isa<LoadInst>(Src) && isa<LoadInst>(Dst))
126 if (
auto D = DI->
depends(Src, Dst,
true)) {
127 assert(
D->isOrdered() &&
"Expected an output, flow or anti dep.");
129 D->isFlow() ?
"flow" :
D->isAnti() ?
"anti" :
"output";
130 dbgs() <<
"Found " << DepType
131 <<
" dependency between Src and Dst\n" 132 <<
" Src:" << *Src <<
"\n Dst:" << *Dst <<
'\n');
133 unsigned Levels =
D->getLevels();
135 for (
unsigned II = 1; II <= Levels; ++II) {
136 const SCEV *Distance =
D->getDistance(II);
138 dyn_cast_or_null<SCEVConstant>(Distance);
147 Dep.push_back(Direction);
148 }
else if (
D->isScalar(II)) {
150 Dep.push_back(Direction);
152 unsigned Dir =
D->getDirection(II);
163 Dep.push_back(Direction);
166 while (Dep.size() !=
Level) {
170 DepMatrix.push_back(Dep);
172 LLVM_DEBUG(
dbgs() <<
"Cannot handle more than " << MaxMemInstrCount
173 <<
" dependencies inside loop\n");
187 unsigned numRows = DepMatrix.size();
188 for (
unsigned i = 0; i < numRows; ++i) {
189 char TmpVal = DepMatrix[i][ToIndx];
190 DepMatrix[i][ToIndx] = DepMatrix[i][FromIndx];
191 DepMatrix[i][FromIndx] = TmpVal;
199 for (
unsigned i = 0; i <= Column; ++i) {
200 if (DepMatrix[Row][i] ==
'<')
202 if (DepMatrix[Row][i] ==
'>')
212 for (
unsigned i = 0; i < Column; ++i) {
213 if (DepMatrix[Row][i] !=
'=' && DepMatrix[Row][i] !=
'S' &&
214 DepMatrix[Row][i] !=
'I')
221 unsigned OuterLoopId,
char InnerDep,
226 if (InnerDep == OuterDep)
232 if (InnerDep ==
'=' || InnerDep ==
'S' || InnerDep ==
'I')
238 if (InnerDep ==
'>') {
241 if (OuterLoopId == 0)
259 unsigned InnerLoopId,
260 unsigned OuterLoopId) {
261 unsigned NumRows = DepMatrix.size();
263 for (
unsigned Row = 0; Row < NumRows; ++Row) {
264 char InnerDep = DepMatrix[Row][InnerLoopId];
265 char OuterDep = DepMatrix[Row][OuterLoopId];
266 if (InnerDep ==
'*' || OuterDep ==
'*')
279 Loop *CurrentLoop = &L;
280 const std::vector<Loop *> *Vec = &CurrentLoop->
getSubLoops();
281 while (!Vec->empty()) {
285 if (Vec->size() != 1)
288 LoopList.push_back(CurrentLoop);
289 CurrentLoop = Vec->front();
292 LoopList.push_back(CurrentLoop);
299 return InnerIndexVar;
305 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
306 !PhiTy->isPointerTy())
313 if (!isa<SCEVConstant>(Step))
326 class LoopInterchangeLegality {
330 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {}
333 bool canInterchangeLoops(
unsigned InnerLoopId,
unsigned OuterLoopId,
334 CharMatrix &DepMatrix);
338 bool isLoopStructureUnderstood(
PHINode *InnerInductionVar);
340 bool currentLimitations();
343 return OuterInnerReductions;
347 bool tightlyNested(
Loop *Outer,
Loop *Inner);
348 bool containsUnsafeInstructions(
BasicBlock *BB);
354 bool findInductionAndReductions(
Loop *L,
373 class LoopInterchangeProfitability {
377 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {}
380 bool isProfitable(
unsigned InnerLoopId,
unsigned OuterLoopId,
381 CharMatrix &DepMatrix);
384 int getInstrOrderCost();
397 class LoopInterchangeTransform {
402 const LoopInterchangeLegality &LIL)
403 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
404 LoopExit(LoopNestExit), LIL(LIL) {}
408 void restructureLoops(
Loop *NewInner,
Loop *NewOuter,
411 void removeChildLoop(
Loop *OuterLoop,
Loop *InnerLoop);
415 void splitInnerLoopHeader();
416 bool adjustLoopLinks();
417 void adjustLoopPreheaders();
418 bool adjustLoopBranches();
430 const LoopInterchangeLegality &LIL;
434 struct LoopInterchange :
public LoopPass {
459 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
460 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
461 DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI();
462 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
463 ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
468 bool isComputableLoopNest(LoopVector LoopList) {
469 for (
Loop *L : LoopList) {
487 unsigned selectLoopForInterchange(
const LoopVector &LoopList) {
490 return LoopList.size() - 1;
493 bool processLoopList(LoopVector LoopList) {
494 bool Changed =
false;
495 unsigned LoopNestDepth = LoopList.size();
496 if (LoopNestDepth < 2) {
497 LLVM_DEBUG(
dbgs() <<
"Loop doesn't contain minimum nesting level.\n");
500 if (LoopNestDepth > MaxLoopNestDepth) {
502 << MaxLoopNestDepth <<
"\n");
505 if (!isComputableLoopNest(LoopList)) {
506 LLVM_DEBUG(
dbgs() <<
"Not valid loop candidate for interchange\n");
510 LLVM_DEBUG(
dbgs() <<
"Processing LoopList of size = " << LoopNestDepth
513 CharMatrix DependencyMatrix;
514 Loop *OuterMostLoop = *(LoopList.begin());
516 OuterMostLoop, DI)) {
520 #ifdef DUMP_DEP_MATRICIES 522 printDepMatrix(DependencyMatrix);
532 unsigned SelecLoopId = selectLoopForInterchange(LoopList);
534 for (
unsigned i = SelecLoopId; i > 0; i--) {
536 processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix);
544 #ifdef DUMP_DEP_MATRICIES 546 printDepMatrix(DependencyMatrix);
548 Changed |= Interchanged;
553 bool processLoop(LoopVector LoopList,
unsigned InnerLoopId,
554 unsigned OuterLoopId,
BasicBlock *LoopNestExit,
555 std::vector<std::vector<char>> &DependencyMatrix) {
557 <<
" and OuterLoopId = " << OuterLoopId <<
"\n");
558 Loop *InnerLoop = LoopList[InnerLoopId];
559 Loop *OuterLoop = LoopList[OuterLoopId];
561 LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE);
562 if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
563 LLVM_DEBUG(
dbgs() <<
"Not interchanging loops. Cannot prove legality.\n");
567 LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE, ORE);
568 if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) {
577 <<
"Loop interchanged with enclosing loop.";
580 LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT, LoopNestExit,
591 bool LoopInterchangeLegality::containsUnsafeInstructions(
BasicBlock *BB) {
597 bool LoopInterchangeLegality::tightlyNested(
Loop *OuterLoop,
Loop *InnerLoop) {
609 if (!OuterLoopHeaderBI)
613 if (Succ != InnerLoopPreHeader && Succ != InnerLoop->
getHeader() &&
614 Succ != OuterLoopLatch)
617 LLVM_DEBUG(
dbgs() <<
"Checking instructions in Loop header and Loop latch\n");
620 if (containsUnsafeInstructions(OuterLoopHeader) ||
621 containsUnsafeInstructions(OuterLoopLatch))
629 bool LoopInterchangeLegality::isLoopStructureUnderstood(
633 for (
unsigned i = 0; i < Num; ++i) {
635 if (isa<Constant>(Val))
645 InnerLoopPreheader &&
669 if (PHI->getNumIncomingValues() == 1)
681 bool LoopInterchangeLegality::findInductionAndReductions(
694 if (OuterInnerReductions.find(&PHI) == OuterInnerReductions.end()) {
696 "across the outer loop.\n");
700 assert(PHI.getNumIncomingValues() == 2 &&
701 "Phis in loop header should have exactly 2 incoming values");
708 [&PHI](
Value *V) {
return V == &PHI; })) {
711 <<
"Failed to recognize PHI as an induction or reduction.\n");
714 OuterInnerReductions.insert(&PHI);
715 OuterInnerReductions.insert(InnerRedPhi);
725 if (PHI.getNumIncomingValues() > 1)
732 if (!isa<PHINode>(Ins) && isOuterLoopExitBlock)
740 bool LoopInterchangeLegality::currentLimitations() {
751 dbgs() <<
"Loops where the latch is not the exiting block are not" 752 <<
" supported currently.\n");
757 <<
"Loops where the latch is not the exiting block cannot be" 758 " interchange currently.";
765 if (!findInductionAndReductions(OuterLoop, Inductions, InnerLoop)) {
767 dbgs() <<
"Only outer loops with induction or reduction PHI nodes " 768 <<
"are supported currently.\n");
773 <<
"Only outer loops with induction or reduction PHI nodes can be" 774 " interchanged currently.";
780 if (Inductions.
size() != 1) {
781 LLVM_DEBUG(
dbgs() <<
"Loops with more than 1 induction variables are not " 782 <<
"supported currently.\n");
787 <<
"Only outer loops with 1 induction variable can be " 788 "interchanged currently.";
794 if (!findInductionAndReductions(InnerLoop, Inductions,
nullptr)) {
796 dbgs() <<
"Only inner loops with induction or reduction PHI nodes " 797 <<
"are supported currently.\n");
802 <<
"Only inner loops with induction or reduction PHI nodes can be" 803 " interchange currently.";
809 if (Inductions.
size() != 1) {
811 dbgs() <<
"We currently only support loops with 1 induction variable." 812 <<
"Failed to interchange due to current limitation\n");
817 <<
"Only inner loops with 1 induction variable can be " 818 "interchanged currently.";
825 if (!isLoopStructureUnderstood(InnerInductionVar)) {
831 <<
"Inner loop structure not understood currently.";
840 dbgs() <<
"Can only handle LCSSA PHIs in inner loops currently.\n");
845 <<
"Only inner loops with LCSSA PHIs can be interchange " 869 if (!InnerIndexVarInc) {
871 dbgs() <<
"Did not find an instruction to increment the induction " 877 <<
"The inner loop does not increment the induction variable.";
886 bool FoundInduction =
false;
889 if (isa<BranchInst>(
I) || isa<CmpInst>(
I) || isa<TruncInst>(
I) ||
895 if (!
I.isIdenticalTo(InnerIndexVarInc)) {
896 LLVM_DEBUG(
dbgs() <<
"Found unsupported instructions between induction " 897 <<
"variable increment and branch.\n");
902 <<
"Found unsupported instruction between induction variable " 903 "increment and branch.";
908 FoundInduction =
true;
913 if (!FoundInduction) {
919 <<
"Did not find the induction variable.";
939 if (PHI.getType()->isFloatingPointTy())
941 for (
unsigned i = 0; i < PHI.getNumIncomingValues(); i++) {
963 bool LoopInterchangeLegality::canInterchangeLoops(
unsigned InnerLoopId,
964 unsigned OuterLoopId,
965 CharMatrix &DepMatrix) {
967 LLVM_DEBUG(
dbgs() <<
"Failed interchange InnerLoopId = " << InnerLoopId
968 <<
" and OuterLoopId = " << OuterLoopId
969 <<
" due to dependence\n");
974 <<
"Cannot interchange loops due to dependences.";
979 for (
auto *BB : OuterLoop->
blocks())
981 if (
CallInst *CI = dyn_cast<CallInst>(&
I)) {
983 if (CI->doesNotReadMemory())
986 dbgs() <<
"Loops with call instructions cannot be interchanged " 992 <<
"Cannot interchange loops due to call instruction.";
1000 if (currentLimitations()) {
1001 LLVM_DEBUG(
dbgs() <<
"Not legal because of current transform limitation\n");
1006 if (!tightlyNested(OuterLoop, InnerLoop)) {
1012 <<
"Cannot interchange loops because they are not tightly " 1019 LLVM_DEBUG(
dbgs() <<
"Found unsupported PHI nodes in outer loop exit.\n");
1024 <<
"Found unsupported PHI node in loop exit.";
1032 int LoopInterchangeProfitability::getInstrOrderCost() {
1033 unsigned GoodOrder, BadOrder;
1034 BadOrder = GoodOrder = 0;
1038 unsigned NumOp =
GEP->getNumOperands();
1039 bool FoundInnerInduction =
false;
1040 bool FoundOuterInduction =
false;
1041 for (
unsigned i = 0; i < NumOp; ++i) {
1042 const SCEV *OperandVal = SE->getSCEV(
GEP->getOperand(i));
1052 if (AR->
getLoop() == InnerLoop) {
1055 FoundInnerInduction =
true;
1056 if (FoundOuterInduction) {
1066 if (AR->
getLoop() == OuterLoop) {
1069 FoundOuterInduction =
true;
1070 if (FoundInnerInduction) {
1079 return GoodOrder - BadOrder;
1083 unsigned OuterLoopId,
1084 CharMatrix &DepMatrix) {
1088 for (
auto &Row : DepMatrix) {
1089 if (Row[InnerLoopId] !=
'S' && Row[InnerLoopId] !=
'I')
1092 if (Row[OuterLoopId] !=
'=')
1098 return !DepMatrix.empty();
1101 bool LoopInterchangeProfitability::isProfitable(
unsigned InnerLoopId,
1102 unsigned OuterLoopId,
1103 CharMatrix &DepMatrix) {
1112 int Cost = getInstrOrderCost();
1126 <<
"Interchanging loops is too costly (cost=" 1127 <<
ore::NV(
"Cost", Cost) <<
", threshold=" 1129 <<
") and it does not improve parallelism.";
1134 void LoopInterchangeTransform::removeChildLoop(
Loop *OuterLoop,
1136 for (
Loop *L : *OuterLoop)
1137 if (L == InnerLoop) {
1138 OuterLoop->removeChildLoop(L);
1167 void LoopInterchangeTransform::restructureLoops(
1174 LI->changeLoopFor(OrigInnerPreHeader, OuterLoopParent);
1177 if (OuterLoopParent) {
1179 removeChildLoop(OuterLoopParent, NewInner);
1180 removeChildLoop(NewInner, NewOuter);
1183 removeChildLoop(NewInner, NewOuter);
1184 LI->changeTopLevelLoop(NewInner, NewOuter);
1186 while (!NewOuter->
empty())
1196 if (LI->getLoopFor(BB) == NewInner)
1205 if (LI->getLoopFor(BB) != NewOuter)
1208 if (BB == OuterHeader || BB == OuterLatch)
1211 LI->changeLoopFor(BB, NewInner);
1217 LI->changeLoopFor(OrigOuterPreHeader, NewOuter);
1220 SE->forgetLoop(NewOuter);
1221 SE->forgetLoop(NewInner);
1225 bool Transformed =
false;
1232 if (!InductionPHI) {
1233 LLVM_DEBUG(
dbgs() <<
"Failed to find the point to split loop latch \n");
1249 splitInnerLoopLatch(InnerIndexVar);
1258 Transformed |= adjustLoopLinks();
1267 void LoopInterchangeTransform::splitInnerLoopLatch(
Instruction *Inc) {
1269 BasicBlock *InnerLoopLatchPred = InnerLoopLatch;
1270 InnerLoopLatch =
SplitBlock(InnerLoopLatchPred, Inc, DT, LI);
1286 unsigned Num = PHI.getNumIncomingValues();
1287 for (
unsigned i = 0; i < Num; ++i) {
1288 if (PHI.getIncomingBlock(i) == OldPred)
1289 PHI.setIncomingBlock(i, NewPred);
1298 std::vector<DominatorTree::UpdateType> &DTUpdates) {
1300 [OldBB](
BasicBlock *BB) {
return BB == OldBB; }) < 2 &&
1301 "BI must jump to OldBB at most once.");
1306 DTUpdates.push_back(
1308 DTUpdates.push_back(
1332 for (
PHINode *
P : LcssaInnerExit) {
1333 bool hasUsersOutside =
false;
1334 for (
auto UI =
P->use_begin(),
E =
P->use_end(); UI !=
E;) {
1337 auto *Usr = cast<Instruction>(U.
getUser());
1338 if (Usr->getParent() != InnerExit) {
1339 hasUsersOutside =
true;
1342 U.
set(
P->getIncomingValueForBlock(InnerLatch));
1344 if (hasUsersOutside)
1347 P->eraseFromParent();
1361 bool LoopInterchangeTransform::adjustLoopBranches() {
1363 std::vector<DominatorTree::UpdateType> DTUpdates;
1369 InnerLoopPreHeader != InnerLoop->
getHeader() && OuterLoopPreHeader &&
1370 InnerLoopPreHeader &&
"Guaranteed by loop-simplify form");
1375 if (isa<PHINode>(OuterLoopPreHeader->
begin()) ||
1378 if (InnerLoopPreHeader == OuterLoop->
getHeader())
1401 if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor ||
1402 !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI ||
1411 if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI)
1414 if (!InnerLoopHeaderSuccessor)
1419 InnerLoopPreHeader, DTUpdates);
1420 updateSuccessor(OuterLoopHeaderBI, OuterLoopLatch, LoopExit, DTUpdates);
1422 InnerLoopHeaderSuccessor, DTUpdates);
1429 OuterLoopPreHeader, DTUpdates);
1432 if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader)
1433 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1);
1435 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0);
1438 InnerLoopLatchSuccessor, DTUpdates);
1441 if (OuterLoopLatchBI->
getSuccessor(0) == OuterLoopHeader)
1442 OuterLoopLatchSuccessor = OuterLoopLatchBI->
getSuccessor(1);
1444 OuterLoopLatchSuccessor = OuterLoopLatchBI->
getSuccessor(0);
1447 OuterLoopLatchSuccessor, DTUpdates);
1448 updateSuccessor(OuterLoopLatchBI, OuterLoopLatchSuccessor, InnerLoopLatch,
1451 DT->applyUpdates(DTUpdates);
1452 restructureLoops(OuterLoop, InnerLoop, InnerLoopPreHeader,
1453 OuterLoopPreHeader);
1455 moveLCSSAPhis(InnerLoopLatchSuccessor, InnerLoopLatch, OuterLoopLatch);
1463 InnerLoopPHIs.
push_back(cast<PHINode>(&PHI));
1465 OuterLoopPHIs.
push_back(cast<PHINode>(&PHI));
1467 auto &OuterInnerReductions = LIL.getOuterInnerReductions();
1468 (void)OuterInnerReductions;
1473 for (
PHINode *PHI : OuterLoopPHIs) {
1475 assert(OuterInnerReductions.find(PHI) != OuterInnerReductions.end() &&
1476 "Expected a reduction PHI node");
1478 for (
PHINode *PHI : InnerLoopPHIs) {
1480 assert(OuterInnerReductions.find(PHI) != OuterInnerReductions.end() &&
1481 "Expected a reduction PHI node");
1493 void LoopInterchangeTransform::adjustLoopPreheaders() {
1512 bool LoopInterchangeTransform::adjustLoopLinks() {
1514 bool Changed = adjustLoopBranches();
1516 adjustLoopPreheaders();
1523 "Interchanges loops for cache reuse",
false,
false)
Pass interface - Implemented by all 'passes'.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
This class represents lattice values for constants.
static bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction *> *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
static LoopVector populateWorklist(Loop &L)
Legacy pass manager pass to access dependence information.
static bool isProfitableForVectorization(unsigned InnerLoopId, unsigned OuterLoopId, CharMatrix &DepMatrix)
void push_back(const T &Elt)
LoopT * removeChildLoop(iterator I)
This removes the specified child from being a subloop of this loop.
The main scalar evolution driver.
This class represents a function call, abstracting a target machine's calling convention.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
static bool containsNoDependence(CharMatrix &DepMatrix, unsigned Row, unsigned Column)
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...
auto count_if(R &&Range, UnaryPredicate P) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
DependenceInfo - This class is the main dependence-analysis driver.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level, Loop *L, DependenceInfo *DI)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
const Loop * getLoop() const
unsigned getNumSuccessors() const
A Use represents the edge between a Value definition and its users.
void addBlockEntry(BlockT *BB)
This adds a basic block directly to the basic block list.
BlockT * getHeader() const
ConstantInt * getValue() 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"))
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix, unsigned InnerLoopId, unsigned OuterLoopId)
Type * getType() const
All values are typed, get the type of this value.
This node represents a polynomial recurrence on the trip count of the specified loop.
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
static bool validDepInterchange(CharMatrix &DepMatrix, unsigned Row, unsigned OuterLoopId, char InnerDep, char OuterDep)
static const unsigned MaxLoopNestDepth
static void updateSuccessor(BranchInst *BI, BasicBlock *OldBB, BasicBlock *NewBB, std::vector< DominatorTree::UpdateType > &DTUpdates)
Update BI to jump to NewBB instead of OldBB.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Value * getOperand(unsigned i) const
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.
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
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.
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug() const
Return a const iterator range over the instructions in the block, skipping any debug instructions...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
loop Interchanges loops for cache reuse
Represent the analysis usage information of a pass.
void splice(iterator where, iplist_impl &L2)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
self_iterator getIterator()
static bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes, DemandedBits *DB=nullptr, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Returns true if Phi is a reduction in TheLoop.
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
Used in the streaming interface as the general argument type.
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
iterator_range< decltype(adl_begin(std::declval< T >)))> drop_begin(T &&t, int n)
static unsigned getIncomingValueNumForOperand(unsigned i)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
void initializeLoopInterchangePass(PassRegistry &)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it...
const InstListType & getInstList() const
Return the underlying instruction list container.
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
A struct for saving information about induction variables.
Pass * createLoopInterchangePass()
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...
LLVM_NODISCARD T pop_back_val()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static const unsigned MaxMemInstrCount
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.
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
iterator_range< user_iterator > users()
INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange", "Interchanges loops for cache reuse", false, false) INITIALIZE_PASS_END(LoopInterchange
LoopT * getParentLoop() const
static PHINode * findInnerReductionPhi(Loop *L, Value *V)
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
This class represents an analyzed expression in the program.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
static void moveLCSSAPhis(BasicBlock *InnerExit, BasicBlock *InnerLatch, BasicBlock *OuterLatch)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
bool mayReadFromMemory() const
Return true if this instruction may read memory.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
static bool isOuterMostDepPositive(CharMatrix &DepMatrix, unsigned Row, unsigned Column)
void removeBlockFromLoop(BlockT *BB)
This removes the specified basic block from the current loop, updating the Blocks as appropriate...
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())
static void interChangeDependencies(CharMatrix &DepMatrix, unsigned FromIndx, unsigned ToIndx)
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
static Value * followLCSSA(Value *SV)
succ_range successors(Instruction *I)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
StringRef - Represent a constant reference to a string, i.e.
static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred, BasicBlock *NewPred)
op_range incoming_values()
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
iterator_range< block_iterator > blocks() const
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
static bool areLoopExitPHIsSupported(Loop *OuterLoop, Loop *InnerLoop)
const BasicBlock * getParent() const
This class represents a constant integer value.
static PHINode * getInductionVariable(Loop *L, ScalarEvolution *SE)
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.