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())
897 unsigned N = DRS.Roots.size() + 1;
898 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]),
ADR);
899 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->
getType(),
N);
900 if (
ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV))
904 for (
unsigned i = 1; i < N - 1; ++i) {
905 const SCEV *NewStepSCEV = SE->getMinusSCEV(SE->getSCEV(DRS.Roots[i]),
906 SE->getSCEV(DRS.Roots[i-1]));
907 if (NewStepSCEV != StepSCEV)
914 bool LoopReroll::DAGRootTracker::
915 findRootsBase(
Instruction *IVU, SmallInstructionSet SubsumedInsts) {
918 if (!IVU_ADR || IVU_ADR->getLoop() != L)
921 std::map<int64_t, Instruction*> V;
922 if (!collectPossibleRoots(IVU, V))
927 if (V.find(0) == V.end())
928 SubsumedInsts.insert(IVU);
932 DRS.BaseInst =
nullptr;
938 DRS.BaseInst = KV.second;
939 DRS.SubsumedInsts = SubsumedInsts;
940 }
else if (DRS.Roots.empty()) {
941 DRS.Roots.push_back(KV.second);
942 }
else if (V.find(KV.first - 1) != V.end()) {
943 DRS.Roots.push_back(KV.second);
946 if (!validateRootSet(DRS))
951 DRS.BaseInst = KV.second;
956 if (!validateRootSet(DRS))
961 RootSets.append(PotentialRootSets.
begin(), PotentialRootSets.
end());
966 bool LoopReroll::DAGRootTracker::findRoots() {
967 Inc = IVToIncMap[IV];
969 assert(RootSets.empty() &&
"Unclean state!");
971 for (
auto *IVU : IV->
users()) {
973 LoopIncs.push_back(cast<Instruction>(IVU));
975 findRootsRecursive(IV, SmallInstructionSet());
976 LoopIncs.push_back(IV);
978 if (!findRootsBase(IV, SmallInstructionSet()))
983 if (RootSets.empty()) {
984 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting because no root sets found!\n");
987 for (
auto &V : RootSets) {
988 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
991 <<
"LRR: Aborting because not all root sets have the same size\n");
996 Scale = RootSets[0].Roots.size() + 1;
998 if (Scale > IL_MaxRerollIterations) {
999 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting - too many iterations found. " 1000 <<
"#Found=" << Scale
1001 <<
", #Max=" << IL_MaxRerollIterations <<
"\n");
1005 LLVM_DEBUG(
dbgs() <<
"LRR: Successfully found roots: Scale=" << Scale
1011 bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
1015 Uses[&
I].resize(IL_End);
1018 SmallInstructionSet Exclude;
1019 for (
auto &DRS : RootSets) {
1020 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1021 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1022 Exclude.insert(DRS.BaseInst);
1024 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
1026 for (
auto &DRS : RootSets) {
1028 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
1029 for (
auto *I : VBase) {
1034 for (
auto *Root : DRS.Roots) {
1036 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
1039 if (V.
size() != VBase.size()) {
1040 LLVM_DEBUG(
dbgs() <<
"LRR: Aborting - use sets are different sizes\n");
1051 for (
auto *I : DRS.SubsumedInsts) {
1052 Uses[
I].set(IL_All);
1059 for (
auto &DRS : RootSets) {
1060 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
1061 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
1062 Exclude.insert(DRS.BaseInst);
1066 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
1068 Uses[
I].set(IL_All);
1078 LoopReroll::DAGRootTracker::nextInstr(
int Val, UsesTy &
In,
1079 const SmallInstructionSet &Exclude,
1080 UsesTy::iterator *StartI) {
1081 UsesTy::iterator I = StartI ? *StartI : In.begin();
1082 while (I != In.end() && (I->second.test(Val) == 0 ||
1083 Exclude.count(I->first) != 0))
1088 bool LoopReroll::DAGRootTracker::isBaseInst(
Instruction *I) {
1089 for (
auto &DRS : RootSets) {
1090 if (DRS.BaseInst == I)
1096 bool LoopReroll::DAGRootTracker::isRootInst(
Instruction *I) {
1097 for (
auto &DRS : RootSets) {
1106 bool LoopReroll::DAGRootTracker::instrDependsOn(
Instruction *I,
1107 UsesTy::iterator Start,
1108 UsesTy::iterator End) {
1109 for (
auto *U : I->
users()) {
1110 for (
auto It = Start; It != End; ++It)
1118 if (isa<DbgInfoIntrinsic>(I))
1126 case Intrinsic::annotation:
1127 case Intrinsic::ptr_annotation:
1128 case Intrinsic::var_annotation:
1151 SmallInstructionSet PossibleRedSet;
1152 SmallInstructionSet PossibleRedLastSet;
1153 SmallInstructionSet PossibleRedPHISet;
1154 Reductions.restrictToScale(Scale, PossibleRedSet,
1155 PossibleRedPHISet, PossibleRedLastSet);
1158 if (!collectUsedInstructions(PossibleRedSet))
1162 for (
auto *I : PossibleRedPHISet) {
1163 Uses[
I].set(IL_All);
1169 if (LoopControlIV && LoopControlIV != IV) {
1170 for (
auto *U : LoopControlIV->users()) {
1173 Uses[IVUser].set(IL_All);
1174 for (
auto *UU : IVUser->
users()) {
1177 Uses[UUser].set(IL_All);
1179 if (isa<SExtInst>(UUser)) {
1181 Uses[UUser].set(IL_All);
1184 if (UU->hasOneUse()) {
1187 Uses[BI].set(IL_All);
1195 for (
auto &KV : Uses) {
1198 dbgs() <<
"LRR: Aborting - instruction is not used in 1 iteration: " 1199 << *KV.first <<
" (#uses=" << KV.second.count() <<
")\n");
1206 dbgs() <<
"LRR: " << KV.second.find_first() <<
"\t" << *KV.first <<
"\n";
1209 for (
unsigned Iter = 1; Iter < Scale; ++Iter) {
1214 bool FutureSideEffects =
false;
1220 SmallInstructionSet Visited;
1221 auto BaseIt = nextInstr(0, Uses, Visited);
1222 auto RootIt = nextInstr(Iter, Uses, Visited);
1223 auto LastRootIt = Uses.begin();
1225 while (
BaseIt != Uses.end() && RootIt != Uses.end()) {
1231 if (isBaseInst(BaseInst)) {
1232 Visited.insert(BaseInst);
1233 BaseIt = nextInstr(0, Uses, Visited);
1236 if (isRootInst(RootInst)) {
1237 LastRootIt = RootIt;
1238 Visited.insert(RootInst);
1239 RootIt = nextInstr(Iter, Uses, Visited);
1242 if (Continue)
continue;
1255 auto TryIt = RootIt;
1257 while (TryIt != Uses.end() &&
1261 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
1264 if (TryIt == Uses.end() || TryIt == RootIt ||
1265 instrDependsOn(TryIt->first, RootIt, TryIt)) {
1267 << *BaseInst <<
" vs. " << *RootInst <<
"\n");
1272 RootInst = TryIt->first;
1283 for (; LastRootIt < RootIt; ++LastRootIt) {
1285 if (LastRootIt->second.find_first() < (int)Iter)
1296 FutureSideEffects =
true;
1302 if (RootIt->second.count() > 1) {
1303 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1304 <<
" vs. " << *RootInst <<
" (prev. case overlap)\n");
1312 for (
auto &K : AST) {
1313 if (K.aliasesUnknownInst(RootInst, *AA)) {
1315 << *BaseInst <<
" vs. " << *RootInst
1316 <<
" (depends on future store)\n");
1329 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1330 <<
" vs. " << *RootInst
1331 <<
" (side effects prevent reordering)\n");
1345 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
1348 bool Swapped =
false, SomeOpMatched =
false;
1356 if (
Instruction *Op2I = dyn_cast<Instruction>(Op2))
1357 if (Reductions.isPairInSame(RootInst, Op2I))
1361 if (BMI != BaseMap.
end()) {
1364 for (
auto &DRS : RootSets) {
1372 if (BaseInst->
getOperand(Swapped ?
unsigned(!j) : j) != Op2) {
1378 if (!Swapped && BaseInst->
isCommutative() && !SomeOpMatched &&
1383 <<
"LRR: iteration root match failed at " << *BaseInst
1384 <<
" vs. " << *RootInst <<
" (operand " << j <<
")\n");
1389 SomeOpMatched =
true;
1393 if ((!PossibleRedLastSet.count(BaseInst) &&
1395 (!PossibleRedLastSet.count(RootInst) &&
1397 LLVM_DEBUG(
dbgs() <<
"LRR: iteration root match failed at " << *BaseInst
1398 <<
" vs. " << *RootInst <<
" (uses outside loop)\n");
1402 Reductions.recordPair(BaseInst, RootInst, Iter);
1403 BaseMap.
insert(std::make_pair(RootInst, BaseInst));
1405 LastRootIt = RootIt;
1406 Visited.insert(BaseInst);
1407 Visited.insert(RootInst);
1408 BaseIt = nextInstr(0, Uses, Visited);
1409 RootIt = nextInstr(Iter, Uses, Visited);
1412 "Mismatched set sizes!");
1415 LLVM_DEBUG(
dbgs() <<
"LRR: Matched all iteration increments for " << *IV
1428 for (
auto &DRS : RootSets) {
1430 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1432 IncrExprs.
push_back(SE->getMinusSCEV(SE->getSCEV(DRS.Roots[0]), IVSCEV));
1438 unsigned I = Uses[&*J].find_first();
1439 if (I > 0 && I < IL_All) {
1441 J++->eraseFromParent();
1449 for (
size_t i = 0,
e = RootSets.size(); i !=
e; ++i)
1451 replaceIV(RootSets[i], StartExprs[i], IncrExprs[i]);
1457 auto Zero = SE->getZero(BackedgeTakenCount->
getType());
1458 auto One = SE->getOne(BackedgeTakenCount->
getType());
1464 auto TripCount = SE->getAddExpr(BackedgeTakenCount, One);
1465 auto ScaledTripCount = SE->getMulExpr(
1466 TripCount, SE->getConstant(BackedgeTakenCount->
getType(), Scale));
1467 auto ScaledBECount = SE->getMinusSCEV(ScaledTripCount, One);
1483 void LoopReroll::DAGRootTracker::replaceIV(DAGRootSet &DRS,
1485 const SCEV *IncrExpr) {
1489 const SCEV *NewIVSCEV =
1498 for (
auto &KV : Uses)
1499 if (KV.second.find_first() == 0)
1500 KV.first->replaceUsesOfWith(Inst, NewIV);
1507 bool LoopReroll::ReductionTracker::validateSelected() {
1509 for (
int i : Reds) {
1510 int PrevIter = 0, BaseCount = 0, Count = 0;
1515 int Iter = PossibleRedIter[J];
1516 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1518 LLVM_DEBUG(
dbgs() <<
"LRR: Out-of-order non-associative reduction: " 1523 if (Iter != PrevIter) {
1524 if (Count != BaseCount) {
1526 <<
"LRR: Iteration " << PrevIter <<
" reduction use count " 1527 << Count <<
" is not equal to the base use count " 1528 << BaseCount <<
"\n");
1550 void LoopReroll::ReductionTracker::replaceSelected() {
1553 for (
int i : Reds) {
1555 for (
int e = PossibleReds[i].
size(); j !=
e; ++j)
1556 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1562 SmallInstructionVector
Users;
1563 for (
User *U : PossibleReds[i].getReducedValue()->
users()) {
1564 Users.push_back(cast<Instruction>(U));
1569 PossibleReds[i][j]);
1617 const SCEV *BackedgeTakenCount,
1618 ReductionTracker &Reductions) {
1619 DAGRootTracker DAGRoots(
this, L, IV, SE, AA, TLI, DT, LI, PreserveLCSSA,
1620 IVToIncMap, LoopControlIV);
1622 if (!DAGRoots.findRoots())
1624 LLVM_DEBUG(
dbgs() <<
"LRR: Found all root induction increments for: " << *IV
1627 if (!DAGRoots.validate(Reductions))
1629 if (!Reductions.validateSelected())
1634 Reductions.replaceSelected();
1635 DAGRoots.replace(BackedgeTakenCount);
1645 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1646 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1647 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1648 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
1650 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1651 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
1662 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
1665 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
1667 LLVM_DEBUG(
dbgs() <<
"LRR: backedge-taken count = " << *BackedgeTakenCount
1672 SmallInstructionVector PossibleIVs;
1674 LoopControlIV =
nullptr;
1675 collectPossibleIVs(L, PossibleIVs);
1677 if (PossibleIVs.empty()) {
1682 ReductionTracker Reductions;
1683 collectPossibleReductions(L, Reductions);
1684 bool Changed =
false;
1689 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.
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt...
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.
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.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
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.
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)
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
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.