68 #define DEBUG_TYPE "loop-reroll" 70 STATISTIC(NumRerolledLoops,
"Number of rerolled loops");
75 cl::desc(
"The maximum number of failures to tolerate" 76 " during fuzzy matching. (default: 400)"));
157 IL_MaxRerollIterations = 32,
164 class LoopReroll :
public LoopPass {
200 struct SimpleLoopReduction {
202 assert(isa<PHINode>(P) &&
"First reduction instruction must be a PHI");
211 assert(Valid &&
"Using invalid reduction");
212 return Instructions.front();
216 assert(Valid &&
"Using invalid reduction");
217 return Instructions.back();
221 assert(Valid &&
"Using invalid reduction");
222 return Instructions[i+1];
225 Instruction *operator [] (
size_t i)
const {
return get(i); }
228 size_t size()
const {
229 assert(Valid &&
"Using invalid reduction");
230 return Instructions.size()-1;
233 using iterator = SmallInstructionVector::iterator;
234 using const_iterator = SmallInstructionVector::const_iterator;
237 assert(Valid &&
"Using invalid reduction");
238 return std::next(Instructions.begin());
241 const_iterator
begin()
const {
242 assert(Valid &&
"Using invalid reduction");
243 return std::next(Instructions.begin());
246 iterator
end() {
return Instructions.
end(); }
247 const_iterator
end()
const {
return Instructions.
end(); }
251 SmallInstructionVector Instructions;
258 struct ReductionTracker {
262 void addSLR(SimpleLoopReduction &SLR) { PossibleReds.
push_back(SLR); }
272 void restrictToScale(uint64_t Scale,
273 SmallInstructionSet &PossibleRedSet,
274 SmallInstructionSet &PossibleRedPHISet,
275 SmallInstructionSet &PossibleRedLastSet) {
276 PossibleRedIdx.clear();
277 PossibleRedIter.clear();
280 for (
unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
281 if (PossibleReds[i].
size() % Scale == 0) {
282 PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
283 PossibleRedPHISet.insert(PossibleReds[i].getPHI());
285 PossibleRedSet.insert(PossibleReds[i].getPHI());
286 PossibleRedIdx[PossibleReds[i].getPHI()] = i;
288 PossibleRedSet.insert(J);
289 PossibleRedIdx[J] = i;
300 if (J1I != PossibleRedIdx.
end()) {
302 if (J2I != PossibleRedIdx.
end() && J1I->second == J2I->second)
313 if (PossibleRedIdx.count(J1)) {
314 assert(PossibleRedIdx.count(J2) &&
315 "Recording reduction vs. non-reduction instruction?");
317 PossibleRedIter[J1] = 0;
318 PossibleRedIter[J2] = i;
320 int Idx = PossibleRedIdx[J1];
321 assert(Idx == PossibleRedIdx[J2] &&
322 "Recording pair from different reductions?");
330 bool validateSelected();
331 void replaceSelected();
335 SmallReductionVector PossibleReds;
370 SmallInstructionVector Roots;
373 SmallInstructionSet SubsumedInsts;
378 struct DAGRootTracker {
385 : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), DT(DT), LI(LI),
386 PreserveLCSSA(PreserveLCSSA), IV(IV), IVToIncMap(IncrMap),
387 LoopControlIV(LoopCtrlIV) {}
393 bool validate(ReductionTracker &Reductions);
404 SmallInstructionSet SubsumedInsts);
405 bool findRootsBase(
Instruction *IVU, SmallInstructionSet SubsumedInsts);
407 std::map<int64_t,Instruction*> &Roots);
408 bool validateRootSet(DAGRootSet &DRS);
410 bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
411 void collectInLoopUserSet(
const SmallInstructionVector &Roots,
412 const SmallInstructionSet &Exclude,
413 const SmallInstructionSet &Final,
416 const SmallInstructionSet &Exclude,
417 const SmallInstructionSet &Final,
420 UsesTy::iterator nextInstr(
int Val, UsesTy &
In,
421 const SmallInstructionSet &Exclude,
422 UsesTy::iterator *StartI=
nullptr);
426 UsesTy::iterator Start,
427 UsesTy::iterator End);
428 void replaceIV(DAGRootSet &DRS,
const SCEV *Start,
const SCEV *IncrExpr);
456 SmallInstructionVector LoopIncs;
472 if (!isa<BranchInst>(TI) || !isa<CmpInst>(I))
474 return I->
hasOneUse() && TI->getOperand(0) ==
I;
478 void collectPossibleIVs(
Loop *L, SmallInstructionVector &PossibleIVs);
479 void collectPossibleReductions(
Loop *L,
480 ReductionTracker &Reductions);
482 const SCEV *BackedgeTakenCount, ReductionTracker &Reductions);
495 return new LoopReroll;
503 if (!L->
contains(cast<Instruction>(U)))
517 if (IVUses != 2 && IVUses != 1)
522 bool IsCompInst = isCompareUsedByBranch(cast<Instruction>(
User));
525 if (IncOrCmpUses != 2 && IncOrCmpUses != 1)
532 if (IsCompInst || IncOrCmpUses != 2)
537 if (IVUses == 2 && IncOrCmpUses != 1)
541 if (
auto *BO = dyn_cast<BinaryOperator>(
User)) {
546 if (
PHINode *PN = dyn_cast<PHINode>(UU)) {
555 if (BO->hasNoSignedWrap() && UUser && UUser->
hasOneUse() &&
556 isa<SExtInst>(UUser))
558 if (!isCompareUsedByBranch(UUser))
565 }
else if (!IsCompInst)
573 void LoopReroll::collectPossibleIVs(
Loop *L,
574 SmallInstructionVector &PossibleIVs) {
578 if (!isa<PHINode>(
I))
580 if (!
I->getType()->isIntegerTy() && !
I->getType()->isPointerTy())
584 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&*
I))) {
585 if (PHISCEV->getLoop() != L)
587 if (!PHISCEV->isAffine())
595 if (isLoopControlIV(L, &*
I)) {
596 assert(!LoopControlIV &&
"Found two loop control only IV");
597 LoopControlIV = &(*I);
599 <<
" = " << *PHISCEV <<
"\n");
601 PossibleIVs.push_back(&*
I);
611 assert(!Valid &&
"Cannot add to an already-valid chain");
626 if (!(isa<PHINode>(Instructions.back()) ||
630 Instructions.push_back(C);
634 if (Instructions.size() < 2 ||
642 if (L->
contains(cast<Instruction>(U)))
643 if (cast<Instruction>(U) != Instructions.front())
647 Instructions.push_back(C);
652 void LoopReroll::collectPossibleReductions(
Loop *L,
653 ReductionTracker &Reductions) {
657 if (!isa<PHINode>(
I))
659 if (!
I->getType()->isSingleValueType())
662 SimpleLoopReduction SLR(&*
I, L);
667 << SLR.size() <<
" chained instructions)\n");
668 Reductions.addSLR(SLR);
684 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
685 Instruction *Root,
const SmallInstructionSet &Exclude,
686 const SmallInstructionSet &Final,
688 SmallInstructionVector Queue(1, Root);
689 while (!Queue.empty()) {
691 if (!Users.
insert(I).second)
697 if (
PHINode *PN = dyn_cast<PHINode>(User)) {
699 if (PN->getIncomingBlock(U) == L->
getHeader())
703 if (L->
contains(User) && !Exclude.count(User)) {
704 Queue.push_back(User);
710 OIE = I->
op_end(); OI != OIE; ++OI) {
721 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
722 const SmallInstructionVector &Roots,
723 const SmallInstructionSet &Exclude,
724 const SmallInstructionSet &Final,
727 collectInLoopUserSet(Root, Exclude, Final, Users);
731 if (
LoadInst *LI = dyn_cast<LoadInst>(I))
732 return LI->isUnordered();
734 return SI->isUnordered();
736 return !
MI->isVolatile();
745 switch (
I->getOpcode()) {
746 default:
return false;
748 case Instruction::Sub:
749 case Instruction::Mul:
750 case Instruction::Shl:
751 case Instruction::AShr:
752 case Instruction::LShr:
753 case Instruction::GetElementPtr:
754 case Instruction::Trunc:
755 case Instruction::ZExt:
756 case Instruction::SExt:
767 (!BO && !isa<GetElementPtrInst>(U)))
770 for (
auto *UU : U->
users()) {
778 bool LoopReroll::DAGRootTracker::
779 collectPossibleRoots(
Instruction *
Base, std::map<int64_t,Instruction*> &Roots) {
780 SmallInstructionVector BaseUsers;
782 for (
auto *
I : Base->
users()) {
786 LoopIncs.push_back(cast<Instruction>(
I));
791 if (
auto *BO = dyn_cast<BinaryOperator>(
I)) {
793 BO->getOpcode() == Instruction::Or)
794 CI = dyn_cast<ConstantInt>(BO->getOperand(1));
795 }
else if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
I)) {
796 Value *LastOperand =
GEP->getOperand(
GEP->getNumOperands()-1);
802 BaseUsers.push_back(II);
812 if (Roots.find(V) != Roots.end())
816 Roots[V] = cast<Instruction>(
I);
820 if (Roots.empty() || (Roots.size() == 1 && BaseUsers.empty()))
826 if (BaseUsers.size()) {
827 if (Roots.find(0) != Roots.end()) {
828 LLVM_DEBUG(
dbgs() <<
"LRR: Multiple roots found for base - aborting!\n");
835 unsigned NumBaseUses = BaseUsers.size();
836 if (NumBaseUses == 0)
837 NumBaseUses = Roots.begin()->second->getNumUses();
840 for (
auto &KV : Roots) {
843 if (!KV.second->hasNUses(NumBaseUses)) {
844 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting - Root and Base #users not the same: " 845 <<
"#Base=" << NumBaseUses
846 <<
", #Root=" << KV.second->getNumUses() <<
"\n");
854 void LoopReroll::DAGRootTracker::
855 findRootsRecursive(
Instruction *
I, SmallInstructionSet SubsumedInsts) {
861 if (I != IV && findRootsBase(I, SubsumedInsts))
864 SubsumedInsts.insert(I);
875 findRootsRecursive(I, SubsumedInsts);
879 bool LoopReroll::DAGRootTracker::validateRootSet(DAGRootSet &DRS) {
880 if (DRS.Roots.empty())
895 unsigned N = DRS.Roots.size() + 1;
896 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]),
ADR);
897 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->
getType(),
N);
898 if (
ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV))
904 bool LoopReroll::DAGRootTracker::
905 findRootsBase(
Instruction *IVU, SmallInstructionSet SubsumedInsts) {
908 if (!IVU_ADR || IVU_ADR->getLoop() != L)
911 std::map<int64_t, Instruction*> V;
912 if (!collectPossibleRoots(IVU, V))
917 if (V.find(0) == V.end())
918 SubsumedInsts.insert(IVU);
922 DRS.BaseInst =
nullptr;
928 DRS.BaseInst = KV.second;
929 DRS.SubsumedInsts = SubsumedInsts;
930 }
else if (DRS.Roots.empty()) {
931 DRS.Roots.push_back(KV.second);
932 }
else if (V.find(KV.first - 1) != V.end()) {
933 DRS.Roots.push_back(KV.second);
936 if (!validateRootSet(DRS))
941 DRS.BaseInst = KV.second;
946 if (!validateRootSet(DRS))
951 RootSets.append(PotentialRootSets.
begin(), PotentialRootSets.
end());
956 bool LoopReroll::DAGRootTracker::findRoots() {
957 Inc = IVToIncMap[IV];
959 assert(RootSets.empty() &&
"Unclean state!");
961 for (
auto *IVU : IV->
users()) {
963 LoopIncs.push_back(cast<Instruction>(IVU));
965 findRootsRecursive(IV, SmallInstructionSet());
966 LoopIncs.push_back(IV);
968 if (!findRootsBase(IV, SmallInstructionSet()))
973 if (RootSets.empty()) {
974 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting because no root sets found!\n");
977 for (
auto &V : RootSets) {
978 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
981 <<
"LRR: Aborting because not all root sets have the same size\n");
986 Scale = RootSets[0].Roots.size() + 1;
988 if (Scale > IL_MaxRerollIterations) {
989 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting - too many iterations found. " 990 <<
"#Found=" << Scale
991 <<
", #Max=" << IL_MaxRerollIterations <<
"\n");
995 LLVM_DEBUG(
dbgs() <<
"LRR: Successfully found roots: Scale=" << Scale
1001 bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
1005 Uses[&
I].resize(IL_End);
1008 SmallInstructionSet Exclude;
1009 for (
auto &DRS : RootSets) {
1010 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1011 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1012 Exclude.insert(DRS.BaseInst);
1014 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
1016 for (
auto &DRS : RootSets) {
1018 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
1019 for (
auto *I : VBase) {
1024 for (
auto *Root : DRS.Roots) {
1026 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
1029 if (V.
size() != VBase.size()) {
1030 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting - use sets are different sizes\n");
1041 for (
auto *I : DRS.SubsumedInsts) {
1042 Uses[
I].set(IL_All);
1049 for (
auto &DRS : RootSets) {
1050 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1051 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1052 Exclude.insert(DRS.BaseInst);
1056 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
1058 Uses[
I].set(IL_All);
1068 LoopReroll::DAGRootTracker::nextInstr(
int Val, UsesTy &
In,
1069 const SmallInstructionSet &Exclude,
1070 UsesTy::iterator *StartI) {
1071 UsesTy::iterator I = StartI ? *StartI : In.begin();
1072 while (I != In.end() && (I->second.test(Val) == 0 ||
1073 Exclude.count(I->first) != 0))
1078 bool LoopReroll::DAGRootTracker::isBaseInst(
Instruction *I) {
1079 for (
auto &DRS : RootSets) {
1080 if (DRS.BaseInst == I)
1086 bool LoopReroll::DAGRootTracker::isRootInst(
Instruction *I) {
1087 for (
auto &DRS : RootSets) {
1096 bool LoopReroll::DAGRootTracker::instrDependsOn(
Instruction *I,
1097 UsesTy::iterator Start,
1098 UsesTy::iterator End) {
1099 for (
auto *U : I->
users()) {
1100 for (
auto It = Start; It != End; ++It)
1108 if (isa<DbgInfoIntrinsic>(I))
1126 bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
1141 SmallInstructionSet PossibleRedSet;
1142 SmallInstructionSet PossibleRedLastSet;
1143 SmallInstructionSet PossibleRedPHISet;
1144 Reductions.restrictToScale(Scale, PossibleRedSet,
1145 PossibleRedPHISet, PossibleRedLastSet);
1148 if (!collectUsedInstructions(PossibleRedSet))
1152 for (
auto *I : PossibleRedPHISet) {
1153 Uses[
I].set(IL_All);
1159 if (LoopControlIV && LoopControlIV != IV) {
1160 for (
auto *U : LoopControlIV->users()) {
1163 Uses[IVUser].set(IL_All);
1164 for (
auto *UU : IVUser->
users()) {
1167 Uses[UUser].set(IL_All);
1169 if (isa<SExtInst>(UUser)) {
1171 Uses[UUser].set(IL_All);
1174 if (UU->hasOneUse()) {
1177 Uses[BI].set(IL_All);
1185 for (
auto &KV : Uses) {
1188 dbgs() <<
"LRR: Aborting - instruction is not used in 1 iteration: " 1189 << *KV.first <<
" (#uses=" << KV.second.count() <<
")\n");
1196 dbgs() <<
"LRR: " << KV.second.find_first() <<
"\t" << *KV.first <<
"\n";
1199 for (
unsigned Iter = 1; Iter < Scale; ++Iter) {
1204 bool FutureSideEffects =
false;
1210 SmallInstructionSet Visited;
1211 auto BaseIt = nextInstr(0, Uses, Visited);
1212 auto RootIt = nextInstr(Iter, Uses, Visited);
1213 auto LastRootIt = Uses.begin();
1215 while (
BaseIt != Uses.end() && RootIt != Uses.end()) {
1221 if (isBaseInst(BaseInst)) {
1222 Visited.insert(BaseInst);
1223 BaseIt = nextInstr(0, Uses, Visited);
1226 if (isRootInst(RootInst)) {
1227 LastRootIt = RootIt;
1228 Visited.insert(RootInst);
1229 RootIt = nextInstr(Iter, Uses, Visited);
1232 if (Continue)
continue;
1245 auto TryIt = RootIt;
1247 while (TryIt != Uses.end() &&
1251 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
1254 if (TryIt == Uses.end() || TryIt == RootIt ||
1255 instrDependsOn(TryIt->first, RootIt, TryIt)) {
1257 << *BaseInst <<
" vs. " << *RootInst <<
"\n");
1262 RootInst = TryIt->first;
1273 for (; LastRootIt < RootIt; ++LastRootIt) {
1275 if (LastRootIt->second.find_first() < (int)Iter)
1286 FutureSideEffects =
true;
1292 if (RootIt->second.count() > 1) {
1293 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1294 <<
" vs. " << *RootInst <<
" (prev. case overlap)\n");
1302 for (
auto &K : AST) {
1303 if (K.aliasesUnknownInst(RootInst, *AA)) {
1305 << *BaseInst <<
" vs. " << *RootInst
1306 <<
" (depends on future store)\n");
1319 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1320 <<
" vs. " << *RootInst
1321 <<
" (side effects prevent reordering)\n");
1335 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
1338 bool Swapped =
false, SomeOpMatched =
false;
1346 if (
Instruction *Op2I = dyn_cast<Instruction>(Op2))
1347 if (Reductions.isPairInSame(RootInst, Op2I))
1351 if (BMI != BaseMap.
end()) {
1354 for (
auto &DRS : RootSets) {
1362 if (BaseInst->
getOperand(Swapped ?
unsigned(!j) : j) != Op2) {
1368 if (!Swapped && BaseInst->
isCommutative() && !SomeOpMatched &&
1373 <<
"LRR: iteration root match failed at " << *BaseInst
1374 <<
" vs. " << *RootInst <<
" (operand " << j <<
")\n");
1379 SomeOpMatched =
true;
1383 if ((!PossibleRedLastSet.count(BaseInst) &&
1385 (!PossibleRedLastSet.count(RootInst) &&
1387 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1388 <<
" vs. " << *RootInst <<
" (uses outside loop)\n");
1392 Reductions.recordPair(BaseInst, RootInst, Iter);
1393 BaseMap.
insert(std::make_pair(RootInst, BaseInst));
1395 LastRootIt = RootIt;
1396 Visited.insert(BaseInst);
1397 Visited.insert(RootInst);
1398 BaseIt = nextInstr(0, Uses, Visited);
1399 RootIt = nextInstr(Iter, Uses, Visited);
1402 "Mismatched set sizes!");
1405 LLVM_DEBUG(
dbgs() <<
"LRR: Matched all iteration increments for " << *IV
1418 for (
auto &DRS : RootSets) {
1420 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1422 IncrExprs.
push_back(SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]), IVSCEV));
1428 unsigned I = Uses[&*J].find_first();
1429 if (I > 0 && I < IL_All) {
1431 J++->eraseFromParent();
1439 for (
size_t i = 0, e = RootSets.size(); i != e; ++i)
1441 replaceIV(RootSets[i], StartExprs[i], IncrExprs[i]);
1447 auto Zero = SE->getZero(BackedgeTakenCount->
getType());
1448 auto One = SE->getOne(BackedgeTakenCount->
getType());
1454 auto TripCount = SE->getAddExpr(BackedgeTakenCount, One);
1455 auto ScaledTripCount = SE->getMulExpr(
1456 TripCount, SE->getConstant(BackedgeTakenCount->
getType(), Scale));
1457 auto ScaledBECount = SE->getMinusSCEV(ScaledTripCount, One);
1473 void LoopReroll::DAGRootTracker::replaceIV(DAGRootSet &DRS,
1475 const SCEV *IncrExpr) {
1479 const SCEV *NewIVSCEV =
1488 for (
auto &KV : Uses)
1489 if (KV.second.find_first() == 0)
1490 KV.first->replaceUsesOfWith(Inst, NewIV);
1497 bool LoopReroll::ReductionTracker::validateSelected() {
1499 for (
int i : Reds) {
1500 int PrevIter = 0, BaseCount = 0, Count = 0;
1505 int Iter = PossibleRedIter[J];
1506 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1508 LLVM_DEBUG(
dbgs() <<
"LRR: Out-of-order non-associative reduction: " 1513 if (Iter != PrevIter) {
1514 if (Count != BaseCount) {
1516 <<
"LRR: Iteration " << PrevIter <<
" reduction use count " 1517 << Count <<
" is not equal to the base use count " 1518 << BaseCount <<
"\n");
1540 void LoopReroll::ReductionTracker::replaceSelected() {
1543 for (
int i : Reds) {
1545 for (
int e = PossibleReds[i].
size(); j != e; ++j)
1546 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1552 SmallInstructionVector
Users;
1553 for (
User *U : PossibleReds[i].getReducedValue()->
users()) {
1554 Users.push_back(cast<Instruction>(U));
1559 PossibleReds[i][j]);
1607 const SCEV *BackedgeTakenCount,
1608 ReductionTracker &Reductions) {
1609 DAGRootTracker DAGRoots(
this, L, IV, SE, AA, TLI, DT, LI, PreserveLCSSA,
1610 IVToIncMap, LoopControlIV);
1612 if (!DAGRoots.findRoots())
1614 LLVM_DEBUG(
dbgs() <<
"LRR: Found all root induction increments for: " << *IV
1617 if (!DAGRoots.validate(Reductions))
1619 if (!Reductions.validateSelected())
1624 Reductions.replaceSelected();
1625 DAGRoots.replace(BackedgeTakenCount);
1635 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1636 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1637 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1638 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1639 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1640 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
1651 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
1654 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
1656 LLVM_DEBUG(
dbgs() <<
"LRR: backedge-taken count = " << *BackedgeTakenCount
1661 SmallInstructionVector PossibleIVs;
1663 LoopControlIV =
nullptr;
1664 collectPossibleIVs(L, PossibleIVs);
1666 if (PossibleIVs.empty()) {
1671 ReductionTracker Reductions;
1672 collectPossibleReductions(L, Reductions);
1673 bool Changed =
false;
1678 if (reroll(PossibleIV, L, Header, BackedgeTakenCount, Reductions)) {
Pass interface - Implemented by all 'passes'.
A parsed version of the target data layout string in and methods for querying it. ...
const_iterator end(StringRef path)
Get end iterator over path.
Pass * createLoopRerollPass()
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
This class represents lattice values for constants.
BinaryOps getOpcode() const
void swapSuccessors()
Swap the successors of this branch instruction.
Implements a dense probed hash-table based set.
void push_back(const T &Elt)
static cl::opt< unsigned > NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400), cl::Hidden, cl::desc("The maximum number of failures to tolerate" " during fuzzy matching. (default: 400)"))
void add(Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo)
These methods are used to add different types of instructions to the alias sets.
The main scalar evolution driver.
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
This class implements a map that also provides access to all stored values in a deterministic order...
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
void initializeLoopRerollPass(PassRegistry &)
An instruction for reading from memory.
reverse_iterator rbegin()
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...
iv Induction Variable Users
This defines the Use class.
iterator begin()
Instruction iterator methods.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static bool isIgnorableInst(const Instruction *I)
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static bool isUnorderedLoadStore(Instruction *I)
A Use represents the edge between a Value definition and its users.
static bool hasUsesOutsideLoop(Instruction *I, Loop *L)
This file implements a class to represent arbitrary precision integral constant values and operations...
BlockT * getHeader() const
ConstantInt * getValue() const
int64_t getSExtValue() const
Get sign extended value.
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.
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Examine each PHI in the given block and delete it if it is dead.
const APInt & getValue() const
Return the constant as an APInt value reference.
An instruction for storing to memory.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Value * getOperand(unsigned i) const
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
iterator find(const_arg_type_t< KeyT > Val)
initializer< Ty > init(const Ty &Val)
friend const_iterator end(StringRef path)
Get end iterator over path.
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(const ValueT &V)
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
static bool isLoopIncrement(User *U, Instruction *IV)
const SCEV * getStart() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
This is the common base class for memset/memcpy/memmove.
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
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.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Type * getType() const
Return the LLVM type of this SCEV expression.
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.
Provides information about what library functions are available for the current target.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
typename VectorType::iterator iterator
bool isCommutative() const
Return true if the instruction is commutative:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< user_iterator > users()
This class uses information about analyze scalars to rewrite expressions in canonical form...
static bool isSimpleArithmeticOp(User *IVU)
Return true if IVU is a "simple" arithmetic operation.
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
unsigned getNumUses() const
This method computes the number of uses of this Value.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
bool mayReadFromMemory() const
Return true if this instruction may read memory.
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)
void setCondition(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
bool hasOneUse() const
Return true if there is exactly one user of this value.
const Instruction * getFirstNonPHIOrDbg() const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
static bool isAssociative(const COFFSection &Section)
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
This class represents a constant integer value.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.