132#define DEBUG_TYPE "loop-reduce"
148 return Opts.stress_ivchain;
158 static const unsigned UnknownAddressSpace =
159 std::numeric_limits<unsigned>::max();
161 Type *MemTy =
nullptr;
162 unsigned AddrSpace = UnknownAddressSpace;
164 MemAccessTy() =
default;
165 MemAccessTy(
Type *Ty,
unsigned AS) : MemTy(Ty), AddrSpace(AS) {}
168 return MemTy ==
Other.MemTy && AddrSpace ==
Other.AddrSpace;
173 static MemAccessTy getUnknown(LLVMContext &Ctx,
174 unsigned AS = UnknownAddressSpace) {
175 return MemAccessTy(Type::getVoidTy(Ctx), AS);
186 SmallBitVector UsedByIndices;
188 void print(raw_ostream &OS)
const;
195 constexpr Immediate(ScalarTy MinVal,
bool Scalable)
196 : FixedOrScalableQuantity(MinVal, Scalable) {}
198 constexpr Immediate(
const FixedOrScalableQuantity<Immediate, int64_t> &
V)
199 : FixedOrScalableQuantity(
V) {}
202 constexpr Immediate() =
delete;
204 static constexpr Immediate getFixed(ScalarTy MinVal) {
205 return {MinVal,
false};
207 static constexpr Immediate getScalable(ScalarTy MinVal) {
208 return {MinVal,
true};
210 static constexpr Immediate
get(ScalarTy MinVal,
bool Scalable) {
211 return {MinVal, Scalable};
213 static constexpr Immediate getZero() {
return {0,
false}; }
214 static constexpr Immediate getFixedMin() {
215 return {std::numeric_limits<int64_t>::min(),
false};
217 static constexpr Immediate getFixedMax() {
218 return {std::numeric_limits<int64_t>::max(),
false};
220 static constexpr Immediate getScalableMin() {
221 return {std::numeric_limits<int64_t>::min(),
true};
223 static constexpr Immediate getScalableMax() {
224 return {std::numeric_limits<int64_t>::max(),
true};
227 constexpr bool isLessThanZero()
const {
return Quantity < 0; }
229 constexpr bool isGreaterThanZero()
const {
return Quantity > 0; }
231 constexpr bool isCompatibleImmediate(
const Immediate &
Imm)
const {
232 return isZero() ||
Imm.isZero() ||
Imm.Scalable == Scalable;
235 constexpr bool isMin()
const {
236 return Quantity == std::numeric_limits<ScalarTy>::min();
239 constexpr bool isMax()
const {
240 return Quantity == std::numeric_limits<ScalarTy>::max();
244 constexpr Immediate addUnsigned(
const Immediate &
RHS)
const {
245 assert(isCompatibleImmediate(
RHS) &&
"Incompatible Immediates");
247 return {
Value, Scalable ||
RHS.isScalable()};
250 constexpr Immediate subUnsigned(
const Immediate &
RHS)
const {
251 assert(isCompatibleImmediate(
RHS) &&
"Incompatible Immediates");
253 return {
Value, Scalable ||
RHS.isScalable()};
257 constexpr Immediate mulUnsigned(
const ScalarTy
RHS)
const {
259 return {
Value, Scalable};
263 const SCEV *getSCEV(ScalarEvolution &SE,
Type *Ty)
const {
270 const SCEV *getNegativeSCEV(ScalarEvolution &SE,
Type *Ty)
const {
277 const SCEV *getUnknownSCEV(ScalarEvolution &SE,
Type *Ty)
const {
292struct KeyOrderTargetImmediate {
293 bool operator()(
const Immediate &
LHS,
const Immediate &
RHS)
const {
294 if (
LHS.isScalable() && !
RHS.isScalable())
296 if (!
LHS.isScalable() &&
RHS.isScalable())
298 return LHS.getKnownMinValue() <
RHS.getKnownMinValue();
305struct KeyOrderSizeTAndImmediate {
306 bool operator()(
const std::pair<size_t, Immediate> &
LHS,
307 const std::pair<size_t, Immediate> &
RHS)
const {
308 size_t LSize =
LHS.first;
309 size_t RSize =
RHS.first;
311 return LSize < RSize;
312 return KeyOrderTargetImmediate()(
LHS.second,
RHS.second);
317#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
319 OS <<
"[NumUses=" << UsedByIndices.
count() <<
']';
331 using RegUsesTy = DenseMap<const SCEV *, RegSortData>;
333 RegUsesTy RegUsesMap;
337 void countRegister(
const SCEV *
Reg,
size_t LUIdx);
338 void dropRegister(
const SCEV *
Reg,
size_t LUIdx);
339 void swapAndDropUse(
size_t LUIdx,
size_t LastLUIdx);
341 bool isRegUsedByUsesOtherThan(
const SCEV *
Reg,
size_t LUIdx)
const;
343 const SmallBitVector &getUsedByIndices(
const SCEV *
Reg)
const;
359RegUseTracker::countRegister(
const SCEV *
Reg,
size_t LUIdx) {
360 std::pair<RegUsesTy::iterator, bool> Pair = RegUsesMap.try_emplace(
Reg);
361 RegSortData &RSD = Pair.first->second;
364 RSD.UsedByIndices.
resize(std::max(RSD.UsedByIndices.
size(), LUIdx + 1));
365 RSD.UsedByIndices.
set(LUIdx);
369RegUseTracker::dropRegister(
const SCEV *
Reg,
size_t LUIdx) {
370 RegUsesTy::iterator It = RegUsesMap.find(
Reg);
371 assert(It != RegUsesMap.end());
372 RegSortData &RSD = It->second;
374 RSD.UsedByIndices.
reset(LUIdx);
378RegUseTracker::swapAndDropUse(
size_t LUIdx,
size_t LastLUIdx) {
379 assert(LUIdx <= LastLUIdx);
383 for (
auto &Pair : RegUsesMap) {
384 SmallBitVector &UsedByIndices = Pair.second.UsedByIndices;
385 if (LUIdx < UsedByIndices.
size())
386 UsedByIndices[LUIdx] =
387 LastLUIdx < UsedByIndices.
size() ? UsedByIndices[LastLUIdx] :
false;
388 UsedByIndices.
resize(std::min(UsedByIndices.
size(), LastLUIdx));
393RegUseTracker::isRegUsedByUsesOtherThan(
const SCEV *
Reg,
size_t LUIdx)
const {
394 RegUsesTy::const_iterator
I = RegUsesMap.find(
Reg);
395 if (
I == RegUsesMap.end())
397 const SmallBitVector &UsedByIndices =
I->second.UsedByIndices;
399 if (i == -1)
return false;
400 if ((
size_t)i != LUIdx)
return true;
404const SmallBitVector &RegUseTracker::getUsedByIndices(
const SCEV *
Reg)
const {
405 RegUsesTy::const_iterator
I = RegUsesMap.find(
Reg);
406 assert(
I != RegUsesMap.end() &&
"Unknown register!");
407 return I->second.UsedByIndices;
410void RegUseTracker::clear() {
421 GlobalValue *BaseGV =
nullptr;
424 Immediate BaseOffset = Immediate::getZero();
427 bool HasBaseReg =
false;
450 const SCEV *ScaledReg =
nullptr;
455 Immediate UnfoldedOffset = Immediate::getZero();
459 void initialMatch(
const SCEV *S,
Loop *L, ScalarEvolution &SE);
463 void canonicalize(
const Loop &L);
467 bool hasZeroEnd()
const;
469 bool countsDownToZero()
const;
471 size_t getNumRegs()
const;
474 void deleteBaseReg(
const SCEV *&S);
476 bool referencesReg(
const SCEV *S)
const;
477 bool hasRegsUsedByUsesOtherThan(
size_t LUIdx,
478 const RegUseTracker &RegUses)
const;
480 void print(raw_ostream &OS)
const;
498 for (
const SCEV *S :
Add->operands())
504 const SCEV *Start, *Step;
519 if (
Mul->getOperand(0)->isAllOnesValue()) {
528 for (
const SCEV *S : MyGood)
530 for (
const SCEV *S : MyBad)
542void Formula::initialMatch(
const SCEV *S,
Loop *L, ScalarEvolution &SE) {
549 BaseRegs.push_back(Sum);
555 BaseRegs.push_back(Sum);
570bool Formula::isCanonical(
const Loop &L)
const {
571 assert((Scale == 0 || ScaledReg) &&
572 "ScaledReg must be non-null if Scale is non-zero");
575 return BaseRegs.size() <= 1;
580 if (Scale == 1 && BaseRegs.empty())
589 return none_of(BaseRegs, [&L](
const SCEV *S) {
600void Formula::canonicalize(
const Loop &L) {
604 if (BaseRegs.empty()) {
606 assert(ScaledReg &&
"Expected 1*reg => reg");
607 assert(Scale == 1 &&
"Expected 1*reg => reg");
608 BaseRegs.push_back(ScaledReg);
616 ScaledReg = BaseRegs.pop_back_val();
624 auto I =
find_if(BaseRegs, [&L](
const SCEV *S) {
627 if (
I != BaseRegs.end())
637bool Formula::unscale() {
641 BaseRegs.push_back(ScaledReg);
646bool Formula::hasZeroEnd()
const {
647 if (UnfoldedOffset || BaseOffset)
649 if (BaseRegs.size() != 1 || ScaledReg)
654bool Formula::countsDownToZero()
const {
657 assert(BaseRegs.size() == 1 &&
"hasZeroEnd should mean one BaseReg");
658 const APInt *StepInt;
666size_t Formula::getNumRegs()
const {
667 return !!ScaledReg + BaseRegs.size();
672Type *Formula::getType()
const {
673 return !BaseRegs.empty() ? BaseRegs.front()->getType() :
674 ScaledReg ? ScaledReg->
getType() :
680void Formula::deleteBaseReg(
const SCEV *&S) {
681 if (&S != &BaseRegs.back())
687bool Formula::referencesReg(
const SCEV *S)
const {
693bool Formula::hasRegsUsedByUsesOtherThan(
size_t LUIdx,
694 const RegUseTracker &RegUses)
const {
696 if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
698 for (
const SCEV *BaseReg : BaseRegs)
699 if (RegUses.isRegUsedByUsesOtherThan(BaseReg, LUIdx))
704#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
705void Formula::print(raw_ostream &OS)
const {
706 ListSeparator
Plus(
" + ");
711 if (BaseOffset.isNonZero())
712 OS <<
Plus << BaseOffset;
714 for (
const SCEV *BaseReg : BaseRegs)
717 if (HasBaseReg && BaseRegs.empty())
718 OS <<
Plus <<
"**error: HasBaseReg**";
719 else if (!HasBaseReg && !BaseRegs.empty())
720 OS <<
Plus <<
"**error: !HasBaseReg**";
723 OS <<
Plus << Scale <<
"*reg(";
730 if (UnfoldedOffset.isNonZero())
731 OS <<
Plus <<
"imm(" << UnfoldedOffset <<
')';
771 bool IgnoreSignificantBits =
false) {
786 if (
RA.isAllOnes()) {
787 if (
LHS->getType()->isPointerTy())
800 const APInt &LA =
C->getAPInt();
809 if ((IgnoreSignificantBits ||
isAddRecSExtable(AR, SE)) && AR->isAffine()) {
811 IgnoreSignificantBits);
812 if (!Step)
return nullptr;
814 IgnoreSignificantBits);
815 if (!Start)
return nullptr;
828 for (
const SCEV *S :
Add->operands()) {
830 if (!
Op)
return nullptr;
858 for (
const SCEV *S :
Mul->operands()) {
861 IgnoreSignificantBits)) {
882 bool PreferScalable) {
885 Immediate Result = Immediate::getZero();
894 C->getSignificantBits() <= 64) {
896 Result = Immediate::getFixed(
C->getSExtValue());
901 if (Opts.lsr_enable_vscale_immediates &&
902 (Result.isZero() || PreferScalable)) {
909 Result = Immediate::getScalable(
C->getSExtValue());
915 if (Result.isNonZero()) {
927 bool PreferScalable =
false) {
934 if (Result.isNonZero())
941 if (Result.isNonZero())
982 if (
SI->getPointerOperand() == OperandVal)
987 switch (
II->getIntrinsicID()) {
988 case Intrinsic::memset:
989 case Intrinsic::prefetch:
990 case Intrinsic::masked_load:
991 if (
II->getArgOperand(0) == OperandVal)
994 case Intrinsic::masked_store:
995 if (
II->getArgOperand(1) == OperandVal)
998 case Intrinsic::memmove:
999 case Intrinsic::memcpy:
1000 if (
II->getArgOperand(0) == OperandVal ||
1001 II->getArgOperand(1) == OperandVal)
1006 if (
TTI.getTgtMemIntrinsic(
II, IntrInfo)) {
1007 if (IntrInfo.
PtrVal == OperandVal)
1013 if (RMW->getPointerOperand() == OperandVal)
1016 if (CmpX->getPointerOperand() == OperandVal)
1025 MemAccessTy AccessTy = MemAccessTy::getUnknown(Inst->
getContext());
1029 AccessTy.MemTy = Ty;
1033 AccessTy.AddrSpace =
SI->getPointerAddressSpace();
1035 AccessTy.AddrSpace = LI->getPointerAddressSpace();
1037 AccessTy.AddrSpace = RMW->getPointerAddressSpace();
1039 AccessTy.AddrSpace = CmpX->getPointerAddressSpace();
1041 switch (
II->getIntrinsicID()) {
1042 case Intrinsic::prefetch:
1043 case Intrinsic::memset:
1044 AccessTy.AddrSpace =
II->getArgOperand(0)->getType()->getPointerAddressSpace();
1045 AccessTy.MemTy = OperandVal->
getType();
1047 case Intrinsic::memmove:
1048 case Intrinsic::memcpy:
1050 AccessTy.MemTy = OperandVal->
getType();
1052 case Intrinsic::masked_load:
1053 AccessTy.AddrSpace =
1054 II->getArgOperand(0)->getType()->getPointerAddressSpace();
1056 case Intrinsic::masked_store:
1057 AccessTy.AddrSpace =
1058 II->getArgOperand(1)->getType()->getPointerAddressSpace();
1062 if (
TTI.getTgtMemIntrinsic(
II, IntrInfo) && IntrInfo.
PtrVal) {
1118 if (!Processed.
insert(S).second)
1122 for (
const SCEV *S :
Add->operands()) {
1129 const SCEV *Op0, *Op1;
1138 Value *UVal = U->getValue();
1142 if (UI && UI->
getOpcode() == Instruction::Mul &&
1175 const LSRUse &LU,
const Formula &
F);
1179 const LSRUse &LU,
const Formula &
F,
1186 const ScalarOptions *Opts =
nullptr;
1187 const Loop *
L =
nullptr;
1188 ScalarEvolution *SE =
nullptr;
1189 const TargetTransformInfo *
TTI =
nullptr;
1190 TargetTransformInfo::LSRCost
C;
1195 Cost(
const ScalarOptions &Opts,
const Loop *L, ScalarEvolution &SE,
1197 : Opts(&Opts),
L(
L), SE(&SE),
TTI(&
TTI), AMK(AMK) {
1206 C.NumPrePostIndex = 0;
1216 return ((
C.Insns |
C.NumRegs |
C.AddRecCost |
C.NumIVMuls |
C.NumBaseAdds |
1217 C.ImmCost |
C.SetupCost |
C.ScaleCost |
C.NumPrePostIndex) !=
1219 ((
C.Insns &
C.NumRegs &
C.AddRecCost &
C.NumIVMuls &
C.NumBaseAdds &
1220 C.ImmCost &
C.SetupCost &
C.ScaleCost &
C.NumPrePostIndex) == ~0
u);
1226 return C.NumRegs == ~0
u;
1229 void RateFormula(
const Formula &
F, SmallPtrSetImpl<const SCEV *> &Regs,
1230 const DenseSet<const SCEV *> &VisitedRegs,
const LSRUse &LU,
1231 bool HardwareLoopProfitable,
1232 SmallPtrSetImpl<const SCEV *> *LoserRegs =
nullptr);
1234 void print(raw_ostream &OS)
const;
1238 void RateRegister(
const Formula &
F,
const SCEV *
Reg,
1239 SmallPtrSetImpl<const SCEV *> &Regs,
const LSRUse &LU,
1240 bool HardwareLoopProfitable);
1241 void RatePrimaryRegister(
const Formula &
F,
const SCEV *
Reg,
1242 SmallPtrSetImpl<const SCEV *> &Regs,
1243 const LSRUse &LU,
bool HardwareLoopProfitable,
1244 SmallPtrSetImpl<const SCEV *> *LoserRegs);
1255 Value *OperandValToReplace =
nullptr;
1265 Immediate
Offset = Immediate::getZero();
1267 LSRFixup() =
default;
1269 bool isUseFullyOutsideLoop(
const Loop *L)
const;
1271 void print(raw_ostream &OS)
const;
1281 DenseSet<SmallVector<const SCEV *, 4>> Uniquifier;
1294 using SCEVUseKindPair = PointerIntPair<const SCEV *, 2, KindType>;
1297 MemAccessTy AccessTy;
1303 Immediate MinOffset = Immediate::getFixedMax();
1304 Immediate MaxOffset = Immediate::getFixedMin();
1308 bool AllFixupsOutsideLoop =
true;
1313 bool AllFixupsUnconditional =
true;
1320 bool RigidFormula =
false;
1328 SmallPtrSet<const SCEV *, 4> Regs;
1330 LSRUse(KindType K, MemAccessTy AT) :
Kind(
K), AccessTy(AT) {}
1332 LSRFixup &getNewFixup() {
1333 Fixups.push_back(LSRFixup());
1337 void pushFixup(LSRFixup &f) {
1339 if (Immediate::isKnownGT(
f.Offset, MaxOffset))
1340 MaxOffset =
f.Offset;
1341 if (Immediate::isKnownLT(
f.Offset, MinOffset))
1342 MinOffset =
f.Offset;
1345 bool HasFormulaWithSameRegs(
const Formula &
F)
const;
1346 float getNotSelectedProbability(
const SCEV *
Reg)
const;
1347 bool InsertFormula(
const Formula &
F,
const Loop &L);
1348 void DeleteFormula(Formula &
F);
1349 void RecomputeRegs(
size_t LUIdx, RegUseTracker &Reguses);
1351 void print(raw_ostream &OS)
const;
1358 LSRUse::KindType Kind, MemAccessTy AccessTy,
1359 GlobalValue *BaseGV, Immediate BaseOffset,
1360 bool HasBaseReg, int64_t Scale,
1361 Instruction *Fixup =
nullptr);
1368 if (
TTI.getIntImmCost(
C->getAPInt(),
C->getType(),
1382 [&](
unsigned i,
const SCEV *
Reg) {
1383 return i + getSetupCost(Reg, Depth - 1, TTI);
1392void Cost::RateRegister(
const Formula &
F,
const SCEV *
Reg,
1393 SmallPtrSetImpl<const SCEV *> &Regs,
const LSRUse &LU,
1394 bool HardwareLoopProfitable) {
1399 if (AR->getLoop() != L) {
1406 if (!AR->getLoop()->contains(L)) {
1416 unsigned LoopCost = 1;
1417 if (LU.Kind == LSRUse::Address &&
1426 F.BaseOffset.isFixed() &&
1427 *Step ==
F.BaseOffset.getFixedValue();
1432 if ((CanPreIndex || CanPostIndex) && LU.AllFixupsUnconditional) {
1433 ++
C.NumPrePostIndex;
1441 if (LU.Kind == LSRUse::ICmpZero &&
F.countsDownToZero() &&
1442 HardwareLoopProfitable)
1444 C.AddRecCost += LoopCost;
1448 const SCEV *StepReg = AR->getOperand(1);
1453 auto IsVScaleStep = [](
const SCEV *
Reg,
const TargetTransformInfo *
TTI) {
1459 if (!Regs.
count(StepReg) && !IsVScaleStep(StepReg,
TTI)) {
1460 RateRegister(
F, StepReg, Regs, LU, HardwareLoopProfitable);
1472 C.SetupCost = std::min<unsigned>(
C.SetupCost, 1 << 16);
1481void Cost::RatePrimaryRegister(
const Formula &
F,
const SCEV *
Reg,
1482 SmallPtrSetImpl<const SCEV *> &Regs,
1483 const LSRUse &LU,
bool HardwareLoopProfitable,
1484 SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1485 if (LoserRegs && LoserRegs->
count(
Reg)) {
1490 RateRegister(
F,
Reg, Regs, LU, HardwareLoopProfitable);
1491 if (LoserRegs && isLoser())
1496void Cost::RateFormula(
const Formula &
F, SmallPtrSetImpl<const SCEV *> &Regs,
1497 const DenseSet<const SCEV *> &VisitedRegs,
1498 const LSRUse &LU,
bool HardwareLoopProfitable,
1499 SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1502 assert(
F.isCanonical(*L) &&
"Cost is accurate only for canonical formula");
1504 unsigned PrevAddRecCost =
C.AddRecCost;
1505 unsigned PrevNumRegs =
C.NumRegs;
1506 unsigned PrevNumBaseAdds =
C.NumBaseAdds;
1507 if (
const SCEV *ScaledReg =
F.ScaledReg) {
1508 if (VisitedRegs.
count(ScaledReg)) {
1512 RatePrimaryRegister(
F, ScaledReg, Regs, LU, HardwareLoopProfitable,
1517 for (
const SCEV *BaseReg :
F.BaseRegs) {
1518 if (VisitedRegs.
count(BaseReg)) {
1522 RatePrimaryRegister(
F, BaseReg, Regs, LU, HardwareLoopProfitable,
1529 size_t NumBaseParts =
F.getNumRegs();
1530 if (NumBaseParts > 1)
1535 C.NumBaseAdds += (
F.UnfoldedOffset.isNonZero());
1541 for (
const LSRFixup &Fixup : LU.Fixups) {
1542 if (
Fixup.Offset.isCompatibleImmediate(
F.BaseOffset)) {
1543 Immediate
Offset =
Fixup.Offset.addUnsigned(
F.BaseOffset);
1547 else if (
Offset.isNonZero())
1549 APInt(64,
Offset.getKnownMinValue(),
true).getSignificantBits();
1553 if (LU.Kind == LSRUse::Address &&
Offset.isNonZero() &&
1564 if (!
valueOr(Opts->lsr_insns_cost,
true)) {
1574 if (
C.NumRegs > TTIRegNum) {
1577 if (PrevNumRegs > TTIRegNum)
1578 C.Insns += (
C.NumRegs - PrevNumRegs);
1580 C.Insns += (
C.NumRegs - TTIRegNum);
1593 if (LU.Kind == LSRUse::ICmpZero && !
F.hasZeroEnd() &&
1597 C.Insns += (
C.AddRecCost - PrevAddRecCost);
1600 if (LU.Kind != LSRUse::ICmpZero)
1601 C.Insns +=
C.NumBaseAdds - PrevNumBaseAdds;
1607 C.Insns = std::numeric_limits<unsigned>::max();
1608 C.NumRegs = std::numeric_limits<unsigned>::max();
1609 C.AddRecCost = std::numeric_limits<unsigned>::max();
1610 C.NumIVMuls = std::numeric_limits<unsigned>::max();
1611 C.NumBaseAdds = std::numeric_limits<unsigned>::max();
1612 C.ImmCost = std::numeric_limits<unsigned>::max();
1613 C.SetupCost = std::numeric_limits<unsigned>::max();
1614 C.ScaleCost = std::numeric_limits<unsigned>::max();
1615 C.NumPrePostIndex = std::numeric_limits<unsigned>::max();
1619bool Cost::isLess(
const Cost &
Other)
const {
1620 if (Opts->lsr_insns_cost == BoolOrDefault::True &&
C.Insns !=
Other.C.Insns)
1621 return C.Insns <
Other.C.Insns;
1625#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1627 if (
valueOr(Opts->lsr_insns_cost,
true))
1628 OS <<
C.Insns <<
" instruction" << (
C.Insns == 1 ?
" " :
"s ");
1629 OS <<
C.NumRegs <<
" reg" << (
C.NumRegs == 1 ?
"" :
"s");
1630 if (
C.AddRecCost != 0)
1631 OS <<
", with addrec cost " <<
C.AddRecCost;
1632 if (
C.NumIVMuls != 0)
1633 OS <<
", plus " <<
C.NumIVMuls <<
" IV mul"
1634 << (
C.NumIVMuls == 1 ?
"" :
"s");
1635 if (
C.NumBaseAdds != 0)
1636 OS <<
", plus " <<
C.NumBaseAdds <<
" base add"
1637 << (
C.NumBaseAdds == 1 ?
"" :
"s");
1638 if (
C.ScaleCost != 0)
1639 OS <<
", plus " <<
C.ScaleCost <<
" scale cost";
1641 OS <<
", plus " <<
C.ImmCost <<
" imm cost";
1642 if (
C.SetupCost != 0)
1643 OS <<
", plus " <<
C.SetupCost <<
" setup cost";
1644 if (
C.NumPrePostIndex != 0)
1645 OS <<
", plus " <<
C.NumPrePostIndex <<
" pre/post-index";
1654bool LSRFixup::isUseFullyOutsideLoop(
const Loop *L)
const {
1657 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1658 if (PN->getIncomingValue(i) == OperandValToReplace &&
1659 L->contains(PN->getIncomingBlock(i)))
1664 return !
L->contains(UserInst);
1667#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1668void LSRFixup::print(raw_ostream &OS)
const {
1673 Store->getOperand(0)->printAsOperand(OS,
false);
1679 OS <<
", OperandValToReplace=";
1682 for (
const Loop *PIL : PostIncLoops) {
1683 OS <<
", PostIncLoop=";
1684 PIL->getHeader()->printAsOperand(OS,
false);
1688 OS <<
", Offset=" <<
Offset;
1698bool LSRUse::HasFormulaWithSameRegs(
const Formula &
F)
const {
1700 if (
F.ScaledReg)
Key.push_back(
F.ScaledReg);
1707float LSRUse::getNotSelectedProbability(
const SCEV *
Reg)
const {
1709 for (
const Formula &
F : Formulae)
1710 if (
F.referencesReg(
Reg))
1712 return ((
float)(Formulae.size() - FNum)) / Formulae.size();
1717bool LSRUse::InsertFormula(
const Formula &
F,
const Loop &L) {
1718 assert(
F.isCanonical(L) &&
"Invalid canonical representation");
1720 if (!Formulae.empty() && RigidFormula)
1724 if (
F.ScaledReg)
Key.push_back(
F.ScaledReg);
1732 assert((!
F.ScaledReg || !
F.ScaledReg->isZero()) &&
1733 "Zero allocated in a scaled register!");
1735 for (
const SCEV *BaseReg :
F.BaseRegs)
1736 assert(!
BaseReg->isZero() &&
"Zero allocated in a base register!");
1740 Formulae.push_back(
F);
1751void LSRUse::DeleteFormula(Formula &
F) {
1752 if (&
F != &Formulae.back())
1754 Formulae.pop_back();
1758void LSRUse::RecomputeRegs(
size_t LUIdx, RegUseTracker &RegUses) {
1760 SmallPtrSet<const SCEV *, 4> OldRegs = std::move(Regs);
1762 for (
const Formula &
F : Formulae) {
1763 if (
F.ScaledReg) Regs.
insert(
F.ScaledReg);
1768 for (
const SCEV *S : OldRegs)
1770 RegUses.dropRegister(S, LUIdx);
1773#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1774void LSRUse::print(raw_ostream &OS)
const {
1775 OS <<
"LSR Use: Kind=";
1777 case Basic: OS <<
"Basic";
break;
1778 case Special: OS <<
"Special";
break;
1779 case ICmpZero: OS <<
"ICmpZero";
break;
1781 OS <<
"Address of ";
1785 OS << *AccessTy.MemTy;
1788 OS <<
" in addrspace(" << AccessTy.AddrSpace <<
')';
1791 OS <<
", Offsets={";
1792 bool NeedComma =
false;
1793 for (
const LSRFixup &Fixup : Fixups) {
1794 if (NeedComma) OS <<
',';
1800 if (AllFixupsOutsideLoop)
1801 OS <<
", all-fixups-outside-loop";
1803 if (AllFixupsUnconditional)
1804 OS <<
", all-fixups-unconditional";
1813 LSRUse::KindType Kind, MemAccessTy AccessTy,
1815 bool HasBaseReg, int64_t Scale,
1818 case LSRUse::Address: {
1819 int64_t FixedOffset =
1820 BaseOffset.isScalable() ? 0 : BaseOffset.getFixedValue();
1821 int64_t ScalableOffset =
1822 BaseOffset.isScalable() ? BaseOffset.getKnownMinValue() : 0;
1823 return TTI.isLegalAddressingMode(AccessTy.MemTy, BaseGV, FixedOffset,
1824 HasBaseReg, Scale, AccessTy.AddrSpace,
1825 Fixup, ScalableOffset);
1827 case LSRUse::ICmpZero:
1834 if (Scale != 0 && HasBaseReg && BaseOffset.isNonZero())
1839 if (Scale != 0 && Scale != -1)
1844 if (BaseOffset.isNonZero()) {
1847 if (BaseOffset.isScalable())
1857 BaseOffset = BaseOffset.getFixed(-(
uint64_t)BaseOffset.getFixedValue());
1858 return TTI.isLegalICmpImmediate(BaseOffset.getFixedValue());
1866 return !BaseGV && Scale == 0 && BaseOffset.isZero();
1868 case LSRUse::Special:
1870 return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset.isZero();
1877 Immediate MinOffset, Immediate MaxOffset,
1878 LSRUse::KindType Kind, MemAccessTy AccessTy,
1880 bool HasBaseReg, int64_t Scale) {
1881 if (BaseOffset.isNonZero() &&
1882 (BaseOffset.isScalable() != MinOffset.isScalable() ||
1883 BaseOffset.isScalable() != MaxOffset.isScalable()))
1886 int64_t
Base = BaseOffset.getKnownMinValue();
1887 int64_t Min = MinOffset.getKnownMinValue();
1888 int64_t Max = MaxOffset.getKnownMinValue();
1891 MinOffset = Immediate::get((
uint64_t)
Base + Min, MinOffset.isScalable());
1894 MaxOffset = Immediate::get((
uint64_t)
Base + Max, MaxOffset.isScalable());
1897 HasBaseReg, Scale) &&
1903 Immediate MinOffset, Immediate MaxOffset,
1904 LSRUse::KindType Kind, MemAccessTy AccessTy,
1905 const Formula &
F,
const Loop &L) {
1913 assert((
F.isCanonical(L) ||
F.Scale != 0));
1915 F.BaseGV,
F.BaseOffset,
F.HasBaseReg,
F.Scale);
1920 Immediate MaxOffset, LSRUse::KindType Kind,
1922 Immediate BaseOffset,
bool HasBaseReg, int64_t Scale) {
1925 BaseOffset, HasBaseReg, Scale) ||
1930 BaseGV, BaseOffset,
true, 0));
1934 Immediate MaxOffset, LSRUse::KindType Kind,
1935 MemAccessTy AccessTy,
const Formula &
F) {
1936 return isLegalUse(
TTI, MinOffset, MaxOffset, Kind, AccessTy,
F.BaseGV,
1937 F.BaseOffset,
F.HasBaseReg,
F.Scale);
1943 return TTI.isLegalAddScalableImmediate(
Offset.getKnownMinValue());
1945 return TTI.isLegalAddImmediate(
Offset.getFixedValue());
1949 const LSRUse &LU,
const Formula &
F) {
1951 if (LU.Kind == LSRUse::Address &&
TTI.LSRWithInstrQueries()) {
1952 for (
const LSRFixup &Fixup : LU.Fixups)
1954 (
F.BaseOffset + Fixup.Offset),
F.HasBaseReg,
1955 F.Scale, Fixup.UserInst))
1961 LU.AccessTy,
F.BaseGV,
F.BaseOffset,
F.HasBaseReg,
1966 const LSRUse &LU,
const Formula &
F,
1975 return F.Scale != 1;
1978 case LSRUse::Address: {
1980 int64_t ScalableMin = 0, ScalableMax = 0, FixedMin = 0, FixedMax = 0;
1981 if (
F.BaseOffset.isScalable()) {
1982 ScalableMin = (
F.BaseOffset + LU.MinOffset).getKnownMinValue();
1983 ScalableMax = (
F.BaseOffset + LU.MaxOffset).getKnownMinValue();
1985 FixedMin = (
F.BaseOffset + LU.MinOffset).getFixedValue();
1986 FixedMax = (
F.BaseOffset + LU.MaxOffset).getFixedValue();
1990 F.HasBaseReg,
F.Scale, LU.AccessTy.AddrSpace);
1993 F.HasBaseReg,
F.Scale, LU.AccessTy.AddrSpace);
1996 "Legal addressing mode has an illegal cost!");
1997 return std::max(ScaleCostMinOffset, ScaleCostMaxOffset);
1999 case LSRUse::ICmpZero:
2001 case LSRUse::Special:
2012 LSRUse::KindType Kind, MemAccessTy AccessTy,
2016 if (BaseOffset.isZero() && !BaseGV)
2021 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
2025 if (!HasBaseReg && Scale == 1) {
2035 if (HasBaseReg && BaseOffset.isNonZero() && Kind != LSRUse::ICmpZero &&
2037 Opts.lsr_drop_scaled_reg_for_vscale)
2047 Immediate MaxOffset, LSRUse::KindType Kind,
2048 MemAccessTy AccessTy,
const SCEV *S,
2051 if (S->
isZero())
return true;
2064 if (BaseOffset.isZero() && !BaseGV)
2067 if (BaseOffset.isScalable())
2072 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
2075 BaseOffset, HasBaseReg, Scale);
2092 const SCEV *IncExpr;
2094 IVInc(Instruction *U,
Value *O,
const SCEV *
E)
2095 : UserInst(
U), IVOperand(
O), IncExpr(
E) {}
2102 const SCEV *ExprBase =
nullptr;
2104 IVChain() =
default;
2105 IVChain(
const IVInc &Head,
const SCEV *
Base)
2106 : Incs(1, Head), ExprBase(
Base) {}
2111 const_iterator
begin()
const {
2113 return std::next(Incs.
begin());
2115 const_iterator
end()
const {
2120 bool hasIncs()
const {
return Incs.
size() >= 2; }
2129 bool isProfitableIncrement(
const SCEV *OperExpr,
2130 const SCEV *IncExpr,
2138 SmallPtrSet<Instruction*, 4> FarUsers;
2139 SmallPtrSet<Instruction*, 4> NearUsers;
2144 const ScalarOptions &Opts;
2146 ScalarEvolution &SE;
2149 AssumptionCache &AC;
2150 TargetLibraryInfo &
TLI;
2151 const TargetTransformInfo &
TTI;
2153 MemorySSAUpdater *MSSAU;
2157 bool HardwareLoopProfitable =
false;
2158 bool ShouldPreserveLCSSA =
false;
2172 SetVector<int64_t, SmallVector<int64_t, 8>, SmallSet<int64_t, 8>> Factors;
2179 SmallSetVector<Type *, 4> Types;
2185 RegUseTracker RegUses;
2190 static const unsigned MaxChains = 8;
2196 SmallPtrSet<Use*, MaxChains> IVIncSet;
2199 SmallVector<llvm::WeakVH, 2> ScalarEvolutionIVs;
2205 SmallSetVector<Instruction *, 4> InsertedNonLCSSAInsts;
2207 void OptimizeShadowIV();
2208 bool FindIVUserForCond(Instruction *
Cond, IVStrideUse *&CondUse);
2210 ICmpInst *OptimizeLoopTermCondToEquality(ICmpInst *
Cond,
2211 IVStrideUse *&CondUse,
2212 BasicBlock *ExitingBlock);
2213 void OptimizeLoopTermCond();
2215 void ChainInstruction(Instruction *UserInst, Instruction *IVOper,
2216 SmallVectorImpl<ChainUsers> &ChainUsersVec);
2217 void FinalizeChain(IVChain &Chain);
2218 void CollectChains();
2219 void GenerateIVChain(
const IVChain &Chain,
2220 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2222 void CollectInterestingTypesAndFactors();
2223 void CollectFixupsAndInitialFormulae();
2226 using UseMapTy = DenseMap<LSRUse::SCEVUseKindPair, size_t>;
2229 bool reconcileNewOffset(LSRUse &LU, Immediate NewOffset,
bool HasBaseReg,
2230 LSRUse::KindType Kind, MemAccessTy AccessTy);
2232 std::pair<size_t, Immediate> getUse(
const SCEV *&Expr, LSRUse::KindType Kind,
2233 MemAccessTy AccessTy);
2235 void DeleteUse(LSRUse &LU,
size_t LUIdx);
2237 LSRUse *FindUseWithSimilarFormula(
const Formula &
F,
const LSRUse &OrigLU);
2239 void InsertInitialFormula(
const SCEV *S, LSRUse &LU,
size_t LUIdx);
2240 void InsertSupplementalFormula(
const SCEV *S, LSRUse &LU,
size_t LUIdx);
2241 void CountRegisters(
const Formula &
F,
size_t LUIdx);
2242 bool InsertFormula(LSRUse &LU,
unsigned LUIdx,
const Formula &
F);
2243 bool IsFixupExecutedEachIncrement(
const LSRFixup &LF)
const;
2245 void CollectLoopInvariantFixupsAndFormulae();
2247 void GenerateReassociations(LSRUse &LU,
unsigned LUIdx, Formula
Base,
2248 unsigned Depth = 0);
2250 void GenerateReassociationsImpl(LSRUse &LU,
unsigned LUIdx,
2252 size_t Idx,
bool IsScaledReg =
false);
2253 void GenerateCombinations(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2254 void GenerateSymbolicOffsetsImpl(LSRUse &LU,
unsigned LUIdx,
2255 const Formula &
Base,
size_t Idx,
2256 bool IsScaledReg =
false);
2257 void GenerateSymbolicOffsets(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2258 void GenerateConstantOffsetsImpl(LSRUse &LU,
unsigned LUIdx,
2259 const Formula &
Base,
2260 const SmallVectorImpl<Immediate> &Worklist,
2261 size_t Idx,
bool IsScaledReg =
false);
2262 void GenerateConstantOffsets(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2263 void GenerateICmpZeroScales(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2264 void GenerateScales(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2265 void GenerateTruncates(LSRUse &LU,
unsigned LUIdx, Formula
Base);
2266 void GenerateCrossUseConstantOffsets();
2267 void GenerateAllReuseFormulae();
2269 void FilterOutUndesirableDedicatedRegisters();
2271 size_t EstimateSearchSpaceComplexity()
const;
2272 void NarrowSearchSpaceByDetectingSupersets();
2273 void NarrowSearchSpaceByCollapsingUnrolledCode();
2274 void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
2275 void NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
2276 void NarrowSearchSpaceByFilterPostInc();
2277 void NarrowSearchSpaceByMergingUsesOutsideLoop();
2278 void NarrowSearchSpaceByDeletingCostlyFormulas();
2279 void NarrowSearchSpaceByPickingWinnerRegs();
2280 void NarrowSearchSpaceUsingHeuristics();
2282 void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
2284 SmallVectorImpl<const Formula *> &Workspace,
2285 const Cost &CurCost,
2286 const SmallPtrSet<const SCEV *, 16> &CurRegs,
2287 DenseSet<const SCEV *> &VisitedRegs)
const;
2288 void Solve(SmallVectorImpl<const Formula *> &Solution)
const;
2292 const SmallVectorImpl<Instruction *> &Inputs)
const;
2295 const LSRUse &LU)
const;
2297 Value *Expand(
const LSRUse &LU,
const LSRFixup &LF,
const Formula &
F,
2299 SmallVectorImpl<WeakTrackingVH> &DeadInsts)
const;
2300 void RewriteForPHI(PHINode *PN,
const LSRUse &LU,
const LSRFixup &LF,
2302 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2303 void Rewrite(
const LSRUse &LU,
const LSRFixup &LF,
const Formula &
F,
2304 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2305 void ImplementSolution(
const SmallVectorImpl<const Formula *> &Solution);
2312 LSRInstance(
const ScalarOptions &Opts,
Loop *L, IVUsers &IU,
2313 ScalarEvolution &SE, DominatorTree &DT, LoopInfo &LI,
2314 const TargetTransformInfo &
TTI, AssumptionCache &AC,
2315 TargetLibraryInfo &
TLI, MemorySSAUpdater *MSSAU,
2316 bool PreserveLCSSA);
2318 bool getChanged()
const {
return Changed; }
2319 const SmallVectorImpl<WeakVH> &getScalarEvolutionIVs()
const {
2320 return ScalarEvolutionIVs;
2323 void print_factors_and_types(raw_ostream &OS)
const;
2324 void print_fixups(raw_ostream &OS)
const;
2325 void print_uses(raw_ostream &OS)
const;
2326 void print(raw_ostream &OS)
const;
2334void LSRInstance::OptimizeShadowIV() {
2344 Type *DestTy =
nullptr;
2345 bool IsSigned =
false;
2361 DestTy = UCast->getDestTy();
2365 DestTy = SCast->getDestTy();
2367 if (!DestTy)
continue;
2387 if (Mantissa == -1)
continue;
2391 unsigned Entry, Latch;
2401 if (!Init)
continue;
2402 Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
2403 (
double)
Init->getSExtValue() :
2404 (
double)
Init->getZExtValue());
2406 BinaryOperator *Incr =
2408 if (!Incr)
continue;
2409 if (Incr->
getOpcode() != Instruction::Add
2410 && Incr->
getOpcode() != Instruction::Sub)
2414 ConstantInt *
C =
nullptr;
2426 if (!
C->getValue().isStrictlyPositive())
2434 Constant *CFP = ConstantFP::get(DestTy,
C->getZExtValue());
2436 Incr->
getOpcode() == Instruction::Add ? Instruction::FAdd
2437 : Instruction::FSub,
2454bool LSRInstance::FindIVUserForCond(Instruction *
Cond, IVStrideUse *&CondUse) {
2455 for (IVStrideUse &U : IU)
2456 if (
U.getUser() ==
Cond) {
2468ICmpInst *LSRInstance::OptimizeLoopTermCondToEquality(
2469 ICmpInst *
Cond, IVStrideUse *&CondUse, BasicBlock *ExitingBlock) {
2470 if (
Cond->isEquality() || ExitingBlock !=
L->getLoopLatch())
2474 if (!
IV || !
IV->getType()->isIntegerTy() ||
IV->getParent() !=
L->getHeader())
2482 if (!Step || Step->
isZero())
2485 const SCEV *ExitCount = SE.
getExitCount(L, ExitingBlock);
2493 const APInt &StepValue = Step->getAPInt();
2494 unsigned RecurrencePeriodLog2 =
2499 const auto *ExitValue =
2502 *
Cond, ExitValue->getAPInt() + StepValue))
2505 LLVM_DEBUG(
dbgs() <<
" Rewriting loop exit to compare " << *
IV <<
" against "
2506 << *ExitValue <<
'\n');
2509 bool TrueContinues =
L->contains(TermBr->getSuccessor(0));
2510 assert(TrueContinues !=
L->contains(TermBr->getSuccessor(1)) &&
2511 "Expected exactly one in-loop successor");
2513 auto Pred = TrueContinues ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
2514 auto *NewCond =
new ICmpInst(TermBr->getIterator(),
Pred,
IV,
2515 ExitValue->getValue(),
"lsr.exitcond");
2516 NewCond->setDebugLoc(
Cond->getDebugLoc());
2517 TermBr->setCondition(NewCond);
2519 if (
Cond->use_empty()) {
2521 Cond->eraseFromParent();
2523 CondUse = &IU.AddUser(NewCond,
IV);
2578Instruction *LSRInstance::OptimizeMax(ICmpInst *
Cond, IVStrideUse *&CondUse) {
2593 const SCEV *IterationCount = SE.
getAddExpr(One, BackedgeTakenCount);
2594 if (IterationCount != SE.
getSCEV(Sel))
return Cond;
2600 const SCEVNAryExpr *
Max =
nullptr;
2602 Pred = ICmpInst::ICMP_SLE;
2605 Pred = ICmpInst::ICMP_SLT;
2608 Pred = ICmpInst::ICMP_ULT;
2617 if (
Max->getNumOperands() != 2)
2620 const SCEV *MaxLHS =
Max->getOperand(0);
2621 const SCEV *MaxRHS =
Max->getOperand(1);
2626 (ICmpInst::isTrueWhenEqual(
Pred) ? !MaxLHS->
isZero() : (MaxLHS != One)))
2637 "Loop condition operand is an addrec in a different loop!");
2641 Value *NewRHS =
nullptr;
2642 if (ICmpInst::isTrueWhenEqual(
Pred)) {
2646 if (BO1->isOne() && SE.
getSCEV(BO->getOperand(0)) == MaxRHS)
2647 NewRHS = BO->getOperand(0);
2650 if (BO1->isOne() && SE.
getSCEV(BO->getOperand(0)) == MaxRHS)
2651 NewRHS = BO->getOperand(0);
2659 NewRHS = SU->getValue();
2671 ICmpInst *NewCond =
new ICmpInst(
Cond->getIterator(),
Pred,
2672 Cond->getOperand(0), NewRHS,
"scmp");
2676 Cond->replaceAllUsesWith(NewCond);
2679 Cond->eraseFromParent();
2681 if (
Cmp->use_empty()) {
2683 Cmp->eraseFromParent();
2690LSRInstance::OptimizeLoopTermCond() {
2691 SmallPtrSet<Instruction *, 4> PostIncs;
2706 SmallVector<BasicBlock*, 8> ExitingBlocks;
2707 L->getExitingBlocks(ExitingBlocks);
2715 for (BasicBlock *ExitingBlock : ExitingBlocks) {
2737 IVStrideUse *CondUse =
nullptr;
2738 if (!FindIVUserForCond(
Cond, CondUse))
2748 Cond = OptimizeMax(Cmp, CondUse);
2753 if (!DT.
dominates(ExitingBlock, LatchBlock))
2757 Cond = OptimizeLoopTermCondToEquality(Cmp, CondUse, ExitingBlock);
2761 if (LatchBlock != ExitingBlock)
2762 for (
const IVStrideUse &UI : IU)
2765 if (&UI != CondUse &&
2769 const SCEV *
A = IU.getStride(*CondUse, L);
2770 const SCEV *
B = IU.getStride(UI, L);
2771 if (!
A || !
B)
continue;
2780 if (
const SCEVConstant *
D =
2782 const ConstantInt *
C =
D->getValue();
2784 if (
C->isOne() ||
C->isMinusOne())
2785 goto decline_post_inc;
2787 if (
C->getValue().getSignificantBits() >= 64 ||
2788 C->getValue().isMinSignedValue())
2789 goto decline_post_inc;
2792 MemAccessTy AccessTy =
2794 int64_t Scale =
C->getSExtValue();
2798 AccessTy.AddrSpace))
2799 goto decline_post_inc;
2804 AccessTy.AddrSpace))
2805 goto decline_post_inc;
2810 LLVM_DEBUG(
dbgs() <<
" Change loop exiting icmp to use postinc iv: "
2818 if (
Cond->hasOneUse()) {
2824 Cond->setName(
L->getHeader()->getName() +
".termcond");
2846 IVIncInsertPos =
L->getLoopLatch()->getTerminator();
2847 for (Instruction *Inst : PostIncs)
2853bool LSRInstance::reconcileNewOffset(LSRUse &LU, Immediate NewOffset,
2854 bool HasBaseReg, LSRUse::KindType Kind,
2855 MemAccessTy AccessTy) {
2856 Immediate NewMinOffset = LU.MinOffset;
2857 Immediate NewMaxOffset = LU.MaxOffset;
2858 MemAccessTy NewAccessTy = AccessTy;
2863 if (LU.Kind != Kind)
2869 if (Kind == LSRUse::Address) {
2870 if (AccessTy.MemTy != LU.AccessTy.MemTy) {
2871 NewAccessTy = MemAccessTy::getUnknown(AccessTy.MemTy->
getContext(),
2872 AccessTy.AddrSpace);
2877 if (Immediate::isKnownLT(NewOffset, LU.MinOffset)) {
2879 LU.MaxOffset - NewOffset, HasBaseReg))
2881 NewMinOffset = NewOffset;
2882 }
else if (Immediate::isKnownGT(NewOffset, LU.MaxOffset)) {
2884 NewOffset - LU.MinOffset, HasBaseReg))
2886 NewMaxOffset = NewOffset;
2892 if (NewAccessTy.MemTy && NewAccessTy.MemTy->
isVoidTy() &&
2893 (NewMinOffset.isScalable() || NewMaxOffset.isScalable()))
2897 LU.MinOffset = NewMinOffset;
2898 LU.MaxOffset = NewMaxOffset;
2899 LU.AccessTy = NewAccessTy;
2906std::pair<size_t, Immediate> LSRInstance::getUse(
const SCEV *&Expr,
2907 LSRUse::KindType Kind,
2908 MemAccessTy AccessTy) {
2909 const SCEV *
Copy = Expr;
2912 Opts, ExprUse, SE, AccessTy.MemTy && AccessTy.MemTy->
isScalableTy());
2919 Offset = Immediate::getFixed(0);
2922 std::pair<UseMapTy::iterator, bool>
P =
2923 UseMap.
try_emplace(LSRUse::SCEVUseKindPair(Expr, Kind));
2926 size_t LUIdx =
P.first->second;
2927 LSRUse &LU =
Uses[LUIdx];
2928 if (reconcileNewOffset(LU,
Offset,
true, Kind, AccessTy))
2930 return std::make_pair(LUIdx,
Offset);
2934 size_t LUIdx =
Uses.size();
2935 P.first->second = LUIdx;
2936 Uses.push_back(LSRUse(Kind, AccessTy));
2937 LSRUse &LU =
Uses[LUIdx];
2941 return std::make_pair(LUIdx,
Offset);
2945void LSRInstance::DeleteUse(LSRUse &LU,
size_t LUIdx) {
2946 if (&LU != &
Uses.back())
2951 RegUses.swapAndDropUse(LUIdx,
Uses.size());
2957LSRInstance::FindUseWithSimilarFormula(
const Formula &OrigF,
2958 const LSRUse &OrigLU) {
2960 for (LSRUse &LU :
Uses) {
2966 if (&LU != &OrigLU && LU.Kind != LSRUse::ICmpZero &&
2967 LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
2968 LU.HasFormulaWithSameRegs(OrigF)) {
2970 for (
const Formula &
F : LU.Formulae) {
2973 if (
F.BaseRegs == OrigF.BaseRegs &&
2974 F.ScaledReg == OrigF.ScaledReg &&
2975 F.BaseGV == OrigF.BaseGV &&
2976 F.Scale == OrigF.Scale &&
2977 F.UnfoldedOffset == OrigF.UnfoldedOffset) {
2978 if (
F.BaseOffset.isZero())
2993void LSRInstance::CollectInterestingTypesAndFactors() {
2994 SmallSetVector<const SCEV *, 4> Strides;
2998 for (
const IVStrideUse &U : IU) {
2999 const SCEV *Expr = IU.getExpr(U);
3017 }
while (!Worklist.
empty());
3021 for (SmallSetVector<const SCEV *, 4>::const_iterator
3023 for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter =
3024 std::next(
I); NewStrideIter !=
E; ++NewStrideIter) {
3025 const SCEV *OldStride = *
I;
3026 const SCEV *NewStride = *NewStrideIter;
3036 if (
const SCEVConstant *Factor =
3039 if (Factor->getAPInt().getSignificantBits() <= 64 && !Factor->isZero())
3040 Factors.insert(Factor->getAPInt().getSExtValue());
3041 }
else if (
const SCEVConstant *Factor =
3045 if (Factor->getAPInt().getSignificantBits() <= 64 && !Factor->isZero())
3046 Factors.insert(Factor->getAPInt().getSExtValue());
3052 if (Types.size() == 1)
3064 for(; OI != OE; ++OI) {
3083 return Trunc->getOperand(0);
3116 if (SubExpr->getSCEVType() ==
scAddExpr)
3119 if (SubExpr->getSCEVType() !=
scMulExpr)
3135bool IVChain::isProfitableIncrement(
const SCEV *OperExpr,
3136 const SCEV *IncExpr,
3137 ScalarEvolution &SE) {
3146 SmallPtrSet<const SCEV*, 8> Processed;
3164 if (!Chain.hasIncs())
3167 if (!
Users.empty()) {
3168 LLVM_DEBUG(
dbgs() <<
"Chain: " << *Chain.Incs[0].UserInst <<
" users:\n";
3170 :
Users) {
dbgs() <<
" " << *Inst <<
"\n"; });
3173 assert(!Chain.Incs.empty() &&
"empty IV chains are not allowed");
3182 && SE.
getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) {
3185 const SCEV *LastIncExpr =
nullptr;
3186 unsigned NumConstIncrements = 0;
3187 unsigned NumVarIncrements = 0;
3188 unsigned NumReusedIncrements = 0;
3190 if (
TTI.isProfitableLSRChainElement(Chain.Incs[0].UserInst))
3193 for (
const IVInc &Inc : Chain) {
3194 if (
TTI.isProfitableLSRChainElement(Inc.UserInst))
3196 if (Inc.IncExpr->isZero())
3202 ++NumConstIncrements;
3206 if (Inc.IncExpr == LastIncExpr)
3207 ++NumReusedIncrements;
3211 LastIncExpr = Inc.IncExpr;
3216 if (NumConstIncrements > 1)
3223 cost += NumVarIncrements;
3227 cost -= NumReusedIncrements;
3229 LLVM_DEBUG(
dbgs() <<
"Chain: " << *Chain.Incs[0].UserInst <<
" Cost: " << cost
3236void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper,
3237 SmallVectorImpl<ChainUsers> &ChainUsersVec) {
3241 const SCEV *
const OperExpr = SE.
getSCEV(NextIV);
3242 const SCEV *
const OperExprBase =
getExprBase(OperExpr);
3246 unsigned ChainIdx = 0, NChains = IVChainVec.size();
3247 const SCEV *LastIncExpr =
nullptr;
3248 for (; ChainIdx < NChains; ++ChainIdx) {
3249 IVChain &Chain = IVChainVec[ChainIdx];
3267 const SCEV *PrevExpr = SE.
getSCEV(PrevIV);
3268 const SCEV *IncExpr = SE.
getMinusSCEV(OperExpr, PrevExpr);
3274 Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) {
3275 LastIncExpr = IncExpr;
3281 if (ChainIdx == NChains) {
3288 LastIncExpr = OperExpr;
3295 IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr),
3297 ChainUsersVec.
resize(NChains);
3298 LLVM_DEBUG(
dbgs() <<
"IV Chain#" << ChainIdx <<
" Head: (" << *UserInst
3299 <<
") IV=" << *LastIncExpr <<
"\n");
3301 LLVM_DEBUG(
dbgs() <<
"IV Chain#" << ChainIdx <<
" Inc: (" << *UserInst
3302 <<
") IV+" << *LastIncExpr <<
"\n");
3304 IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr));
3306 IVChain &Chain = IVChainVec[ChainIdx];
3308 SmallPtrSet<Instruction*,4> &NearUsers = ChainUsersVec[ChainIdx].NearUsers;
3310 if (!LastIncExpr->
isZero()) {
3311 ChainUsersVec[ChainIdx].FarUsers.insert_range(NearUsers);
3320 for (Instruction *OtherUse : IVOper->
users()) {
3323 IVChain::const_iterator IncIter = Chain.Incs.begin();
3324 IVChain::const_iterator IncEnd = Chain.Incs.end();
3325 for( ; IncIter != IncEnd; ++IncIter) {
3326 if (IncIter->UserInst == OtherUse)
3329 if (IncIter != IncEnd)
3334 && IU.isIVUserOrOperand(OtherUse)) {
3337 NearUsers.
insert(OtherUse);
3342 ChainUsersVec[ChainIdx].FarUsers.
erase(UserInst);
3367void LSRInstance::CollectChains() {
3371 SmallVector<BasicBlock *,8> LatchPath;
3374 Rung->
getBlock() != LoopHeader; Rung = Rung->getIDom()) {
3380 for (BasicBlock *BB :
reverse(LatchPath)) {
3381 for (Instruction &
I : *BB) {
3387 if (IU.isEphemeral(&
I))
3397 for (
unsigned ChainIdx = 0, NChains = IVChainVec.size();
3398 ChainIdx < NChains; ++ChainIdx) {
3399 ChainUsersVec[ChainIdx].NearUsers.
erase(&
I);
3402 SmallPtrSet<Instruction*, 4> UniqueOperands;
3405 while (IVOpIter != IVOpEnd) {
3407 if (UniqueOperands.
insert(IVOpInst).second)
3408 ChainInstruction(&
I, IVOpInst, ChainUsersVec);
3409 IVOpIter =
findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3414 for (PHINode &PN :
L->getHeader()->phis()) {
3421 ChainInstruction(&PN, IncV, ChainUsersVec);
3424 unsigned ChainIdx = 0;
3425 for (
unsigned UsersIdx = 0, NChains = IVChainVec.size();
3426 UsersIdx < NChains; ++UsersIdx) {
3429 ChainUsersVec[UsersIdx].FarUsers, SE,
TTI))
3432 if (ChainIdx != UsersIdx)
3433 IVChainVec[ChainIdx] = IVChainVec[UsersIdx];
3434 FinalizeChain(IVChainVec[ChainIdx]);
3437 IVChainVec.resize(ChainIdx);
3440void LSRInstance::FinalizeChain(IVChain &Chain) {
3441 assert(!Chain.Incs.empty() &&
"empty IV chains are not allowed");
3442 LLVM_DEBUG(
dbgs() <<
"Final Chain: " << *Chain.Incs[0].UserInst <<
"\n");
3444 for (
const IVInc &Inc : Chain) {
3446 auto UseI =
find(Inc.UserInst->operands(), Inc.IVOperand);
3447 assert(UseI != Inc.UserInst->op_end() &&
"cannot find IV operand");
3448 IVIncSet.insert(UseI);
3457 Immediate IncOffset = Immediate::getZero();
3466 C->getSignificantBits() > 64)
3468 IncOffset = Immediate::getScalable(
C->getSExtValue());
3476 nullptr, IncOffset,
false))
3484void LSRInstance::GenerateIVChain(
const IVChain &Chain,
3485 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
3488 const IVInc &Head = Chain.Incs[0];
3493 Value *IVSrc =
nullptr;
3494 while (IVOpIter != IVOpEnd) {
3505 if (SE.
getSCEV(*IVOpIter) == Head.IncExpr
3506 || SE.
getSCEV(IVSrc) == Head.IncExpr) {
3509 IVOpIter =
findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3511 if (IVOpIter == IVOpEnd) {
3513 LLVM_DEBUG(
dbgs() <<
"Concealed chain head: " << *Head.UserInst <<
"\n");
3516 assert(IVSrc &&
"Failed to find IV chain source");
3521 const SCEV *LeftOverExpr =
nullptr;
3522 const SCEV *Accum = SE.
getZero(IntTy);
3526 for (
const IVInc &Inc : Chain) {
3529 InsertPt =
L->getLoopLatch()->getTerminator();
3533 Value *IVOper = IVSrc;
3534 if (!Inc.IncExpr->isZero()) {
3539 LeftOverExpr = LeftOverExpr
3545 bool FoundBase =
false;
3546 for (
auto [MapScev, MapIVOper] :
reverse(Bases)) {
3547 const SCEV *Remainder = SE.
getMinusSCEV(Accum, MapScev);
3549 if (!Remainder->
isZero()) {
3551 Value *IncV =
Rewriter.expandCodeFor(Remainder, IntTy, InsertPt);
3552 const SCEV *IVOperExpr =
3554 IVOper =
Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
3563 if (!FoundBase && LeftOverExpr && !LeftOverExpr->
isZero()) {
3566 Value *IncV =
Rewriter.expandCodeFor(LeftOverExpr, IntTy, InsertPt);
3569 IVOper =
Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
3574 assert(IVTy == IVOper->
getType() &&
"inconsistent IV increment type");
3577 LeftOverExpr =
nullptr;
3581 if (IVTy != OperTy) {
3583 "cannot extend a chained IV");
3585 IVOper = Builder.CreateTruncOrBitCast(IVOper, OperTy,
"lsr.chain");
3587 Inc.UserInst->replaceUsesOfWith(Inc.IVOperand, IVOper);
3594 for (PHINode &Phi :
L->getHeader()->phis()) {
3598 Phi.getIncomingValueForBlock(
L->getLoopLatch()));
3601 Value *IVOper = IVSrc;
3603 if (IVTy != PostIncTy) {
3605 IRBuilder<> Builder(
L->getLoopLatch()->getTerminator());
3606 Builder.SetCurrentDebugLocation(PostIncV->
getDebugLoc());
3607 IVOper = Builder.CreatePointerCast(IVSrc, PostIncTy,
"lsr.chain");
3609 Phi.replaceUsesOfWith(PostIncV, IVOper);
3615void LSRInstance::CollectFixupsAndInitialFormulae() {
3616 CondBrInst *ExitBranch =
nullptr;
3620 SmallPtrSet<const SCEV *, 16> Regs;
3621 DenseSet<const SCEV *> VisitedRegs;
3622 DenseSet<size_t> VisitedLSRUse;
3624 for (
const IVStrideUse &U : IU) {
3629 assert(UseI != UserInst->
op_end() &&
"cannot find IV operand");
3630 if (IVIncSet.count(UseI)) {
3631 LLVM_DEBUG(
dbgs() <<
"Use is in profitable chain: " << **UseI <<
'\n');
3635 LSRUse::KindType
Kind = LSRUse::Basic;
3636 MemAccessTy AccessTy;
3638 Kind = LSRUse::Address;
3642 const SCEV *S = IU.getExpr(U);
3658 if (CI->isEquality()) {
3661 Value *
NV = CI->getOperand(1);
3662 if (NV ==
U.getOperandValToReplace()) {
3663 CI->setOperand(1, CI->getOperand(0));
3664 CI->setOperand(0, NV);
3665 NV = CI->getOperand(1);
3672 (!
NV->getType()->isPointerTy() ||
3679 Kind = LSRUse::ICmpZero;
3681 }
else if (
L->isLoopInvariant(NV) &&
3684 !
NV->getType()->isPointerTy()) {
3695 Kind = LSRUse::ICmpZero;
3702 for (
size_t i = 0, e = Factors.size(); i != e; ++i)
3703 if (Factors[i] != -1)
3704 Factors.insert(-(
uint64_t)Factors[i]);
3710 std::pair<size_t, Immediate>
P = getUse(S, Kind, AccessTy);
3711 size_t LUIdx =
P.first;
3713 LSRUse &LU =
Uses[LUIdx];
3716 LSRFixup &LF = LU.getNewFixup();
3717 LF.UserInst = UserInst;
3718 LF.OperandValToReplace =
U.getOperandValToReplace();
3719 LF.PostIncLoops = TmpPostIncLoops;
3721 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3722 LU.AllFixupsUnconditional &= IsFixupExecutedEachIncrement(LF);
3725 if (!VisitedLSRUse.
count(LUIdx) && !LF.isUseFullyOutsideLoop(L)) {
3727 F.initialMatch(S, L, SE);
3728 BaselineCost.RateFormula(
F, Regs, VisitedRegs, LU,
3729 HardwareLoopProfitable);
3730 VisitedLSRUse.
insert(LUIdx);
3734 if (LU.Formulae.empty()) {
3735 InsertInitialFormula(S, LU, LUIdx);
3736 CountRegisters(LU.Formulae.back(), LUIdx);
3745void LSRInstance::InsertInitialFormula(
const SCEV *S, LSRUse &LU,
3749 LU.RigidFormula =
true;
3752 F.initialMatch(S, L, SE);
3753 bool Inserted = InsertFormula(LU, LUIdx,
F);
3754 assert(Inserted &&
"Initial formula already exists!"); (void)Inserted;
3760LSRInstance::InsertSupplementalFormula(
const SCEV *S,
3761 LSRUse &LU,
size_t LUIdx) {
3763 F.BaseRegs.push_back(S);
3764 F.HasBaseReg =
true;
3765 bool Inserted = InsertFormula(LU, LUIdx,
F);
3766 assert(Inserted &&
"Supplemental formula already exists!"); (void)Inserted;
3770void LSRInstance::CountRegisters(
const Formula &
F,
size_t LUIdx) {
3772 RegUses.countRegister(
F.ScaledReg, LUIdx);
3773 for (
const SCEV *BaseReg :
F.BaseRegs)
3774 RegUses.countRegister(BaseReg, LUIdx);
3779bool LSRInstance::InsertFormula(LSRUse &LU,
unsigned LUIdx,
const Formula &
F) {
3782 "Formula is illegal");
3784 if (!LU.InsertFormula(
F, *L))
3787 CountRegisters(
F, LUIdx);
3793bool LSRInstance::IsFixupExecutedEachIncrement(
const LSRFixup &LF)
const {
3805LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
3807 SmallPtrSet<const SCEV *, 32> Visited;
3814 while (!Worklist.
empty()) {
3818 if (!Visited.
insert(S).second)
3829 const Value *
V = US->getValue();
3832 if (
L->contains(Inst))
continue;
3836 for (
const Use &U :
V->uses()) {
3846 if (UserInst->
getParent()->getParent() !=
L->getHeader()->getParent())
3868 bool HasIncompatibleEHPTerminatedBlock =
false;
3870 for (
unsigned int I = 0;
I < PhiNode->getNumIncomingValues();
I++) {
3871 if (PhiNode->getIncomingValue(
I) == ExpectedValue) {
3872 if (PhiNode->getIncomingBlock(
I)->getTerminator()->isEHPad()) {
3873 HasIncompatibleEHPTerminatedBlock =
true;
3878 if (HasIncompatibleEHPTerminatedBlock) {
3901 unsigned OtherIdx = !
U.getOperandNo();
3902 Value *OtherOp = ICI->getOperand(OtherIdx);
3912 std::pair<size_t, Immediate>
P =
3913 getUse(S, LSRUse::Basic, MemAccessTy());
3914 size_t LUIdx =
P.first;
3916 LSRUse &LU =
Uses[LUIdx];
3917 LSRFixup &LF = LU.getNewFixup();
3918 LF.UserInst =
const_cast<Instruction *
>(UserInst);
3919 LF.OperandValToReplace =
U;
3921 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3922 LU.AllFixupsUnconditional &= IsFixupExecutedEachIncrement(LF);
3923 InsertSupplementalFormula(US, LU, LUIdx);
3924 CountRegisters(LU.Formulae.back(),
Uses.size() - 1);
3940 unsigned Depth = 0) {
3947 for (
const SCEV *S :
Add->operands()) {
3954 const SCEV *Start, *Step;
3959 if (Start->isZero())
3968 Remainder =
nullptr;
3970 if (Remainder != Start) {
3992 LSRUse &LU,
const SCEV *S,
const Loop *L,
3994 if (LU.Kind != LSRUse::Address ||
3995 !LU.AccessTy.getType()->isIntOrIntVectorTy())
4001 if (
TTI.isIndexedLoadLegal(
TTI.MIM_PostInc, S->
getType()) ||
4010void LSRInstance::GenerateReassociationsImpl(LSRUse &LU,
unsigned LUIdx,
4011 const Formula &
Base,
4012 unsigned Depth,
size_t Idx,
4022 const SCEV *Remainder =
CollectSubexprs(BaseReg,
nullptr, AddOps, L, SE);
4026 if (AddOps.
size() == 1)
4040 LU.AccessTy, *J,
Base.getNumRegs() > 1))
4045 InnerAddOps.append(std::next(J), std::as_const(AddOps).
end());
4049 if (InnerAddOps.size() == 1 &&
4051 LU.AccessTy, InnerAddOps[0],
Base.getNumRegs() > 1))
4054 const SCEV *InnerSum = SE.
getAddExpr(InnerAddOps);
4059 if (
F.UnfoldedOffset.isNonZero() &&
F.UnfoldedOffset.isScalable())
4068 Immediate::getFixed((
uint64_t)
F.UnfoldedOffset.getFixedValue() +
4071 F.ScaledReg =
nullptr;
4074 F.BaseRegs.erase(
F.BaseRegs.begin() + Idx);
4075 }
else if (IsScaledReg)
4076 F.ScaledReg = InnerSum;
4078 F.BaseRegs[
Idx] = InnerSum;
4086 Immediate::getFixed((
uint64_t)
F.UnfoldedOffset.getFixedValue() +
4089 F.BaseRegs.push_back(*J);
4094 if (InsertFormula(LU, LUIdx,
F))
4101 GenerateReassociations(LU, LUIdx, LU.Formulae.back(),
4107void LSRInstance::GenerateReassociations(LSRUse &LU,
unsigned LUIdx,
4109 assert(
Base.isCanonical(*L) &&
"Input must be in the canonical form");
4114 for (
size_t i = 0, e =
Base.BaseRegs.size(); i != e; ++i)
4115 GenerateReassociationsImpl(LU, LUIdx,
Base,
Depth, i);
4117 if (
Base.Scale == 1)
4118 GenerateReassociationsImpl(LU, LUIdx,
Base,
Depth,
4124void LSRInstance::GenerateCombinations(LSRUse &LU,
unsigned LUIdx,
4127 if (
Base.BaseRegs.size() + (
Base.Scale == 1) +
4128 (
Base.UnfoldedOffset.isNonZero()) <=
4136 Formula NewBase =
Base;
4137 NewBase.BaseRegs.clear();
4138 Type *CombinedIntegerType =
nullptr;
4139 for (
const SCEV *BaseReg :
Base.BaseRegs) {
4142 if (!CombinedIntegerType)
4144 Ops.push_back(BaseReg);
4147 NewBase.BaseRegs.push_back(BaseReg);
4151 if (
Ops.size() == 0)
4156 auto GenerateFormula = [&](
const SCEV *Sum) {
4157 Formula
F = NewBase;
4165 F.BaseRegs.push_back(Sum);
4167 (void)InsertFormula(LU, LUIdx,
F);
4171 if (
Ops.size() > 1) {
4178 if (NewBase.UnfoldedOffset.isNonZero() && NewBase.UnfoldedOffset.isFixed()) {
4179 assert(CombinedIntegerType &&
"Missing a type for the unfolded offset");
4181 NewBase.UnfoldedOffset.getFixedValue(),
true));
4182 NewBase.UnfoldedOffset = Immediate::getFixed(0);
4188void LSRInstance::GenerateSymbolicOffsetsImpl(LSRUse &LU,
unsigned LUIdx,
4189 const Formula &
Base,
size_t Idx,
4193 if (
G->isZero() || !GV)
4197 if (!
isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
F))
4202 F.BaseRegs[
Idx] =
G;
4203 (void)InsertFormula(LU, LUIdx,
F);
4207void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU,
unsigned LUIdx,
4210 if (
Base.BaseGV)
return;
4212 for (
size_t i = 0, e =
Base.BaseRegs.size(); i != e; ++i)
4213 GenerateSymbolicOffsetsImpl(LU, LUIdx,
Base, i);
4214 if (
Base.Scale == 1)
4215 GenerateSymbolicOffsetsImpl(LU, LUIdx,
Base, -1,
4220void LSRInstance::GenerateConstantOffsetsImpl(
4221 LSRUse &LU,
unsigned LUIdx,
const Formula &
Base,
4222 const SmallVectorImpl<Immediate> &Worklist,
size_t Idx,
bool IsScaledReg) {
4224 auto GenerateOffset = [&](
const SCEV *
G, Immediate
Offset) {
4226 if (!
Base.BaseOffset.isCompatibleImmediate(
Offset))
4228 F.BaseOffset =
Base.BaseOffset.subUnsigned(
Offset);
4230 if (
isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
F)) {
4232 const SCEV *NewOffset =
Offset.getSCEV(SE,
G->getType());
4238 F.ScaledReg =
nullptr;
4240 F.deleteBaseReg(
F.BaseRegs[Idx]);
4242 }
else if (IsScaledReg)
4245 F.BaseRegs[
Idx] = NewG;
4247 (void)InsertFormula(LU, LUIdx,
F);
4262 const APInt *StepInt;
4267 for (Immediate
Offset : Worklist) {
4269 Offset = Immediate::getFixed(
Offset.getFixedValue() - Step);
4275 for (Immediate
Offset : Worklist)
4282 if (
G->isZero() ||
Imm.isZero() ||
4283 !
Base.BaseOffset.isCompatibleImmediate(
Imm))
4286 F.BaseOffset =
F.BaseOffset.addUnsigned(
Imm);
4287 if (!
isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
F))
4292 F.BaseRegs[
Idx] =
G;
4297 (void)InsertFormula(LU, LUIdx,
F);
4301void LSRInstance::GenerateConstantOffsets(LSRUse &LU,
unsigned LUIdx,
4307 if (LU.MaxOffset != LU.MinOffset)
4310 for (
size_t i = 0, e =
Base.BaseRegs.size(); i != e; ++i)
4311 GenerateConstantOffsetsImpl(LU, LUIdx,
Base, Worklist, i);
4312 if (
Base.Scale == 1)
4313 GenerateConstantOffsetsImpl(LU, LUIdx,
Base, Worklist, -1,
4319void LSRInstance::GenerateICmpZeroScales(LSRUse &LU,
unsigned LUIdx,
4321 if (LU.Kind != LSRUse::ICmpZero)
return;
4329 if (LU.MinOffset != LU.MaxOffset)
return;
4332 if (
Base.ScaledReg &&
Base.ScaledReg->getType()->isPointerTy())
4334 for (
const SCEV *BaseReg :
Base.BaseRegs)
4335 if (
BaseReg->getType()->isPointerTy())
4337 assert(!
Base.BaseGV &&
"ICmpZero use is not legal!");
4340 for (int64_t Factor : Factors) {
4345 if (
Base.BaseOffset.isMin() && Factor == -1)
4348 if (
Base.BaseOffset.isNonZero() &&
Base.BaseOffset.isScalable())
4350 Immediate NewBaseOffset =
Base.BaseOffset.mulUnsigned(Factor);
4351 assert(Factor != 0 &&
"Zero factor not expected!");
4352 if (NewBaseOffset.getFixedValue() / Factor !=
4353 Base.BaseOffset.getFixedValue())
4361 Immediate
Offset = LU.MinOffset;
4362 if (
Offset.isMin() && Factor == -1)
4365 if (
Offset.getFixedValue() / Factor != LU.MinOffset.getFixedValue())
4373 F.BaseOffset = NewBaseOffset;
4380 F.BaseOffset =
F.BaseOffset.addUnsigned(
Offset).subUnsigned(LU.MinOffset);
4382 const SCEV *FactorS = SE.
getConstant(IntTy, Factor);
4385 for (
size_t i = 0, e =
F.BaseRegs.size(); i != e; ++i) {
4399 if (
F.UnfoldedOffset.isNonZero()) {
4400 if (
F.UnfoldedOffset.isMin() && Factor == -1)
4402 F.UnfoldedOffset =
F.UnfoldedOffset.mulUnsigned(Factor);
4403 if (
F.UnfoldedOffset.getFixedValue() / Factor !=
4404 Base.UnfoldedOffset.getFixedValue())
4408 IntTy,
F.UnfoldedOffset.getFixedValue()))
4413 (void)InsertFormula(LU, LUIdx,
F);
4420void LSRInstance::GenerateScales(LSRUse &LU,
unsigned LUIdx, Formula
Base) {
4427 if (
Base.Scale != 0 && !
Base.unscale())
4430 assert(
Base.Scale == 0 &&
"unscale did not did its job!");
4433 for (int64_t Factor : Factors) {
4434 Base.Scale = Factor;
4435 Base.HasBaseReg =
Base.BaseRegs.size() > 1;
4437 if (!
isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4441 if (LU.Kind == LSRUse::Basic &&
4442 isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special,
4443 LU.AccessTy,
Base) &&
4444 LU.AllFixupsOutsideLoop)
4445 LU.Kind = LSRUse::Special;
4451 if (LU.Kind == LSRUse::ICmpZero && !
Base.HasBaseReg &&
4452 Base.BaseOffset.isZero() && !
Base.BaseGV)
4455 for (
size_t i = 0, e =
Base.BaseRegs.size(); i != e; ++i) {
4457 if (AR && (AR->
getLoop() == L || LU.AllFixupsOutsideLoop)) {
4458 const SCEV *FactorS = SE.
getConstant(IntTy, Factor);
4463 if (
const SCEV *Quotient =
getExactSDiv(AR, FactorS, SE,
true))
4464 if (!Quotient->isZero()) {
4467 F.ScaledReg = Quotient;
4468 F.deleteBaseReg(
F.BaseRegs[i]);
4472 if (
F.Scale == 1 && (
F.BaseRegs.empty() ||
4473 (AR->
getLoop() != L && LU.AllFixupsOutsideLoop)))
4477 if (
F.Scale == 1 && LU.AllFixupsOutsideLoop)
4479 (void)InsertFormula(LU, LUIdx,
F);
4495 const SCEV *Result =
nullptr;
4496 for (
auto &L :
Loops) {
4500 if (!New || (Result && New != Result))
4505 assert(Result &&
"failed to create expression");
4510void LSRInstance::GenerateTruncates(LSRUse &LU,
unsigned LUIdx, Formula
Base) {
4512 if (
Base.BaseGV)
return;
4522 if (
Base.ScaledReg &&
Base.ScaledReg->getType()->isPointerTy())
4525 [](
const SCEV *S) { return S->getType()->isPointerTy(); }))
4529 for (
auto &LF : LU.Fixups)
4530 Loops.push_back(LF.PostIncLoops);
4532 for (
Type *SrcTy : Types) {
4541 const SCEV *NewScaledReg =
4543 if (!NewScaledReg || NewScaledReg->
isZero())
4545 F.ScaledReg = NewScaledReg;
4547 bool HasZeroBaseReg =
false;
4548 for (
const SCEV *&BaseReg :
F.BaseRegs) {
4549 const SCEV *NewBaseReg =
4551 if (!NewBaseReg || NewBaseReg->
isZero()) {
4552 HasZeroBaseReg =
true;
4562 if (!
F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses))
4566 (void)InsertFormula(LU, LUIdx,
F);
4579 const SCEV *OrigReg;
4581 WorkItem(
size_t LI, Immediate
I,
const SCEV *R)
4582 : LUIdx(LI),
Imm(
I), OrigReg(
R) {}
4584 void print(raw_ostream &OS)
const;
4590#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4591void WorkItem::print(raw_ostream &OS)
const {
4592 OS <<
"in formulae referencing " << *OrigReg <<
" in use " << LUIdx
4593 <<
" , add offset " <<
Imm;
4603void LSRInstance::GenerateCrossUseConstantOffsets() {
4605 using ImmMapTy = std::map<Immediate, const SCEV *, KeyOrderTargetImmediate>;
4607 DenseMap<const SCEV *, ImmMapTy>
Map;
4608 DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
4610 for (
const SCEV *Use : RegUses) {
4614 auto Pair =
Map.try_emplace(
Reg);
4617 Pair.first->second.insert(std::make_pair(
Imm, Use));
4618 UsedByIndicesMap[
Reg] |= RegUses.getUsedByIndices(Use);
4625 SmallSet<std::pair<size_t, Immediate>, 32, KeyOrderSizeTAndImmediate>
4627 for (
const SCEV *
Reg : Sequence) {
4628 const ImmMapTy &Imms =
Map.find(
Reg)->second;
4631 if (Imms.size() == 1)
4635 for (
const auto &Entry
4637 <<
' ' <<
Entry.first;
4641 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
4643 const SCEV *OrigReg = J->second;
4645 Immediate JImm = J->first;
4646 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
4649 UsedByIndicesMap[
Reg].
count() == 1) {
4657 Immediate
First = Imms.begin()->first;
4658 Immediate
Last = std::prev(Imms.end())->first;
4659 if (!
First.isCompatibleImmediate(
Last)) {
4666 bool Scalable =
First.isScalable() ||
Last.isScalable();
4667 int64_t FI =
First.getKnownMinValue();
4668 int64_t LI =
Last.getKnownMinValue();
4671 int64_t Avg = (FI & LI) + ((FI ^ LI) >> 1);
4674 Avg = Avg + ((FI ^ LI) & ((
uint64_t)Avg >> 63));
4675 ImmMapTy::const_iterator OtherImms[] = {
4676 Imms.begin(), std::prev(Imms.end()),
4677 Imms.lower_bound(Immediate::get(Avg, Scalable))};
4678 for (
const auto &M : OtherImms) {
4679 if (M == J || M == JE)
continue;
4680 if (!JImm.isCompatibleImmediate(
M->first))
4684 Immediate
Imm = JImm.subUnsigned(
M->first);
4685 for (
unsigned LUIdx : UsedByIndices.
set_bits())
4687 if (UniqueItems.
insert(std::make_pair(LUIdx,
Imm)).second)
4695 UsedByIndicesMap.
clear();
4696 UniqueItems.
clear();
4699 for (
const WorkItem &WI : WorkItems) {
4700 size_t LUIdx = WI.LUIdx;
4701 LSRUse &LU =
Uses[LUIdx];
4702 Immediate
Imm = WI.Imm;
4703 const SCEV *OrigReg = WI.OrigReg;
4706 const SCEV *NegImmS =
Imm.getNegativeSCEV(SE, IntTy);
4710 for (
size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
4711 Formula
F = LU.Formulae[
L];
4718 if (
F.ScaledReg == OrigReg) {
4719 if (!
F.BaseOffset.isCompatibleImmediate(
Imm))
4721 Immediate
Offset =
F.BaseOffset.addUnsigned(
Imm.mulUnsigned(
F.Scale));
4723 const SCEV *S =
Offset.getNegativeSCEV(SE, IntTy);
4724 if (
F.referencesReg(S))
4727 NewF.BaseOffset =
Offset;
4728 if (!
isLegalUse(
TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4731 NewF.ScaledReg = SE.
getAddExpr(NegImmS, NewF.ScaledReg);
4740 if (NewF.BaseOffset.isNonZero() && NewF.BaseOffset.isScalable())
4742 if (
C->getValue()->isNegative() !=
4743 (NewF.BaseOffset.isLessThanZero()) &&
4744 (
C->getAPInt().abs() * APInt(
BitWidth,
F.Scale))
4745 .ule(std::abs(NewF.BaseOffset.getFixedValue())))
4750 NewF.canonicalize(*this->L);
4751 (void)InsertFormula(LU, LUIdx, NewF);
4754 for (
size_t N = 0, NE =
F.BaseRegs.size();
N != NE; ++
N) {
4756 if (BaseReg != OrigReg)
4759 if (!NewF.BaseOffset.isCompatibleImmediate(
Imm) ||
4760 !NewF.UnfoldedOffset.isCompatibleImmediate(
Imm) ||
4761 !NewF.BaseOffset.isCompatibleImmediate(NewF.UnfoldedOffset))
4763 NewF.BaseOffset = NewF.BaseOffset.addUnsigned(
Imm);
4765 LU.Kind, LU.AccessTy, NewF)) {
4769 Immediate NewUnfoldedOffset = NewF.UnfoldedOffset.addUnsigned(
Imm);
4773 NewF.UnfoldedOffset = NewUnfoldedOffset;
4775 NewF.BaseRegs[
N] = SE.
getAddExpr(NegImmS, BaseReg);
4780 for (
const SCEV *NewReg : NewF.BaseRegs)
4782 if (NewF.BaseOffset.isNonZero() && NewF.BaseOffset.isScalable())
4784 if ((
C->getAPInt() + NewF.BaseOffset.getFixedValue())
4786 .slt(std::abs(NewF.BaseOffset.getFixedValue())) &&
4787 (
C->getAPInt() + NewF.BaseOffset.getFixedValue())
4790 NewF.BaseOffset.getFixedValue()))
4795 NewF.canonicalize(*this->L);
4796 (void)InsertFormula(LU, LUIdx, NewF);
4807LSRInstance::GenerateAllReuseFormulae() {
4810 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
4811 LSRUse &LU =
Uses[LUIdx];
4812 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4813 GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
4814 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4815 GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
4817 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
4818 LSRUse &LU =
Uses[LUIdx];
4819 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4820 GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]);
4821 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4822 GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]);
4823 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4824 GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]);
4825 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4826 GenerateScales(LU, LUIdx, LU.Formulae[i]);
4828 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
4829 LSRUse &LU =
Uses[LUIdx];
4830 for (
size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4831 GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
4834 GenerateCrossUseConstantOffsets();
4837 "After generating reuse formulae:\n";
4838 print_uses(
dbgs()));
4843void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
4844 DenseSet<const SCEV *> VisitedRegs;
4845 SmallPtrSet<const SCEV *, 16> Regs;
4846 SmallPtrSet<const SCEV *, 16> LoserRegs;
4848 bool ChangedFormulae =
false;
4853 using BestFormulaeTy = DenseMap<SmallVector<const SCEV *, 4>,
size_t>;
4855 BestFormulaeTy BestFormulae;
4857 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
4858 LSRUse &LU =
Uses[LUIdx];
4863 for (
size_t FIdx = 0, NumForms = LU.Formulae.size();
4864 FIdx != NumForms; ++FIdx) {
4865 Formula &
F = LU.Formulae[FIdx];
4874 Cost CostF(Opts, L, SE,
TTI, AMK);
4876 CostF.RateFormula(
F, Regs, VisitedRegs, LU, HardwareLoopProfitable,
4878 if (CostF.isLoser()) {
4890 for (
const SCEV *
Reg :
F.BaseRegs) {
4891 if (RegUses.isRegUsedByUsesOtherThan(
Reg, LUIdx))
4895 RegUses.isRegUsedByUsesOtherThan(
F.ScaledReg, LUIdx))
4896 Key.push_back(
F.ScaledReg);
4901 std::pair<BestFormulaeTy::const_iterator, bool>
P =
4902 BestFormulae.insert(std::make_pair(
Key, FIdx));
4906 Formula &Best = LU.Formulae[
P.first->second];
4908 Cost CostBest(Opts, L, SE,
TTI, AMK);
4910 CostBest.RateFormula(Best, Regs, VisitedRegs, LU,
4911 HardwareLoopProfitable);
4912 if (CostF.isLess(CostBest))
4916 " in favor of formula ";
4917 Best.print(
dbgs());
dbgs() <<
'\n');
4920 ChangedFormulae =
true;
4922 LU.DeleteFormula(
F);
4930 LU.RecomputeRegs(LUIdx, RegUses);
4933 BestFormulae.clear();
4938 "After filtering out undesirable candidates:\n";
4946size_t LSRInstance::EstimateSearchSpaceComplexity()
const {
4948 for (
const LSRUse &LU :
Uses) {
4949 size_t FSize = LU.Formulae.size();
4950 if (FSize >= Opts.lsr_complexity_limit) {
4951 Power = Opts.lsr_complexity_limit;
4955 if (Power >= Opts.lsr_complexity_limit)
4964void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
4965 if (EstimateSearchSpaceComplexity() >= Opts.lsr_complexity_limit) {
4968 LLVM_DEBUG(
dbgs() <<
"Narrowing the search space by eliminating formulae "
4969 "which use a superset of registers used by other "
4972 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
4973 LSRUse &LU =
Uses[LUIdx];
4975 for (
size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
4976 Formula &
F = LU.Formulae[i];
4977 if (
F.BaseOffset.isNonZero() &&
F.BaseOffset.isScalable())
4983 I =
F.BaseRegs.begin(),
E =
F.BaseRegs.end();
I !=
E; ++
I) {
4989 Immediate::getFixed(NewF.BaseOffset.getFixedValue() +
4990 (
uint64_t)
C->getValue()->getSExtValue());
4991 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
4992 (
I -
F.BaseRegs.begin()));
4993 if (LU.HasFormulaWithSameRegs(NewF)) {
4996 LU.DeleteFormula(
F);
5007 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
5008 (
I -
F.BaseRegs.begin()));
5009 if (LU.HasFormulaWithSameRegs(NewF)) {
5012 LU.DeleteFormula(
F);
5023 LU.RecomputeRegs(LUIdx, RegUses);
5032void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
5033 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5037 dbgs() <<
"The search space is too complex.\n"
5038 "Narrowing the search space by assuming that uses separated "
5039 "by a constant offset will use the same registers.\n");
5043 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5044 LSRUse &LU =
Uses[LUIdx];
5045 for (
const Formula &
F : LU.Formulae) {
5046 if (
F.BaseOffset.isZero() || (
F.Scale != 0 &&
F.Scale != 1))
5048 assert((LU.Kind == LSRUse::Address || LU.Kind == LSRUse::ICmpZero) &&
5049 "Only address and cmp uses expected to have nonzero BaseOffset");
5051 LSRUse *LUThatHas = FindUseWithSimilarFormula(
F, LU);
5055 if (!reconcileNewOffset(*LUThatHas,
F.BaseOffset,
false,
5056 LU.Kind, LU.AccessTy))
5061 LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
5062 LUThatHas->AllFixupsUnconditional &= LU.AllFixupsUnconditional;
5065 for (LSRFixup &Fixup : LU.Fixups) {
5066 Fixup.Offset +=
F.BaseOffset;
5067 LUThatHas->pushFixup(Fixup);
5072 Type *FixupType = LUThatHas->Fixups[0].OperandValToReplace->getType();
5073 for (LSRFixup &Fixup : LUThatHas->Fixups)
5074 assert(
Fixup.OperandValToReplace->getType() == FixupType &&
5075 "Expected all fixups to have the same type");
5080 for (
size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
5081 Formula &
F = LUThatHas->Formulae[i];
5082 if (!
isLegalUse(
TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset,
5083 LUThatHas->Kind, LUThatHas->AccessTy,
F)) {
5085 LUThatHas->DeleteFormula(
F);
5093 LUThatHas->RecomputeRegs(LUThatHas - &
Uses.front(), RegUses);
5096 DeleteUse(LU, LUIdx);
5109void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
5110 if (EstimateSearchSpaceComplexity() >= Opts.lsr_complexity_limit) {
5113 LLVM_DEBUG(
dbgs() <<
"Narrowing the search space by re-filtering out "
5114 "undesirable dedicated registers.\n");
5116 FilterOutUndesirableDedicatedRegisters();
5131void LSRInstance::NarrowSearchSpaceByFilterFormulaWithSameScaledReg() {
5132 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5136 dbgs() <<
"The search space is too complex.\n"
5137 "Narrowing the search space by choosing the best Formula "
5138 "from the Formulae with the same Scale and ScaledReg.\n");
5141 using BestFormulaeTy = DenseMap<std::pair<const SCEV *, int64_t>,
size_t>;
5143 BestFormulaeTy BestFormulae;
5145 bool ChangedFormulae =
false;
5147 DenseSet<const SCEV *> VisitedRegs;
5148 SmallPtrSet<const SCEV *, 16> Regs;
5150 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5151 LSRUse &LU =
Uses[LUIdx];
5156 auto IsBetterThan = [&](Formula &FA, Formula &FB) {
5161 size_t FARegNum = 0;
5162 for (
const SCEV *
Reg : FA.BaseRegs) {
5163 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(
Reg);
5164 FARegNum += (NumUses - UsedByIndices.
count() + 1);
5166 size_t FBRegNum = 0;
5167 for (
const SCEV *
Reg : FB.BaseRegs) {
5168 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(
Reg);
5169 FBRegNum += (NumUses - UsedByIndices.
count() + 1);
5171 if (FARegNum != FBRegNum)
5172 return FARegNum < FBRegNum;
5176 Cost CostFA(Opts, L, SE,
TTI, AMK);
5177 Cost CostFB(Opts, L, SE,
TTI, AMK);
5179 CostFA.RateFormula(FA, Regs, VisitedRegs, LU, HardwareLoopProfitable);
5181 CostFB.RateFormula(FB, Regs, VisitedRegs, LU, HardwareLoopProfitable);
5182 return CostFA.isLess(CostFB);
5186 for (
size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
5188 Formula &
F = LU.Formulae[FIdx];
5191 auto P = BestFormulae.insert({{
F.ScaledReg,
F.Scale}, FIdx});
5195 Formula &Best = LU.Formulae[
P.first->second];
5196 if (IsBetterThan(
F, Best))
5200 " in favor of formula ";
5201 Best.print(
dbgs());
dbgs() <<
'\n');
5203 ChangedFormulae =
true;
5205 LU.DeleteFormula(
F);
5211 LU.RecomputeRegs(LUIdx, RegUses);
5214 BestFormulae.clear();
5219 "After filtering out undesirable candidates:\n";
5226void LSRInstance::NarrowSearchSpaceByFilterPostInc() {
5229 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5233 "Narrowing the search space by choosing the lowest "
5234 "register Formula for PostInc Uses.\n");
5236 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5237 LSRUse &LU =
Uses[LUIdx];
5239 if (LU.Kind != LSRUse::Address)
5245 size_t MinRegs = std::numeric_limits<size_t>::max();
5246 for (
const Formula &
F : LU.Formulae)
5247 MinRegs = std::min(
F.getNumRegs(), MinRegs);
5250 for (
size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
5252 Formula &
F = LU.Formulae[FIdx];
5253 if (
F.getNumRegs() > MinRegs) {
5256 LU.DeleteFormula(
F);
5263 LU.RecomputeRegs(LUIdx, RegUses);
5265 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5272void LSRInstance::NarrowSearchSpaceByMergingUsesOutsideLoop() {
5273 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5277 dbgs() <<
"The search space is too complex.\n"
5278 "Narrowing the search space by merging uses with fixups "
5279 "entirely outside the loop with uses inside the loop.\n");
5281 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5282 LSRUse &LU =
Uses[LUIdx];
5285 if (!LU.AllFixupsOutsideLoop || LU.Formulae.empty() ||
5286 LU.Kind == LSRUse::ICmpZero)
5293 LSRUse *LUToMergeWith =
nullptr;
5294 const Formula &ThisF = LU.Formulae[0];
5295 for (LSRUse &OtherLU :
Uses) {
5297 if (OtherLU.AllFixupsOutsideLoop)
5301 if (OtherLU.Kind == LSRUse::ICmpZero)
5304 if (OtherLU.Formulae.empty())
5307 if (
any_of(OtherLU.Formulae, [&](
const Formula &
F) {
5308 return !isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, OtherLU.Kind,
5309 OtherLU.AccessTy, F);
5316 const Formula &OtherF = OtherLU.Formulae[0];
5317 if (ThisF.BaseRegs == OtherF.BaseRegs &&
5318 ThisF.ScaledReg == OtherF.ScaledReg &&
5319 ThisF.BaseGV == OtherF.BaseGV && ThisF.Scale == OtherF.Scale &&
5320 ThisF.UnfoldedOffset == OtherF.UnfoldedOffset &&
5321 ThisF.BaseOffset == OtherF.BaseOffset) {
5322 LUToMergeWith = &OtherLU;
5333 for (LSRFixup &Fixup : LU.Fixups) {
5334 LUToMergeWith->pushFixup(Fixup);
5338 DeleteUse(LU, LUIdx);
5388void LSRInstance::NarrowSearchSpaceByDeletingCostlyFormulas() {
5389 if (EstimateSearchSpaceComplexity() < Opts.lsr_complexity_limit)
5397 SmallPtrSet<const SCEV *, 4> UniqRegs;
5401 DenseMap <const SCEV *, float> RegNumMap;
5402 for (
const SCEV *
Reg : RegUses) {
5406 for (
const LSRUse &LU :
Uses) {
5407 if (!LU.Regs.count(
Reg))
5409 float P = LU.getNotSelectedProbability(
Reg);
5415 RegNumMap.
insert(std::make_pair(
Reg, PNotSel));
5419 dbgs() <<
"Narrowing the search space by deleting costly formulas\n");
5422 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5423 LSRUse &LU =
Uses[LUIdx];
5425 if (LU.Formulae.size() < 2)
5430 float FMinRegNum = LU.Formulae[0].getNumRegs();
5431 float FMinARegNum = LU.Formulae[0].getNumRegs();
5433 for (
size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
5434 Formula &
F = LU.Formulae[i];
5437 for (
const SCEV *BaseReg :
F.BaseRegs) {
5438 if (UniqRegs.
count(BaseReg))
5440 FRegNum += RegNumMap[
BaseReg] / LU.getNotSelectedProbability(BaseReg);
5443 RegNumMap[
BaseReg] / LU.getNotSelectedProbability(BaseReg);
5445 if (
const SCEV *ScaledReg =
F.ScaledReg) {
5446 if (!UniqRegs.
count(ScaledReg)) {
5448 RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
5451 RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
5454 if (FMinRegNum > FRegNum ||
5455 (FMinRegNum == FRegNum && FMinARegNum > FARegNum)) {
5456 FMinRegNum = FRegNum;
5457 FMinARegNum = FARegNum;
5462 dbgs() <<
" with min reg num " << FMinRegNum <<
'\n');
5464 std::swap(LU.Formulae[MinIdx], LU.Formulae[0]);
5465 while (LU.Formulae.size() != 1) {
5468 LU.Formulae.pop_back();
5470 LU.RecomputeRegs(LUIdx, RegUses);
5471 assert(LU.Formulae.size() == 1 &&
"Should be exactly 1 min regs formula");
5472 Formula &
F = LU.Formulae[0];
5488 MemAccessTy AccessType) {
5498 return TTI.isLegalAddressingMode(
5499 AccessType.MemTy,
nullptr,
5500 Diff->getSExtValue(),
5501 true, 0, AccessType.AddrSpace) &&
5502 !
TTI.isLegalAddressingMode(
5503 AccessType.MemTy,
nullptr,
5504 -Diff->getSExtValue(),
5505 true, 0, AccessType.AddrSpace);
5511void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
5514 SmallPtrSet<const SCEV *, 4> Taken;
5515 while (EstimateSearchSpaceComplexity() >= Opts.lsr_complexity_limit) {
5522 const SCEV *Best =
nullptr;
5523 unsigned BestNum = 0;
5524 for (
const SCEV *
Reg : RegUses) {
5529 BestNum = RegUses.getUsedByIndices(
Reg).count();
5531 unsigned Count = RegUses.getUsedByIndices(
Reg).count();
5532 if (
Count > BestNum) {
5540 if (
Count == BestNum) {
5541 int LUIdx = RegUses.getUsedByIndices(
Reg).find_first();
5542 if (LUIdx >= 0 &&
Uses[LUIdx].Kind == LSRUse::Address &&
5544 Uses[LUIdx].AccessTy)) {
5551 assert(Best &&
"Failed to find best LSRUse candidate");
5553 LLVM_DEBUG(
dbgs() <<
"Narrowing the search space by assuming " << *Best
5554 <<
" will yield profitable reuse.\n");
5559 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx) {
5560 LSRUse &LU =
Uses[LUIdx];
5561 if (!LU.Regs.count(Best))
continue;
5564 for (
size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
5565 Formula &
F = LU.Formulae[i];
5566 if (!
F.referencesReg(Best)) {
5568 LU.DeleteFormula(
F);
5572 assert(e != 0 &&
"Use has no formulae left! Is Regs inconsistent?");
5578 LU.RecomputeRegs(LUIdx, RegUses);
5589void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
5590 NarrowSearchSpaceByDetectingSupersets();
5591 NarrowSearchSpaceByCollapsingUnrolledCode();
5592 NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
5593 if (Opts.lsr_filter_same_scaled_reg)
5594 NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
5595 NarrowSearchSpaceByFilterPostInc();
5596 NarrowSearchSpaceByMergingUsesOutsideLoop();
5597 if (Opts.lsr_exp_narrow)
5598 NarrowSearchSpaceByDeletingCostlyFormulas();
5600 NarrowSearchSpaceByPickingWinnerRegs();
5604void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
5606 SmallVectorImpl<const Formula *> &Workspace,
5607 const Cost &CurCost,
5608 const SmallPtrSet<const SCEV *, 16> &CurRegs,
5609 DenseSet<const SCEV *> &VisitedRegs)
const {
5620 const LSRUse &LU =
Uses[Workspace.
size()];
5626 SmallSetVector<const SCEV *, 4> ReqRegs;
5627 for (
const SCEV *S : CurRegs)
5628 if (LU.Regs.count(S))
5631 SmallPtrSet<const SCEV *, 16> NewRegs;
5632 Cost NewCost(Opts, L, SE,
TTI, AMK);
5633 for (
const Formula &
F : LU.Formulae) {
5641 int NumReqRegsToFind = std::min(
F.getNumRegs(), ReqRegs.
size());
5642 for (
const SCEV *
Reg : ReqRegs) {
5643 if ((
F.ScaledReg &&
F.ScaledReg ==
Reg) ||
5646 if (NumReqRegsToFind == 0)
5650 if (NumReqRegsToFind != 0) {
5661 NewCost.RateFormula(
F, NewRegs, VisitedRegs, LU, HardwareLoopProfitable);
5662 if (NewCost.isLess(SolutionCost)) {
5664 if (Workspace.
size() !=
Uses.size()) {
5665 SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
5666 NewRegs, VisitedRegs);
5667 if (
F.getNumRegs() == 1 && Workspace.
size() == 1)
5668 VisitedRegs.
insert(
F.ScaledReg ?
F.ScaledReg :
F.BaseRegs[0]);
5671 dbgs() <<
".\nRegs:\n";
5672 for (
const SCEV *S : NewRegs)
dbgs()
5673 <<
"- " << *S <<
"\n";
5676 SolutionCost = NewCost;
5677 Solution = Workspace;
5686void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution)
const {
5688 Cost SolutionCost(Opts, L, SE,
TTI, AMK);
5689 SolutionCost.Lose();
5690 Cost CurCost(Opts, L, SE,
TTI, AMK);
5691 SmallPtrSet<const SCEV *, 16> CurRegs;
5692 DenseSet<const SCEV *> VisitedRegs;
5696 SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
5697 CurRegs, VisitedRegs);
5698 if (Solution.
empty()) {
5705 "The chosen solution requires ";
5706 SolutionCost.print(
dbgs());
dbgs() <<
":\n";
5707 for (
size_t i = 0, e =
Uses.size(); i != e; ++i) {
5712 Solution[i]->print(
dbgs());
5718 const bool EnableDropUnprofitableSolution =
valueOr(
5721 if (BaselineCost.isLess(SolutionCost)) {
5722 if (!EnableDropUnprofitableSolution)
5724 dbgs() <<
"Baseline is more profitable than chosen solution, "
5725 "add option 'lsr-drop-solution' to drop LSR solution.\n");
5728 "solution, dropping LSR solution.\n";);
5739 const SmallVectorImpl<Instruction *> &Inputs)
5743 bool AllDominate =
true;
5750 for (Instruction *Inst : Inputs) {
5751 if (Inst == Tentative || !DT.
dominates(Inst, Tentative)) {
5752 AllDominate =
false;
5757 if (Tentative->
getParent() == Inst->getParent() &&
5758 (!BetterPos || !DT.
dominates(Inst, BetterPos)))
5768 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
5769 unsigned IPLoopDepth = IPLoop ? IPLoop->
getLoopDepth() : 0;
5773 if (!Rung)
return IP;
5774 Rung = Rung->getIDom();
5775 if (!Rung)
return IP;
5776 IDom = Rung->getBlock();
5779 const Loop *IDomLoop = LI.getLoopFor(IDom);
5780 unsigned IDomDepth = IDomLoop ? IDomLoop->
getLoopDepth() : 0;
5781 if (IDomDepth <= IPLoopDepth &&
5782 (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
5799 SmallVector<Instruction *, 4> Inputs;
5802 if (LU.Kind == LSRUse::ICmpZero)
5803 if (Instruction *
I =
5806 if (LF.PostIncLoops.
count(L)) {
5807 if (LF.isUseFullyOutsideLoop(L))
5808 Inputs.
push_back(
L->getLoopLatch()->getTerminator());
5814 for (
const Loop *PIL : LF.PostIncLoops) {
5815 if (PIL == L)
continue;
5820 if (!ExitingBlocks.
empty()) {
5822 for (
unsigned i = 1, e = ExitingBlocks.
size(); i != e; ++i)
5829 "Insertion point must be a normal instruction");
5839 while (IP->isEHPad()) ++IP;
5844 while (
Rewriter.isInsertedInstruction(&*IP) && IP != LowestIP)
5852Value *LSRInstance::Expand(
const LSRUse &LU,
const LSRFixup &LF,
5854 SmallVectorImpl<WeakTrackingVH> &DeadInsts)
const {
5855 if (LU.RigidFormula)
5856 return LF.OperandValToReplace;
5860 IP = AdjustInsertPositionForExpand(IP, LF, LU);
5865 Rewriter.setPostInc(LF.PostIncLoops);
5870 Type *Ty =
F.getType();
5883 if (LU.Kind == LSRUse::ICmpZero && OpTy->
isPointerTy()) {
5892 for (
const SCEV *
Reg :
F.BaseRegs) {
5893 assert(!
Reg->isZero() &&
"Zero allocated in a base register!");
5901 Value *ICmpScaledV =
nullptr;
5903 const SCEV *ScaledS =
F.ScaledReg;
5909 if (LU.Kind == LSRUse::ICmpZero) {
5919 "The only scale supported by ICmpZero uses is -1!");
5920 ICmpScaledV =
Rewriter.expandCodeFor(ScaledS,
nullptr);
5928 if (!
Ops.empty() && LU.Kind == LSRUse::Address &&
5938 Ops.push_back(ScaledS);
5964 assert(
F.BaseOffset.isCompatibleImmediate(LF.Offset) &&
5965 "Expanding mismatched offsets\n");
5967 Immediate
Offset =
F.BaseOffset.addUnsigned(LF.Offset);
5968 if (
Offset.isNonZero()) {
5969 if (LU.Kind == LSRUse::ICmpZero) {
5985 Ops.push_back(
Offset.getUnknownSCEV(SE, IntTy));
5990 Immediate UnfoldedOffset =
F.UnfoldedOffset;
5991 if (UnfoldedOffset.isNonZero()) {
5993 Ops.push_back(UnfoldedOffset.getUnknownSCEV(SE, IntTy));
6007 if (LU.Kind == LSRUse::ICmpZero) {
6011 assert(!
F.BaseGV &&
"ICmp does not support folding a global value and "
6012 "a scale at the same time!");
6013 if (
F.Scale == -1) {
6014 if (ICmpScaledV->
getType() != OpTy) {
6024 assert((
F.Scale == 0 ||
F.Scale == 1) &&
6025 "ICmp does not support folding a global value and "
6026 "a scale at the same time!");
6032 if (
C->getType() != OpTy) {
6036 assert(
C &&
"Cast of ConstantInt should have folded");
6049void LSRInstance::RewriteForPHI(PHINode *PN,
const LSRUse &LU,
6050 const LSRFixup &LF,
const Formula &
F,
6051 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
6052 DenseMap<BasicBlock *, Value *>
Inserted;
6056 bool needUpdateFixups =
false;
6067 Loop *PNLoop = LI.getLoopFor(Parent);
6068 if (!PNLoop || Parent != PNLoop->
getHeader()) {
6072 CriticalEdgeSplittingOptions SplitOptions(&DT, &LI, MSSAU);
6074 SplitOptions.setMergeIdenticalEdges().setKeepOneInputPHIs();
6075 if (ShouldPreserveLCSSA)
6076 SplitOptions = SplitOptions.setPreserveLCSSA();
6080 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
6082 MSSAU, ShouldPreserveLCSSA);
6092 if (
L->contains(BB) && !
L->contains(PN))
6100 needUpdateFixups =
true;
6102 if (
auto *OldIncomingInst =
6109 std::pair<DenseMap<BasicBlock *, Value *>::iterator,
bool> Pair =
6122 LF.OperandValToReplace->
getType(),
"tmp",
6129 if (
L->contains(
I) && !
L->contains(BB))
6130 InsertedNonLCSSAInsts.insert(
I);
6133 Pair.first->second = FullV;
6140 if (needUpdateFixups) {
6141 for (LSRUse &LU :
Uses)
6142 for (LSRFixup &Fixup : LU.Fixups)
6146 if (
Fixup.UserInst == PN) {
6149 bool foundInOriginalPHI =
false;
6151 if (val ==
Fixup.OperandValToReplace) {
6152 foundInOriginalPHI =
true;
6157 if (foundInOriginalPHI)
6168 if (val ==
Fixup.OperandValToReplace)
6169 Fixup.UserInst = NewPN;
6179void LSRInstance::Rewrite(
const LSRUse &LU,
const LSRFixup &LF,
6181 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
6185 RewriteForPHI(PN, LU, LF,
F, DeadInsts);
6193 if (FullV->
getType() != OpTy &&
6194 !(LU.Kind == LSRUse::ICmpZero && OpTy->
isPointerTy())) {
6206 if (LU.Kind == LSRUse::ICmpZero)
6222 const LSRFixup &Fixup,
const LSRUse &LU,
6226 if (LU.Kind != LSRUse::Address)
6227 return IVIncInsertPos;
6231 Type *Ty =
I->getType();
6234 return IVIncInsertPos;
6241 return IVIncInsertPos;
6248void LSRInstance::ImplementSolution(
6249 const SmallVectorImpl<const Formula *> &Solution) {
6255 for (
const IVChain &Chain : IVChainVec) {
6261 for (
size_t LUIdx = 0, NumUses =
Uses.size(); LUIdx != NumUses; ++LUIdx)
6262 for (
const LSRFixup &Fixup :
Uses[LUIdx].Fixups) {
6265 Rewriter.setIVIncInsertPos(L, InsertPos);
6266 Rewrite(
Uses[LUIdx], Fixup, *Solution[LUIdx], DeadInsts);
6270 auto InsertedInsts = InsertedNonLCSSAInsts.takeVector();
6273 for (
const IVChain &Chain : IVChainVec) {
6274 GenerateIVChain(Chain, DeadInsts);
6278 for (
const WeakVH &
IV :
Rewriter.getInsertedIVs())
6296 for (PHINode &PN :
L->getHeader()->phis()) {
6297 BinaryOperator *BO =
nullptr;
6303 case Instruction::Sub:
6308 case Instruction::Add:
6325 [&](Use &U) {return DT.dominates(IVIncInsertPos, U);}))
6334LSRInstance::LSRInstance(
const ScalarOptions &Opts,
Loop *L, IVUsers &IU,
6335 ScalarEvolution &SE, DominatorTree &DT, LoopInfo &LI,
6336 const TargetTransformInfo &
TTI, AssumptionCache &AC,
6337 TargetLibraryInfo &
TLI, MemorySSAUpdater *MSSAU,
6339 : Opts(Opts), IU(IU), SE(SE), DT(DT), LI(LI), AC(AC),
TLI(
TLI),
TTI(
TTI),
6340 L(
L), MSSAU(MSSAU), AMK(Opts.lsr_preferred_addressing_mode.value_or(
6341 TTI.getPreferredAddressingMode(
L, &SE))),
6342 Rewriter(SE,
"lsr", PreserveLCSSA), ShouldPreserveLCSSA(PreserveLCSSA),
6343 BaselineCost(Opts,
L, SE,
TTI, AMK) {
6345 if (!
L->isLoopSimplifyForm())
6353 unsigned NumUsers = 0;
6357 LLVM_DEBUG(
dbgs() <<
"LSR skipping loop, too many IV Users in " << U
6365 auto FirstNonPHI = PN->
getParent()->getFirstNonPHIIt();
6375 L->getHeader()->printAsOperand(
dbgs(),
false);
6381 HardwareLoopProfitable =
6382 TTI.isHardwareLoopProfitable(L, SE, AC, &
TLI, HWLoopInfo);
6386#if LLVM_ENABLE_ABI_BREAKING_CHECKS
6389 Rewriter.disableCanonicalMode();
6390 Rewriter.enableLSRMode();
6394 OptimizeLoopTermCond();
6397 if (IU.empty())
return;
6400 if (!
L->isInnermost()) {
6413 CollectInterestingTypesAndFactors();
6414 CollectFixupsAndInitialFormulae();
6415 CollectLoopInvariantFixupsAndFormulae();
6421 print_uses(
dbgs()));
6423 BaselineCost.print(
dbgs());
dbgs() <<
"\n");
6427 GenerateAllReuseFormulae();
6429 FilterOutUndesirableDedicatedRegisters();
6430 NarrowSearchSpaceUsingHeuristics();
6440 if (Solution.
empty())
6445 for (
const LSRUse &LU :
Uses) {
6446 for (
const Formula &
F : LU.Formulae)
6448 F) &&
"Illegal formula generated!");
6453 ImplementSolution(Solution);
6456#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6457void LSRInstance::print_factors_and_types(
raw_ostream &OS)
const {
6458 if (Factors.empty() &&
Types.empty())
return;
6460 OS <<
"LSR has identified the following interesting factors and types: ";
6463 for (int64_t Factor : Factors)
6464 OS <<
LS <<
'*' << Factor;
6466 for (
Type *Ty : Types)
6467 OS <<
LS <<
'(' << *Ty <<
')';
6471void LSRInstance::print_fixups(raw_ostream &OS)
const {
6472 OS <<
"LSR is examining the following fixup sites:\n";
6473 for (
const LSRUse &LU :
Uses)
6474 for (
const LSRFixup &LF : LU.Fixups) {
6481void LSRInstance::print_uses(raw_ostream &OS)
const {
6482 OS <<
"LSR is examining the following uses:\n";
6483 for (
const LSRUse &LU :
Uses) {
6487 for (
const Formula &
F : LU.Formulae) {
6495void LSRInstance::print(raw_ostream &OS)
const {
6496 print_factors_and_types(OS);
6508class LoopStrengthReduce :
public LoopPass {
6512 LoopStrengthReduce();
6515 bool runOnLoop(
Loop *L, LPPassManager &LPM)
override;
6516 void getAnalysisUsage(AnalysisUsage &AU)
const override;
6521LoopStrengthReduce::LoopStrengthReduce() : LoopPass(
ID) {
6525void LoopStrengthReduce::getAnalysisUsage(
AnalysisUsage &AU)
const {
6552ToDwarfOpIter(SmallVectorImpl<uint64_t> &Expr) {
6553 llvm::DIExpression::expr_op_iterator Begin =
6554 llvm::DIExpression::expr_op_iterator(Expr.
begin());
6555 llvm::DIExpression::expr_op_iterator
End =
6556 llvm::DIExpression::expr_op_iterator(Expr.
end());
6557 return {Begin,
End};
6560struct SCEVDbgValueBuilder {
6561 SCEVDbgValueBuilder() =
default;
6562 SCEVDbgValueBuilder(
const SCEVDbgValueBuilder &
Base) { clone(
Base); }
6564 void clone(
const SCEVDbgValueBuilder &
Base) {
6565 LocationOps =
Base.LocationOps;
6570 LocationOps.
clear();
6577 SmallVector<Value *, 2> LocationOps;
6587 unsigned ArgIndex = 0;
6588 if (It != LocationOps.
end()) {
6589 ArgIndex = std::distance(LocationOps.
begin(), It);
6591 ArgIndex = LocationOps.
size();
6597 void pushValue(
const SCEVUnknown *U) {
6602 bool pushConst(
const SCEVConstant *
C) {
6603 if (
C->getAPInt().getSignificantBits() > 64)
6605 Expr.
push_back(llvm::dwarf::DW_OP_consts);
6606 Expr.
push_back(
C->getAPInt().getSExtValue());
6613 return ToDwarfOpIter(Expr);
6618 bool pushArithmeticExpr(
const llvm::SCEVCommutativeExpr *CommExpr,
6621 "Expected arithmetic SCEV type");
6623 unsigned EmitOperator = 0;
6624 for (
const auto &
Op : CommExpr->
operands()) {
6627 if (EmitOperator >= 1)
6628 pushOperator(DwarfOp);
6635 bool pushCast(
const llvm::SCEVCastExpr *
C,
bool IsSigned) {
6636 const llvm::SCEV *Inner =
C->getOperand(0);
6637 const llvm::Type *
Type =
C->getType();
6639 bool Success = pushSCEV(Inner);
6641 IsSigned ? llvm::dwarf::DW_ATE_signed
6642 : llvm::dwarf::DW_ATE_unsigned};
6643 for (
const auto &
Op : CastOps)
6649 bool pushSCEV(
const llvm::SCEV *S) {
6652 Success &= pushConst(StartInt);
6657 pushLocation(
U->getValue());
6660 Success &= pushArithmeticExpr(MulRec, llvm::dwarf::DW_OP_mul);
6663 Success &= pushSCEV(UDiv->getLHS());
6664 Success &= pushSCEV(UDiv->getRHS());
6665 pushOperator(llvm::dwarf::DW_OP_div);
6671 "Unexpected cast type in SCEV.");
6675 Success &= pushArithmeticExpr(AddExpr, llvm::dwarf::DW_OP_plus);
6690 bool isIdentityFunction(
uint64_t Op,
const SCEV *S) {
6692 if (
C->getAPInt().getSignificantBits() > 64)
6694 int64_t
I =
C->getAPInt().getSExtValue();
6696 case llvm::dwarf::DW_OP_plus:
6697 case llvm::dwarf::DW_OP_minus:
6699 case llvm::dwarf::DW_OP_mul:
6700 case llvm::dwarf::DW_OP_div:
6713 bool SCEVToValueExpr(
const llvm::SCEVAddRecExpr &SAR, ScalarEvolution &SE) {
6719 if (!isIdentityFunction(llvm::dwarf::DW_OP_mul, Stride)) {
6720 if (!pushSCEV(Stride))
6722 pushOperator(llvm::dwarf::DW_OP_mul);
6724 if (!isIdentityFunction(llvm::dwarf::DW_OP_plus, Start)) {
6725 if (!pushSCEV(Start))
6727 pushOperator(llvm::dwarf::DW_OP_plus);
6733 void createOffsetExpr(int64_t
Offset,
Value *OffsetValue) {
6734 pushLocation(OffsetValue);
6737 dbgs() <<
"scev-salvage: Generated IV offset expression. Offset: "
6738 << std::to_string(
Offset) <<
"\n");
6744 bool createIterCountExpr(
const SCEV *S,
6745 const SCEVDbgValueBuilder &IterationCount,
6746 ScalarEvolution &SE) {
6755 LLVM_DEBUG(
dbgs() <<
"scev-salvage: Location to salvage SCEV: " << *S
6759 if (!Rec->isAffine())
6767 clone(IterationCount);
6768 if (!SCEVToValueExpr(*Rec, SE))
6779 bool SCEVToIterCountExpr(
const llvm::SCEVAddRecExpr &SAR,
6780 ScalarEvolution &SE) {
6786 if (!isIdentityFunction(llvm::dwarf::DW_OP_minus, Start)) {
6787 if (!pushSCEV(Start))
6789 pushOperator(llvm::dwarf::DW_OP_minus);
6791 if (!isIdentityFunction(llvm::dwarf::DW_OP_div, Stride)) {
6792 if (!pushSCEV(Stride))
6794 pushOperator(llvm::dwarf::DW_OP_div);
6802 void appendToVectors(SmallVectorImpl<uint64_t> &DestExpr,
6803 SmallVectorImpl<Value *> &DestLocations) {
6805 "Expected the locations vector to contain the IV");
6810 "Expected the location ops to contain the IV.");
6814 for (
const auto &
Op : LocationOps) {
6815 auto It =
find(DestLocations,
Op);
6816 if (It != DestLocations.
end()) {
6818 DestIndexMap.
push_back(std::distance(DestLocations.
begin(), It));
6826 for (
const auto &
Op : expr_ops()) {
6829 Op.appendToVector(DestExpr);
6836 uint64_t NewIndex = DestIndexMap[Arg.getIndex()];
6844struct DVIRecoveryRec {
6845 DVIRecoveryRec(DbgVariableRecord *DVR)
6846 : DbgRef(DVR), Expr(DVR->getExpression()), HadLocationArgList(
false) {}
6848 DbgVariableRecord *DbgRef;
6850 bool HadLocationArgList;
6856 for (
auto &RE : RecoveryExprs)
6858 RecoveryExprs.clear();
6861 ~DVIRecoveryRec() { clear(); }
6869 auto expr_ops = ToDwarfOpIter(Expr);
6871 for (
auto Op : expr_ops)
6880template <
typename T>
6884 "contain any DW_OP_llvm_arg operands.");
6890template <
typename T>
6895 "Expected expression that references DIArglist locations using "
6896 "DW_OP_llvm_arg operands.");
6898 for (
Value *
V : Locations)
6915 if (NumLLVMArgs == 0) {
6922 "Lone LLVM_arg in a DIExpression should refer to location-op 0.");
6952 LLVM_DEBUG(
dbgs() <<
"scev-salvage: restore dbg.value to pre-LSR state\n"
6953 <<
"scev-salvage: post-LSR: " << *DbgVal <<
'\n');
6954 assert(DVIRec.Expr &&
"Expected an expression");
6959 if (!DVIRec.HadLocationArgList) {
6960 assert(DVIRec.LocationOps.size() == 1 &&
6961 "Unexpected number of location ops.");
6965 Value *CachedValue =
6970 for (
WeakVH VH : DVIRec.LocationOps) {
6978 LLVM_DEBUG(
dbgs() <<
"scev-salvage: pre-LSR: " << *DbgVal <<
'\n');
6983 const SCEV *SCEVInductionVar,
6984 SCEVDbgValueBuilder IterCountExpr) {
6998 LocationOpIndexMap.
assign(DVIRec.LocationOps.size(), -1);
7000 NewLocationOps.
push_back(LSRInductionVar);
7002 for (
unsigned i = 0; i < DVIRec.LocationOps.size(); i++) {
7003 WeakVH VH = DVIRec.LocationOps[i];
7009 LocationOpIndexMap[i] = NewLocationOps.
size() - 1;
7011 <<
" now at index " << LocationOpIndexMap[i] <<
"\n");
7019 LLVM_DEBUG(
dbgs() <<
"scev-salvage: SCEV for location at index: " << i
7020 <<
" refers to a location that is now undef or erased. "
7021 "Salvage abandoned.\n");
7025 LLVM_DEBUG(
dbgs() <<
"scev-salvage: salvaging location at index " << i
7026 <<
" with SCEV: " << *DVIRec.SCEVs[i] <<
"\n");
7028 DVIRec.RecoveryExprs[i] = std::make_unique<SCEVDbgValueBuilder>();
7029 SCEVDbgValueBuilder *SalvageExpr = DVIRec.RecoveryExprs[i].get();
7033 if (std::optional<APInt>
Offset =
7035 if (
Offset->getSignificantBits() <= 64)
7036 SalvageExpr->createOffsetExpr(
Offset->getSExtValue(), LSRInductionVar);
7039 }
else if (!SalvageExpr->createIterCountExpr(DVIRec.SCEVs[i], IterCountExpr,
7048 assert(DVIRec.RecoveryExprs.size() == 1 &&
7049 "Expected only a single recovery expression for an empty "
7051 assert(DVIRec.RecoveryExprs[0] &&
7052 "Expected a SCEVDbgSalvageBuilder for location 0");
7053 SCEVDbgValueBuilder *
B = DVIRec.RecoveryExprs[0].get();
7054 B->appendToVectors(
NewExpr, NewLocationOps);
7056 for (
const auto &
Op : DVIRec.Expr->
expr_ops()) {
7064 uint64_t LocationArgIndex = Arg.getIndex();
7065 SCEVDbgValueBuilder *DbgBuilder =
7066 DVIRec.RecoveryExprs[LocationArgIndex].get();
7072 assert(LocationOpIndexMap[LocationArgIndex] != -1 &&
7073 "Expected a positive index for the location-op position.");
7074 NewExpr.push_back(LocationOpIndexMap[LocationArgIndex]);
7078 DbgBuilder->appendToVectors(
NewExpr, NewLocationOps);
7082 LLVM_DEBUG(
dbgs() <<
"scev-salvage: Updated DVI: " << *DVIRec.DbgRef <<
"\n");
7090 SmallVector<std::unique_ptr<DVIRecoveryRec>, 2> &DVIToUpdate) {
7091 if (DVIToUpdate.empty())
7095 assert(SCEVInductionVar &&
7096 "Anticipated a SCEV for the post-LSR induction variable");
7100 if (!IVAddRec->isAffine())
7108 SCEVDbgValueBuilder IterCountExpr;
7109 IterCountExpr.pushLocation(LSRInductionVar);
7110 if (!IterCountExpr.SCEVToIterCountExpr(*IVAddRec, SE))
7113 LLVM_DEBUG(
dbgs() <<
"scev-salvage: IV SCEV: " << *SCEVInductionVar
7116 for (
auto &DVIRec : DVIToUpdate) {
7117 SalvageDVI(L, SE, LSRInductionVar, *DVIRec, SCEVInductionVar,
7128 SmallVector<std::unique_ptr<DVIRecoveryRec>, 2> &SalvageableDVISCEVs) {
7129 for (
const auto &
B : L->getBlocks()) {
7130 for (
auto &
I : *
B) {
7132 if (!DbgVal.isDbgValue() && !DbgVal.isDbgAssign())
7137 if (DbgVal.isKillLocation())
7142 const auto &HasTranslatableLocationOps =
7144 for (
const auto LocOp : DbgValToTranslate.location_ops()) {
7158 if (!HasTranslatableLocationOps(DbgVal))
7161 std::unique_ptr<DVIRecoveryRec> NewRec =
7162 std::make_unique<DVIRecoveryRec>(&DbgVal);
7166 NewRec->RecoveryExprs.resize(DbgVal.getNumVariableLocationOps());
7167 for (
const auto LocOp : DbgVal.location_ops()) {
7168 NewRec->SCEVs.push_back(SE.
getSCEV(LocOp));
7169 NewRec->LocationOps.push_back(LocOp);
7170 NewRec->HadLocationArgList = DbgVal.hasArgList();
7172 SalvageableDVISCEVs.push_back(std::move(NewRec));
7182 const LSRInstance &LSR) {
7184 auto IsSuitableIV = [&](
PHINode *
P) {
7195 for (
const WeakVH &
IV : LSR.getScalarEvolutionIVs()) {
7202 if (IsSuitableIV(
P))
7206 for (
PHINode &
P : L.getHeader()->phis()) {
7207 if (IsSuitableIV(&
P))
7226 std::unique_ptr<MemorySSAUpdater> MSSAU;
7228 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
7231 const LSRInstance &Reducer = LSRInstance(Opts, L, IU, SE, DT, LI,
TTI, AC,
7232 TLI, MSSAU.get(), PreserveLCSSA);
7233 Changed |= Reducer.getChanged();
7237 if (Opts.enable_lsr_phielim && L->isLoopSimplifyForm()) {
7240#if LLVM_ENABLE_ABI_BREAKING_CHECKS
7243 unsigned numFolded = Rewriter.replaceCongruentIVs(L, &DT, DeadInsts, &
TTI);
7257 L->getUniqueExitBlocks(ExitBlocks);
7266 if (L->isRecursivelyLCSSAForm(DT, LI) && L->getExitBlock()) {
7280 if (SalvageableDVIRecords.
empty())
7286 for (
const auto &L : LI) {
7290 LLVM_DEBUG(
dbgs() <<
"scev-salvage: SCEV salvaging not possible. An IV "
7291 "could not be identified.\n");
7295 for (
auto &Rec : SalvageableDVIRecords)
7297 SalvageableDVIRecords.
clear();
7301bool LoopStrengthReduce::runOnLoop(
Loop *L, LPPassManager & ) {
7305 auto &IU = getAnalysis<IVUsersWrapperPass>().getIU();
7306 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
7307 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
7308 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
7309 const auto &
TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
7310 *
L->getHeader()->getParent());
7311 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
7312 *
L->getHeader()->getParent());
7313 auto &
TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
7314 *
L->getHeader()->getParent());
7315 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
7318 MSSA = &MSSAAnalysis->getMSSA();
7337char LoopStrengthReduce::ID = 0;
7340 "Loop Strength Reduction",
false,
false)
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AArch64 Predicate As Counter Loop Rewrites
This file implements a class to represent arbitrary precision integral constant values and operations...
Function Alias Analysis false
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static bool isAddressUse(const Use &U)
The use is the address of the function as a value.
static const Function * getParent(const Value *V)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
early cse Early CSE w MemorySSA
Module.h This file contains the declarations for the Module class.
This defines the Use class.
iv Induction Variable Users
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
SmallVector< MachineOperand, 4 > Cond
This header provides classes for managing per-loop analyses.
static bool SalvageDVI(llvm::Loop *L, ScalarEvolution &SE, llvm::PHINode *LSRInductionVar, DVIRecoveryRec &DVIRec, const SCEV *SCEVInductionVar, SCEVDbgValueBuilder IterCountExpr)
static Value * getWideOperand(Value *Oper)
IVChain logic must consistently peek base TruncInst operands, so wrap it in a convenient helper.
static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE)
Return true if the given add can be sign-extended without changing its value.
static bool stressIVChain(const ScalarOptions &Opts)
static bool mayUsePostIncMode(const TargetTransformInfo &TTI, LSRUse &LU, const SCEV *S, const Loop *L, ScalarEvolution &SE)
Return true if the SCEV represents a value that may end up as a post-increment operation.
static void restorePreTransformState(DVIRecoveryRec &DVIRec)
Restore the DVI's pre-LSR arguments. Substitute undef for any erased values.
static bool containsAddRecDependentOnLoop(const SCEV *S, const Loop &L)
static User::op_iterator findIVOperand(User::op_iterator OI, User::op_iterator OE, Loop *L, ScalarEvolution &SE)
Helper for CollectChains that finds an IV operand (computed by an AddRec in this loop) within [OI,...
static bool isLegalUse(const TargetTransformInfo &TTI, Immediate MinOffset, Immediate MaxOffset, LSRUse::KindType Kind, MemAccessTy AccessTy, GlobalValue *BaseGV, Immediate BaseOffset, bool HasBaseReg, int64_t Scale)
Test whether we know how to expand the current formula.
static void DbgGatherSalvagableDVI(Loop *L, ScalarEvolution &SE, SmallVector< std::unique_ptr< DVIRecoveryRec >, 2 > &SalvageableDVISCEVs)
Identify and cache salvageable DVI locations and expressions along with the corresponding SCEV(s).
static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE)
Return true if the given mul can be sign-extended without changing its value.
static const unsigned MaxSCEVSalvageExpressionSize
Limit the size of expression that SCEV-based salvaging will attempt to translate into a DIExpression.
static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE)
Return true if this AddRec is already a phi in its loop.
static InstructionCost getScalingFactorCost(const TargetTransformInfo &TTI, const LSRUse &LU, const Formula &F, const Loop &L)
static void DoInitialMatch(const SCEV *S, Loop *L, SmallVectorImpl< SCEVUse > &Good, SmallVectorImpl< SCEVUse > &Bad, ScalarEvolution &SE)
Recursion helper for initialMatch.
static void updateDVIWithLocation(T &DbgVal, Value *Location, SmallVectorImpl< uint64_t > &Ops)
Overwrites DVI with the location and Ops as the DIExpression.
static bool ReduceLoopStrength(Loop *L, IVUsers &IU, ScalarEvolution &SE, DominatorTree &DT, LoopInfo &LI, const TargetTransformInfo &TTI, AssumptionCache &AC, TargetLibraryInfo &TLI, MemorySSA *MSSA, bool PreserveLCSSA)
static bool isLegalAddImmediate(const TargetTransformInfo &TTI, Immediate Offset)
static Instruction * getFixupInsertPos(const TargetTransformInfo &TTI, const LSRFixup &Fixup, const LSRUse &LU, Instruction *IVIncInsertPos, DominatorTree &DT)
static const SCEV * getExprBase(const SCEV *S)
Return an approximation of this SCEV expression's "base", or NULL for any constant.
static llvm::PHINode * GetInductionVariable(const Loop &L, ScalarEvolution &SE, const LSRInstance &LSR)
Ideally pick the PHI IV inserted by ScalarEvolutionExpander.
static bool IsSimplerBaseSCEVForTarget(const TargetTransformInfo &TTI, ScalarEvolution &SE, const SCEV *Best, const SCEV *Reg, MemAccessTy AccessType)
static const unsigned MaxIVUsers
MaxIVUsers is an arbitrary threshold that provides an early opportunity for bail out.
static Immediate extractImmediate(const ScalarOptions &Opts, SCEVUse &S, ScalarEvolution &SE, bool PreferScalable=false)
If S involves the addition of a constant integer value, return that integer value,...
static bool isHighCostExpansion(const SCEV *S, SmallPtrSetImpl< const SCEV * > &Processed, ScalarEvolution &SE)
Check if expanding this expression is likely to incur significant cost.
static Value * getValueOrPoison(WeakVH &VH, LLVMContext &C)
Cached location ops may be erased during LSR, in which case a poison is required when restoring from ...
static MemAccessTy getAccessType(const TargetTransformInfo &TTI, Instruction *Inst, Value *OperandVal)
Return the type of the memory being accessed.
static unsigned numLLVMArgOps(SmallVectorImpl< uint64_t > &Expr)
Returns the total number of DW_OP_llvm_arg operands in the expression.
static bool isAlwaysFoldable(const ScalarOptions &Opts, const TargetTransformInfo &TTI, LSRUse::KindType Kind, MemAccessTy AccessTy, GlobalValue *BaseGV, Immediate BaseOffset, bool HasBaseReg)
static void DbgRewriteSalvageableDVIs(llvm::Loop *L, ScalarEvolution &SE, llvm::PHINode *LSRInductionVar, SmallVector< std::unique_ptr< DVIRecoveryRec >, 2 > &DVIToUpdate)
Obtain an expression for the iteration count, then attempt to salvage the dbg.value intrinsics.
static void UpdateDbgValue(DVIRecoveryRec &DVIRec, SmallVectorImpl< Value * > &NewLocationOps, SmallVectorImpl< uint64_t > &NewExpr)
Write the new expression and new location ops for the dbg.value.
static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE)
Return true if the given addrec can be sign-extended without changing its value.
static bool isAMCompletelyFolded(const TargetTransformInfo &TTI, const LSRUse &LU, const Formula &F)
Check if the addressing mode defined by F is completely folded in LU at isel time.
static Immediate extractImmediateOperand(const ScalarOptions &Opts, MutableArrayRef< SCEVUse > Ops, ScalarEvolution &SE, bool PreferScalable)
Extracts an immediate operand from Ops and replaces the operand with zero.
static void updateDVIWithLocations(T &DbgVal, SmallVectorImpl< Value * > &Locations, SmallVectorImpl< uint64_t > &Ops)
Overwrite DVI with locations placed into a DIArglist.
static bool canFoldIVIncExpr(const ScalarOptions &Opts, const SCEV *IncExpr, Instruction *UserInst, Value *Operand, const TargetTransformInfo &TTI)
Return true if the IVInc can be folded into an addressing mode.
static GlobalValue * ExtractSymbol(SCEVUse &S, ScalarEvolution &SE)
If S involves the addition of a GlobalValue address, return that symbol, and mutate S to point to a n...
static bool isProfitableChain(IVChain &Chain, SmallPtrSetImpl< Instruction * > &Users, ScalarEvolution &SE, const TargetTransformInfo &TTI)
Return true if the number of registers needed for the chain is estimated to be less than the number r...
static const SCEV * CollectSubexprs(const SCEV *S, const SCEVConstant *C, SmallVectorImpl< const SCEV * > &Ops, const Loop *L, ScalarEvolution &SE, unsigned Depth=0)
Split S into subexpressions which can be pulled out into separate registers.
static const SCEV * getExactSDiv(const SCEV *LHS, const SCEV *RHS, ScalarEvolution &SE, bool IgnoreSignificantBits=false)
Return an expression for LHS /s RHS, if it can be determined and if the remainder is known to be zero...
static const SCEV * getAnyExtendConsideringPostIncUses(ArrayRef< PostIncLoopSet > Loops, const SCEV *Expr, Type *ToTy, ScalarEvolution &SE)
Extend/Truncate Expr to ToTy considering post-inc uses in Loops.
static unsigned getSetupCost(const SCEV *Reg, unsigned Depth, const TargetTransformInfo &TTI)
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the PointerIntPair class.
Remove Loads Into Fake Uses
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
SI optimize exec mask operations pre RA
This file implements a set that has insertion order iteration characteristics.
This file implements the SmallBitVector class.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
static SymbolRef::Type getType(const Symbol *Sym)
virt reg Virtual Register Rewriter
static const uint32_t IV[8]
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool isNegative() const
Determine sign of this APInt.
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
unsigned countr_zero() const
Count the number of trailing zero bits.
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
int64_t getSExtValue() const
Get sign extended value.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
InstListType::iterator iterator
Instruction iterators...
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
LLVM_ABI bool isLandingPad() const
Return true if this basic block is a landing pad.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
BinaryOps getOpcode() const
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static LLVM_ABI Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Value * getCondition() const
static LLVM_ABI bool isValueValidForType(Type *Ty, uint64_t V)
This static method returns true if the type Ty is big enough to represent the value V.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
iterator_range< expr_op_iterator > expr_ops() const
static LLVM_ABI DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
unsigned getNumElements() const
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
LLVM_ABI bool isComplex() const
Return whether the location is computed on the expression stack, meaning it cannot be a simple regist...
LLVM_ABI LLVMContext & getContext()
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI bool isKillLocation() const
void setRawLocation(Metadata *NewLocation)
Use of this should generally be avoided; instead, replaceVariableLocationOp and addVariableLocationOp...
void setExpression(DIExpression *NewExpr)
DIExpression * getExpression() const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
PointerType * getType() const
Global values are always pointers.
IVStrideUse - Keep track of one use of a strided induction variable.
void transformToPostInc(const Loop *L)
transformToPostInc - Transform the expression to post-inc form for the given loop.
Value * getOperandValToReplace() const
getOperandValToReplace - Return the Value of the operand in the user instruction that this IVStrideUs...
void setUser(Instruction *NewUser)
setUser - Assign a new user instruction for this use.
Analysis pass that exposes the IVUsers for a loop.
ilist< IVStrideUse >::const_iterator const_iterator
LLVM_ABI void print(raw_ostream &OS) const
CostType getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
LLVM_ABI bool isLifetimeStartOrEnd() const LLVM_READONLY
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI Type * getAccessType() const LLVM_READONLY
Return the type this instruction accesses in memory, if any.
iterator_range< user_iterator > users()
const char * getOpcodeName() const
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
An instruction for reading from memory.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
BlockT * getHeader() const
unsigned getLoopDepth() const
Return the nesting level of this loop.
The legacy pass manager's analysis pass to compute loop information.
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
An analysis that produces MemorySSA for a function.
Encapsulates MemorySSA, including all data associated with memory accesses.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
iterator_range< const_block_iterator > blocks() const
op_range incoming_values()
void setIncomingValue(unsigned i, Value *V)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static unsigned getIncomingValueNumForOperand(unsigned i)
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const Loop * getLoop() const
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
This class uses information about analyze scalars to rewrite expressions in canonical form.
This node represents multiplication of some number of SCEVs.
bool hasNoUnsignedWrap() const
ArrayRef< SCEVUse > operands() const
bool hasNoSignedWrap() const
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
unsigned short getExpressionSize() const
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI ArrayRef< SCEVUse > operands() const
Return operands of this SCEV expression.
Type * getType() const
Return the LLVM type of this SCEV expression.
static constexpr auto FlagNone
SCEVTypes getSCEVType() const
The main scalar evolution driver.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI SCEVUse getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEVFlagsPair Flags)
Get an add recurrence expression for the specified loop.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI const SCEV * getAnyExtendExpr(SCEVUse Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
LLVM_ABI bool containsUndefs(const SCEV *S) const
Return true if the SCEV expression contains an undef value.
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI const SCEV * getVScale(Type *Ty)
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
Return true if the given SCEV changes value in a known way in the specified loop.
LLVM_ABI const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
LLVM_ABI SCEVUse getSignExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
APInt getUnsignedRangeMax(const SCEV *S)
Determine the max of the unsigned range for a particular SCEV.
LLVM_ABI const SCEV * getUnknown(Value *V)
LLVM_ABI std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
LLVM_ABI bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
LLVM_ABI bool containsErasedValue(const SCEV *S) const
Return true if the SCEV expression contains a Value that has been optimised out and is now a nullptr.
LLVMContext & getContext() const
size_type size() const
Determine the number of elements in the SetVector.
iterator end()
Get an iterator to the end of the SetVector.
iterator begin()
Get an iterator to the beginning of the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
iterator_range< const_set_bits_iterator > set_bits() const
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
size_type size() const
Returns the number of bits in this bitvector.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
size_type count() const
Returns the number of bits which are set.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
typename SuperClass::const_iterator const_iterator
typename SuperClass::iterator iterator
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static StackOffset get(int64_t Fixed, int64_t Scalable)
An instruction for storing to memory.
Provides information about what library functions are available for the current target.
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
LLVM_ABI bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
bool isVoidTy() const
Return true if this is 'void'.
void setOperand(unsigned i, Value *Val)
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
iterator_range< user_iterator > users()
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
iterator_range< use_iterator > uses()
A nullable Value handle that is nullable.
std::pair< iterator, bool > insert(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
const ParentTy * getParent() const
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
match_bind< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
bool match(const SCEV *S, const Pattern &P)
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
cst_pred_ty< is_specific_cst > m_scev_SpecificInt(uint64_t V)
Match an SCEV constant with a plain unsigned integer.
@ Init
Initialization function, sets up the coroutine frame.
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
@ DW_OP_LLVM_convert
Only used in LLVM metadata.
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< PhiNode * > Phi
NodeAddr< UseNode * > Use
friend class Instruction
Iterator for Instructions in a `BasicBlock.
LLVM_ABI iterator begin() const
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
unsigned KindType
For isa, dyn_cast, etc operations on TelemetryInfo.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
@ Store
The extracted value is stored (ExtractElement only).
bool operator!=(uint64_t V1, const APInt &V2)
SCEVUseT< const SCEV * > SCEVUse
LLVM_ABI bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, SmallPtrSetImpl< PHINode * > *KnownNonDeadPHIs=nullptr)
Examine each PHI in the given block and delete it if it is dead.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
const ScalarOptions & getScalarOptions(const LLVMContext &C)
LLVM_ABI char & LoopSimplifyID
bool isa_and_nonnull(const Y &Val)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
DomTreeNodeBase< BasicBlock > DomTreeNode
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
LLVM_ABI void initializeLoopStrengthReducePass(PassRegistry &)
auto reverse(ContainerTy &&C)
LLVM_ABI const SCEV * denormalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE)
Denormalize S to be post-increment for all loops present in Loops.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI const SCEV * normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE, bool CheckInvertible=true)
Normalize S to be post-increment for all loops present in Loops.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
DWARFExpression::Operation Op
LLVM_ABI Pass * createLoopStrengthReducePass()
LLVM_ABI BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructionsPermissive(SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow instructions that are not...
constexpr unsigned BitWidth
LLVM_ABI bool formLCSSAForInstructions(SmallVectorImpl< Instruction * > &Worklist, const DominatorTree &DT, const LoopInfo &LI, ScalarEvolution *SE, SmallVectorImpl< PHINode * > *PHIsToRemove=nullptr, SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
SmallPtrSet< const Loop *, 2 > PostIncLoopSet
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, SCEVExpander &Rewriter, DominatorTree *DT, ReplaceExitVal ReplaceExitValue, SmallVector< WeakTrackingVH, 16 > &DeadInsts)
If the final value of any expressions that are recurrent in the loop can be computed,...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
constexpr bool valueOr(BoolOrDefault X, bool Default)
@ End
Run the machine verifier once, at the end of the pipeline.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Attributes of a target dependent hardware loop.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
TargetTransformInfo & TTI
Information about a load/store intrinsic defined by the target.
Value * PtrVal
This is the pointer that the intrinsic is loading from or storing to.
SCEVPtrT getPointer() const