LLVM 23.0.0git
LoopStrengthReduce.cpp
Go to the documentation of this file.
1//===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This transformation analyzes and transforms the induction variables (and
10// computations derived from them) into forms suitable for efficient execution
11// on the target.
12//
13// This pass performs a strength reduction on array references inside loops that
14// have as one or more of their components the loop induction variable, it
15// rewrites expressions to take advantage of scaled-index addressing modes
16// available on the target, and it performs a variety of other optimizations
17// related to loop induction variables.
18//
19// Terminology note: this code has a lot of handling for "post-increment" or
20// "post-inc" users. This is not talking about post-increment addressing modes;
21// it is instead talking about code like this:
22//
23// %i = phi [ 0, %entry ], [ %i.next, %latch ]
24// ...
25// %i.next = add %i, 1
26// %c = icmp eq %i.next, %n
27//
28// The SCEV for %i is {0,+,1}<%L>. The SCEV for %i.next is {1,+,1}<%L>, however
29// it's useful to think about these as the same register, with some uses using
30// the value of the register before the add and some using it after. In this
31// example, the icmp is a post-increment user, since it uses %i.next, which is
32// the value of the induction variable after the increment. The other common
33// case of post-increment users is users outside the loop.
34//
35// TODO: More sophistication in the way Formulae are generated and filtered.
36//
37// TODO: Handle multiple loops at a time.
38//
39// TODO: Should the addressing mode BaseGV be changed to a ConstantExpr instead
40// of a GlobalValue?
41//
42// TODO: When truncation is free, truncate ICmp users' operands to make it a
43// smaller encoding (on x86 at least).
44//
45// TODO: When a negated register is used by an add (such as in a list of
46// multiple base registers, or as the increment expression in an addrec),
47// we may not actually need both reg and (-1 * reg) in registers; the
48// negation can be implemented by using a sub instead of an add. The
49// lack of support for taking this into consideration when making
50// register pressure decisions is partly worked around by the "Special"
51// use kind.
52//
53//===----------------------------------------------------------------------===//
54
56#include "llvm/ADT/APInt.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/DenseSet.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SetVector.h"
64#include "llvm/ADT/SmallSet.h"
66#include "llvm/ADT/Statistic.h"
84#include "llvm/IR/BasicBlock.h"
85#include "llvm/IR/Constant.h"
86#include "llvm/IR/Constants.h"
89#include "llvm/IR/Dominators.h"
90#include "llvm/IR/GlobalValue.h"
91#include "llvm/IR/IRBuilder.h"
92#include "llvm/IR/InstrTypes.h"
93#include "llvm/IR/Instruction.h"
96#include "llvm/IR/Module.h"
97#include "llvm/IR/Operator.h"
98#include "llvm/IR/Type.h"
99#include "llvm/IR/Use.h"
100#include "llvm/IR/User.h"
101#include "llvm/IR/Value.h"
102#include "llvm/IR/ValueHandle.h"
104#include "llvm/Pass.h"
105#include "llvm/Support/Casting.h"
108#include "llvm/Support/Debug.h"
118#include <algorithm>
119#include <cassert>
120#include <cstddef>
121#include <cstdint>
122#include <iterator>
123#include <limits>
124#include <map>
125#include <numeric>
126#include <optional>
127#include <utility>
128
129using namespace llvm;
130using namespace SCEVPatternMatch;
131
132#define DEBUG_TYPE "loop-reduce"
133
134/// MaxIVUsers is an arbitrary threshold that provides an early opportunity for
135/// bail out. This threshold is far beyond the number of users that LSR can
136/// conceivably solve, so it should not affect generated code, but catches the
137/// worst cases before LSR burns too much compile time and stack space.
138static const unsigned MaxIVUsers = 200;
139
140/// Limit the size of expression that SCEV-based salvaging will attempt to
141/// translate into a DIExpression.
142/// Choose a maximum size such that debuginfo is not excessively increased and
143/// the salvaging is not too expensive for the compiler.
144static const unsigned MaxSCEVSalvageExpressionSize = 64;
145
146// Cleanup congruent phis after LSR phi expansion.
148 "enable-lsr-phielim", cl::Hidden, cl::init(true),
149 cl::desc("Enable LSR phi elimination"));
150
151// The flag adds instruction count to solutions cost comparison.
153 "lsr-insns-cost", cl::Hidden, cl::init(true),
154 cl::desc("Add instruction count to a LSR cost model"));
155
156// Flag to choose how to narrow complex lsr solution
158 "lsr-exp-narrow", cl::Hidden, cl::init(false),
159 cl::desc("Narrow LSR complex solution using"
160 " expectation of registers number"));
161
162// Flag to narrow search space by filtering non-optimal formulae with
163// the same ScaledReg and Scale.
165 "lsr-filter-same-scaled-reg", cl::Hidden, cl::init(true),
166 cl::desc("Narrow LSR search space by filtering non-optimal formulae"
167 " with the same ScaledReg and Scale"));
168
170 "lsr-preferred-addressing-mode", cl::Hidden, cl::init(TTI::AMK_None),
171 cl::desc("A flag that overrides the target's preferred addressing mode."),
173 clEnumValN(TTI::AMK_None, "none", "Don't prefer any addressing mode"),
174 clEnumValN(TTI::AMK_PreIndexed, "preindexed",
175 "Prefer pre-indexed addressing mode"),
176 clEnumValN(TTI::AMK_PostIndexed, "postindexed",
177 "Prefer post-indexed addressing mode"),
178 clEnumValN(TTI::AMK_All, "all", "Consider all addressing modes")));
179
181 "lsr-complexity-limit", cl::Hidden,
182 cl::init(std::numeric_limits<uint16_t>::max()),
183 cl::desc("LSR search space complexity limit"));
184
186 "lsr-setupcost-depth-limit", cl::Hidden, cl::init(7),
187 cl::desc("The limit on recursion depth for LSRs setup cost"));
188
190 "lsr-drop-solution", cl::Hidden,
191 cl::desc("Attempt to drop solution if it is less profitable"));
192
194 "lsr-enable-vscale-immediates", cl::Hidden, cl::init(true),
195 cl::desc("Enable analysis of vscale-relative immediates in LSR"));
196
198 "lsr-drop-scaled-reg-for-vscale", cl::Hidden, cl::init(true),
199 cl::desc("Avoid using scaled registers with vscale-relative addressing"));
200
201#ifndef NDEBUG
202// Stress test IV chain generation.
204 "stress-ivchain", cl::Hidden, cl::init(false),
205 cl::desc("Stress test LSR IV chains"));
206#else
207static bool StressIVChain = false;
208#endif
209
210namespace {
211
212struct MemAccessTy {
213 /// Used in situations where the accessed memory type is unknown.
214 static const unsigned UnknownAddressSpace =
215 std::numeric_limits<unsigned>::max();
216
217 Type *MemTy = nullptr;
218 unsigned AddrSpace = UnknownAddressSpace;
219
220 MemAccessTy() = default;
221 MemAccessTy(Type *Ty, unsigned AS) : MemTy(Ty), AddrSpace(AS) {}
222
223 bool operator==(MemAccessTy Other) const {
224 return MemTy == Other.MemTy && AddrSpace == Other.AddrSpace;
225 }
226
227 bool operator!=(MemAccessTy Other) const { return !(*this == Other); }
228
229 static MemAccessTy getUnknown(LLVMContext &Ctx,
230 unsigned AS = UnknownAddressSpace) {
231 return MemAccessTy(Type::getVoidTy(Ctx), AS);
232 }
233
234 Type *getType() { return MemTy; }
235};
236
237/// This class holds data which is used to order reuse candidates.
238class RegSortData {
239public:
240 /// This represents the set of LSRUse indices which reference
241 /// a particular register.
242 SmallBitVector UsedByIndices;
243
244 void print(raw_ostream &OS) const;
245 void dump() const;
246};
247
248// An offset from an address that is either scalable or fixed. Used for
249// per-target optimizations of addressing modes.
250class Immediate : public details::FixedOrScalableQuantity<Immediate, int64_t> {
251 constexpr Immediate(ScalarTy MinVal, bool Scalable)
252 : FixedOrScalableQuantity(MinVal, Scalable) {}
253
254 constexpr Immediate(const FixedOrScalableQuantity<Immediate, int64_t> &V)
255 : FixedOrScalableQuantity(V) {}
256
257public:
258 constexpr Immediate() = delete;
259
260 static constexpr Immediate getFixed(ScalarTy MinVal) {
261 return {MinVal, false};
262 }
263 static constexpr Immediate getScalable(ScalarTy MinVal) {
264 return {MinVal, true};
265 }
266 static constexpr Immediate get(ScalarTy MinVal, bool Scalable) {
267 return {MinVal, Scalable};
268 }
269 static constexpr Immediate getZero() { return {0, false}; }
270 static constexpr Immediate getFixedMin() {
271 return {std::numeric_limits<int64_t>::min(), false};
272 }
273 static constexpr Immediate getFixedMax() {
274 return {std::numeric_limits<int64_t>::max(), false};
275 }
276 static constexpr Immediate getScalableMin() {
277 return {std::numeric_limits<int64_t>::min(), true};
278 }
279 static constexpr Immediate getScalableMax() {
280 return {std::numeric_limits<int64_t>::max(), true};
281 }
282
283 constexpr bool isLessThanZero() const { return Quantity < 0; }
284
285 constexpr bool isGreaterThanZero() const { return Quantity > 0; }
286
287 constexpr bool isCompatibleImmediate(const Immediate &Imm) const {
288 return isZero() || Imm.isZero() || Imm.Scalable == Scalable;
289 }
290
291 constexpr bool isMin() const {
292 return Quantity == std::numeric_limits<ScalarTy>::min();
293 }
294
295 constexpr bool isMax() const {
296 return Quantity == std::numeric_limits<ScalarTy>::max();
297 }
298
299 // Arithmetic 'operators' that cast to unsigned types first.
300 constexpr Immediate addUnsigned(const Immediate &RHS) const {
301 assert(isCompatibleImmediate(RHS) && "Incompatible Immediates");
302 ScalarTy Value = (uint64_t)Quantity + RHS.getKnownMinValue();
303 return {Value, Scalable || RHS.isScalable()};
304 }
305
306 constexpr Immediate subUnsigned(const Immediate &RHS) const {
307 assert(isCompatibleImmediate(RHS) && "Incompatible Immediates");
308 ScalarTy Value = (uint64_t)Quantity - RHS.getKnownMinValue();
309 return {Value, Scalable || RHS.isScalable()};
310 }
311
312 // Scale the quantity by a constant without caring about runtime scalability.
313 constexpr Immediate mulUnsigned(const ScalarTy RHS) const {
314 ScalarTy Value = (uint64_t)Quantity * RHS;
315 return {Value, Scalable};
316 }
317
318 // Helpers for generating SCEVs with vscale terms where needed.
319 const SCEV *getSCEV(ScalarEvolution &SE, Type *Ty) const {
320 const SCEV *S = SE.getConstant(Ty, Quantity);
321 if (Scalable)
322 S = SE.getMulExpr(S, SE.getVScale(S->getType()));
323 return S;
324 }
325
326 const SCEV *getNegativeSCEV(ScalarEvolution &SE, Type *Ty) const {
327 const SCEV *NegS = SE.getConstant(Ty, -(uint64_t)Quantity);
328 if (Scalable)
329 NegS = SE.getMulExpr(NegS, SE.getVScale(NegS->getType()));
330 return NegS;
331 }
332
333 const SCEV *getUnknownSCEV(ScalarEvolution &SE, Type *Ty) const {
334 // TODO: Avoid implicit trunc?
335 // See https://github.com/llvm/llvm-project/issues/112510.
336 const SCEV *SU = SE.getUnknown(
337 ConstantInt::getSigned(Ty, Quantity, /*ImplicitTrunc=*/true));
338 if (Scalable)
339 SU = SE.getMulExpr(SU, SE.getVScale(SU->getType()));
340 return SU;
341 }
342};
343
344// This is needed for the Compare type of std::map when Immediate is used
345// as a key. We don't need it to be fully correct against any value of vscale,
346// just to make sure that vscale-related terms in the map are considered against
347// each other rather than being mixed up and potentially missing opportunities.
348struct KeyOrderTargetImmediate {
349 bool operator()(const Immediate &LHS, const Immediate &RHS) const {
350 if (LHS.isScalable() && !RHS.isScalable())
351 return false;
352 if (!LHS.isScalable() && RHS.isScalable())
353 return true;
354 return LHS.getKnownMinValue() < RHS.getKnownMinValue();
355 }
356};
357
358// This would be nicer if we could be generic instead of directly using size_t,
359// but there doesn't seem to be a type trait for is_orderable or
360// is_lessthan_comparable or similar.
361struct KeyOrderSizeTAndImmediate {
362 bool operator()(const std::pair<size_t, Immediate> &LHS,
363 const std::pair<size_t, Immediate> &RHS) const {
364 size_t LSize = LHS.first;
365 size_t RSize = RHS.first;
366 if (LSize != RSize)
367 return LSize < RSize;
368 return KeyOrderTargetImmediate()(LHS.second, RHS.second);
369 }
370};
371} // end anonymous namespace
372
373#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
374void RegSortData::print(raw_ostream &OS) const {
375 OS << "[NumUses=" << UsedByIndices.count() << ']';
376}
377
378LLVM_DUMP_METHOD void RegSortData::dump() const {
379 print(errs()); errs() << '\n';
380}
381#endif
382
383namespace {
384
385/// Map register candidates to information about how they are used.
386class RegUseTracker {
387 using RegUsesTy = DenseMap<const SCEV *, RegSortData>;
388
389 RegUsesTy RegUsesMap;
391
392public:
393 void countRegister(const SCEV *Reg, size_t LUIdx);
394 void dropRegister(const SCEV *Reg, size_t LUIdx);
395 void swapAndDropUse(size_t LUIdx, size_t LastLUIdx);
396
397 bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const;
398
399 const SmallBitVector &getUsedByIndices(const SCEV *Reg) const;
400
401 void clear();
402
405
406 iterator begin() { return RegSequence.begin(); }
407 iterator end() { return RegSequence.end(); }
408 const_iterator begin() const { return RegSequence.begin(); }
409 const_iterator end() const { return RegSequence.end(); }
410};
411
412} // end anonymous namespace
413
414void
415RegUseTracker::countRegister(const SCEV *Reg, size_t LUIdx) {
416 std::pair<RegUsesTy::iterator, bool> Pair = RegUsesMap.try_emplace(Reg);
417 RegSortData &RSD = Pair.first->second;
418 if (Pair.second)
419 RegSequence.push_back(Reg);
420 RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1));
421 RSD.UsedByIndices.set(LUIdx);
422}
423
424void
425RegUseTracker::dropRegister(const SCEV *Reg, size_t LUIdx) {
426 RegUsesTy::iterator It = RegUsesMap.find(Reg);
427 assert(It != RegUsesMap.end());
428 RegSortData &RSD = It->second;
429 assert(RSD.UsedByIndices.size() > LUIdx);
430 RSD.UsedByIndices.reset(LUIdx);
431}
432
433void
434RegUseTracker::swapAndDropUse(size_t LUIdx, size_t LastLUIdx) {
435 assert(LUIdx <= LastLUIdx);
436
437 // Update RegUses. The data structure is not optimized for this purpose;
438 // we must iterate through it and update each of the bit vectors.
439 for (auto &Pair : RegUsesMap) {
440 SmallBitVector &UsedByIndices = Pair.second.UsedByIndices;
441 if (LUIdx < UsedByIndices.size())
442 UsedByIndices[LUIdx] =
443 LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : false;
444 UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx));
445 }
446}
447
448bool
449RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const {
450 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
451 if (I == RegUsesMap.end())
452 return false;
453 const SmallBitVector &UsedByIndices = I->second.UsedByIndices;
454 int i = UsedByIndices.find_first();
455 if (i == -1) return false;
456 if ((size_t)i != LUIdx) return true;
457 return UsedByIndices.find_next(i) != -1;
458}
459
460const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const {
461 RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
462 assert(I != RegUsesMap.end() && "Unknown register!");
463 return I->second.UsedByIndices;
464}
465
466void RegUseTracker::clear() {
467 RegUsesMap.clear();
468 RegSequence.clear();
469}
470
471namespace {
472
473/// This class holds information that describes a formula for computing
474/// satisfying a use. It may include broken-out immediates and scaled registers.
475struct Formula {
476 /// Global base address used for complex addressing.
477 GlobalValue *BaseGV = nullptr;
478
479 /// Base offset for complex addressing.
480 Immediate BaseOffset = Immediate::getZero();
481
482 /// Whether any complex addressing has a base register.
483 bool HasBaseReg = false;
484
485 /// The scale of any complex addressing.
486 int64_t Scale = 0;
487
488 /// The list of "base" registers for this use. When this is non-empty. The
489 /// canonical representation of a formula is
490 /// 1. BaseRegs.size > 1 implies ScaledReg != NULL and
491 /// 2. ScaledReg != NULL implies Scale != 1 || !BaseRegs.empty().
492 /// 3. The reg containing recurrent expr related with currect loop in the
493 /// formula should be put in the ScaledReg.
494 /// #1 enforces that the scaled register is always used when at least two
495 /// registers are needed by the formula: e.g., reg1 + reg2 is reg1 + 1 * reg2.
496 /// #2 enforces that 1 * reg is reg.
497 /// #3 ensures invariant regs with respect to current loop can be combined
498 /// together in LSR codegen.
499 /// This invariant can be temporarily broken while building a formula.
500 /// However, every formula inserted into the LSRInstance must be in canonical
501 /// form.
503
504 /// The 'scaled' register for this use. This should be non-null when Scale is
505 /// not zero.
506 const SCEV *ScaledReg = nullptr;
507
508 /// An additional constant offset which added near the use. This requires a
509 /// temporary register, but the offset itself can live in an add immediate
510 /// field rather than a register.
511 Immediate UnfoldedOffset = Immediate::getZero();
512
513 Formula() = default;
514
515 void initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE);
516
517 bool isCanonical(const Loop &L) const;
518
519 void canonicalize(const Loop &L);
520
521 bool unscale();
522
523 bool hasZeroEnd() const;
524
525 bool countsDownToZero() const;
526
527 size_t getNumRegs() const;
528 Type *getType() const;
529
530 void deleteBaseReg(const SCEV *&S);
531
532 bool referencesReg(const SCEV *S) const;
533 bool hasRegsUsedByUsesOtherThan(size_t LUIdx,
534 const RegUseTracker &RegUses) const;
535
536 void print(raw_ostream &OS) const;
537 void dump() const;
538};
539
540} // end anonymous namespace
541
542/// Recursion helper for initialMatch.
543static void DoInitialMatch(const SCEV *S, Loop *L,
546 // Collect expressions which properly dominate the loop header.
547 if (SE.properlyDominates(S, L->getHeader())) {
548 Good.push_back(S);
549 return;
550 }
551
552 // Look at add operands.
553 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
554 for (const SCEV *S : Add->operands())
555 DoInitialMatch(S, L, Good, Bad, SE);
556 return;
557 }
558
559 // Look at addrec operands.
560 const SCEV *Start, *Step;
561 const Loop *ARLoop;
562 if (match(S,
563 m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(ARLoop))) &&
564 !Start->isZero()) {
565 DoInitialMatch(Start, L, Good, Bad, SE);
566 DoInitialMatch(SE.getAddRecExpr(SE.getConstant(S->getType(), 0), Step,
567 // FIXME: AR->getNoWrapFlags()
568 ARLoop, SCEV::FlagAnyWrap),
569 L, Good, Bad, SE);
570 return;
571 }
572
573 // Handle a multiplication by -1 (negation) if it didn't fold.
574 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S))
575 if (Mul->getOperand(0)->isAllOnesValue()) {
577 const SCEV *NewMul = SE.getMulExpr(Ops);
578
581 DoInitialMatch(NewMul, L, MyGood, MyBad, SE);
582 const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue(
583 SE.getEffectiveSCEVType(NewMul->getType())));
584 for (const SCEV *S : MyGood)
585 Good.push_back(SE.getMulExpr(NegOne, S));
586 for (const SCEV *S : MyBad)
587 Bad.push_back(SE.getMulExpr(NegOne, S));
588 return;
589 }
590
591 // Ok, we can't do anything interesting. Just stuff the whole thing into a
592 // register and hope for the best.
593 Bad.push_back(S);
594}
595
596/// Incorporate loop-variant parts of S into this Formula, attempting to keep
597/// all loop-invariant and loop-computable values in a single base register.
598void Formula::initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) {
601 DoInitialMatch(S, L, Good, Bad, SE);
602 if (!Good.empty()) {
603 const SCEV *Sum = SE.getAddExpr(Good);
604 if (!Sum->isZero())
605 BaseRegs.push_back(Sum);
606 HasBaseReg = true;
607 }
608 if (!Bad.empty()) {
609 const SCEV *Sum = SE.getAddExpr(Bad);
610 if (!Sum->isZero())
611 BaseRegs.push_back(Sum);
612 HasBaseReg = true;
613 }
614 canonicalize(*L);
615}
616
617static bool containsAddRecDependentOnLoop(const SCEV *S, const Loop &L) {
618 return SCEVExprContains(S, [&L](const SCEV *S) {
619 return isa<SCEVAddRecExpr>(S) && (cast<SCEVAddRecExpr>(S)->getLoop() == &L);
620 });
621}
622
623/// Check whether or not this formula satisfies the canonical
624/// representation.
625/// \see Formula::BaseRegs.
626bool Formula::isCanonical(const Loop &L) const {
627 assert((Scale == 0 || ScaledReg) &&
628 "ScaledReg must be non-null if Scale is non-zero");
629
630 if (!ScaledReg)
631 return BaseRegs.size() <= 1;
632
633 if (Scale != 1)
634 return true;
635
636 if (Scale == 1 && BaseRegs.empty())
637 return false;
638
639 if (containsAddRecDependentOnLoop(ScaledReg, L))
640 return true;
641
642 // If ScaledReg is not a recurrent expr, or it is but its loop is not current
643 // loop, meanwhile BaseRegs contains a recurrent expr reg related with current
644 // loop, we want to swap the reg in BaseRegs with ScaledReg.
645 return none_of(BaseRegs, [&L](const SCEV *S) {
647 });
648}
649
650/// Helper method to morph a formula into its canonical representation.
651/// \see Formula::BaseRegs.
652/// Every formula having more than one base register, must use the ScaledReg
653/// field. Otherwise, we would have to do special cases everywhere in LSR
654/// to treat reg1 + reg2 + ... the same way as reg1 + 1*reg2 + ...
655/// On the other hand, 1*reg should be canonicalized into reg.
656void Formula::canonicalize(const Loop &L) {
657 if (isCanonical(L))
658 return;
659
660 if (BaseRegs.empty()) {
661 // No base reg? Use scale reg with scale = 1 as such.
662 assert(ScaledReg && "Expected 1*reg => reg");
663 assert(Scale == 1 && "Expected 1*reg => reg");
664 BaseRegs.push_back(ScaledReg);
665 Scale = 0;
666 ScaledReg = nullptr;
667 return;
668 }
669
670 // Keep the invariant sum in BaseRegs and one of the variant sum in ScaledReg.
671 if (!ScaledReg) {
672 ScaledReg = BaseRegs.pop_back_val();
673 Scale = 1;
674 }
675
676 // If ScaledReg is an invariant with respect to L, find the reg from
677 // BaseRegs containing the recurrent expr related with Loop L. Swap the
678 // reg with ScaledReg.
679 if (!containsAddRecDependentOnLoop(ScaledReg, L)) {
680 auto I = find_if(BaseRegs, [&L](const SCEV *S) {
682 });
683 if (I != BaseRegs.end())
684 std::swap(ScaledReg, *I);
685 }
686 assert(isCanonical(L) && "Failed to canonicalize?");
687}
688
689/// Get rid of the scale in the formula.
690/// In other words, this method morphes reg1 + 1*reg2 into reg1 + reg2.
691/// \return true if it was possible to get rid of the scale, false otherwise.
692/// \note After this operation the formula may not be in the canonical form.
693bool Formula::unscale() {
694 if (Scale != 1)
695 return false;
696 Scale = 0;
697 BaseRegs.push_back(ScaledReg);
698 ScaledReg = nullptr;
699 return true;
700}
701
702bool Formula::hasZeroEnd() const {
703 if (UnfoldedOffset || BaseOffset)
704 return false;
705 if (BaseRegs.size() != 1 || ScaledReg)
706 return false;
707 return true;
708}
709
710bool Formula::countsDownToZero() const {
711 if (!hasZeroEnd())
712 return false;
713 assert(BaseRegs.size() == 1 && "hasZeroEnd should mean one BaseReg");
714 const APInt *StepInt;
715 if (!match(BaseRegs[0], m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(StepInt))))
716 return false;
717 return StepInt->isNegative();
718}
719
720/// Return the total number of register operands used by this formula. This does
721/// not include register uses implied by non-constant addrec strides.
722size_t Formula::getNumRegs() const {
723 return !!ScaledReg + BaseRegs.size();
724}
725
726/// Return the type of this formula, if it has one, or null otherwise. This type
727/// is meaningless except for the bit size.
728Type *Formula::getType() const {
729 return !BaseRegs.empty() ? BaseRegs.front()->getType() :
730 ScaledReg ? ScaledReg->getType() :
731 BaseGV ? BaseGV->getType() :
732 nullptr;
733}
734
735/// Delete the given base reg from the BaseRegs list.
736void Formula::deleteBaseReg(const SCEV *&S) {
737 if (&S != &BaseRegs.back())
738 std::swap(S, BaseRegs.back());
739 BaseRegs.pop_back();
740}
741
742/// Test if this formula references the given register.
743bool Formula::referencesReg(const SCEV *S) const {
744 return S == ScaledReg || is_contained(BaseRegs, S);
745}
746
747/// Test whether this formula uses registers which are used by uses other than
748/// the use with the given index.
749bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx,
750 const RegUseTracker &RegUses) const {
751 if (ScaledReg)
752 if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
753 return true;
754 for (const SCEV *BaseReg : BaseRegs)
755 if (RegUses.isRegUsedByUsesOtherThan(BaseReg, LUIdx))
756 return true;
757 return false;
758}
759
760#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
761void Formula::print(raw_ostream &OS) const {
762 ListSeparator Plus(" + ");
763 if (BaseGV) {
764 OS << Plus;
765 BaseGV->printAsOperand(OS, /*PrintType=*/false);
766 }
767 if (BaseOffset.isNonZero())
768 OS << Plus << BaseOffset;
769
770 for (const SCEV *BaseReg : BaseRegs)
771 OS << Plus << "reg(" << *BaseReg << ')';
772
773 if (HasBaseReg && BaseRegs.empty())
774 OS << Plus << "**error: HasBaseReg**";
775 else if (!HasBaseReg && !BaseRegs.empty())
776 OS << Plus << "**error: !HasBaseReg**";
777
778 if (Scale != 0) {
779 OS << Plus << Scale << "*reg(";
780 if (ScaledReg)
781 OS << *ScaledReg;
782 else
783 OS << "<unknown>";
784 OS << ')';
785 }
786 if (UnfoldedOffset.isNonZero())
787 OS << Plus << "imm(" << UnfoldedOffset << ')';
788}
789
790LLVM_DUMP_METHOD void Formula::dump() const {
791 print(errs()); errs() << '\n';
792}
793#endif
794
795/// Return true if the given addrec can be sign-extended without changing its
796/// value.
798 Type *WideTy =
800 return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
801}
802
803/// Return true if the given add can be sign-extended without changing its
804/// value.
805static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) {
806 Type *WideTy =
807 IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1);
808 return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy));
809}
810
811/// Return true if the given mul can be sign-extended without changing its
812/// value.
813static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) {
814 Type *WideTy =
816 SE.getTypeSizeInBits(M->getType()) * M->getNumOperands());
817 return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy));
818}
819
820/// Return an expression for LHS /s RHS, if it can be determined and if the
821/// remainder is known to be zero, or null otherwise. If IgnoreSignificantBits
822/// is true, expressions like (X * Y) /s Y are simplified to X, ignoring that
823/// the multiplication may overflow, which is useful when the result will be
824/// used in a context where the most significant bits are ignored.
825static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
826 ScalarEvolution &SE,
827 bool IgnoreSignificantBits = false) {
828 // Handle the trivial case, which works for any SCEV type.
829 if (LHS == RHS)
830 return SE.getConstant(LHS->getType(), 1);
831
832 // Handle a few RHS special cases.
834 if (RC) {
835 const APInt &RA = RC->getAPInt();
836 // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do
837 // some folding.
838 if (RA.isAllOnes()) {
839 if (LHS->getType()->isPointerTy())
840 return nullptr;
841 return SE.getMulExpr(LHS, RC);
842 }
843 // Handle x /s 1 as x.
844 if (RA == 1)
845 return LHS;
846 }
847
848 // Check for a division of a constant by a constant.
850 if (!RC)
851 return nullptr;
852 const APInt &LA = C->getAPInt();
853 const APInt &RA = RC->getAPInt();
854 if (LA.srem(RA) != 0)
855 return nullptr;
856 return SE.getConstant(LA.sdiv(RA));
857 }
858
859 // Distribute the sdiv over addrec operands, if the addrec doesn't overflow.
861 if ((IgnoreSignificantBits || isAddRecSExtable(AR, SE)) && AR->isAffine()) {
862 const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE,
863 IgnoreSignificantBits);
864 if (!Step) return nullptr;
865 const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE,
866 IgnoreSignificantBits);
867 if (!Start) return nullptr;
868 // FlagNW is independent of the start value, step direction, and is
869 // preserved with smaller magnitude steps.
870 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
871 return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap);
872 }
873 return nullptr;
874 }
875
876 // Distribute the sdiv over add operands, if the add doesn't overflow.
878 if (IgnoreSignificantBits || isAddSExtable(Add, SE)) {
880 for (const SCEV *S : Add->operands()) {
881 const SCEV *Op = getExactSDiv(S, RHS, SE, IgnoreSignificantBits);
882 if (!Op) return nullptr;
883 Ops.push_back(Op);
884 }
885 return SE.getAddExpr(Ops);
886 }
887 return nullptr;
888 }
889
890 // Check for a multiply operand that we can pull RHS out of.
892 if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) {
893 // Handle special case C1*X*Y /s C2*X*Y.
894 if (const SCEVMulExpr *MulRHS = dyn_cast<SCEVMulExpr>(RHS)) {
895 if (IgnoreSignificantBits || isMulSExtable(MulRHS, SE)) {
896 const SCEVConstant *LC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
897 const SCEVConstant *RC =
898 dyn_cast<SCEVConstant>(MulRHS->getOperand(0));
899 if (LC && RC) {
901 SmallVector<const SCEV *, 4> ROps(drop_begin(MulRHS->operands()));
902 if (LOps == ROps)
903 return getExactSDiv(LC, RC, SE, IgnoreSignificantBits);
904 }
905 }
906 }
907
909 bool Found = false;
910 for (const SCEV *S : Mul->operands()) {
911 if (!Found)
912 if (const SCEV *Q = getExactSDiv(S, RHS, SE,
913 IgnoreSignificantBits)) {
914 S = Q;
915 Found = true;
916 }
917 Ops.push_back(S);
918 }
919 return Found ? SE.getMulExpr(Ops) : nullptr;
920 }
921 return nullptr;
922 }
923
924 // Otherwise we don't know.
925 return nullptr;
926}
927
928/// Extracts an immediate operand from \p Ops and replaces the operand with
929/// zero. If \p PreferScalable is true and \p Ops contains both a scalable and
930/// non-scalable offsets, the scalable offset will be extracted.
932 ScalarEvolution &SE,
933 bool PreferScalable) {
934 const APInt *C;
935 SCEVUse *Op = nullptr;
936 Immediate Result = Immediate::getZero();
937
938 // Ops are sorted by their SCEVType (the order of SCEVTypes enum). So, for an
939 // AddExpr the possible order of operands is:
940 // Constant < VScale < Truncate < ZeroExtend < SignExtend < MulExpr < ...
941
942 // This means fixed-size immediates will always appear on the LHS:
943 SCEVUse &S = Ops.front();
944 if (match(S, m_scev_APInt(C)) && !C->isZero() &&
945 C->getSignificantBits() <= 64) {
946 Op = &S;
947 Result = Immediate::getFixed(C->getSExtValue());
948 }
949
950 // But scalable immediates, which are MulExpr(Vscale, Constant), can appear
951 // later in the operand list:
952 if (EnableVScaleImmediates && (Result.isZero() || PreferScalable)) {
953 for (SCEVUse &S : Ops) {
954 // We know anything past scMulExpr will not be a vscale immediate.
955 if (S->getSCEVType() > scMulExpr)
956 break;
958 Op = &S;
959 Result = Immediate::getScalable(C->getSExtValue());
960 break;
961 }
962 }
963 }
964
965 if (Result.isNonZero()) {
966 SCEVUse &S = *Op;
967 S = SE.getConstant(S->getType(), 0);
968 }
969
970 return Result;
971}
972
973/// If S involves the addition of a constant integer value, return that integer
974/// value, and mutate S to point to a new SCEV with that value excluded.
975static Immediate ExtractImmediate(SCEVUse &S, ScalarEvolution &SE,
976 bool PreferScalable = false) {
977 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
978 SmallVector<SCEVUse, 8> NewOps(Add->operands());
979 Immediate Result = ExtractImmediateOperand(NewOps, SE, PreferScalable);
980 if (Result.isZero())
981 Result = ExtractImmediate(NewOps.front(), SE, PreferScalable);
982 if (Result.isNonZero())
983 S = SE.getAddExpr(NewOps);
984 return Result;
985 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
986 SmallVector<SCEVUse, 8> NewOps(AR->operands());
987 Immediate Result = ExtractImmediate(NewOps.front(), SE, PreferScalable);
988 if (Result.isNonZero())
989 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
990 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
992 return Result;
993 }
994 return ExtractImmediateOperand({S}, SE, PreferScalable);
995}
996
997/// If S involves the addition of a GlobalValue address, return that symbol, and
998/// mutate S to point to a new SCEV with that value excluded.
1000 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
1001 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) {
1002 S = SE.getConstant(GV->getType(), 0);
1003 return GV;
1004 }
1005 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
1006 SmallVector<SCEVUse, 8> NewOps(Add->operands());
1007 GlobalValue *Result = ExtractSymbol(NewOps.back(), SE);
1008 if (Result)
1009 S = SE.getAddExpr(NewOps);
1010 return Result;
1011 } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
1012 SmallVector<SCEVUse, 8> NewOps(AR->operands());
1013 GlobalValue *Result = ExtractSymbol(NewOps.front(), SE);
1014 if (Result)
1015 S = SE.getAddRecExpr(NewOps, AR->getLoop(),
1016 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
1018 return Result;
1019 }
1020 return nullptr;
1021}
1022
1023/// Returns true if the specified instruction is using the specified value as an
1024/// address.
1026 Instruction *Inst, Value *OperandVal) {
1027 bool isAddress = isa<LoadInst>(Inst);
1028 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1029 if (SI->getPointerOperand() == OperandVal)
1030 isAddress = true;
1031 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
1032 // Addressing modes can also be folded into prefetches and a variety
1033 // of intrinsics.
1034 switch (II->getIntrinsicID()) {
1035 case Intrinsic::memset:
1036 case Intrinsic::prefetch:
1037 case Intrinsic::masked_load:
1038 if (II->getArgOperand(0) == OperandVal)
1039 isAddress = true;
1040 break;
1041 case Intrinsic::masked_store:
1042 if (II->getArgOperand(1) == OperandVal)
1043 isAddress = true;
1044 break;
1045 case Intrinsic::memmove:
1046 case Intrinsic::memcpy:
1047 if (II->getArgOperand(0) == OperandVal ||
1048 II->getArgOperand(1) == OperandVal)
1049 isAddress = true;
1050 break;
1051 default: {
1052 MemIntrinsicInfo IntrInfo;
1053 if (TTI.getTgtMemIntrinsic(II, IntrInfo)) {
1054 if (IntrInfo.PtrVal == OperandVal)
1055 isAddress = true;
1056 }
1057 }
1058 }
1059 } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(Inst)) {
1060 if (RMW->getPointerOperand() == OperandVal)
1061 isAddress = true;
1062 } else if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst)) {
1063 if (CmpX->getPointerOperand() == OperandVal)
1064 isAddress = true;
1065 }
1066 return isAddress;
1067}
1068
1069/// Return the type of the memory being accessed.
1070static MemAccessTy getAccessType(const TargetTransformInfo &TTI,
1071 Instruction *Inst, Value *OperandVal) {
1072 MemAccessTy AccessTy = MemAccessTy::getUnknown(Inst->getContext());
1073
1074 // First get the type of memory being accessed.
1075 if (Type *Ty = Inst->getAccessType())
1076 AccessTy.MemTy = Ty;
1077
1078 // Then get the pointer address space.
1079 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1080 AccessTy.AddrSpace = SI->getPointerAddressSpace();
1081 } else if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
1082 AccessTy.AddrSpace = LI->getPointerAddressSpace();
1083 } else if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(Inst)) {
1084 AccessTy.AddrSpace = RMW->getPointerAddressSpace();
1085 } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst)) {
1086 AccessTy.AddrSpace = CmpX->getPointerAddressSpace();
1087 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
1088 switch (II->getIntrinsicID()) {
1089 case Intrinsic::prefetch:
1090 case Intrinsic::memset:
1091 AccessTy.AddrSpace = II->getArgOperand(0)->getType()->getPointerAddressSpace();
1092 AccessTy.MemTy = OperandVal->getType();
1093 break;
1094 case Intrinsic::memmove:
1095 case Intrinsic::memcpy:
1096 AccessTy.AddrSpace = OperandVal->getType()->getPointerAddressSpace();
1097 AccessTy.MemTy = OperandVal->getType();
1098 break;
1099 case Intrinsic::masked_load:
1100 AccessTy.AddrSpace =
1101 II->getArgOperand(0)->getType()->getPointerAddressSpace();
1102 break;
1103 case Intrinsic::masked_store:
1104 AccessTy.AddrSpace =
1105 II->getArgOperand(1)->getType()->getPointerAddressSpace();
1106 break;
1107 default: {
1108 MemIntrinsicInfo IntrInfo;
1109 if (TTI.getTgtMemIntrinsic(II, IntrInfo) && IntrInfo.PtrVal) {
1110 AccessTy.AddrSpace
1111 = IntrInfo.PtrVal->getType()->getPointerAddressSpace();
1112 }
1113
1114 break;
1115 }
1116 }
1117 }
1118
1119 return AccessTy;
1120}
1121
1122/// Return true if this AddRec is already a phi in its loop.
1123static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
1124 for (PHINode &PN : AR->getLoop()->getHeader()->phis()) {
1125 if (SE.isSCEVable(PN.getType()) &&
1126 (SE.getEffectiveSCEVType(PN.getType()) ==
1127 SE.getEffectiveSCEVType(AR->getType())) &&
1128 SE.getSCEV(&PN) == AR)
1129 return true;
1130 }
1131 return false;
1132}
1133
1134/// Check if expanding this expression is likely to incur significant cost. This
1135/// is tricky because SCEV doesn't track which expressions are actually computed
1136/// by the current IR.
1137///
1138/// We currently allow expansion of IV increments that involve adds,
1139/// multiplication by constants, and AddRecs from existing phis.
1140///
1141/// TODO: Allow UDivExpr if we can find an existing IV increment that is an
1142/// obvious multiple of the UDivExpr.
1143static bool isHighCostExpansion(const SCEV *S,
1145 ScalarEvolution &SE) {
1146 // Zero/One operand expressions
1147 switch (S->getSCEVType()) {
1148 case scUnknown:
1149 case scConstant:
1150 case scVScale:
1151 return false;
1152 case scTruncate:
1153 return isHighCostExpansion(cast<SCEVTruncateExpr>(S)->getOperand(),
1154 Processed, SE);
1155 case scZeroExtend:
1156 return isHighCostExpansion(cast<SCEVZeroExtendExpr>(S)->getOperand(),
1157 Processed, SE);
1158 case scSignExtend:
1159 return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(),
1160 Processed, SE);
1161 default:
1162 break;
1163 }
1164
1165 if (!Processed.insert(S).second)
1166 return false;
1167
1168 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
1169 for (const SCEV *S : Add->operands()) {
1170 if (isHighCostExpansion(S, Processed, SE))
1171 return true;
1172 }
1173 return false;
1174 }
1175
1176 const SCEV *Op0, *Op1;
1177 if (match(S, m_scev_Mul(m_SCEV(Op0), m_SCEV(Op1)))) {
1178 // Multiplication by a constant is ok
1179 if (isa<SCEVConstant>(Op0))
1180 return isHighCostExpansion(Op1, Processed, SE);
1181
1182 // If we have the value of one operand, check if an existing
1183 // multiplication already generates this expression.
1184 if (const auto *U = dyn_cast<SCEVUnknown>(Op1)) {
1185 Value *UVal = U->getValue();
1186 for (User *UR : UVal->users()) {
1187 // If U is a constant, it may be used by a ConstantExpr.
1189 if (UI && UI->getOpcode() == Instruction::Mul &&
1190 SE.isSCEVable(UI->getType())) {
1191 return SE.getSCEV(UI) == S;
1192 }
1193 }
1194 }
1195 }
1196
1197 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
1198 if (isExistingPhi(AR, SE))
1199 return false;
1200 }
1201
1202 // Fow now, consider any other type of expression (div/mul/min/max) high cost.
1203 return true;
1204}
1205
1206namespace {
1207
1208class LSRUse;
1209
1210} // end anonymous namespace
1211
1212/// Check if the addressing mode defined by \p F is completely
1213/// folded in \p LU at isel time.
1214/// This includes address-mode folding and special icmp tricks.
1215/// This function returns true if \p LU can accommodate what \p F
1216/// defines and up to 1 base + 1 scaled + offset.
1217/// In other words, if \p F has several base registers, this function may
1218/// still return true. Therefore, users still need to account for
1219/// additional base registers and/or unfolded offsets to derive an
1220/// accurate cost model.
1221static bool isAMCompletelyFolded(const TargetTransformInfo &TTI,
1222 const LSRUse &LU, const Formula &F);
1223
1224// Get the cost of the scaling factor used in F for LU.
1225static InstructionCost getScalingFactorCost(const TargetTransformInfo &TTI,
1226 const LSRUse &LU, const Formula &F,
1227 const Loop &L);
1228
1229namespace {
1230
1231/// This class is used to measure and compare candidate formulae.
1232class Cost {
1233 const Loop *L = nullptr;
1234 ScalarEvolution *SE = nullptr;
1235 const TargetTransformInfo *TTI = nullptr;
1236 TargetTransformInfo::LSRCost C;
1238
1239public:
1240 Cost() = delete;
1241 Cost(const Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI,
1243 L(L), SE(&SE), TTI(&TTI), AMK(AMK) {
1244 C.Insns = 0;
1245 C.NumRegs = 0;
1246 C.AddRecCost = 0;
1247 C.NumIVMuls = 0;
1248 C.NumBaseAdds = 0;
1249 C.ImmCost = 0;
1250 C.SetupCost = 0;
1251 C.ScaleCost = 0;
1252 }
1253
1254 bool isLess(const Cost &Other) const;
1255
1256 void Lose();
1257
1258#ifndef NDEBUG
1259 // Once any of the metrics loses, they must all remain losers.
1260 bool isValid() {
1261 return ((C.Insns | C.NumRegs | C.AddRecCost | C.NumIVMuls | C.NumBaseAdds
1262 | C.ImmCost | C.SetupCost | C.ScaleCost) != ~0u)
1263 || ((C.Insns & C.NumRegs & C.AddRecCost & C.NumIVMuls & C.NumBaseAdds
1264 & C.ImmCost & C.SetupCost & C.ScaleCost) == ~0u);
1265 }
1266#endif
1267
1268 bool isLoser() {
1269 assert(isValid() && "invalid cost");
1270 return C.NumRegs == ~0u;
1271 }
1272
1273 void RateFormula(const Formula &F, SmallPtrSetImpl<const SCEV *> &Regs,
1274 const DenseSet<const SCEV *> &VisitedRegs, const LSRUse &LU,
1275 bool HardwareLoopProfitable,
1276 SmallPtrSetImpl<const SCEV *> *LoserRegs = nullptr);
1277
1278 void print(raw_ostream &OS) const;
1279 void dump() const;
1280
1281private:
1282 void RateRegister(const Formula &F, const SCEV *Reg,
1283 SmallPtrSetImpl<const SCEV *> &Regs, const LSRUse &LU,
1284 bool HardwareLoopProfitable);
1285 void RatePrimaryRegister(const Formula &F, const SCEV *Reg,
1286 SmallPtrSetImpl<const SCEV *> &Regs,
1287 const LSRUse &LU, bool HardwareLoopProfitable,
1288 SmallPtrSetImpl<const SCEV *> *LoserRegs);
1289};
1290
1291/// An operand value in an instruction which is to be replaced with some
1292/// equivalent, possibly strength-reduced, replacement.
1293struct LSRFixup {
1294 /// The instruction which will be updated.
1295 Instruction *UserInst = nullptr;
1296
1297 /// The operand of the instruction which will be replaced. The operand may be
1298 /// used more than once; every instance will be replaced.
1299 Value *OperandValToReplace = nullptr;
1300
1301 /// If this user is to use the post-incremented value of an induction
1302 /// variable, this set is non-empty and holds the loops associated with the
1303 /// induction variable.
1304 PostIncLoopSet PostIncLoops;
1305
1306 /// A constant offset to be added to the LSRUse expression. This allows
1307 /// multiple fixups to share the same LSRUse with different offsets, for
1308 /// example in an unrolled loop.
1309 Immediate Offset = Immediate::getZero();
1310
1311 LSRFixup() = default;
1312
1313 bool isUseFullyOutsideLoop(const Loop *L) const;
1314
1315 void print(raw_ostream &OS) const;
1316 void dump() const;
1317};
1318
1319/// This class holds the state that LSR keeps for each use in IVUsers, as well
1320/// as uses invented by LSR itself. It includes information about what kinds of
1321/// things can be folded into the user, information about the user itself, and
1322/// information about how the use may be satisfied. TODO: Represent multiple
1323/// users of the same expression in common?
1324class LSRUse {
1325 DenseSet<SmallVector<const SCEV *, 4>> Uniquifier;
1326
1327public:
1328 /// An enum for a kind of use, indicating what types of scaled and immediate
1329 /// operands it might support.
1330 enum KindType {
1331 Basic, ///< A normal use, with no folding.
1332 Special, ///< A special case of basic, allowing -1 scales.
1333 Address, ///< An address use; folding according to TargetLowering
1334 ICmpZero ///< An equality icmp with both operands folded into one.
1335 // TODO: Add a generic icmp too?
1336 };
1337
1338 using SCEVUseKindPair = PointerIntPair<const SCEV *, 2, KindType>;
1339
1340 KindType Kind;
1341 MemAccessTy AccessTy;
1342
1343 /// The list of operands which are to be replaced.
1345
1346 /// Keep track of the min and max offsets of the fixups.
1347 Immediate MinOffset = Immediate::getFixedMax();
1348 Immediate MaxOffset = Immediate::getFixedMin();
1349
1350 /// This records whether all of the fixups using this LSRUse are outside of
1351 /// the loop, in which case some special-case heuristics may be used.
1352 bool AllFixupsOutsideLoop = true;
1353
1354 /// This records whether all of the fixups using this LSRUse are unconditional
1355 /// within the loop, meaning they will be executed on every path to the loop
1356 /// latch. This includes fixups before early exits.
1357 bool AllFixupsUnconditional = true;
1358
1359 /// RigidFormula is set to true to guarantee that this use will be associated
1360 /// with a single formula--the one that initially matched. Some SCEV
1361 /// expressions cannot be expanded. This allows LSR to consider the registers
1362 /// used by those expressions without the need to expand them later after
1363 /// changing the formula.
1364 bool RigidFormula = false;
1365
1366 /// A list of ways to build a value that can satisfy this user. After the
1367 /// list is populated, one of these is selected heuristically and used to
1368 /// formulate a replacement for OperandValToReplace in UserInst.
1369 SmallVector<Formula, 12> Formulae;
1370
1371 /// The set of register candidates used by all formulae in this LSRUse.
1372 SmallPtrSet<const SCEV *, 4> Regs;
1373
1374 LSRUse(KindType K, MemAccessTy AT) : Kind(K), AccessTy(AT) {}
1375
1376 LSRFixup &getNewFixup() {
1377 Fixups.push_back(LSRFixup());
1378 return Fixups.back();
1379 }
1380
1381 void pushFixup(LSRFixup &f) {
1382 Fixups.push_back(f);
1383 if (Immediate::isKnownGT(f.Offset, MaxOffset))
1384 MaxOffset = f.Offset;
1385 if (Immediate::isKnownLT(f.Offset, MinOffset))
1386 MinOffset = f.Offset;
1387 }
1388
1389 bool HasFormulaWithSameRegs(const Formula &F) const;
1390 float getNotSelectedProbability(const SCEV *Reg) const;
1391 bool InsertFormula(const Formula &F, const Loop &L);
1392 void DeleteFormula(Formula &F);
1393 void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses);
1394
1395 void print(raw_ostream &OS) const;
1396 void dump() const;
1397};
1398
1399} // end anonymous namespace
1400
1401static bool isAMCompletelyFolded(const TargetTransformInfo &TTI,
1402 LSRUse::KindType Kind, MemAccessTy AccessTy,
1403 GlobalValue *BaseGV, Immediate BaseOffset,
1404 bool HasBaseReg, int64_t Scale,
1405 Instruction *Fixup = nullptr);
1406
1407static unsigned getSetupCost(const SCEV *Reg, unsigned Depth,
1408 const TargetTransformInfo &TTI) {
1409 if (isa<SCEVUnknown>(Reg))
1410 return 1;
1411 if (const auto *C = dyn_cast<SCEVConstant>(Reg)) {
1412 if (TTI.getIntImmCost(C->getAPInt(), C->getType(),
1415 return 0;
1416 return 1;
1417 }
1418 if (Depth == 0)
1419 return 0;
1420 if (const auto *S = dyn_cast<SCEVAddRecExpr>(Reg))
1421 return getSetupCost(S->getStart(), Depth - 1, TTI);
1422 if (auto S = dyn_cast<SCEVIntegralCastExpr>(Reg))
1423 return getSetupCost(S->getOperand(), Depth - 1, TTI);
1424 if (auto S = dyn_cast<SCEVNAryExpr>(Reg))
1425 return std::accumulate(S->operands().begin(), S->operands().end(), 0,
1426 [&](unsigned i, const SCEV *Reg) {
1427 return i + getSetupCost(Reg, Depth - 1, TTI);
1428 });
1429 if (auto S = dyn_cast<SCEVUDivExpr>(Reg))
1430 return getSetupCost(S->getLHS(), Depth - 1, TTI) +
1431 getSetupCost(S->getRHS(), Depth - 1, TTI);
1432 return 0;
1433}
1434
1435/// Tally up interesting quantities from the given register.
1436void Cost::RateRegister(const Formula &F, const SCEV *Reg,
1437 SmallPtrSetImpl<const SCEV *> &Regs, const LSRUse &LU,
1438 bool HardwareLoopProfitable) {
1439 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
1440 // If this is an addrec for another loop, it should be an invariant
1441 // with respect to L since L is the innermost loop (at least
1442 // for now LSR only handles innermost loops).
1443 if (AR->getLoop() != L) {
1444 // If the AddRec exists, consider it's register free and leave it alone.
1445 if (isExistingPhi(AR, *SE) && !(AMK & TTI::AMK_PostIndexed))
1446 return;
1447
1448 // It is bad to allow LSR for current loop to add induction variables
1449 // for its sibling loops.
1450 if (!AR->getLoop()->contains(L)) {
1451 Lose();
1452 return;
1453 }
1454
1455 // Otherwise, it will be an invariant with respect to Loop L.
1456 ++C.NumRegs;
1457 return;
1458 }
1459
1460 unsigned LoopCost = 1;
1461 if (TTI->isIndexedLoadLegal(TTI->MIM_PostInc, AR->getType()) ||
1462 TTI->isIndexedStoreLegal(TTI->MIM_PostInc, AR->getType())) {
1463 const SCEV *Start;
1464 const APInt *Step;
1465 if (match(AR, m_scev_AffineAddRec(m_SCEV(Start), m_scev_APInt(Step)))) {
1466 // If the step size matches the base offset, we could use pre-indexed
1467 // addressing.
1468 bool CanPreIndex = (AMK & TTI::AMK_PreIndexed) &&
1469 F.BaseOffset.isFixed() &&
1470 *Step == F.BaseOffset.getFixedValue();
1471 bool CanPostIndex = (AMK & TTI::AMK_PostIndexed) &&
1472 !isa<SCEVConstant>(Start) &&
1473 SE->isLoopInvariant(Start, L);
1474 // We can only pre or post index when the load/store is unconditional.
1475 if ((CanPreIndex || CanPostIndex) && LU.AllFixupsUnconditional)
1476 LoopCost = 0;
1477 }
1478 }
1479
1480 // If the loop counts down to zero and we'll be using a hardware loop then
1481 // the addrec will be combined into the hardware loop instruction.
1482 if (LU.Kind == LSRUse::ICmpZero && F.countsDownToZero() &&
1483 HardwareLoopProfitable)
1484 LoopCost = 0;
1485 C.AddRecCost += LoopCost;
1486
1487 // Add the step value register, if it needs one.
1488 // TODO: The non-affine case isn't precisely modeled here.
1489 const SCEV *StepReg = AR->getOperand(1);
1490 if (!AR->isAffine() || !isa<SCEVConstant>(StepReg)) {
1491 // If the step amount is a constant multiplied by vscale then it can form
1492 // the immediate value of an add and doesn't use a register, so long as
1493 // the immediate value is legal.
1494 auto IsVScaleStep = [](const SCEV *Reg, const TargetTransformInfo *TTI) {
1495 const APInt *X;
1497 return false;
1498 return TTI->isLegalAddScalableImmediate(X->getLimitedValue());
1499 };
1500 if (!Regs.count(StepReg) && !IsVScaleStep(StepReg, TTI)) {
1501 RateRegister(F, StepReg, Regs, LU, HardwareLoopProfitable);
1502 if (isLoser())
1503 return;
1504 }
1505 }
1506 }
1507 ++C.NumRegs;
1508
1509 // Rough heuristic; favor registers which don't require extra setup
1510 // instructions in the preheader.
1511 C.SetupCost += getSetupCost(Reg, SetupCostDepthLimit, *TTI);
1512 // Ensure we don't, even with the recusion limit, produce invalid costs.
1513 C.SetupCost = std::min<unsigned>(C.SetupCost, 1 << 16);
1514
1515 C.NumIVMuls += isa<SCEVMulExpr>(Reg) &&
1517}
1518
1519/// Record this register in the set. If we haven't seen it before, rate
1520/// it. Optional LoserRegs provides a way to declare any formula that refers to
1521/// one of those regs an instant loser.
1522void Cost::RatePrimaryRegister(const Formula &F, const SCEV *Reg,
1523 SmallPtrSetImpl<const SCEV *> &Regs,
1524 const LSRUse &LU, bool HardwareLoopProfitable,
1525 SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1526 if (LoserRegs && LoserRegs->count(Reg)) {
1527 Lose();
1528 return;
1529 }
1530 if (Regs.insert(Reg).second) {
1531 RateRegister(F, Reg, Regs, LU, HardwareLoopProfitable);
1532 if (LoserRegs && isLoser())
1533 LoserRegs->insert(Reg);
1534 }
1535}
1536
1537void Cost::RateFormula(const Formula &F, SmallPtrSetImpl<const SCEV *> &Regs,
1538 const DenseSet<const SCEV *> &VisitedRegs,
1539 const LSRUse &LU, bool HardwareLoopProfitable,
1540 SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1541 if (isLoser())
1542 return;
1543 assert(F.isCanonical(*L) && "Cost is accurate only for canonical formula");
1544 // Tally up the registers.
1545 unsigned PrevAddRecCost = C.AddRecCost;
1546 unsigned PrevNumRegs = C.NumRegs;
1547 unsigned PrevNumBaseAdds = C.NumBaseAdds;
1548 if (const SCEV *ScaledReg = F.ScaledReg) {
1549 if (VisitedRegs.count(ScaledReg)) {
1550 Lose();
1551 return;
1552 }
1553 RatePrimaryRegister(F, ScaledReg, Regs, LU, HardwareLoopProfitable,
1554 LoserRegs);
1555 if (isLoser())
1556 return;
1557 }
1558 for (const SCEV *BaseReg : F.BaseRegs) {
1559 if (VisitedRegs.count(BaseReg)) {
1560 Lose();
1561 return;
1562 }
1563 RatePrimaryRegister(F, BaseReg, Regs, LU, HardwareLoopProfitable,
1564 LoserRegs);
1565 if (isLoser())
1566 return;
1567 }
1568
1569 // Determine how many (unfolded) adds we'll need inside the loop.
1570 size_t NumBaseParts = F.getNumRegs();
1571 if (NumBaseParts > 1)
1572 // Do not count the base and a possible second register if the target
1573 // allows to fold 2 registers.
1574 C.NumBaseAdds +=
1575 NumBaseParts - (1 + (F.Scale && isAMCompletelyFolded(*TTI, LU, F)));
1576 C.NumBaseAdds += (F.UnfoldedOffset.isNonZero());
1577
1578 // Accumulate non-free scaling amounts.
1579 C.ScaleCost += getScalingFactorCost(*TTI, LU, F, *L).getValue();
1580
1581 // Tally up the non-zero immediates.
1582 for (const LSRFixup &Fixup : LU.Fixups) {
1583 if (Fixup.Offset.isCompatibleImmediate(F.BaseOffset)) {
1584 Immediate Offset = Fixup.Offset.addUnsigned(F.BaseOffset);
1585 if (F.BaseGV)
1586 C.ImmCost += 64; // Handle symbolic values conservatively.
1587 // TODO: This should probably be the pointer size.
1588 else if (Offset.isNonZero())
1589 C.ImmCost +=
1590 APInt(64, Offset.getKnownMinValue(), true).getSignificantBits();
1591
1592 // Check with target if this offset with this instruction is
1593 // specifically not supported.
1594 if (LU.Kind == LSRUse::Address && Offset.isNonZero() &&
1595 !isAMCompletelyFolded(*TTI, LSRUse::Address, LU.AccessTy, F.BaseGV,
1596 Offset, F.HasBaseReg, F.Scale, Fixup.UserInst))
1597 C.NumBaseAdds++;
1598 } else {
1599 // Incompatible immediate type, increase cost to avoid using
1600 C.ImmCost += 2048;
1601 }
1602 }
1603
1604 // If we don't count instruction cost exit here.
1605 if (!InsnsCost) {
1606 assert(isValid() && "invalid cost");
1607 return;
1608 }
1609
1610 // Treat every new register that exceeds TTI.getNumberOfRegisters() - 1 as
1611 // additional instruction (at least fill).
1612 // TODO: Need distinguish register class?
1613 unsigned TTIRegNum = TTI->getNumberOfRegisters(
1614 TTI->getRegisterClassForType(false, F.getType())) - 1;
1615 if (C.NumRegs > TTIRegNum) {
1616 // Cost already exceeded TTIRegNum, then only newly added register can add
1617 // new instructions.
1618 if (PrevNumRegs > TTIRegNum)
1619 C.Insns += (C.NumRegs - PrevNumRegs);
1620 else
1621 C.Insns += (C.NumRegs - TTIRegNum);
1622 }
1623
1624 // If ICmpZero formula ends with not 0, it could not be replaced by
1625 // just add or sub. We'll need to compare final result of AddRec.
1626 // That means we'll need an additional instruction. But if the target can
1627 // macro-fuse a compare with a branch, don't count this extra instruction.
1628 // For -10 + {0, +, 1}:
1629 // i = i + 1;
1630 // cmp i, 10
1631 //
1632 // For {-10, +, 1}:
1633 // i = i + 1;
1634 if (LU.Kind == LSRUse::ICmpZero && !F.hasZeroEnd() &&
1635 !TTI->canMacroFuseCmp())
1636 C.Insns++;
1637 // Each new AddRec adds 1 instruction to calculation.
1638 C.Insns += (C.AddRecCost - PrevAddRecCost);
1639
1640 // BaseAdds adds instructions for unfolded registers.
1641 if (LU.Kind != LSRUse::ICmpZero)
1642 C.Insns += C.NumBaseAdds - PrevNumBaseAdds;
1643 assert(isValid() && "invalid cost");
1644}
1645
1646/// Set this cost to a losing value.
1647void Cost::Lose() {
1648 C.Insns = std::numeric_limits<unsigned>::max();
1649 C.NumRegs = std::numeric_limits<unsigned>::max();
1650 C.AddRecCost = std::numeric_limits<unsigned>::max();
1651 C.NumIVMuls = std::numeric_limits<unsigned>::max();
1652 C.NumBaseAdds = std::numeric_limits<unsigned>::max();
1653 C.ImmCost = std::numeric_limits<unsigned>::max();
1654 C.SetupCost = std::numeric_limits<unsigned>::max();
1655 C.ScaleCost = std::numeric_limits<unsigned>::max();
1656}
1657
1658/// Choose the lower cost.
1659bool Cost::isLess(const Cost &Other) const {
1660 if (InsnsCost.getNumOccurrences() > 0 && InsnsCost &&
1661 C.Insns != Other.C.Insns)
1662 return C.Insns < Other.C.Insns;
1663 return TTI->isLSRCostLess(C, Other.C);
1664}
1665
1666#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1667void Cost::print(raw_ostream &OS) const {
1668 if (InsnsCost)
1669 OS << C.Insns << " instruction" << (C.Insns == 1 ? " " : "s ");
1670 OS << C.NumRegs << " reg" << (C.NumRegs == 1 ? "" : "s");
1671 if (C.AddRecCost != 0)
1672 OS << ", with addrec cost " << C.AddRecCost;
1673 if (C.NumIVMuls != 0)
1674 OS << ", plus " << C.NumIVMuls << " IV mul"
1675 << (C.NumIVMuls == 1 ? "" : "s");
1676 if (C.NumBaseAdds != 0)
1677 OS << ", plus " << C.NumBaseAdds << " base add"
1678 << (C.NumBaseAdds == 1 ? "" : "s");
1679 if (C.ScaleCost != 0)
1680 OS << ", plus " << C.ScaleCost << " scale cost";
1681 if (C.ImmCost != 0)
1682 OS << ", plus " << C.ImmCost << " imm cost";
1683 if (C.SetupCost != 0)
1684 OS << ", plus " << C.SetupCost << " setup cost";
1685}
1686
1687LLVM_DUMP_METHOD void Cost::dump() const {
1688 print(errs()); errs() << '\n';
1689}
1690#endif
1691
1692/// Test whether this fixup always uses its value outside of the given loop.
1693bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const {
1694 // PHI nodes use their value in their incoming blocks.
1695 if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) {
1696 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1697 if (PN->getIncomingValue(i) == OperandValToReplace &&
1698 L->contains(PN->getIncomingBlock(i)))
1699 return false;
1700 return true;
1701 }
1702
1703 return !L->contains(UserInst);
1704}
1705
1706#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1707void LSRFixup::print(raw_ostream &OS) const {
1708 OS << "UserInst=";
1709 // Store is common and interesting enough to be worth special-casing.
1710 if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) {
1711 OS << "store ";
1712 Store->getOperand(0)->printAsOperand(OS, /*PrintType=*/false);
1713 } else if (UserInst->getType()->isVoidTy())
1714 OS << UserInst->getOpcodeName();
1715 else
1716 UserInst->printAsOperand(OS, /*PrintType=*/false);
1717
1718 OS << ", OperandValToReplace=";
1719 OperandValToReplace->printAsOperand(OS, /*PrintType=*/false);
1720
1721 for (const Loop *PIL : PostIncLoops) {
1722 OS << ", PostIncLoop=";
1723 PIL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
1724 }
1725
1726 if (Offset.isNonZero())
1727 OS << ", Offset=" << Offset;
1728}
1729
1730LLVM_DUMP_METHOD void LSRFixup::dump() const {
1731 print(errs()); errs() << '\n';
1732}
1733#endif
1734
1735/// Test whether this use as a formula which has the same registers as the given
1736/// formula.
1737bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const {
1739 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1740 // Unstable sort by host order ok, because this is only used for uniquifying.
1741 llvm::sort(Key);
1742 return Uniquifier.count(Key);
1743}
1744
1745/// The function returns a probability of selecting formula without Reg.
1746float LSRUse::getNotSelectedProbability(const SCEV *Reg) const {
1747 unsigned FNum = 0;
1748 for (const Formula &F : Formulae)
1749 if (F.referencesReg(Reg))
1750 FNum++;
1751 return ((float)(Formulae.size() - FNum)) / Formulae.size();
1752}
1753
1754/// If the given formula has not yet been inserted, add it to the list, and
1755/// return true. Return false otherwise. The formula must be in canonical form.
1756bool LSRUse::InsertFormula(const Formula &F, const Loop &L) {
1757 assert(F.isCanonical(L) && "Invalid canonical representation");
1758
1759 if (!Formulae.empty() && RigidFormula)
1760 return false;
1761
1763 if (F.ScaledReg) Key.push_back(F.ScaledReg);
1764 // Unstable sort by host order ok, because this is only used for uniquifying.
1765 llvm::sort(Key);
1766
1767 if (!Uniquifier.insert(Key).second)
1768 return false;
1769
1770 // Using a register to hold the value of 0 is not profitable.
1771 assert((!F.ScaledReg || !F.ScaledReg->isZero()) &&
1772 "Zero allocated in a scaled register!");
1773#ifndef NDEBUG
1774 for (const SCEV *BaseReg : F.BaseRegs)
1775 assert(!BaseReg->isZero() && "Zero allocated in a base register!");
1776#endif
1777
1778 // Add the formula to the list.
1779 Formulae.push_back(F);
1780
1781 // Record registers now being used by this use.
1782 Regs.insert_range(F.BaseRegs);
1783 if (F.ScaledReg)
1784 Regs.insert(F.ScaledReg);
1785
1786 return true;
1787}
1788
1789/// Remove the given formula from this use's list.
1790void LSRUse::DeleteFormula(Formula &F) {
1791 if (&F != &Formulae.back())
1792 std::swap(F, Formulae.back());
1793 Formulae.pop_back();
1794}
1795
1796/// Recompute the Regs field, and update RegUses.
1797void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) {
1798 // Now that we've filtered out some formulae, recompute the Regs set.
1799 SmallPtrSet<const SCEV *, 4> OldRegs = std::move(Regs);
1800 Regs.clear();
1801 for (const Formula &F : Formulae) {
1802 if (F.ScaledReg) Regs.insert(F.ScaledReg);
1803 Regs.insert_range(F.BaseRegs);
1804 }
1805
1806 // Update the RegTracker.
1807 for (const SCEV *S : OldRegs)
1808 if (!Regs.count(S))
1809 RegUses.dropRegister(S, LUIdx);
1810}
1811
1812#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1813void LSRUse::print(raw_ostream &OS) const {
1814 OS << "LSR Use: Kind=";
1815 switch (Kind) {
1816 case Basic: OS << "Basic"; break;
1817 case Special: OS << "Special"; break;
1818 case ICmpZero: OS << "ICmpZero"; break;
1819 case Address:
1820 OS << "Address of ";
1821 if (AccessTy.MemTy->isPointerTy())
1822 OS << "pointer"; // the full pointer type could be really verbose
1823 else {
1824 OS << *AccessTy.MemTy;
1825 }
1826
1827 OS << " in addrspace(" << AccessTy.AddrSpace << ')';
1828 }
1829
1830 OS << ", Offsets={";
1831 bool NeedComma = false;
1832 for (const LSRFixup &Fixup : Fixups) {
1833 if (NeedComma) OS << ',';
1834 OS << Fixup.Offset;
1835 NeedComma = true;
1836 }
1837 OS << '}';
1838
1839 if (AllFixupsOutsideLoop)
1840 OS << ", all-fixups-outside-loop";
1841
1842 if (AllFixupsUnconditional)
1843 OS << ", all-fixups-unconditional";
1844}
1845
1846LLVM_DUMP_METHOD void LSRUse::dump() const {
1847 print(errs()); errs() << '\n';
1848}
1849#endif
1850
1852 LSRUse::KindType Kind, MemAccessTy AccessTy,
1853 GlobalValue *BaseGV, Immediate BaseOffset,
1854 bool HasBaseReg, int64_t Scale,
1855 Instruction *Fixup /* = nullptr */) {
1856 switch (Kind) {
1857 case LSRUse::Address: {
1858 int64_t FixedOffset =
1859 BaseOffset.isScalable() ? 0 : BaseOffset.getFixedValue();
1860 int64_t ScalableOffset =
1861 BaseOffset.isScalable() ? BaseOffset.getKnownMinValue() : 0;
1862 return TTI.isLegalAddressingMode(AccessTy.MemTy, BaseGV, FixedOffset,
1863 HasBaseReg, Scale, AccessTy.AddrSpace,
1864 Fixup, ScalableOffset);
1865 }
1866 case LSRUse::ICmpZero:
1867 // There's not even a target hook for querying whether it would be legal to
1868 // fold a GV into an ICmp.
1869 if (BaseGV)
1870 return false;
1871
1872 // ICmp only has two operands; don't allow more than two non-trivial parts.
1873 if (Scale != 0 && HasBaseReg && BaseOffset.isNonZero())
1874 return false;
1875
1876 // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by
1877 // putting the scaled register in the other operand of the icmp.
1878 if (Scale != 0 && Scale != -1)
1879 return false;
1880
1881 // If we have low-level target information, ask the target if it can fold an
1882 // integer immediate on an icmp.
1883 if (BaseOffset.isNonZero()) {
1884 // We don't have an interface to query whether the target supports
1885 // icmpzero against scalable quantities yet.
1886 if (BaseOffset.isScalable())
1887 return false;
1888
1889 // We have one of:
1890 // ICmpZero BaseReg + BaseOffset => ICmp BaseReg, -BaseOffset
1891 // ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset
1892 // Offs is the ICmp immediate.
1893 if (Scale == 0)
1894 // The cast does the right thing with
1895 // std::numeric_limits<int64_t>::min().
1896 BaseOffset = BaseOffset.getFixed(-(uint64_t)BaseOffset.getFixedValue());
1897 return TTI.isLegalICmpImmediate(BaseOffset.getFixedValue());
1898 }
1899
1900 // ICmpZero BaseReg + -1*ScaleReg => ICmp BaseReg, ScaleReg
1901 return true;
1902
1903 case LSRUse::Basic:
1904 // Only handle single-register values.
1905 return !BaseGV && Scale == 0 && BaseOffset.isZero();
1906
1907 case LSRUse::Special:
1908 // Special case Basic to handle -1 scales.
1909 return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset.isZero();
1910 }
1911
1912 llvm_unreachable("Invalid LSRUse Kind!");
1913}
1914
1916 Immediate MinOffset, Immediate MaxOffset,
1917 LSRUse::KindType Kind, MemAccessTy AccessTy,
1918 GlobalValue *BaseGV, Immediate BaseOffset,
1919 bool HasBaseReg, int64_t Scale) {
1920 if (BaseOffset.isNonZero() &&
1921 (BaseOffset.isScalable() != MinOffset.isScalable() ||
1922 BaseOffset.isScalable() != MaxOffset.isScalable()))
1923 return false;
1924 // Check for overflow.
1925 int64_t Base = BaseOffset.getKnownMinValue();
1926 int64_t Min = MinOffset.getKnownMinValue();
1927 int64_t Max = MaxOffset.getKnownMinValue();
1928 if (((int64_t)((uint64_t)Base + Min) > Base) != (Min > 0))
1929 return false;
1930 MinOffset = Immediate::get((uint64_t)Base + Min, MinOffset.isScalable());
1931 if (((int64_t)((uint64_t)Base + Max) > Base) != (Max > 0))
1932 return false;
1933 MaxOffset = Immediate::get((uint64_t)Base + Max, MaxOffset.isScalable());
1934
1935 return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MinOffset,
1936 HasBaseReg, Scale) &&
1937 isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MaxOffset,
1938 HasBaseReg, Scale);
1939}
1940
1942 Immediate MinOffset, Immediate MaxOffset,
1943 LSRUse::KindType Kind, MemAccessTy AccessTy,
1944 const Formula &F, const Loop &L) {
1945 // For the purpose of isAMCompletelyFolded either having a canonical formula
1946 // or a scale not equal to zero is correct.
1947 // Problems may arise from non canonical formulae having a scale == 0.
1948 // Strictly speaking it would best to just rely on canonical formulae.
1949 // However, when we generate the scaled formulae, we first check that the
1950 // scaling factor is profitable before computing the actual ScaledReg for
1951 // compile time sake.
1952 assert((F.isCanonical(L) || F.Scale != 0));
1953 return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy,
1954 F.BaseGV, F.BaseOffset, F.HasBaseReg, F.Scale);
1955}
1956
1957/// Test whether we know how to expand the current formula.
1958static bool isLegalUse(const TargetTransformInfo &TTI, Immediate MinOffset,
1959 Immediate MaxOffset, LSRUse::KindType Kind,
1960 MemAccessTy AccessTy, GlobalValue *BaseGV,
1961 Immediate BaseOffset, bool HasBaseReg, int64_t Scale) {
1962 // We know how to expand completely foldable formulae.
1963 return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
1964 BaseOffset, HasBaseReg, Scale) ||
1965 // Or formulae that use a base register produced by a sum of base
1966 // registers.
1967 (Scale == 1 &&
1968 isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy,
1969 BaseGV, BaseOffset, true, 0));
1970}
1971
1972static bool isLegalUse(const TargetTransformInfo &TTI, Immediate MinOffset,
1973 Immediate MaxOffset, LSRUse::KindType Kind,
1974 MemAccessTy AccessTy, const Formula &F) {
1975 return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, F.BaseGV,
1976 F.BaseOffset, F.HasBaseReg, F.Scale);
1977}
1978
1980 Immediate Offset) {
1981 if (Offset.isScalable())
1982 return TTI.isLegalAddScalableImmediate(Offset.getKnownMinValue());
1983
1984 return TTI.isLegalAddImmediate(Offset.getFixedValue());
1985}
1986
1988 const LSRUse &LU, const Formula &F) {
1989 // Target may want to look at the user instructions.
1990 if (LU.Kind == LSRUse::Address && TTI.LSRWithInstrQueries()) {
1991 for (const LSRFixup &Fixup : LU.Fixups)
1992 if (!isAMCompletelyFolded(TTI, LSRUse::Address, LU.AccessTy, F.BaseGV,
1993 (F.BaseOffset + Fixup.Offset), F.HasBaseReg,
1994 F.Scale, Fixup.UserInst))
1995 return false;
1996 return true;
1997 }
1998
1999 return isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind,
2000 LU.AccessTy, F.BaseGV, F.BaseOffset, F.HasBaseReg,
2001 F.Scale);
2002}
2003
2005 const LSRUse &LU, const Formula &F,
2006 const Loop &L) {
2007 if (!F.Scale)
2008 return 0;
2009
2010 // If the use is not completely folded in that instruction, we will have to
2011 // pay an extra cost only for scale != 1.
2012 if (!isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind,
2013 LU.AccessTy, F, L))
2014 return F.Scale != 1;
2015
2016 switch (LU.Kind) {
2017 case LSRUse::Address: {
2018 // Check the scaling factor cost with both the min and max offsets.
2019 int64_t ScalableMin = 0, ScalableMax = 0, FixedMin = 0, FixedMax = 0;
2020 if (F.BaseOffset.isScalable()) {
2021 ScalableMin = (F.BaseOffset + LU.MinOffset).getKnownMinValue();
2022 ScalableMax = (F.BaseOffset + LU.MaxOffset).getKnownMinValue();
2023 } else {
2024 FixedMin = (F.BaseOffset + LU.MinOffset).getFixedValue();
2025 FixedMax = (F.BaseOffset + LU.MaxOffset).getFixedValue();
2026 }
2027 InstructionCost ScaleCostMinOffset = TTI.getScalingFactorCost(
2028 LU.AccessTy.MemTy, F.BaseGV, StackOffset::get(FixedMin, ScalableMin),
2029 F.HasBaseReg, F.Scale, LU.AccessTy.AddrSpace);
2030 InstructionCost ScaleCostMaxOffset = TTI.getScalingFactorCost(
2031 LU.AccessTy.MemTy, F.BaseGV, StackOffset::get(FixedMax, ScalableMax),
2032 F.HasBaseReg, F.Scale, LU.AccessTy.AddrSpace);
2033
2034 assert(ScaleCostMinOffset.isValid() && ScaleCostMaxOffset.isValid() &&
2035 "Legal addressing mode has an illegal cost!");
2036 return std::max(ScaleCostMinOffset, ScaleCostMaxOffset);
2037 }
2038 case LSRUse::ICmpZero:
2039 case LSRUse::Basic:
2040 case LSRUse::Special:
2041 // The use is completely folded, i.e., everything is folded into the
2042 // instruction.
2043 return 0;
2044 }
2045
2046 llvm_unreachable("Invalid LSRUse Kind!");
2047}
2048
2050 LSRUse::KindType Kind, MemAccessTy AccessTy,
2051 GlobalValue *BaseGV, Immediate BaseOffset,
2052 bool HasBaseReg) {
2053 // Fast-path: zero is always foldable.
2054 if (BaseOffset.isZero() && !BaseGV)
2055 return true;
2056
2057 // Conservatively, create an address with an immediate and a
2058 // base and a scale.
2059 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
2060
2061 // Canonicalize a scale of 1 to a base register if the formula doesn't
2062 // already have a base register.
2063 if (!HasBaseReg && Scale == 1) {
2064 Scale = 0;
2065 HasBaseReg = true;
2066 }
2067
2068 // FIXME: Try with + without a scale? Maybe based on TTI?
2069 // I think basereg + scaledreg + immediateoffset isn't a good 'conservative'
2070 // default for many architectures, not just AArch64 SVE. More investigation
2071 // needed later to determine if this should be used more widely than just
2072 // on scalable types.
2073 if (HasBaseReg && BaseOffset.isNonZero() && Kind != LSRUse::ICmpZero &&
2074 AccessTy.MemTy && AccessTy.MemTy->isScalableTy() && DropScaledForVScale)
2075 Scale = 0;
2076
2077 return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, BaseOffset,
2078 HasBaseReg, Scale);
2079}
2080
2082 ScalarEvolution &SE, Immediate MinOffset,
2083 Immediate MaxOffset, LSRUse::KindType Kind,
2084 MemAccessTy AccessTy, const SCEV *S,
2085 bool HasBaseReg) {
2086 // Fast-path: zero is always foldable.
2087 if (S->isZero()) return true;
2088
2089 // Conservatively, create an address with an immediate and a
2090 // base and a scale.
2091 SCEVUse SCopy = S;
2092 Immediate BaseOffset = ExtractImmediate(SCopy, SE);
2093 GlobalValue *BaseGV = ExtractSymbol(SCopy, SE);
2094
2095 // If there's anything else involved, it's not foldable.
2096 if (!SCopy->isZero())
2097 return false;
2098
2099 // Fast-path: zero is always foldable.
2100 if (BaseOffset.isZero() && !BaseGV)
2101 return true;
2102
2103 if (BaseOffset.isScalable())
2104 return false;
2105
2106 // Conservatively, create an address with an immediate and a
2107 // base and a scale.
2108 int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
2109
2110 return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
2111 BaseOffset, HasBaseReg, Scale);
2112}
2113
2114namespace {
2115
2116/// An individual increment in a Chain of IV increments. Relate an IV user to
2117/// an expression that computes the IV it uses from the IV used by the previous
2118/// link in the Chain.
2119///
2120/// For the head of a chain, IncExpr holds the absolute SCEV expression for the
2121/// original IVOperand. The head of the chain's IVOperand is only valid during
2122/// chain collection, before LSR replaces IV users. During chain generation,
2123/// IncExpr can be used to find the new IVOperand that computes the same
2124/// expression.
2125struct IVInc {
2126 Instruction *UserInst;
2127 Value* IVOperand;
2128 const SCEV *IncExpr;
2129
2130 IVInc(Instruction *U, Value *O, const SCEV *E)
2131 : UserInst(U), IVOperand(O), IncExpr(E) {}
2132};
2133
2134// The list of IV increments in program order. We typically add the head of a
2135// chain without finding subsequent links.
2136struct IVChain {
2138 const SCEV *ExprBase = nullptr;
2139
2140 IVChain() = default;
2141 IVChain(const IVInc &Head, const SCEV *Base)
2142 : Incs(1, Head), ExprBase(Base) {}
2143
2144 using const_iterator = SmallVectorImpl<IVInc>::const_iterator;
2145
2146 // Return the first increment in the chain.
2147 const_iterator begin() const {
2148 assert(!Incs.empty());
2149 return std::next(Incs.begin());
2150 }
2151 const_iterator end() const {
2152 return Incs.end();
2153 }
2154
2155 // Returns true if this chain contains any increments.
2156 bool hasIncs() const { return Incs.size() >= 2; }
2157
2158 // Add an IVInc to the end of this chain.
2159 void add(const IVInc &X) { Incs.push_back(X); }
2160
2161 // Returns the last UserInst in the chain.
2162 Instruction *tailUserInst() const { return Incs.back().UserInst; }
2163
2164 // Returns true if IncExpr can be profitably added to this chain.
2165 bool isProfitableIncrement(const SCEV *OperExpr,
2166 const SCEV *IncExpr,
2167 ScalarEvolution&);
2168};
2169
2170/// Helper for CollectChains to track multiple IV increment uses. Distinguish
2171/// between FarUsers that definitely cross IV increments and NearUsers that may
2172/// be used between IV increments.
2173struct ChainUsers {
2174 SmallPtrSet<Instruction*, 4> FarUsers;
2175 SmallPtrSet<Instruction*, 4> NearUsers;
2176};
2177
2178/// This class holds state for the main loop strength reduction logic.
2179class LSRInstance {
2180 IVUsers &IU;
2181 ScalarEvolution &SE;
2182 DominatorTree &DT;
2183 LoopInfo &LI;
2184 AssumptionCache &AC;
2185 TargetLibraryInfo &TLI;
2186 const TargetTransformInfo &TTI;
2187 Loop *const L;
2188 MemorySSAUpdater *MSSAU;
2190 mutable SCEVExpander Rewriter;
2191 bool Changed = false;
2192 bool HardwareLoopProfitable = false;
2193 bool ShouldPreserveLCSSA = false;
2194
2195 /// This is the insert position that the current loop's induction variable
2196 /// increment should be placed. In simple loops, this is the latch block's
2197 /// terminator. But in more complicated cases, this is a position which will
2198 /// dominate all the in-loop post-increment users.
2199 Instruction *IVIncInsertPos = nullptr;
2200
2201 /// Interesting factors between use strides.
2202 ///
2203 /// We explicitly use a SetVector which contains a SmallSet, instead of the
2204 /// default, a SmallDenseSet, because we need to use the full range of
2205 /// int64_ts, and there's currently no good way of doing that with
2206 /// SmallDenseSet.
2207 SetVector<int64_t, SmallVector<int64_t, 8>, SmallSet<int64_t, 8>> Factors;
2208
2209 /// The cost of the current SCEV, the best solution by LSR will be dropped if
2210 /// the solution is not profitable.
2211 Cost BaselineCost;
2212
2213 /// Interesting use types, to facilitate truncation reuse.
2214 SmallSetVector<Type *, 4> Types;
2215
2216 /// The list of interesting uses.
2218
2219 /// Track which uses use which register candidates.
2220 RegUseTracker RegUses;
2221
2222 // Limit the number of chains to avoid quadratic behavior. We don't expect to
2223 // have more than a few IV increment chains in a loop. Missing a Chain falls
2224 // back to normal LSR behavior for those uses.
2225 static const unsigned MaxChains = 8;
2226
2227 /// IV users can form a chain of IV increments.
2229
2230 /// IV users that belong to profitable IVChains.
2231 SmallPtrSet<Use*, MaxChains> IVIncSet;
2232
2233 /// Induction variables that were generated and inserted by the SCEV Expander.
2234 SmallVector<llvm::WeakVH, 2> ScalarEvolutionIVs;
2235
2236 // Inserting instructions in the loop and using them as PHI's input could
2237 // break LCSSA in case if PHI's parent block is not a loop exit (i.e. the
2238 // corresponding incoming block is not loop exiting). So collect all such
2239 // instructions to form LCSSA for them later.
2240 SmallSetVector<Instruction *, 4> InsertedNonLCSSAInsts;
2241
2242 void OptimizeShadowIV();
2243 bool FindIVUserForCond(Instruction *Cond, IVStrideUse *&CondUse);
2244 Instruction *OptimizeMax(ICmpInst *Cond, IVStrideUse *&CondUse);
2245 void OptimizeLoopTermCond();
2246
2247 void ChainInstruction(Instruction *UserInst, Instruction *IVOper,
2248 SmallVectorImpl<ChainUsers> &ChainUsersVec);
2249 void FinalizeChain(IVChain &Chain);
2250 void CollectChains();
2251 void GenerateIVChain(const IVChain &Chain,
2252 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2253
2254 void CollectInterestingTypesAndFactors();
2255 void CollectFixupsAndInitialFormulae();
2256
2257 // Support for sharing of LSRUses between LSRFixups.
2258 using UseMapTy = DenseMap<LSRUse::SCEVUseKindPair, size_t>;
2259 UseMapTy UseMap;
2260
2261 bool reconcileNewOffset(LSRUse &LU, Immediate NewOffset, bool HasBaseReg,
2262 LSRUse::KindType Kind, MemAccessTy AccessTy);
2263
2264 std::pair<size_t, Immediate> getUse(const SCEV *&Expr, LSRUse::KindType Kind,
2265 MemAccessTy AccessTy);
2266
2267 void DeleteUse(LSRUse &LU, size_t LUIdx);
2268
2269 LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU);
2270
2271 void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
2272 void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
2273 void CountRegisters(const Formula &F, size_t LUIdx);
2274 bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F);
2275 bool IsFixupExecutedEachIncrement(const LSRFixup &LF) const;
2276
2277 void CollectLoopInvariantFixupsAndFormulae();
2278
2279 void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base,
2280 unsigned Depth = 0);
2281
2282 void GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx,
2283 const Formula &Base, unsigned Depth,
2284 size_t Idx, bool IsScaledReg = false);
2285 void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base);
2286 void GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx,
2287 const Formula &Base, size_t Idx,
2288 bool IsScaledReg = false);
2289 void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
2290 void GenerateConstantOffsetsImpl(LSRUse &LU, unsigned LUIdx,
2291 const Formula &Base,
2292 const SmallVectorImpl<Immediate> &Worklist,
2293 size_t Idx, bool IsScaledReg = false);
2294 void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
2295 void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base);
2296 void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base);
2297 void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base);
2298 void GenerateCrossUseConstantOffsets();
2299 void GenerateAllReuseFormulae();
2300
2301 void FilterOutUndesirableDedicatedRegisters();
2302
2303 size_t EstimateSearchSpaceComplexity() const;
2304 void NarrowSearchSpaceByDetectingSupersets();
2305 void NarrowSearchSpaceByCollapsingUnrolledCode();
2306 void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
2307 void NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
2308 void NarrowSearchSpaceByFilterPostInc();
2309 void NarrowSearchSpaceByMergingUsesOutsideLoop();
2310 void NarrowSearchSpaceByDeletingCostlyFormulas();
2311 void NarrowSearchSpaceByPickingWinnerRegs();
2312 void NarrowSearchSpaceUsingHeuristics();
2313
2314 void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
2315 Cost &SolutionCost,
2316 SmallVectorImpl<const Formula *> &Workspace,
2317 const Cost &CurCost,
2318 const SmallPtrSet<const SCEV *, 16> &CurRegs,
2319 DenseSet<const SCEV *> &VisitedRegs) const;
2320 void Solve(SmallVectorImpl<const Formula *> &Solution) const;
2321
2323 HoistInsertPosition(BasicBlock::iterator IP,
2324 const SmallVectorImpl<Instruction *> &Inputs) const;
2325 BasicBlock::iterator AdjustInsertPositionForExpand(BasicBlock::iterator IP,
2326 const LSRFixup &LF,
2327 const LSRUse &LU) const;
2328
2329 Value *Expand(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
2331 SmallVectorImpl<WeakTrackingVH> &DeadInsts) const;
2332 void RewriteForPHI(PHINode *PN, const LSRUse &LU, const LSRFixup &LF,
2333 const Formula &F,
2334 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2335 void Rewrite(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
2336 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
2337 void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution);
2338
2339public:
2340 // TODO(boomanaiden154): The PreserveLCSSA flag is a hack to allow
2341 // experimentation with the NewPM which requires LCSSA preservation while
2342 // some of the details are worked out in LSR. Eventually it should be set
2343 // to true and removed.
2344 LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE, DominatorTree &DT,
2345 LoopInfo &LI, const TargetTransformInfo &TTI, AssumptionCache &AC,
2346 TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU,
2347 bool PreserveLCSSA);
2348
2349 bool getChanged() const { return Changed; }
2350 const SmallVectorImpl<WeakVH> &getScalarEvolutionIVs() const {
2351 return ScalarEvolutionIVs;
2352 }
2353
2354 void print_factors_and_types(raw_ostream &OS) const;
2355 void print_fixups(raw_ostream &OS) const;
2356 void print_uses(raw_ostream &OS) const;
2357 void print(raw_ostream &OS) const;
2358 void dump() const;
2359};
2360
2361} // end anonymous namespace
2362
2363/// If IV is used in a int-to-float cast inside the loop then try to eliminate
2364/// the cast operation.
2365void LSRInstance::OptimizeShadowIV() {
2366 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
2367 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2368 return;
2369
2370 for (IVUsers::const_iterator UI = IU.begin(), E = IU.end();
2371 UI != E; /* empty */) {
2372 IVUsers::const_iterator CandidateUI = UI;
2373 ++UI;
2374 Instruction *ShadowUse = CandidateUI->getUser();
2375 Type *DestTy = nullptr;
2376 bool IsSigned = false;
2377
2378 /* If shadow use is a int->float cast then insert a second IV
2379 to eliminate this cast.
2380
2381 for (unsigned i = 0; i < n; ++i)
2382 foo((double)i);
2383
2384 is transformed into
2385
2386 double d = 0.0;
2387 for (unsigned i = 0; i < n; ++i, ++d)
2388 foo(d);
2389 */
2390 if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
2391 IsSigned = false;
2392 DestTy = UCast->getDestTy();
2393 }
2394 else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
2395 IsSigned = true;
2396 DestTy = SCast->getDestTy();
2397 }
2398 if (!DestTy) continue;
2399
2400 // If target does not support DestTy natively then do not apply
2401 // this transformation.
2402 if (!TTI.isTypeLegal(DestTy)) continue;
2403
2404 PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
2405 if (!PH) continue;
2406 if (PH->getNumIncomingValues() != 2) continue;
2407
2408 // If the calculation in integers overflows, the result in FP type will
2409 // differ. So we only can do this transformation if we are guaranteed to not
2410 // deal with overflowing values
2411 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(PH));
2412 if (!AR) continue;
2413 if (IsSigned && !AR->hasNoSignedWrap()) continue;
2414 if (!IsSigned && !AR->hasNoUnsignedWrap()) continue;
2415
2416 Type *SrcTy = PH->getType();
2417 int Mantissa = DestTy->getFPMantissaWidth();
2418 if (Mantissa == -1) continue;
2419 if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa)
2420 continue;
2421
2422 unsigned Entry, Latch;
2423 if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
2424 Entry = 0;
2425 Latch = 1;
2426 } else {
2427 Entry = 1;
2428 Latch = 0;
2429 }
2430
2431 ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
2432 if (!Init) continue;
2433 Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
2434 (double)Init->getSExtValue() :
2435 (double)Init->getZExtValue());
2436
2437 BinaryOperator *Incr =
2439 if (!Incr) continue;
2440 if (Incr->getOpcode() != Instruction::Add
2441 && Incr->getOpcode() != Instruction::Sub)
2442 continue;
2443
2444 /* Initialize new IV, double d = 0.0 in above example. */
2445 ConstantInt *C = nullptr;
2446 if (Incr->getOperand(0) == PH)
2448 else if (Incr->getOperand(1) == PH)
2450 else
2451 continue;
2452
2453 if (!C) continue;
2454
2455 // Ignore negative constants, as the code below doesn't handle them
2456 // correctly. TODO: Remove this restriction.
2457 if (!C->getValue().isStrictlyPositive())
2458 continue;
2459
2460 /* Add new PHINode. */
2461 PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH->getIterator());
2462 NewPH->setDebugLoc(PH->getDebugLoc());
2463
2464 /* create new increment. '++d' in above example. */
2465 Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
2466 BinaryOperator *NewIncr = BinaryOperator::Create(
2467 Incr->getOpcode() == Instruction::Add ? Instruction::FAdd
2468 : Instruction::FSub,
2469 NewPH, CFP, "IV.S.next.", Incr->getIterator());
2470 NewIncr->setDebugLoc(Incr->getDebugLoc());
2471
2472 NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
2473 NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
2474
2475 /* Remove cast operation */
2476 ShadowUse->replaceAllUsesWith(NewPH);
2477 ShadowUse->eraseFromParent();
2478 Changed = true;
2479 break;
2480 }
2481}
2482
2483/// If Cond has an operand that is an expression of an IV, set the IV user and
2484/// stride information and return true, otherwise return false.
2485bool LSRInstance::FindIVUserForCond(Instruction *Cond, IVStrideUse *&CondUse) {
2486 for (IVStrideUse &U : IU)
2487 if (U.getUser() == Cond) {
2488 // NOTE: we could handle setcc instructions with multiple uses here, but
2489 // InstCombine does it as well for simple uses, it's not clear that it
2490 // occurs enough in real life to handle.
2491 CondUse = &U;
2492 return true;
2493 }
2494 return false;
2495}
2496
2497/// Rewrite the loop's terminating condition if it uses a max computation.
2498///
2499/// This is a narrow solution to a specific, but acute, problem. For loops
2500/// like this:
2501///
2502/// i = 0;
2503/// do {
2504/// p[i] = 0.0;
2505/// } while (++i < n);
2506///
2507/// the trip count isn't just 'n', because 'n' might not be positive. And
2508/// unfortunately this can come up even for loops where the user didn't use
2509/// a C do-while loop. For example, seemingly well-behaved top-test loops
2510/// will commonly be lowered like this:
2511///
2512/// if (n > 0) {
2513/// i = 0;
2514/// do {
2515/// p[i] = 0.0;
2516/// } while (++i < n);
2517/// }
2518///
2519/// and then it's possible for subsequent optimization to obscure the if
2520/// test in such a way that indvars can't find it.
2521///
2522/// When indvars can't find the if test in loops like this, it creates a
2523/// max expression, which allows it to give the loop a canonical
2524/// induction variable:
2525///
2526/// i = 0;
2527/// max = n < 1 ? 1 : n;
2528/// do {
2529/// p[i] = 0.0;
2530/// } while (++i != max);
2531///
2532/// Canonical induction variables are necessary because the loop passes
2533/// are designed around them. The most obvious example of this is the
2534/// LoopInfo analysis, which doesn't remember trip count values. It
2535/// expects to be able to rediscover the trip count each time it is
2536/// needed, and it does this using a simple analysis that only succeeds if
2537/// the loop has a canonical induction variable.
2538///
2539/// However, when it comes time to generate code, the maximum operation
2540/// can be quite costly, especially if it's inside of an outer loop.
2541///
2542/// This function solves this problem by detecting this type of loop and
2543/// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
2544/// the instructions for the maximum computation.
2545Instruction *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse *&CondUse) {
2546 // Check that the loop matches the pattern we're looking for.
2547 if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
2548 Cond->getPredicate() != CmpInst::ICMP_NE)
2549 return Cond;
2550
2551 SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
2552 if (!Sel || !Sel->hasOneUse()) return Cond;
2553
2554 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
2555 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2556 return Cond;
2557 const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1);
2558
2559 // Add one to the backedge-taken count to get the trip count.
2560 const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount);
2561 if (IterationCount != SE.getSCEV(Sel)) return Cond;
2562
2563 // Check for a max calculation that matches the pattern. There's no check
2564 // for ICMP_ULE here because the comparison would be with zero, which
2565 // isn't interesting.
2566 CmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
2567 const SCEVNAryExpr *Max = nullptr;
2568 if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) {
2569 Pred = ICmpInst::ICMP_SLE;
2570 Max = S;
2571 } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) {
2572 Pred = ICmpInst::ICMP_SLT;
2573 Max = S;
2574 } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) {
2575 Pred = ICmpInst::ICMP_ULT;
2576 Max = U;
2577 } else {
2578 // No match; bail.
2579 return Cond;
2580 }
2581
2582 // To handle a max with more than two operands, this optimization would
2583 // require additional checking and setup.
2584 if (Max->getNumOperands() != 2)
2585 return Cond;
2586
2587 const SCEV *MaxLHS = Max->getOperand(0);
2588 const SCEV *MaxRHS = Max->getOperand(1);
2589
2590 // ScalarEvolution canonicalizes constants to the left. For < and >, look
2591 // for a comparison with 1. For <= and >=, a comparison with zero.
2592 if (!MaxLHS ||
2593 (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One)))
2594 return Cond;
2595
2596 // Check the relevant induction variable for conformance to
2597 // the pattern.
2598 const SCEV *IV = SE.getSCEV(Cond->getOperand(0));
2599 if (!match(IV,
2601 return Cond;
2602
2603 assert(cast<SCEVAddRecExpr>(IV)->getLoop() == L &&
2604 "Loop condition operand is an addrec in a different loop!");
2605
2606 // Check the right operand of the select, and remember it, as it will
2607 // be used in the new comparison instruction.
2608 Value *NewRHS = nullptr;
2609 if (ICmpInst::isTrueWhenEqual(Pred)) {
2610 // Look for n+1, and grab n.
2611 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1)))
2612 if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1)))
2613 if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS)
2614 NewRHS = BO->getOperand(0);
2615 if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2)))
2616 if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1)))
2617 if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS)
2618 NewRHS = BO->getOperand(0);
2619 if (!NewRHS)
2620 return Cond;
2621 } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS)
2622 NewRHS = Sel->getOperand(1);
2623 else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS)
2624 NewRHS = Sel->getOperand(2);
2625 else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS))
2626 NewRHS = SU->getValue();
2627 else
2628 // Max doesn't match expected pattern.
2629 return Cond;
2630
2631 // Determine the new comparison opcode. It may be signed or unsigned,
2632 // and the original comparison may be either equality or inequality.
2633 if (Cond->getPredicate() == CmpInst::ICMP_EQ)
2634 Pred = CmpInst::getInversePredicate(Pred);
2635
2636 // Ok, everything looks ok to change the condition into an SLT or SGE and
2637 // delete the max calculation.
2638 ICmpInst *NewCond = new ICmpInst(Cond->getIterator(), Pred,
2639 Cond->getOperand(0), NewRHS, "scmp");
2640
2641 // Delete the max calculation instructions.
2642 NewCond->setDebugLoc(Cond->getDebugLoc());
2643 Cond->replaceAllUsesWith(NewCond);
2644 CondUse->setUser(NewCond);
2646 Cond->eraseFromParent();
2647 Sel->eraseFromParent();
2648 if (Cmp->use_empty()) {
2649 salvageDebugInfo(*Cmp);
2650 Cmp->eraseFromParent();
2651 }
2652 return NewCond;
2653}
2654
2655/// Change loop terminating condition to use the postinc iv when possible.
2656void
2657LSRInstance::OptimizeLoopTermCond() {
2658 SmallPtrSet<Instruction *, 4> PostIncs;
2659
2660 // We need a different set of heuristics for rotated and non-rotated loops.
2661 // If a loop is rotated then the latch is also the backedge, so inserting
2662 // post-inc expressions just before the latch is ideal. To reduce live ranges
2663 // it also makes sense to rewrite terminating conditions to use post-inc
2664 // expressions.
2665 //
2666 // If the loop is not rotated then the latch is not a backedge; the latch
2667 // check is done in the loop head. Adding post-inc expressions before the
2668 // latch will cause overlapping live-ranges of pre-inc and post-inc expressions
2669 // in the loop body. In this case we do *not* want to use post-inc expressions
2670 // in the latch check, and we want to insert post-inc expressions before
2671 // the backedge.
2672 BasicBlock *LatchBlock = L->getLoopLatch();
2673 SmallVector<BasicBlock*, 8> ExitingBlocks;
2674 L->getExitingBlocks(ExitingBlocks);
2675 if (!llvm::is_contained(ExitingBlocks, LatchBlock)) {
2676 // The backedge doesn't exit the loop; treat this as a head-tested loop.
2677 IVIncInsertPos = LatchBlock->getTerminator();
2678 return;
2679 }
2680
2681 // Otherwise treat this as a rotated loop.
2682 for (BasicBlock *ExitingBlock : ExitingBlocks) {
2683 // Get the terminating condition for the loop if possible. If we
2684 // can, we want to change it to use a post-incremented version of its
2685 // induction variable, to allow coalescing the live ranges for the IV into
2686 // one register value.
2687
2688 CondBrInst *TermBr = dyn_cast<CondBrInst>(ExitingBlock->getTerminator());
2689 if (!TermBr)
2690 continue;
2691
2693 // If the argument to TermBr is an extractelement, then the source of that
2694 // instruction is what's generated the condition.
2696 if (Extract)
2697 Cond = dyn_cast<Instruction>(Extract->getVectorOperand());
2698 // FIXME: We could do more here, like handling logical operations where one
2699 // side is a cmp that uses an induction variable.
2700 if (!Cond)
2701 continue;
2702
2703 // Search IVUsesByStride to find Cond's IVUse if there is one.
2704 IVStrideUse *CondUse = nullptr;
2705 if (!FindIVUserForCond(Cond, CondUse))
2706 continue;
2707
2708 // If the trip count is computed in terms of a max (due to ScalarEvolution
2709 // being unable to find a sufficient guard, for example), change the loop
2710 // comparison to use SLT or ULT instead of NE.
2711 // One consequence of doing this now is that it disrupts the count-down
2712 // optimization. That's not always a bad thing though, because in such
2713 // cases it may still be worthwhile to avoid a max.
2714 if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
2715 Cond = OptimizeMax(Cmp, CondUse);
2716
2717 // If this exiting block dominates the latch block, it may also use
2718 // the post-inc value if it won't be shared with other uses.
2719 // Check for dominance.
2720 if (!DT.dominates(ExitingBlock, LatchBlock))
2721 continue;
2722
2723 // Conservatively avoid trying to use the post-inc value in non-latch
2724 // exits if there may be pre-inc users in intervening blocks.
2725 if (LatchBlock != ExitingBlock)
2726 for (const IVStrideUse &UI : IU)
2727 // Test if the use is reachable from the exiting block. This dominator
2728 // query is a conservative approximation of reachability.
2729 if (&UI != CondUse &&
2730 !DT.properlyDominates(UI.getUser()->getParent(), ExitingBlock)) {
2731 // Conservatively assume there may be reuse if the quotient of their
2732 // strides could be a legal scale.
2733 const SCEV *A = IU.getStride(*CondUse, L);
2734 const SCEV *B = IU.getStride(UI, L);
2735 if (!A || !B) continue;
2736 if (SE.getTypeSizeInBits(A->getType()) !=
2737 SE.getTypeSizeInBits(B->getType())) {
2738 if (SE.getTypeSizeInBits(A->getType()) >
2739 SE.getTypeSizeInBits(B->getType()))
2740 B = SE.getSignExtendExpr(B, A->getType());
2741 else
2742 A = SE.getSignExtendExpr(A, B->getType());
2743 }
2744 if (const SCEVConstant *D =
2746 const ConstantInt *C = D->getValue();
2747 // Stride of one or negative one can have reuse with non-addresses.
2748 if (C->isOne() || C->isMinusOne())
2749 goto decline_post_inc;
2750 // Avoid weird situations.
2751 if (C->getValue().getSignificantBits() >= 64 ||
2752 C->getValue().isMinSignedValue())
2753 goto decline_post_inc;
2754 // Check for possible scaled-address reuse.
2755 if (isAddressUse(TTI, UI.getUser(), UI.getOperandValToReplace())) {
2756 MemAccessTy AccessTy =
2757 getAccessType(TTI, UI.getUser(), UI.getOperandValToReplace());
2758 int64_t Scale = C->getSExtValue();
2759 if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr,
2760 /*BaseOffset=*/0,
2761 /*HasBaseReg=*/true, Scale,
2762 AccessTy.AddrSpace))
2763 goto decline_post_inc;
2764 Scale = -Scale;
2765 if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr,
2766 /*BaseOffset=*/0,
2767 /*HasBaseReg=*/true, Scale,
2768 AccessTy.AddrSpace))
2769 goto decline_post_inc;
2770 }
2771 }
2772 }
2773
2774 LLVM_DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: "
2775 << *Cond << '\n');
2776
2777 // It's possible for the setcc instruction to be anywhere in the loop, and
2778 // possible for it to have multiple users. If it is not immediately before
2779 // the exiting block branch, move it.
2780 if (isa_and_nonnull<CmpInst>(Cond) && Cond->getNextNode() != TermBr &&
2781 !Extract) {
2782 if (Cond->hasOneUse()) {
2783 Cond->moveBefore(TermBr->getIterator());
2784 } else {
2785 // Clone the terminating condition and insert into the loopend.
2786 Instruction *OldCond = Cond;
2787 Cond = Cond->clone();
2788 Cond->setName(L->getHeader()->getName() + ".termcond");
2789 Cond->insertInto(ExitingBlock, TermBr->getIterator());
2790
2791 // Clone the IVUse, as the old use still exists!
2792 CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
2793 TermBr->replaceUsesOfWith(OldCond, Cond);
2794 }
2795 }
2796
2797 // If we get to here, we know that we can transform the setcc instruction to
2798 // use the post-incremented version of the IV, allowing us to coalesce the
2799 // live ranges for the IV correctly.
2800 CondUse->transformToPostInc(L);
2801 Changed = true;
2802
2803 PostIncs.insert(Cond);
2804 decline_post_inc:;
2805 }
2806
2807 // Determine an insertion point for the loop induction variable increment. It
2808 // must dominate all the post-inc comparisons we just set up, and it must
2809 // dominate the loop latch edge.
2810 IVIncInsertPos = L->getLoopLatch()->getTerminator();
2811 for (Instruction *Inst : PostIncs)
2812 IVIncInsertPos = DT.findNearestCommonDominator(IVIncInsertPos, Inst);
2813}
2814
2815/// Determine if the given use can accommodate a fixup at the given offset and
2816/// other details. If so, update the use and return true.
2817bool LSRInstance::reconcileNewOffset(LSRUse &LU, Immediate NewOffset,
2818 bool HasBaseReg, LSRUse::KindType Kind,
2819 MemAccessTy AccessTy) {
2820 Immediate NewMinOffset = LU.MinOffset;
2821 Immediate NewMaxOffset = LU.MaxOffset;
2822 MemAccessTy NewAccessTy = AccessTy;
2823
2824 // Check for a mismatched kind. It's tempting to collapse mismatched kinds to
2825 // something conservative, however this can pessimize in the case that one of
2826 // the uses will have all its uses outside the loop, for example.
2827 if (LU.Kind != Kind)
2828 return false;
2829
2830 // Check for a mismatched access type, and fall back conservatively as needed.
2831 // TODO: Be less conservative when the type is similar and can use the same
2832 // addressing modes.
2833 if (Kind == LSRUse::Address) {
2834 if (AccessTy.MemTy != LU.AccessTy.MemTy) {
2835 NewAccessTy = MemAccessTy::getUnknown(AccessTy.MemTy->getContext(),
2836 AccessTy.AddrSpace);
2837 }
2838 }
2839
2840 // Conservatively assume HasBaseReg is true for now.
2841 if (Immediate::isKnownLT(NewOffset, LU.MinOffset)) {
2842 if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr,
2843 LU.MaxOffset - NewOffset, HasBaseReg))
2844 return false;
2845 NewMinOffset = NewOffset;
2846 } else if (Immediate::isKnownGT(NewOffset, LU.MaxOffset)) {
2847 if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr,
2848 NewOffset - LU.MinOffset, HasBaseReg))
2849 return false;
2850 NewMaxOffset = NewOffset;
2851 }
2852
2853 // FIXME: We should be able to handle some level of scalable offset support
2854 // for 'void', but in order to get basic support up and running this is
2855 // being left out.
2856 if (NewAccessTy.MemTy && NewAccessTy.MemTy->isVoidTy() &&
2857 (NewMinOffset.isScalable() || NewMaxOffset.isScalable()))
2858 return false;
2859
2860 // Update the use.
2861 LU.MinOffset = NewMinOffset;
2862 LU.MaxOffset = NewMaxOffset;
2863 LU.AccessTy = NewAccessTy;
2864 return true;
2865}
2866
2867/// Return an LSRUse index and an offset value for a fixup which needs the given
2868/// expression, with the given kind and optional access type. Either reuse an
2869/// existing use or create a new one, as needed.
2870std::pair<size_t, Immediate> LSRInstance::getUse(const SCEV *&Expr,
2871 LSRUse::KindType Kind,
2872 MemAccessTy AccessTy) {
2873 const SCEV *Copy = Expr;
2874 SCEVUse ExprUse = Expr;
2875 Immediate Offset = ExtractImmediate(
2876 ExprUse, SE, AccessTy.MemTy && AccessTy.MemTy->isScalableTy());
2877 Expr = ExprUse;
2878
2879 // Basic uses can't accept any offset, for example.
2880 if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ nullptr,
2881 Offset, /*HasBaseReg=*/ true)) {
2882 Expr = Copy;
2883 Offset = Immediate::getFixed(0);
2884 }
2885
2886 std::pair<UseMapTy::iterator, bool> P =
2887 UseMap.try_emplace(LSRUse::SCEVUseKindPair(Expr, Kind));
2888 if (!P.second) {
2889 // A use already existed with this base.
2890 size_t LUIdx = P.first->second;
2891 LSRUse &LU = Uses[LUIdx];
2892 if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy))
2893 // Reuse this use.
2894 return std::make_pair(LUIdx, Offset);
2895 }
2896
2897 // Create a new use.
2898 size_t LUIdx = Uses.size();
2899 P.first->second = LUIdx;
2900 Uses.push_back(LSRUse(Kind, AccessTy));
2901 LSRUse &LU = Uses[LUIdx];
2902
2903 LU.MinOffset = Offset;
2904 LU.MaxOffset = Offset;
2905 return std::make_pair(LUIdx, Offset);
2906}
2907
2908/// Delete the given use from the Uses list.
2909void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) {
2910 if (&LU != &Uses.back())
2911 std::swap(LU, Uses.back());
2912 Uses.pop_back();
2913
2914 // Update RegUses.
2915 RegUses.swapAndDropUse(LUIdx, Uses.size());
2916}
2917
2918/// Look for a use distinct from OrigLU which is has a formula that has the same
2919/// registers as the given formula.
2920LSRUse *
2921LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF,
2922 const LSRUse &OrigLU) {
2923 // Search all uses for the formula. This could be more clever.
2924 for (LSRUse &LU : Uses) {
2925 // Check whether this use is close enough to OrigLU, to see whether it's
2926 // worthwhile looking through its formulae.
2927 // Ignore ICmpZero uses because they may contain formulae generated by
2928 // GenerateICmpZeroScales, in which case adding fixup offsets may
2929 // be invalid.
2930 if (&LU != &OrigLU && LU.Kind != LSRUse::ICmpZero &&
2931 LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
2932 LU.HasFormulaWithSameRegs(OrigF)) {
2933 // Scan through this use's formulae.
2934 for (const Formula &F : LU.Formulae) {
2935 // Check to see if this formula has the same registers and symbols
2936 // as OrigF.
2937 if (F.BaseRegs == OrigF.BaseRegs &&
2938 F.ScaledReg == OrigF.ScaledReg &&
2939 F.BaseGV == OrigF.BaseGV &&
2940 F.Scale == OrigF.Scale &&
2941 F.UnfoldedOffset == OrigF.UnfoldedOffset) {
2942 if (F.BaseOffset.isZero())
2943 return &LU;
2944 // This is the formula where all the registers and symbols matched;
2945 // there aren't going to be any others. Since we declined it, we
2946 // can skip the rest of the formulae and proceed to the next LSRUse.
2947 break;
2948 }
2949 }
2950 }
2951 }
2952
2953 // Nothing looked good.
2954 return nullptr;
2955}
2956
2957void LSRInstance::CollectInterestingTypesAndFactors() {
2958 SmallSetVector<const SCEV *, 4> Strides;
2959
2960 // Collect interesting types and strides.
2962 for (const IVStrideUse &U : IU) {
2963 const SCEV *Expr = IU.getExpr(U);
2964 if (!Expr)
2965 continue;
2966
2967 // Collect interesting types.
2968 Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
2969
2970 // Add strides for mentioned loops.
2971 Worklist.push_back(Expr);
2972 do {
2973 const SCEV *S = Worklist.pop_back_val();
2974 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2975 if (AR->getLoop() == L)
2976 Strides.insert(AR->getStepRecurrence(SE));
2977 Worklist.push_back(AR->getStart());
2978 } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2979 append_range(Worklist, Add->operands());
2980 }
2981 } while (!Worklist.empty());
2982 }
2983
2984 // Compute interesting factors from the set of interesting strides.
2985 for (SmallSetVector<const SCEV *, 4>::const_iterator
2986 I = Strides.begin(), E = Strides.end(); I != E; ++I)
2987 for (SmallSetVector<const SCEV *, 4>::const_iterator NewStrideIter =
2988 std::next(I); NewStrideIter != E; ++NewStrideIter) {
2989 const SCEV *OldStride = *I;
2990 const SCEV *NewStride = *NewStrideIter;
2991
2992 if (SE.getTypeSizeInBits(OldStride->getType()) !=
2993 SE.getTypeSizeInBits(NewStride->getType())) {
2994 if (SE.getTypeSizeInBits(OldStride->getType()) >
2995 SE.getTypeSizeInBits(NewStride->getType()))
2996 NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType());
2997 else
2998 OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType());
2999 }
3000 if (const SCEVConstant *Factor =
3001 dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride,
3002 SE, true))) {
3003 if (Factor->getAPInt().getSignificantBits() <= 64 && !Factor->isZero())
3004 Factors.insert(Factor->getAPInt().getSExtValue());
3005 } else if (const SCEVConstant *Factor =
3007 NewStride,
3008 SE, true))) {
3009 if (Factor->getAPInt().getSignificantBits() <= 64 && !Factor->isZero())
3010 Factors.insert(Factor->getAPInt().getSExtValue());
3011 }
3012 }
3013
3014 // If all uses use the same type, don't bother looking for truncation-based
3015 // reuse.
3016 if (Types.size() == 1)
3017 Types.clear();
3018
3019 LLVM_DEBUG(print_factors_and_types(dbgs()));
3020}
3021
3022/// Helper for CollectChains that finds an IV operand (computed by an AddRec in
3023/// this loop) within [OI,OE) or returns OE. If IVUsers mapped Instructions to
3024/// IVStrideUses, we could partially skip this.
3025static User::op_iterator
3027 Loop *L, ScalarEvolution &SE) {
3028 for(; OI != OE; ++OI) {
3029 if (Instruction *Oper = dyn_cast<Instruction>(*OI)) {
3030 if (!SE.isSCEVable(Oper->getType()))
3031 continue;
3032
3033 if (const SCEVAddRecExpr *AR =
3035 if (AR->getLoop() == L)
3036 break;
3037 }
3038 }
3039 }
3040 return OI;
3041}
3042
3043/// IVChain logic must consistently peek base TruncInst operands, so wrap it in
3044/// a convenient helper.
3046 if (TruncInst *Trunc = dyn_cast<TruncInst>(Oper))
3047 return Trunc->getOperand(0);
3048 return Oper;
3049}
3050
3051/// Return an approximation of this SCEV expression's "base", or NULL for any
3052/// constant. Returning the expression itself is conservative. Returning a
3053/// deeper subexpression is more precise and valid as long as it isn't less
3054/// complex than another subexpression. For expressions involving multiple
3055/// unscaled values, we need to return the pointer-type SCEVUnknown. This avoids
3056/// forming chains across objects, such as: PrevOper==a[i], IVOper==b[i],
3057/// IVInc==b-a.
3058///
3059/// Since SCEVUnknown is the rightmost type, and pointers are the rightmost
3060/// SCEVUnknown, we simply return the rightmost SCEV operand.
3061static const SCEV *getExprBase(const SCEV *S) {
3062 switch (S->getSCEVType()) {
3063 default: // including scUnknown.
3064 return S;
3065 case scConstant:
3066 case scVScale:
3067 return nullptr;
3068 case scTruncate:
3069 return getExprBase(cast<SCEVTruncateExpr>(S)->getOperand());
3070 case scZeroExtend:
3071 return getExprBase(cast<SCEVZeroExtendExpr>(S)->getOperand());
3072 case scSignExtend:
3073 return getExprBase(cast<SCEVSignExtendExpr>(S)->getOperand());
3074 case scAddExpr: {
3075 // Skip over scaled operands (scMulExpr) to follow add operands as long as
3076 // there's nothing more complex.
3077 // FIXME: not sure if we want to recognize negation.
3078 const SCEVAddExpr *Add = cast<SCEVAddExpr>(S);
3079 for (const SCEV *SubExpr : reverse(Add->operands())) {
3080 if (SubExpr->getSCEVType() == scAddExpr)
3081 return getExprBase(SubExpr);
3082
3083 if (SubExpr->getSCEVType() != scMulExpr)
3084 return SubExpr;
3085 }
3086 return S; // all operands are scaled, be conservative.
3087 }
3088 case scAddRecExpr:
3089 return getExprBase(cast<SCEVAddRecExpr>(S)->getStart());
3090 }
3091 llvm_unreachable("Unknown SCEV kind!");
3092}
3093
3094/// Return true if the chain increment is profitable to expand into a loop
3095/// invariant value, which may require its own register. A profitable chain
3096/// increment will be an offset relative to the same base. We allow such offsets
3097/// to potentially be used as chain increment as long as it's not obviously
3098/// expensive to expand using real instructions.
3099bool IVChain::isProfitableIncrement(const SCEV *OperExpr,
3100 const SCEV *IncExpr,
3101 ScalarEvolution &SE) {
3102 // Aggressively form chains when -stress-ivchain.
3103 if (StressIVChain)
3104 return true;
3105
3106 // Do not replace a constant offset from IV head with a nonconstant IV
3107 // increment.
3108 if (!isa<SCEVConstant>(IncExpr)) {
3109 const SCEV *HeadExpr = SE.getSCEV(getWideOperand(Incs[0].IVOperand));
3110 if (isa<SCEVConstant>(SE.getMinusSCEV(OperExpr, HeadExpr)))
3111 return false;
3112 }
3113
3114 SmallPtrSet<const SCEV*, 8> Processed;
3115 return !isHighCostExpansion(IncExpr, Processed, SE);
3116}
3117
3118/// Return true if the number of registers needed for the chain is estimated to
3119/// be less than the number required for the individual IV users. First prohibit
3120/// any IV users that keep the IV live across increments (the Users set should
3121/// be empty). Next count the number and type of increments in the chain.
3122///
3123/// Chaining IVs can lead to considerable code bloat if ISEL doesn't
3124/// effectively use postinc addressing modes. Only consider it profitable it the
3125/// increments can be computed in fewer registers when chained.
3126///
3127/// TODO: Consider IVInc free if it's already used in another chains.
3128static bool isProfitableChain(IVChain &Chain,
3130 ScalarEvolution &SE,
3131 const TargetTransformInfo &TTI) {
3132 if (StressIVChain)
3133 return true;
3134
3135 if (!Chain.hasIncs())
3136 return false;
3137
3138 if (!Users.empty()) {
3139 LLVM_DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " users:\n";
3140 for (Instruction *Inst
3141 : Users) { dbgs() << " " << *Inst << "\n"; });
3142 return false;
3143 }
3144 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
3145
3146 // The chain itself may require a register, so initialize cost to 1.
3147 int cost = 1;
3148
3149 // A complete chain likely eliminates the need for keeping the original IV in
3150 // a register. LSR does not currently know how to form a complete chain unless
3151 // the header phi already exists.
3152 if (isa<PHINode>(Chain.tailUserInst())
3153 && SE.getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) {
3154 --cost;
3155 }
3156 const SCEV *LastIncExpr = nullptr;
3157 unsigned NumConstIncrements = 0;
3158 unsigned NumVarIncrements = 0;
3159 unsigned NumReusedIncrements = 0;
3160
3161 if (TTI.isProfitableLSRChainElement(Chain.Incs[0].UserInst))
3162 return true;
3163
3164 for (const IVInc &Inc : Chain) {
3165 if (TTI.isProfitableLSRChainElement(Inc.UserInst))
3166 return true;
3167 if (Inc.IncExpr->isZero())
3168 continue;
3169
3170 // Incrementing by zero or some constant is neutral. We assume constants can
3171 // be folded into an addressing mode or an add's immediate operand.
3172 if (isa<SCEVConstant>(Inc.IncExpr)) {
3173 ++NumConstIncrements;
3174 continue;
3175 }
3176
3177 if (Inc.IncExpr == LastIncExpr)
3178 ++NumReusedIncrements;
3179 else
3180 ++NumVarIncrements;
3181
3182 LastIncExpr = Inc.IncExpr;
3183 }
3184 // An IV chain with a single increment is handled by LSR's postinc
3185 // uses. However, a chain with multiple increments requires keeping the IV's
3186 // value live longer than it needs to be if chained.
3187 if (NumConstIncrements > 1)
3188 --cost;
3189
3190 // Materializing increment expressions in the preheader that didn't exist in
3191 // the original code may cost a register. For example, sign-extended array
3192 // indices can produce ridiculous increments like this:
3193 // IV + ((sext i32 (2 * %s) to i64) + (-1 * (sext i32 %s to i64)))
3194 cost += NumVarIncrements;
3195
3196 // Reusing variable increments likely saves a register to hold the multiple of
3197 // the stride.
3198 cost -= NumReusedIncrements;
3199
3200 LLVM_DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " Cost: " << cost
3201 << "\n");
3202
3203 return cost < 0;
3204}
3205
3206/// Add this IV user to an existing chain or make it the head of a new chain.
3207void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper,
3208 SmallVectorImpl<ChainUsers> &ChainUsersVec) {
3209 // When IVs are used as types of varying widths, they are generally converted
3210 // to a wider type with some uses remaining narrow under a (free) trunc.
3211 Value *const NextIV = getWideOperand(IVOper);
3212 const SCEV *const OperExpr = SE.getSCEV(NextIV);
3213 const SCEV *const OperExprBase = getExprBase(OperExpr);
3214
3215 // Visit all existing chains. Check if its IVOper can be computed as a
3216 // profitable loop invariant increment from the last link in the Chain.
3217 unsigned ChainIdx = 0, NChains = IVChainVec.size();
3218 const SCEV *LastIncExpr = nullptr;
3219 for (; ChainIdx < NChains; ++ChainIdx) {
3220 IVChain &Chain = IVChainVec[ChainIdx];
3221
3222 // Prune the solution space aggressively by checking that both IV operands
3223 // are expressions that operate on the same unscaled SCEVUnknown. This
3224 // "base" will be canceled by the subsequent getMinusSCEV call. Checking
3225 // first avoids creating extra SCEV expressions.
3226 if (!StressIVChain && Chain.ExprBase != OperExprBase)
3227 continue;
3228
3229 Value *PrevIV = getWideOperand(Chain.Incs.back().IVOperand);
3230 if (PrevIV->getType() != NextIV->getType())
3231 continue;
3232
3233 // A phi node terminates a chain.
3234 if (isa<PHINode>(UserInst) && isa<PHINode>(Chain.tailUserInst()))
3235 continue;
3236
3237 // The increment must be loop-invariant so it can be kept in a register.
3238 const SCEV *PrevExpr = SE.getSCEV(PrevIV);
3239 const SCEV *IncExpr = SE.getMinusSCEV(OperExpr, PrevExpr);
3240 if (isa<SCEVCouldNotCompute>(IncExpr) || !SE.isLoopInvariant(IncExpr, L))
3241 continue;
3242
3243 if (Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) {
3244 LastIncExpr = IncExpr;
3245 break;
3246 }
3247 }
3248 // If we haven't found a chain, create a new one, unless we hit the max. Don't
3249 // bother for phi nodes, because they must be last in the chain.
3250 if (ChainIdx == NChains) {
3251 if (isa<PHINode>(UserInst))
3252 return;
3253 if (NChains >= MaxChains && !StressIVChain) {
3254 LLVM_DEBUG(dbgs() << "IV Chain Limit\n");
3255 return;
3256 }
3257 LastIncExpr = OperExpr;
3258 // IVUsers may have skipped over sign/zero extensions. We don't currently
3259 // attempt to form chains involving extensions unless they can be hoisted
3260 // into this loop's AddRec.
3261 if (!isa<SCEVAddRecExpr>(LastIncExpr))
3262 return;
3263 ++NChains;
3264 IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr),
3265 OperExprBase));
3266 ChainUsersVec.resize(NChains);
3267 LLVM_DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Head: (" << *UserInst
3268 << ") IV=" << *LastIncExpr << "\n");
3269 } else {
3270 LLVM_DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Inc: (" << *UserInst
3271 << ") IV+" << *LastIncExpr << "\n");
3272 // Add this IV user to the end of the chain.
3273 IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr));
3274 }
3275 IVChain &Chain = IVChainVec[ChainIdx];
3276
3277 SmallPtrSet<Instruction*,4> &NearUsers = ChainUsersVec[ChainIdx].NearUsers;
3278 // This chain's NearUsers become FarUsers.
3279 if (!LastIncExpr->isZero()) {
3280 ChainUsersVec[ChainIdx].FarUsers.insert_range(NearUsers);
3281 NearUsers.clear();
3282 }
3283
3284 // All other uses of IVOperand become near uses of the chain.
3285 // We currently ignore intermediate values within SCEV expressions, assuming
3286 // they will eventually be used be the current chain, or can be computed
3287 // from one of the chain increments. To be more precise we could
3288 // transitively follow its user and only add leaf IV users to the set.
3289 for (User *U : IVOper->users()) {
3290 Instruction *OtherUse = dyn_cast<Instruction>(U);
3291 if (!OtherUse)
3292 continue;
3293 // Uses in the chain will no longer be uses if the chain is formed.
3294 // Include the head of the chain in this iteration (not Chain.begin()).
3295 IVChain::const_iterator IncIter = Chain.Incs.begin();
3296 IVChain::const_iterator IncEnd = Chain.Incs.end();
3297 for( ; IncIter != IncEnd; ++IncIter) {
3298 if (IncIter->UserInst == OtherUse)
3299 break;
3300 }
3301 if (IncIter != IncEnd)
3302 continue;
3303
3304 if (SE.isSCEVable(OtherUse->getType())
3305 && !isa<SCEVUnknown>(SE.getSCEV(OtherUse))
3306 && IU.isIVUserOrOperand(OtherUse)) {
3307 continue;
3308 }
3309 NearUsers.insert(OtherUse);
3310 }
3311
3312 // Since this user is part of the chain, it's no longer considered a use
3313 // of the chain.
3314 ChainUsersVec[ChainIdx].FarUsers.erase(UserInst);
3315}
3316
3317/// Populate the vector of Chains.
3318///
3319/// This decreases ILP at the architecture level. Targets with ample registers,
3320/// multiple memory ports, and no register renaming probably don't want
3321/// this. However, such targets should probably disable LSR altogether.
3322///
3323/// The job of LSR is to make a reasonable choice of induction variables across
3324/// the loop. Subsequent passes can easily "unchain" computation exposing more
3325/// ILP *within the loop* if the target wants it.
3326///
3327/// Finding the best IV chain is potentially a scheduling problem. Since LSR
3328/// will not reorder memory operations, it will recognize this as a chain, but
3329/// will generate redundant IV increments. Ideally this would be corrected later
3330/// by a smart scheduler:
3331/// = A[i]
3332/// = A[i+x]
3333/// A[i] =
3334/// A[i+x] =
3335///
3336/// TODO: Walk the entire domtree within this loop, not just the path to the
3337/// loop latch. This will discover chains on side paths, but requires
3338/// maintaining multiple copies of the Chains state.
3339void LSRInstance::CollectChains() {
3340 LLVM_DEBUG(dbgs() << "Collecting IV Chains.\n");
3341 SmallVector<ChainUsers, 8> ChainUsersVec;
3342
3343 SmallVector<BasicBlock *,8> LatchPath;
3344 BasicBlock *LoopHeader = L->getHeader();
3345 for (DomTreeNode *Rung = DT.getNode(L->getLoopLatch());
3346 Rung->getBlock() != LoopHeader; Rung = Rung->getIDom()) {
3347 LatchPath.push_back(Rung->getBlock());
3348 }
3349 LatchPath.push_back(LoopHeader);
3350
3351 // Walk the instruction stream from the loop header to the loop latch.
3352 for (BasicBlock *BB : reverse(LatchPath)) {
3353 for (Instruction &I : *BB) {
3354 // Skip instructions that weren't seen by IVUsers analysis.
3355 if (isa<PHINode>(I) || !IU.isIVUserOrOperand(&I))
3356 continue;
3357
3358 // Skip ephemeral values, as they don't produce real code.
3359 if (IU.isEphemeral(&I))
3360 continue;
3361
3362 // Ignore users that are part of a SCEV expression. This way we only
3363 // consider leaf IV Users. This effectively rediscovers a portion of
3364 // IVUsers analysis but in program order this time.
3365 if (SE.isSCEVable(I.getType()) && !isa<SCEVUnknown>(SE.getSCEV(&I)))
3366 continue;
3367
3368 // Remove this instruction from any NearUsers set it may be in.
3369 for (unsigned ChainIdx = 0, NChains = IVChainVec.size();
3370 ChainIdx < NChains; ++ChainIdx) {
3371 ChainUsersVec[ChainIdx].NearUsers.erase(&I);
3372 }
3373 // Search for operands that can be chained.
3374 SmallPtrSet<Instruction*, 4> UniqueOperands;
3375 User::op_iterator IVOpEnd = I.op_end();
3376 User::op_iterator IVOpIter = findIVOperand(I.op_begin(), IVOpEnd, L, SE);
3377 while (IVOpIter != IVOpEnd) {
3378 Instruction *IVOpInst = cast<Instruction>(*IVOpIter);
3379 if (UniqueOperands.insert(IVOpInst).second)
3380 ChainInstruction(&I, IVOpInst, ChainUsersVec);
3381 IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3382 }
3383 } // Continue walking down the instructions.
3384 } // Continue walking down the domtree.
3385 // Visit phi backedges to determine if the chain can generate the IV postinc.
3386 for (PHINode &PN : L->getHeader()->phis()) {
3387 if (!SE.isSCEVable(PN.getType()))
3388 continue;
3389
3390 Instruction *IncV =
3391 dyn_cast<Instruction>(PN.getIncomingValueForBlock(L->getLoopLatch()));
3392 if (IncV)
3393 ChainInstruction(&PN, IncV, ChainUsersVec);
3394 }
3395 // Remove any unprofitable chains.
3396 unsigned ChainIdx = 0;
3397 for (unsigned UsersIdx = 0, NChains = IVChainVec.size();
3398 UsersIdx < NChains; ++UsersIdx) {
3399 if (!isProfitableChain(IVChainVec[UsersIdx],
3400 ChainUsersVec[UsersIdx].FarUsers, SE, TTI))
3401 continue;
3402 // Preserve the chain at UsesIdx.
3403 if (ChainIdx != UsersIdx)
3404 IVChainVec[ChainIdx] = IVChainVec[UsersIdx];
3405 FinalizeChain(IVChainVec[ChainIdx]);
3406 ++ChainIdx;
3407 }
3408 IVChainVec.resize(ChainIdx);
3409}
3410
3411void LSRInstance::FinalizeChain(IVChain &Chain) {
3412 assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
3413 LLVM_DEBUG(dbgs() << "Final Chain: " << *Chain.Incs[0].UserInst << "\n");
3414
3415 for (const IVInc &Inc : Chain) {
3416 LLVM_DEBUG(dbgs() << " Inc: " << *Inc.UserInst << "\n");
3417 auto UseI = find(Inc.UserInst->operands(), Inc.IVOperand);
3418 assert(UseI != Inc.UserInst->op_end() && "cannot find IV operand");
3419 IVIncSet.insert(UseI);
3420 }
3421}
3422
3423/// Return true if the IVInc can be folded into an addressing mode.
3424static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
3425 Value *Operand, const TargetTransformInfo &TTI) {
3426 const SCEVConstant *IncConst = dyn_cast<SCEVConstant>(IncExpr);
3427 Immediate IncOffset = Immediate::getZero();
3428 if (IncConst) {
3429 if (IncConst && IncConst->getAPInt().getSignificantBits() > 64)
3430 return false;
3431 IncOffset = Immediate::getFixed(IncConst->getValue()->getSExtValue());
3432 } else {
3433 // Look for mul(vscale, constant), to detect a scalable offset.
3434 const APInt *C;
3435 if (!match(IncExpr, m_scev_Mul(m_scev_APInt(C), m_SCEVVScale())) ||
3436 C->getSignificantBits() > 64)
3437 return false;
3438 IncOffset = Immediate::getScalable(C->getSExtValue());
3439 }
3440
3441 if (!isAddressUse(TTI, UserInst, Operand))
3442 return false;
3443
3444 MemAccessTy AccessTy = getAccessType(TTI, UserInst, Operand);
3445 if (!isAlwaysFoldable(TTI, LSRUse::Address, AccessTy, /*BaseGV=*/nullptr,
3446 IncOffset, /*HasBaseReg=*/false))
3447 return false;
3448
3449 return true;
3450}
3451
3452/// Generate an add or subtract for each IVInc in a chain to materialize the IV
3453/// user's operand from the previous IV user's operand.
3454void LSRInstance::GenerateIVChain(const IVChain &Chain,
3455 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
3456 // Find the new IVOperand for the head of the chain. It may have been replaced
3457 // by LSR.
3458 const IVInc &Head = Chain.Incs[0];
3459 User::op_iterator IVOpEnd = Head.UserInst->op_end();
3460 // findIVOperand returns IVOpEnd if it can no longer find a valid IV user.
3461 User::op_iterator IVOpIter = findIVOperand(Head.UserInst->op_begin(),
3462 IVOpEnd, L, SE);
3463 Value *IVSrc = nullptr;
3464 while (IVOpIter != IVOpEnd) {
3465 IVSrc = getWideOperand(*IVOpIter);
3466
3467 // If this operand computes the expression that the chain needs, we may use
3468 // it. (Check this after setting IVSrc which is used below.)
3469 //
3470 // Note that if Head.IncExpr is wider than IVSrc, then this phi is too
3471 // narrow for the chain, so we can no longer use it. We do allow using a
3472 // wider phi, assuming the LSR checked for free truncation. In that case we
3473 // should already have a truncate on this operand such that
3474 // getSCEV(IVSrc) == IncExpr.
3475 if (SE.getSCEV(*IVOpIter) == Head.IncExpr
3476 || SE.getSCEV(IVSrc) == Head.IncExpr) {
3477 break;
3478 }
3479 IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3480 }
3481 if (IVOpIter == IVOpEnd) {
3482 // Gracefully give up on this chain.
3483 LLVM_DEBUG(dbgs() << "Concealed chain head: " << *Head.UserInst << "\n");
3484 return;
3485 }
3486 assert(IVSrc && "Failed to find IV chain source");
3487
3488 LLVM_DEBUG(dbgs() << "Generate chain at: " << *IVSrc << "\n");
3489 Type *IVTy = IVSrc->getType();
3490 Type *IntTy = SE.getEffectiveSCEVType(IVTy);
3491 const SCEV *LeftOverExpr = nullptr;
3492 const SCEV *Accum = SE.getZero(IntTy);
3494 Bases.emplace_back(Accum, IVSrc);
3495
3496 for (const IVInc &Inc : Chain) {
3497 Instruction *InsertPt = Inc.UserInst;
3498 if (isa<PHINode>(InsertPt))
3499 InsertPt = L->getLoopLatch()->getTerminator();
3500
3501 // IVOper will replace the current IV User's operand. IVSrc is the IV
3502 // value currently held in a register.
3503 Value *IVOper = IVSrc;
3504 if (!Inc.IncExpr->isZero()) {
3505 // IncExpr was the result of subtraction of two narrow values, so must
3506 // be signed.
3507 const SCEV *IncExpr = SE.getNoopOrSignExtend(Inc.IncExpr, IntTy);
3508 Accum = SE.getAddExpr(Accum, IncExpr);
3509 LeftOverExpr = LeftOverExpr ?
3510 SE.getAddExpr(LeftOverExpr, IncExpr) : IncExpr;
3511 }
3512
3513 // Look through each base to see if any can produce a nice addressing mode.
3514 bool FoundBase = false;
3515 for (auto [MapScev, MapIVOper] : reverse(Bases)) {
3516 const SCEV *Remainder = SE.getMinusSCEV(Accum, MapScev);
3517 if (canFoldIVIncExpr(Remainder, Inc.UserInst, Inc.IVOperand, TTI)) {
3518 if (!Remainder->isZero()) {
3519 Rewriter.clearPostInc();
3520 Value *IncV = Rewriter.expandCodeFor(Remainder, IntTy, InsertPt);
3521 const SCEV *IVOperExpr =
3522 SE.getAddExpr(SE.getUnknown(MapIVOper), SE.getUnknown(IncV));
3523 IVOper = Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
3524 } else {
3525 IVOper = MapIVOper;
3526 }
3527
3528 FoundBase = true;
3529 break;
3530 }
3531 }
3532 if (!FoundBase && LeftOverExpr && !LeftOverExpr->isZero()) {
3533 // Expand the IV increment.
3534 Rewriter.clearPostInc();
3535 Value *IncV = Rewriter.expandCodeFor(LeftOverExpr, IntTy, InsertPt);
3536 const SCEV *IVOperExpr = SE.getAddExpr(SE.getUnknown(IVSrc),
3537 SE.getUnknown(IncV));
3538 IVOper = Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
3539
3540 // If an IV increment can't be folded, use it as the next IV value.
3541 if (!canFoldIVIncExpr(LeftOverExpr, Inc.UserInst, Inc.IVOperand, TTI)) {
3542 assert(IVTy == IVOper->getType() && "inconsistent IV increment type");
3543 Bases.emplace_back(Accum, IVOper);
3544 IVSrc = IVOper;
3545 LeftOverExpr = nullptr;
3546 }
3547 }
3548 Type *OperTy = Inc.IVOperand->getType();
3549 if (IVTy != OperTy) {
3550 assert(SE.getTypeSizeInBits(IVTy) >= SE.getTypeSizeInBits(OperTy) &&
3551 "cannot extend a chained IV");
3552 IRBuilder<> Builder(InsertPt);
3553 IVOper = Builder.CreateTruncOrBitCast(IVOper, OperTy, "lsr.chain");
3554 }
3555 Inc.UserInst->replaceUsesOfWith(Inc.IVOperand, IVOper);
3556 if (auto *OperandIsInstr = dyn_cast<Instruction>(Inc.IVOperand))
3557 DeadInsts.emplace_back(OperandIsInstr);
3558 }
3559 // If LSR created a new, wider phi, we may also replace its postinc. We only
3560 // do this if we also found a wide value for the head of the chain.
3561 if (isa<PHINode>(Chain.tailUserInst())) {
3562 for (PHINode &Phi : L->getHeader()->phis()) {
3563 if (Phi.getType() != IVSrc->getType())
3564 continue;
3566 Phi.getIncomingValueForBlock(L->getLoopLatch()));
3567 if (!PostIncV || (SE.getSCEV(PostIncV) != SE.getSCEV(IVSrc)))
3568 continue;
3569 Value *IVOper = IVSrc;
3570 Type *PostIncTy = PostIncV->getType();
3571 if (IVTy != PostIncTy) {
3572 assert(PostIncTy->isPointerTy() && "mixing int/ptr IV types");
3573 IRBuilder<> Builder(L->getLoopLatch()->getTerminator());
3574 Builder.SetCurrentDebugLocation(PostIncV->getDebugLoc());
3575 IVOper = Builder.CreatePointerCast(IVSrc, PostIncTy, "lsr.chain");
3576 }
3577 Phi.replaceUsesOfWith(PostIncV, IVOper);
3578 DeadInsts.emplace_back(PostIncV);
3579 }
3580 }
3581}
3582
3583void LSRInstance::CollectFixupsAndInitialFormulae() {
3584 CondBrInst *ExitBranch = nullptr;
3585 bool SaveCmp = TTI.canSaveCmp(L, &ExitBranch, &SE, &LI, &DT, &AC, &TLI);
3586
3587 // For calculating baseline cost
3588 SmallPtrSet<const SCEV *, 16> Regs;
3589 DenseSet<const SCEV *> VisitedRegs;
3590 DenseSet<size_t> VisitedLSRUse;
3591
3592 for (const IVStrideUse &U : IU) {
3593 Instruction *UserInst = U.getUser();
3594 // Skip IV users that are part of profitable IV Chains.
3595 User::op_iterator UseI =
3596 find(UserInst->operands(), U.getOperandValToReplace());
3597 assert(UseI != UserInst->op_end() && "cannot find IV operand");
3598 if (IVIncSet.count(UseI)) {
3599 LLVM_DEBUG(dbgs() << "Use is in profitable chain: " << **UseI << '\n');
3600 continue;
3601 }
3602
3603 LSRUse::KindType Kind = LSRUse::Basic;
3604 MemAccessTy AccessTy;
3605 if (isAddressUse(TTI, UserInst, U.getOperandValToReplace())) {
3606 Kind = LSRUse::Address;
3607 AccessTy = getAccessType(TTI, UserInst, U.getOperandValToReplace());
3608 }
3609
3610 const SCEV *S = IU.getExpr(U);
3611 if (!S)
3612 continue;
3613 PostIncLoopSet TmpPostIncLoops = U.getPostIncLoops();
3614
3615 // Equality (== and !=) ICmps are special. We can rewrite (i == N) as
3616 // (N - i == 0), and this allows (N - i) to be the expression that we work
3617 // with rather than just N or i, so we can consider the register
3618 // requirements for both N and i at the same time. Limiting this code to
3619 // equality icmps is not a problem because all interesting loops use
3620 // equality icmps, thanks to IndVarSimplify.
3621 if (ICmpInst *CI = dyn_cast<ICmpInst>(UserInst)) {
3622 // If CI can be saved in some target, like replaced inside hardware loop
3623 // in PowerPC, no need to generate initial formulae for it.
3624 if (SaveCmp && CI == dyn_cast<ICmpInst>(ExitBranch->getCondition()))
3625 continue;
3626 if (CI->isEquality()) {
3627 // Swap the operands if needed to put the OperandValToReplace on the
3628 // left, for consistency.
3629 Value *NV = CI->getOperand(1);
3630 if (NV == U.getOperandValToReplace()) {
3631 CI->setOperand(1, CI->getOperand(0));
3632 CI->setOperand(0, NV);
3633 NV = CI->getOperand(1);
3634 Changed = true;
3635 }
3636
3637 // x == y --> x - y == 0
3638 const SCEV *N = SE.getSCEV(NV);
3639 if (SE.isLoopInvariant(N, L) && Rewriter.isSafeToExpand(N) &&
3640 (!NV->getType()->isPointerTy() ||
3641 SE.getPointerBase(N) == SE.getPointerBase(S))) {
3642 // S is normalized, so normalize N before folding it into S
3643 // to keep the result normalized.
3644 N = normalizeForPostIncUse(N, TmpPostIncLoops, SE);
3645 if (!N)
3646 continue;
3647 Kind = LSRUse::ICmpZero;
3648 S = SE.getMinusSCEV(N, S);
3649 } else if (L->isLoopInvariant(NV) &&
3650 (!isa<Instruction>(NV) ||
3651 DT.dominates(cast<Instruction>(NV), L->getHeader())) &&
3652 !NV->getType()->isPointerTy()) {
3653 // If we can't generally expand the expression (e.g. it contains
3654 // a divide), but it is already at a loop invariant point before the
3655 // loop, wrap it in an unknown (to prevent the expander from trying
3656 // to re-expand in a potentially unsafe way.) The restriction to
3657 // integer types is required because the unknown hides the base, and
3658 // SCEV can't compute the difference of two unknown pointers.
3659 N = SE.getUnknown(NV);
3660 N = normalizeForPostIncUse(N, TmpPostIncLoops, SE);
3661 if (!N)
3662 continue;
3663 Kind = LSRUse::ICmpZero;
3664 S = SE.getMinusSCEV(N, S);
3666 }
3667
3668 // -1 and the negations of all interesting strides (except the negation
3669 // of -1) are now also interesting.
3670 for (size_t i = 0, e = Factors.size(); i != e; ++i)
3671 if (Factors[i] != -1)
3672 Factors.insert(-(uint64_t)Factors[i]);
3673 Factors.insert(-1);
3674 }
3675 }
3676
3677 // Get or create an LSRUse.
3678 std::pair<size_t, Immediate> P = getUse(S, Kind, AccessTy);
3679 size_t LUIdx = P.first;
3680 Immediate Offset = P.second;
3681 LSRUse &LU = Uses[LUIdx];
3682
3683 // Record the fixup.
3684 LSRFixup &LF = LU.getNewFixup();
3685 LF.UserInst = UserInst;
3686 LF.OperandValToReplace = U.getOperandValToReplace();
3687 LF.PostIncLoops = TmpPostIncLoops;
3688 LF.Offset = Offset;
3689 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3690 LU.AllFixupsUnconditional &= IsFixupExecutedEachIncrement(LF);
3691
3692 // Create SCEV as Formula for calculating baseline cost
3693 if (!VisitedLSRUse.count(LUIdx) && !LF.isUseFullyOutsideLoop(L)) {
3694 Formula F;
3695 F.initialMatch(S, L, SE);
3696 BaselineCost.RateFormula(F, Regs, VisitedRegs, LU,
3697 HardwareLoopProfitable);
3698 VisitedLSRUse.insert(LUIdx);
3699 }
3700
3701 // If this is the first use of this LSRUse, give it a formula.
3702 if (LU.Formulae.empty()) {
3703 InsertInitialFormula(S, LU, LUIdx);
3704 CountRegisters(LU.Formulae.back(), LUIdx);
3705 }
3706 }
3707
3708 LLVM_DEBUG(print_fixups(dbgs()));
3709}
3710
3711/// Insert a formula for the given expression into the given use, separating out
3712/// loop-variant portions from loop-invariant and loop-computable portions.
3713void LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU,
3714 size_t LUIdx) {
3715 // Mark uses whose expressions cannot be expanded.
3716 if (!Rewriter.isSafeToExpand(S))
3717 LU.RigidFormula = true;
3718
3719 Formula F;
3720 F.initialMatch(S, L, SE);
3721 bool Inserted = InsertFormula(LU, LUIdx, F);
3722 assert(Inserted && "Initial formula already exists!"); (void)Inserted;
3723}
3724
3725/// Insert a simple single-register formula for the given expression into the
3726/// given use.
3727void
3728LSRInstance::InsertSupplementalFormula(const SCEV *S,
3729 LSRUse &LU, size_t LUIdx) {
3730 Formula F;
3731 F.BaseRegs.push_back(S);
3732 F.HasBaseReg = true;
3733 bool Inserted = InsertFormula(LU, LUIdx, F);
3734 assert(Inserted && "Supplemental formula already exists!"); (void)Inserted;
3735}
3736
3737/// Note which registers are used by the given formula, updating RegUses.
3738void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) {
3739 if (F.ScaledReg)
3740 RegUses.countRegister(F.ScaledReg, LUIdx);
3741 for (const SCEV *BaseReg : F.BaseRegs)
3742 RegUses.countRegister(BaseReg, LUIdx);
3743}
3744
3745/// If the given formula has not yet been inserted, add it to the list, and
3746/// return true. Return false otherwise.
3747bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) {
3748 // Do not insert formula that we will not be able to expand.
3749 assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F) &&
3750 "Formula is illegal");
3751
3752 if (!LU.InsertFormula(F, *L))
3753 return false;
3754
3755 CountRegisters(F, LUIdx);
3756 return true;
3757}
3758
3759/// Test whether this fixup will be executed each time the corresponding IV
3760/// increment instruction is executed.
3761bool LSRInstance::IsFixupExecutedEachIncrement(const LSRFixup &LF) const {
3762 // If the fixup block dominates the IV increment block then there is no path
3763 // through the loop to the increment that doesn't pass through the fixup.
3764 return DT.dominates(LF.UserInst->getParent(), IVIncInsertPos->getParent());
3765}
3766
3767/// Check for other uses of loop-invariant values which we're tracking. These
3768/// other uses will pin these values in registers, making them less profitable
3769/// for elimination.
3770/// TODO: This currently misses non-constant addrec step registers.
3771/// TODO: Should this give more weight to users inside the loop?
3772void
3773LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
3774 SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
3775 SmallPtrSet<const SCEV *, 32> Visited;
3776
3777 // Don't collect outside uses if we are favoring postinc - the instructions in
3778 // the loop are more important than the ones outside of it.
3779 if (AMK == TTI::AMK_PostIndexed)
3780 return;
3781
3782 while (!Worklist.empty()) {
3783 const SCEV *S = Worklist.pop_back_val();
3784
3785 // Don't process the same SCEV twice
3786 if (!Visited.insert(S).second)
3787 continue;
3788
3789 if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S))
3790 append_range(Worklist, N->operands());
3791 else if (const SCEVIntegralCastExpr *C = dyn_cast<SCEVIntegralCastExpr>(S))
3792 Worklist.push_back(C->getOperand());
3793 else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
3794 Worklist.push_back(D->getLHS());
3795 Worklist.push_back(D->getRHS());
3796 } else if (const SCEVUnknown *US = dyn_cast<SCEVUnknown>(S)) {
3797 const Value *V = US->getValue();
3798 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
3799 // Look for instructions defined outside the loop.
3800 if (L->contains(Inst)) continue;
3801 } else if (isa<Constant>(V))
3802 // Constants can be re-materialized.
3803 continue;
3804 for (const Use &U : V->uses()) {
3805 const Instruction *UserInst = dyn_cast<Instruction>(U.getUser());
3806 // Ignore non-instructions.
3807 if (!UserInst)
3808 continue;
3809 // Don't bother if the instruction is an EHPad.
3810 if (UserInst->isEHPad())
3811 continue;
3812 // Ignore instructions in other functions (as can happen with
3813 // Constants).
3814 if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
3815 continue;
3816 // Ignore instructions not dominated by the loop.
3817 const BasicBlock *UseBB = !isa<PHINode>(UserInst) ?
3818 UserInst->getParent() :
3819 cast<PHINode>(UserInst)->getIncomingBlock(
3821 if (!DT.dominates(L->getHeader(), UseBB))
3822 continue;
3823 // Don't bother if the instruction is in a BB which ends in an EHPad.
3824 if (UseBB->getTerminator()->isEHPad())
3825 continue;
3826
3827 // Ignore cases in which the currently-examined value could come from
3828 // a basic block terminated with an EHPad. This checks all incoming
3829 // blocks of the phi node since it is possible that the same incoming
3830 // value comes from multiple basic blocks, only some of which may end
3831 // in an EHPad. If any of them do, a subsequent rewrite attempt by this
3832 // pass would try to insert instructions into an EHPad, hitting an
3833 // assertion.
3834 if (isa<PHINode>(UserInst)) {
3835 const auto *PhiNode = cast<PHINode>(UserInst);
3836 bool HasIncompatibleEHPTerminatedBlock = false;
3837 llvm::Value *ExpectedValue = U;
3838 for (unsigned int I = 0; I < PhiNode->getNumIncomingValues(); I++) {
3839 if (PhiNode->getIncomingValue(I) == ExpectedValue) {
3840 if (PhiNode->getIncomingBlock(I)->getTerminator()->isEHPad()) {
3841 HasIncompatibleEHPTerminatedBlock = true;
3842 break;
3843 }
3844 }
3845 }
3846 if (HasIncompatibleEHPTerminatedBlock) {
3847 continue;
3848 }
3849 }
3850
3851 // Don't bother rewriting PHIs in catchswitch blocks.
3852 if (isa<CatchSwitchInst>(UserInst->getParent()->getTerminator()))
3853 continue;
3854 // Ignore uses which are part of other SCEV expressions, to avoid
3855 // analyzing them multiple times.
3856 if (SE.isSCEVable(UserInst->getType())) {
3857 const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst));
3858 // If the user is a no-op, look through to its uses.
3859 if (!isa<SCEVUnknown>(UserS))
3860 continue;
3861 if (UserS == US) {
3862 Worklist.push_back(
3863 SE.getUnknown(const_cast<Instruction *>(UserInst)));
3864 continue;
3865 }
3866 }
3867 // Ignore icmp instructions which are already being analyzed.
3868 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) {
3869 unsigned OtherIdx = !U.getOperandNo();
3870 Value *OtherOp = ICI->getOperand(OtherIdx);
3871 if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L))
3872 continue;
3873 }
3874
3875 // Do not consider uses inside lifetime intrinsics. These are not
3876 // actually materialized.
3877 if (UserInst->isLifetimeStartOrEnd())
3878 continue;
3879
3880 std::pair<size_t, Immediate> P =
3881 getUse(S, LSRUse::Basic, MemAccessTy());
3882 size_t LUIdx = P.first;
3883 Immediate Offset = P.second;
3884 LSRUse &LU = Uses[LUIdx];
3885 LSRFixup &LF = LU.getNewFixup();
3886 LF.UserInst = const_cast<Instruction *>(UserInst);
3887 LF.OperandValToReplace = U;
3888 LF.Offset = Offset;
3889 LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3890 LU.AllFixupsUnconditional &= IsFixupExecutedEachIncrement(LF);
3891 InsertSupplementalFormula(US, LU, LUIdx);
3892 CountRegisters(LU.Formulae.back(), Uses.size() - 1);
3893 break;
3894 }
3895 }
3896 }
3897}
3898
3899/// Split S into subexpressions which can be pulled out into separate
3900/// registers. If C is non-null, multiply each subexpression by C.
3901///
3902/// Return remainder expression after factoring the subexpressions captured by
3903/// Ops. If Ops is complete, return NULL.
3904static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3906 const Loop *L,
3907 ScalarEvolution &SE,
3908 unsigned Depth = 0) {
3909 // Arbitrarily cap recursion to protect compile time.
3910 if (Depth >= 3)
3911 return S;
3912
3913 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3914 // Break out add operands.
3915 for (const SCEV *S : Add->operands()) {
3916 const SCEV *Remainder = CollectSubexprs(S, C, Ops, L, SE, Depth+1);
3917 if (Remainder)
3918 Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3919 }
3920 return nullptr;
3921 }
3922 const SCEV *Start, *Step;
3923 const SCEVConstant *Op0;
3924 const SCEV *Op1;
3925 if (match(S, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step)))) {
3926 // Split a non-zero base out of an addrec.
3927 if (Start->isZero())
3928 return S;
3929
3930 const SCEV *Remainder = CollectSubexprs(Start, C, Ops, L, SE, Depth + 1);
3931 // Split the non-zero AddRec unless it is part of a nested recurrence that
3932 // does not pertain to this loop.
3933 if (Remainder && (cast<SCEVAddRecExpr>(S)->getLoop() == L ||
3934 !isa<SCEVAddRecExpr>(Remainder))) {
3935 Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3936 Remainder = nullptr;
3937 }
3938 if (Remainder != Start) {
3939 if (!Remainder)
3940 Remainder = SE.getConstant(S->getType(), 0);
3941 return SE.getAddRecExpr(Remainder, Step,
3942 cast<SCEVAddRecExpr>(S)->getLoop(),
3943 // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
3945 }
3946 } else if (match(S, m_scev_Mul(m_SCEVConstant(Op0), m_SCEV(Op1)))) {
3947 // Break (C * (a + b + c)) into C*a + C*b + C*c.
3948 C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3949 const SCEV *Remainder = CollectSubexprs(Op1, C, Ops, L, SE, Depth + 1);
3950 if (Remainder)
3951 Ops.push_back(SE.getMulExpr(C, Remainder));
3952 return nullptr;
3953 }
3954 return S;
3955}
3956
3957/// Return true if the SCEV represents a value that may end up as a
3958/// post-increment operation.
3960 LSRUse &LU, const SCEV *S, const Loop *L,
3961 ScalarEvolution &SE) {
3962 if (LU.Kind != LSRUse::Address ||
3963 !LU.AccessTy.getType()->isIntOrIntVectorTy())
3964 return false;
3965 const SCEV *Start;
3966 if (!match(S, m_scev_AffineAddRec(m_SCEV(Start), m_SCEVConstant())))
3967 return false;
3968 // Check if a post-indexed load/store can be used.
3969 if (TTI.isIndexedLoadLegal(TTI.MIM_PostInc, S->getType()) ||
3970 TTI.isIndexedStoreLegal(TTI.MIM_PostInc, S->getType())) {
3971 if (!isa<SCEVConstant>(Start) && SE.isLoopInvariant(Start, L))
3972 return true;
3973 }
3974 return false;
3975}
3976
3977/// Helper function for LSRInstance::GenerateReassociations.
3978void LSRInstance::GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx,
3979 const Formula &Base,
3980 unsigned Depth, size_t Idx,
3981 bool IsScaledReg) {
3982 const SCEV *BaseReg = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
3983 // Don't generate reassociations for the base register of a value that
3984 // may generate a post-increment operator. The reason is that the
3985 // reassociations cause extra base+register formula to be created,
3986 // and possibly chosen, but the post-increment is more efficient.
3987 if (AMK == TTI::AMK_PostIndexed && mayUsePostIncMode(TTI, LU, BaseReg, L, SE))
3988 return;
3990 const SCEV *Remainder = CollectSubexprs(BaseReg, nullptr, AddOps, L, SE);
3991 if (Remainder)
3992 AddOps.push_back(Remainder);
3993
3994 if (AddOps.size() == 1)
3995 return;
3996
3998 JE = AddOps.end();
3999 J != JE; ++J) {
4000 // Loop-variant "unknown" values are uninteresting; we won't be able to
4001 // do anything meaningful with them.
4002 if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L))
4003 continue;
4004
4005 // Don't pull a constant into a register if the constant could be folded
4006 // into an immediate field.
4007 if (isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
4008 LU.AccessTy, *J, Base.getNumRegs() > 1))
4009 continue;
4010
4011 // Collect all operands except *J.
4012 SmallVector<SCEVUse, 8> InnerAddOps(std::as_const(AddOps).begin(), J);
4013 InnerAddOps.append(std::next(J), std::as_const(AddOps).end());
4014
4015 // Don't leave just a constant behind in a register if the constant could
4016 // be folded into an immediate field.
4017 if (InnerAddOps.size() == 1 &&
4018 isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
4019 LU.AccessTy, InnerAddOps[0], Base.getNumRegs() > 1))
4020 continue;
4021
4022 const SCEV *InnerSum = SE.getAddExpr(InnerAddOps);
4023 if (InnerSum->isZero())
4024 continue;
4025 Formula F = Base;
4026
4027 if (F.UnfoldedOffset.isNonZero() && F.UnfoldedOffset.isScalable())
4028 continue;
4029
4030 // Add the remaining pieces of the add back into the new formula.
4031 const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum);
4032 if (InnerSumSC && SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 &&
4033 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset.getFixedValue() +
4034 InnerSumSC->getValue()->getZExtValue())) {
4035 F.UnfoldedOffset =
4036 Immediate::getFixed((uint64_t)F.UnfoldedOffset.getFixedValue() +
4037 InnerSumSC->getValue()->getZExtValue());
4038 if (IsScaledReg) {
4039 F.ScaledReg = nullptr;
4040 F.Scale = 0;
4041 } else
4042 F.BaseRegs.erase(F.BaseRegs.begin() + Idx);
4043 } else if (IsScaledReg)
4044 F.ScaledReg = InnerSum;
4045 else
4046 F.BaseRegs[Idx] = InnerSum;
4047
4048 // Add J as its own register, or an unfolded immediate.
4049 const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J);
4050 if (SC && SE.getTypeSizeInBits(SC->getType()) <= 64 &&
4051 TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset.getFixedValue() +
4052 SC->getValue()->getZExtValue()))
4053 F.UnfoldedOffset =
4054 Immediate::getFixed((uint64_t)F.UnfoldedOffset.getFixedValue() +
4055 SC->getValue()->getZExtValue());
4056 else
4057 F.BaseRegs.push_back(*J);
4058 // We may have changed the number of register in base regs, adjust the
4059 // formula accordingly.
4060 F.canonicalize(*L);
4061
4062 if (InsertFormula(LU, LUIdx, F))
4063 // If that formula hadn't been seen before, recurse to find more like
4064 // it.
4065 // Add check on Log16(AddOps.size()) - same as Log2_32(AddOps.size()) >> 2)
4066 // Because just Depth is not enough to bound compile time.
4067 // This means that every time AddOps.size() is greater 16^x we will add
4068 // x to Depth.
4069 GenerateReassociations(LU, LUIdx, LU.Formulae.back(),
4070 Depth + 1 + (Log2_32(AddOps.size()) >> 2));
4071 }
4072}
4073
4074/// Split out subexpressions from adds and the bases of addrecs.
4075void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx,
4076 Formula Base, unsigned Depth) {
4077 assert(Base.isCanonical(*L) && "Input must be in the canonical form");
4078 // Arbitrarily cap recursion to protect compile time.
4079 if (Depth >= 3)
4080 return;
4081
4082 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
4083 GenerateReassociationsImpl(LU, LUIdx, Base, Depth, i);
4084
4085 if (Base.Scale == 1)
4086 GenerateReassociationsImpl(LU, LUIdx, Base, Depth,
4087 /* Idx */ -1, /* IsScaledReg */ true);
4088}
4089
4090/// Generate a formula consisting of all of the loop-dominating registers added
4091/// into a single register.
4092void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx,
4093 Formula Base) {
4094 // This method is only interesting on a plurality of registers.
4095 if (Base.BaseRegs.size() + (Base.Scale == 1) +
4096 (Base.UnfoldedOffset.isNonZero()) <=
4097 1)
4098 return;
4099
4100 // Flatten the representation, i.e., reg1 + 1*reg2 => reg1 + reg2, before
4101 // processing the formula.
4102 Base.unscale();
4104 Formula NewBase = Base;
4105 NewBase.BaseRegs.clear();
4106 Type *CombinedIntegerType = nullptr;
4107 for (const SCEV *BaseReg : Base.BaseRegs) {
4108 if (SE.properlyDominates(BaseReg, L->getHeader()) &&
4109 !SE.hasComputableLoopEvolution(BaseReg, L)) {
4110 if (!CombinedIntegerType)
4111 CombinedIntegerType = SE.getEffectiveSCEVType(BaseReg->getType());
4112 Ops.push_back(BaseReg);
4113 }
4114 else
4115 NewBase.BaseRegs.push_back(BaseReg);
4116 }
4117
4118 // If no register is relevant, we're done.
4119 if (Ops.size() == 0)
4120 return;
4121
4122 // Utility function for generating the required variants of the combined
4123 // registers.
4124 auto GenerateFormula = [&](const SCEV *Sum) {
4125 Formula F = NewBase;
4126
4127 // TODO: If Sum is zero, it probably means ScalarEvolution missed an
4128 // opportunity to fold something. For now, just ignore such cases
4129 // rather than proceed with zero in a register.
4130 if (Sum->isZero())
4131 return;
4132
4133 F.BaseRegs.push_back(Sum);
4134 F.canonicalize(*L);
4135 (void)InsertFormula(LU, LUIdx, F);
4136 };
4137
4138 // If we collected at least two registers, generate a formula combining them.
4139 if (Ops.size() > 1) {
4140 SmallVector<SCEVUse, 4> OpsCopy(Ops); // Don't let SE modify Ops.
4141 GenerateFormula(SE.getAddExpr(OpsCopy));
4142 }
4143
4144 // If we have an unfolded offset, generate a formula combining it with the
4145 // registers collected.
4146 if (NewBase.UnfoldedOffset.isNonZero() && NewBase.UnfoldedOffset.isFixed()) {
4147 assert(CombinedIntegerType && "Missing a type for the unfolded offset");
4148 Ops.push_back(SE.getConstant(CombinedIntegerType,
4149 NewBase.UnfoldedOffset.getFixedValue(), true));
4150 NewBase.UnfoldedOffset = Immediate::getFixed(0);
4151 GenerateFormula(SE.getAddExpr(Ops));
4152 }
4153}
4154
4155/// Helper function for LSRInstance::GenerateSymbolicOffsets.
4156void LSRInstance::GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx,
4157 const Formula &Base, size_t Idx,
4158 bool IsScaledReg) {
4159 SCEVUse G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
4160 GlobalValue *GV = ExtractSymbol(G, SE);
4161 if (G->isZero() || !GV)
4162 return;
4163 Formula F = Base;
4164 F.BaseGV = GV;
4165 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
4166 return;
4167 if (IsScaledReg)
4168 F.ScaledReg = G;
4169 else
4170 F.BaseRegs[Idx] = G;
4171 (void)InsertFormula(LU, LUIdx, F);
4172}
4173
4174/// Generate reuse formulae using symbolic offsets.
4175void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx,
4176 Formula Base) {
4177 // We can't add a symbolic offset if the address already contains one.
4178 if (Base.BaseGV) return;
4179
4180 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
4181 GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, i);
4182 if (Base.Scale == 1)
4183 GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, /* Idx */ -1,
4184 /* IsScaledReg */ true);
4185}
4186
4187/// Helper function for LSRInstance::GenerateConstantOffsets.
4188void LSRInstance::GenerateConstantOffsetsImpl(
4189 LSRUse &LU, unsigned LUIdx, const Formula &Base,
4190 const SmallVectorImpl<Immediate> &Worklist, size_t Idx, bool IsScaledReg) {
4191
4192 auto GenerateOffset = [&](const SCEV *G, Immediate Offset) {
4193 Formula F = Base;
4194 if (!Base.BaseOffset.isCompatibleImmediate(Offset))
4195 return;
4196 F.BaseOffset = Base.BaseOffset.subUnsigned(Offset);
4197
4198 if (isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F)) {
4199 // Add the offset to the base register.
4200 const SCEV *NewOffset = Offset.getSCEV(SE, G->getType());
4201 const SCEV *NewG = SE.getAddExpr(NewOffset, G);
4202 // If it cancelled out, drop the base register, otherwise update it.
4203 if (NewG->isZero()) {
4204 if (IsScaledReg) {
4205 F.Scale = 0;
4206 F.ScaledReg = nullptr;
4207 } else
4208 F.deleteBaseReg(F.BaseRegs[Idx]);
4209 F.canonicalize(*L);
4210 } else if (IsScaledReg)
4211 F.ScaledReg = NewG;
4212 else
4213 F.BaseRegs[Idx] = NewG;
4214
4215 (void)InsertFormula(LU, LUIdx, F);
4216 }
4217 };
4218
4219 SCEVUse G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
4220
4221 // With constant offsets and constant steps, we can generate pre-inc
4222 // accesses by having the offset equal the step. So, for access #0 with a
4223 // step of 8, we generate a G - 8 base which would require the first access
4224 // to be ((G - 8) + 8),+,8. The pre-indexed access then updates the pointer
4225 // for itself and hopefully becomes the base for other accesses. This means
4226 // means that a single pre-indexed access can be generated to become the new
4227 // base pointer for each iteration of the loop, resulting in no extra add/sub
4228 // instructions for pointer updating.
4229 if ((AMK & TTI::AMK_PreIndexed) && LU.Kind == LSRUse::Address) {
4230 const APInt *StepInt;
4231 if (match(G, m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(StepInt)))) {
4232 int64_t Step = StepInt->isNegative() ? StepInt->getSExtValue()
4233 : StepInt->getZExtValue();
4234
4235 for (Immediate Offset : Worklist) {
4236 if (Offset.isFixed()) {
4237 Offset = Immediate::getFixed(Offset.getFixedValue() - Step);
4238 GenerateOffset(G, Offset);
4239 }
4240 }
4241 }
4242 }
4243 for (Immediate Offset : Worklist)
4244 GenerateOffset(G, Offset);
4245
4246 // TODO: It likely makes sense to extract the immediate corresponding to the
4247 // access type (i.e., set PreferScalable to AccessTy.MemTy &&
4248 // AccessTy.MemTy->isScalableTy()).
4249 Immediate Imm = ExtractImmediate(G, SE, /*PreferScalable=*/false);
4250 if (G->isZero() || Imm.isZero() ||
4251 !Base.BaseOffset.isCompatibleImmediate(Imm))
4252 return;
4253 Formula F = Base;
4254 F.BaseOffset = F.BaseOffset.addUnsigned(Imm);
4255 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
4256 return;
4257 if (IsScaledReg) {
4258 F.ScaledReg = G;
4259 } else {
4260 F.BaseRegs[Idx] = G;
4261 // We may generate non canonical Formula if G is a recurrent expr reg
4262 // related with current loop while F.ScaledReg is not.
4263 F.canonicalize(*L);
4264 }
4265 (void)InsertFormula(LU, LUIdx, F);
4266}
4267
4268/// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets.
4269void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx,
4270 Formula Base) {
4271 // TODO: For now, just add the min and max offset, because it usually isn't
4272 // worthwhile looking at everything inbetween.
4274 Worklist.push_back(LU.MinOffset);
4275 if (LU.MaxOffset != LU.MinOffset)
4276 Worklist.push_back(LU.MaxOffset);
4277
4278 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
4279 GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, i);
4280 if (Base.Scale == 1)
4281 GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, /* Idx */ -1,
4282 /* IsScaledReg */ true);
4283}
4284
4285/// For ICmpZero, check to see if we can scale up the comparison. For example, x
4286/// == y -> x*c == y*c.
4287void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx,
4288 Formula Base) {
4289 if (LU.Kind != LSRUse::ICmpZero) return;
4290
4291 // Determine the integer type for the base formula.
4292 Type *IntTy = Base.getType();
4293 if (!IntTy) return;
4294 if (SE.getTypeSizeInBits(IntTy) > 64) return;
4295
4296 // Don't do this if there is more than one offset.
4297 if (LU.MinOffset != LU.MaxOffset) return;
4298
4299 // Check if transformation is valid. It is illegal to multiply pointer.
4300 if (Base.ScaledReg && Base.ScaledReg->getType()->isPointerTy())
4301 return;
4302 for (const SCEV *BaseReg : Base.BaseRegs)
4303 if (BaseReg->getType()->isPointerTy())
4304 return;
4305 assert(!Base.BaseGV && "ICmpZero use is not legal!");
4306
4307 // Check each interesting stride.
4308 for (int64_t Factor : Factors) {
4309 // Check that Factor can be represented by IntTy
4310 if (!ConstantInt::isValueValidForType(IntTy, Factor))
4311 continue;
4312 // Check that the multiplication doesn't overflow.
4313 if (Base.BaseOffset.isMin() && Factor == -1)
4314 continue;
4315 // Not supporting scalable immediates.
4316 if (Base.BaseOffset.isNonZero() && Base.BaseOffset.isScalable())
4317 continue;
4318 Immediate NewBaseOffset = Base.BaseOffset.mulUnsigned(Factor);
4319 assert(Factor != 0 && "Zero factor not expected!");
4320 if (NewBaseOffset.getFixedValue() / Factor !=
4321 Base.BaseOffset.getFixedValue())
4322 continue;
4323 // If the offset will be truncated at this use, check that it is in bounds.
4324 if (!IntTy->isPointerTy() &&
4325 !ConstantInt::isValueValidForType(IntTy, NewBaseOffset.getFixedValue()))
4326 continue;
4327
4328 // Check that multiplying with the use offset doesn't overflow.
4329 Immediate Offset = LU.MinOffset;
4330 if (Offset.isMin() && Factor == -1)
4331 continue;
4332 Offset = Offset.mulUnsigned(Factor);
4333 if (Offset.getFixedValue() / Factor != LU.MinOffset.getFixedValue())
4334 continue;
4335 // If the offset will be truncated at this use, check that it is in bounds.
4336 if (!IntTy->isPointerTy() &&
4337 !ConstantInt::isValueValidForType(IntTy, Offset.getFixedValue()))
4338 continue;
4339
4340 Formula F = Base;
4341 F.BaseOffset = NewBaseOffset;
4342
4343 // Check that this scale is legal.
4344 if (!isLegalUse(TTI, Offset, Offset, LU.Kind, LU.AccessTy, F))
4345 continue;
4346
4347 // Compensate for the use having MinOffset built into it.
4348 F.BaseOffset = F.BaseOffset.addUnsigned(Offset).subUnsigned(LU.MinOffset);
4349
4350 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
4351
4352 // Check that multiplying with each base register doesn't overflow.
4353 for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) {
4354 F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS);
4355 if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i])
4356 goto next;
4357 }
4358
4359 // Check that multiplying with the scaled register doesn't overflow.
4360 if (F.ScaledReg) {
4361 F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS);
4362 if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg)
4363 continue;
4364 }
4365
4366 // Check that multiplying with the unfolded offset doesn't overflow.
4367 if (F.UnfoldedOffset.isNonZero()) {
4368 if (F.UnfoldedOffset.isMin() && Factor == -1)
4369 continue;
4370 F.UnfoldedOffset = F.UnfoldedOffset.mulUnsigned(Factor);
4371 if (F.UnfoldedOffset.getFixedValue() / Factor !=
4372 Base.UnfoldedOffset.getFixedValue())
4373 continue;
4374 // If the offset will be truncated, check that it is in bounds.
4376 IntTy, F.UnfoldedOffset.getFixedValue()))
4377 continue;
4378 }
4379
4380 // If we make it here and it's legal, add it.
4381 (void)InsertFormula(LU, LUIdx, F);
4382 next:;
4383 }
4384}
4385
4386/// Generate stride factor reuse formulae by making use of scaled-offset address
4387/// modes, for example.
4388void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) {
4389 // Determine the integer type for the base formula.
4390 Type *IntTy = Base.getType();
4391 if (!IntTy) return;
4392
4393 // If this Formula already has a scaled register, we can't add another one.
4394 // Try to unscale the formula to generate a better scale.
4395 if (Base.Scale != 0 && !Base.unscale())
4396 return;
4397
4398 assert(Base.Scale == 0 && "unscale did not did its job!");
4399
4400 // Check each interesting stride.
4401 for (int64_t Factor : Factors) {
4402 Base.Scale = Factor;
4403 Base.HasBaseReg = Base.BaseRegs.size() > 1;
4404 // Check whether this scale is going to be legal.
4405 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4406 Base)) {
4407 // As a special-case, handle special out-of-loop Basic users specially.
4408 // TODO: Reconsider this special case.
4409 if (LU.Kind == LSRUse::Basic &&
4410 isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special,
4411 LU.AccessTy, Base) &&
4412 LU.AllFixupsOutsideLoop)
4413 LU.Kind = LSRUse::Special;
4414 else
4415 continue;
4416 }
4417 // For an ICmpZero, negating a solitary base register won't lead to
4418 // new solutions.
4419 if (LU.Kind == LSRUse::ICmpZero && !Base.HasBaseReg &&
4420 Base.BaseOffset.isZero() && !Base.BaseGV)
4421 continue;
4422 // For each addrec base reg, if its loop is current loop, apply the scale.
4423 for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
4424 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i]);
4425 if (AR && (AR->getLoop() == L || LU.AllFixupsOutsideLoop)) {
4426 const SCEV *FactorS = SE.getConstant(IntTy, Factor);
4427 if (FactorS->isZero())
4428 continue;
4429 // Divide out the factor, ignoring high bits, since we'll be
4430 // scaling the value back up in the end.
4431 if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true))
4432 if (!Quotient->isZero()) {
4433 // TODO: This could be optimized to avoid all the copying.
4434 Formula F = Base;
4435 F.ScaledReg = Quotient;
4436 F.deleteBaseReg(F.BaseRegs[i]);
4437 // The canonical representation of 1*reg is reg, which is already in
4438 // Base. In that case, do not try to insert the formula, it will be
4439 // rejected anyway.
4440 if (F.Scale == 1 && (F.BaseRegs.empty() ||
4441 (AR->getLoop() != L && LU.AllFixupsOutsideLoop)))
4442 continue;
4443 // If AllFixupsOutsideLoop is true and F.Scale is 1, we may generate
4444 // non canonical Formula with ScaledReg's loop not being L.
4445 if (F.Scale == 1 && LU.AllFixupsOutsideLoop)
4446 F.canonicalize(*L);
4447 (void)InsertFormula(LU, LUIdx, F);
4448 }
4449 }
4450 }
4451 }
4452}
4453
4454/// Extend/Truncate \p Expr to \p ToTy considering post-inc uses in \p Loops.
4455/// For all PostIncLoopSets in \p Loops, first de-normalize \p Expr, then
4456/// perform the extension/truncate and normalize again, as the normalized form
4457/// can result in folds that are not valid in the post-inc use contexts. The
4458/// expressions for all PostIncLoopSets must match, otherwise return nullptr.
4459static const SCEV *
4461 const SCEV *Expr, Type *ToTy,
4462 ScalarEvolution &SE) {
4463 const SCEV *Result = nullptr;
4464 for (auto &L : Loops) {
4465 auto *DenormExpr = denormalizeForPostIncUse(Expr, L, SE);
4466 const SCEV *NewDenormExpr = SE.getAnyExtendExpr(DenormExpr, ToTy);
4467 const SCEV *New = normalizeForPostIncUse(NewDenormExpr, L, SE);
4468 if (!New || (Result && New != Result))
4469 return nullptr;
4470 Result = New;
4471 }
4472
4473 assert(Result && "failed to create expression");
4474 return Result;
4475}
4476
4477/// Generate reuse formulae from different IV types.
4478void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) {
4479 // Don't bother truncating symbolic values.
4480 if (Base.BaseGV) return;
4481
4482 // Determine the integer type for the base formula.
4483 Type *DstTy = Base.getType();
4484 if (!DstTy) return;
4485 if (DstTy->isPointerTy())
4486 return;
4487
4488 // It is invalid to extend a pointer type so exit early if ScaledReg or
4489 // any of the BaseRegs are pointers.
4490 if (Base.ScaledReg && Base.ScaledReg->getType()->isPointerTy())
4491 return;
4492 if (any_of(Base.BaseRegs,
4493 [](const SCEV *S) { return S->getType()->isPointerTy(); }))
4494 return;
4495
4497 for (auto &LF : LU.Fixups)
4498 Loops.push_back(LF.PostIncLoops);
4499
4500 for (Type *SrcTy : Types) {
4501 if (SrcTy != DstTy && TTI.isTruncateFree(SrcTy, DstTy)) {
4502 Formula F = Base;
4503
4504 // Sometimes SCEV is able to prove zero during ext transform. It may
4505 // happen if SCEV did not do all possible transforms while creating the
4506 // initial node (maybe due to depth limitations), but it can do them while
4507 // taking ext.
4508 if (F.ScaledReg) {
4509 const SCEV *NewScaledReg =
4510 getAnyExtendConsideringPostIncUses(Loops, F.ScaledReg, SrcTy, SE);
4511 if (!NewScaledReg || NewScaledReg->isZero())
4512 continue;
4513 F.ScaledReg = NewScaledReg;
4514 }
4515 bool HasZeroBaseReg = false;
4516 for (const SCEV *&BaseReg : F.BaseRegs) {
4517 const SCEV *NewBaseReg =
4518 getAnyExtendConsideringPostIncUses(Loops, BaseReg, SrcTy, SE);
4519 if (!NewBaseReg || NewBaseReg->isZero()) {
4520 HasZeroBaseReg = true;
4521 break;
4522 }
4523 BaseReg = NewBaseReg;
4524 }
4525 if (HasZeroBaseReg)
4526 continue;
4527
4528 // TODO: This assumes we've done basic processing on all uses and
4529 // have an idea what the register usage is.
4530 if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses))
4531 continue;
4532
4533 F.canonicalize(*L);
4534 (void)InsertFormula(LU, LUIdx, F);
4535 }
4536 }
4537}
4538
4539namespace {
4540
4541/// Helper class for GenerateCrossUseConstantOffsets. It's used to defer
4542/// modifications so that the search phase doesn't have to worry about the data
4543/// structures moving underneath it.
4544struct WorkItem {
4545 size_t LUIdx;
4546 Immediate Imm;
4547 const SCEV *OrigReg;
4548
4549 WorkItem(size_t LI, Immediate I, const SCEV *R)
4550 : LUIdx(LI), Imm(I), OrigReg(R) {}
4551
4552 void print(raw_ostream &OS) const;
4553 void dump() const;
4554};
4555
4556} // end anonymous namespace
4557
4558#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4559void WorkItem::print(raw_ostream &OS) const {
4560 OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx
4561 << " , add offset " << Imm;
4562}
4563
4564LLVM_DUMP_METHOD void WorkItem::dump() const {
4565 print(errs()); errs() << '\n';
4566}
4567#endif
4568
4569/// Look for registers which are a constant distance apart and try to form reuse
4570/// opportunities between them.
4571void LSRInstance::GenerateCrossUseConstantOffsets() {
4572 // Group the registers by their value without any added constant offset.
4573 using ImmMapTy = std::map<Immediate, const SCEV *, KeyOrderTargetImmediate>;
4574
4575 DenseMap<const SCEV *, ImmMapTy> Map;
4576 DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
4578 for (const SCEV *Use : RegUses) {
4579 SCEVUse Reg = Use; // Make a copy for ExtractImmediate to modify.
4580 // TODO: Extract both scalable and fixed immediates (if present)?
4581 Immediate Imm = ExtractImmediate(Reg, SE);
4582 auto Pair = Map.try_emplace(Reg);
4583 if (Pair.second)
4584 Sequence.push_back(Reg);
4585 Pair.first->second.insert(std::make_pair(Imm, Use));
4586 UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(Use);
4587 }
4588
4589 // Now examine each set of registers with the same base value. Build up
4590 // a list of work to do and do the work in a separate step so that we're
4591 // not adding formulae and register counts while we're searching.
4592 SmallVector<WorkItem, 32> WorkItems;
4593 SmallSet<std::pair<size_t, Immediate>, 32, KeyOrderSizeTAndImmediate>
4594 UniqueItems;
4595 for (const SCEV *Reg : Sequence) {
4596 const ImmMapTy &Imms = Map.find(Reg)->second;
4597
4598 // It's not worthwhile looking for reuse if there's only one offset.
4599 if (Imms.size() == 1)
4600 continue;
4601
4602 LLVM_DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
4603 for (const auto &Entry
4604 : Imms) dbgs()
4605 << ' ' << Entry.first;
4606 dbgs() << '\n');
4607
4608 // Examine each offset.
4609 for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
4610 J != JE; ++J) {
4611 const SCEV *OrigReg = J->second;
4612
4613 Immediate JImm = J->first;
4614 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
4615
4616 if (!isa<SCEVConstant>(OrigReg) &&
4617 UsedByIndicesMap[Reg].count() == 1) {
4618 LLVM_DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg
4619 << '\n');
4620 continue;
4621 }
4622
4623 // Conservatively examine offsets between this orig reg a few selected
4624 // other orig regs.
4625 Immediate First = Imms.begin()->first;
4626 Immediate Last = std::prev(Imms.end())->first;
4627 if (!First.isCompatibleImmediate(Last)) {
4628 LLVM_DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg
4629 << "\n");
4630 continue;
4631 }
4632 // Only scalable if both terms are scalable, or if one is scalable and
4633 // the other is 0.
4634 bool Scalable = First.isScalable() || Last.isScalable();
4635 int64_t FI = First.getKnownMinValue();
4636 int64_t LI = Last.getKnownMinValue();
4637 // Compute (First + Last) / 2 without overflow using the fact that
4638 // First + Last = 2 * (First + Last) + (First ^ Last).
4639 int64_t Avg = (FI & LI) + ((FI ^ LI) >> 1);
4640 // If the result is negative and FI is odd and LI even (or vice versa),
4641 // we rounded towards -inf. Add 1 in that case, to round towards 0.
4642 Avg = Avg + ((FI ^ LI) & ((uint64_t)Avg >> 63));
4643 ImmMapTy::const_iterator OtherImms[] = {
4644 Imms.begin(), std::prev(Imms.end()),
4645 Imms.lower_bound(Immediate::get(Avg, Scalable))};
4646 for (const auto &M : OtherImms) {
4647 if (M == J || M == JE) continue;
4648 if (!JImm.isCompatibleImmediate(M->first))
4649 continue;
4650
4651 // Compute the difference between the two.
4652 Immediate Imm = JImm.subUnsigned(M->first);
4653 for (unsigned LUIdx : UsedByIndices.set_bits())
4654 // Make a memo of this use, offset, and register tuple.
4655 if (UniqueItems.insert(std::make_pair(LUIdx, Imm)).second)
4656 WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg));
4657 }
4658 }
4659 }
4660
4661 Map.clear();
4662 Sequence.clear();
4663 UsedByIndicesMap.clear();
4664 UniqueItems.clear();
4665
4666 // Now iterate through the worklist and add new formulae.
4667 for (const WorkItem &WI : WorkItems) {
4668 size_t LUIdx = WI.LUIdx;
4669 LSRUse &LU = Uses[LUIdx];
4670 Immediate Imm = WI.Imm;
4671 const SCEV *OrigReg = WI.OrigReg;
4672
4673 Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
4674 const SCEV *NegImmS = Imm.getNegativeSCEV(SE, IntTy);
4675 unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
4676
4677 // TODO: Use a more targeted data structure.
4678 for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
4679 Formula F = LU.Formulae[L];
4680 // FIXME: The code for the scaled and unscaled registers looks
4681 // very similar but slightly different. Investigate if they
4682 // could be merged. That way, we would not have to unscale the
4683 // Formula.
4684 F.unscale();
4685 // Use the immediate in the scaled register.
4686 if (F.ScaledReg == OrigReg) {
4687 if (!F.BaseOffset.isCompatibleImmediate(Imm))
4688 continue;
4689 Immediate Offset = F.BaseOffset.addUnsigned(Imm.mulUnsigned(F.Scale));
4690 // Don't create 50 + reg(-50).
4691 const SCEV *S = Offset.getNegativeSCEV(SE, IntTy);
4692 if (F.referencesReg(S))
4693 continue;
4694 Formula NewF = F;
4695 NewF.BaseOffset = Offset;
4696 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4697 NewF))
4698 continue;
4699 NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg);
4700
4701 // If the new scale is a constant in a register, and adding the constant
4702 // value to the immediate would produce a value closer to zero than the
4703 // immediate itself, then the formula isn't worthwhile.
4704 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg)) {
4705 // FIXME: Do we need to do something for scalable immediates here?
4706 // A scalable SCEV won't be constant, but we might still have
4707 // something in the offset? Bail out for now to be safe.
4708 if (NewF.BaseOffset.isNonZero() && NewF.BaseOffset.isScalable())
4709 continue;
4710 if (C->getValue()->isNegative() !=
4711 (NewF.BaseOffset.isLessThanZero()) &&
4712 (C->getAPInt().abs() * APInt(BitWidth, F.Scale))
4713 .ule(std::abs(NewF.BaseOffset.getFixedValue())))
4714 continue;
4715 }
4716
4717 // OK, looks good.
4718 NewF.canonicalize(*this->L);
4719 (void)InsertFormula(LU, LUIdx, NewF);
4720 } else {
4721 // Use the immediate in a base register.
4722 for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) {
4723 const SCEV *BaseReg = F.BaseRegs[N];
4724 if (BaseReg != OrigReg)
4725 continue;
4726 Formula NewF = F;
4727 if (!NewF.BaseOffset.isCompatibleImmediate(Imm) ||
4728 !NewF.UnfoldedOffset.isCompatibleImmediate(Imm) ||
4729 !NewF.BaseOffset.isCompatibleImmediate(NewF.UnfoldedOffset))
4730 continue;
4731 NewF.BaseOffset = NewF.BaseOffset.addUnsigned(Imm);
4732 if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset,
4733 LU.Kind, LU.AccessTy, NewF)) {
4734 if (AMK == TTI::AMK_PostIndexed &&
4735 mayUsePostIncMode(TTI, LU, OrigReg, this->L, SE))
4736 continue;
4737 Immediate NewUnfoldedOffset = NewF.UnfoldedOffset.addUnsigned(Imm);
4738 if (!isLegalAddImmediate(TTI, NewUnfoldedOffset))
4739 continue;
4740 NewF = F;
4741 NewF.UnfoldedOffset = NewUnfoldedOffset;
4742 }
4743 NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg);
4744
4745 // If the new formula has a constant in a register, and adding the
4746 // constant value to the immediate would produce a value closer to
4747 // zero than the immediate itself, then the formula isn't worthwhile.
4748 for (const SCEV *NewReg : NewF.BaseRegs)
4749 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewReg)) {
4750 if (NewF.BaseOffset.isNonZero() && NewF.BaseOffset.isScalable())
4751 goto skip_formula;
4752 if ((C->getAPInt() + NewF.BaseOffset.getFixedValue())
4753 .abs()
4754 .slt(std::abs(NewF.BaseOffset.getFixedValue())) &&
4755 (C->getAPInt() + NewF.BaseOffset.getFixedValue())
4756 .countr_zero() >=
4758 NewF.BaseOffset.getFixedValue()))
4759 goto skip_formula;
4760 }
4761
4762 // Ok, looks good.
4763 NewF.canonicalize(*this->L);
4764 (void)InsertFormula(LU, LUIdx, NewF);
4765 break;
4766 skip_formula:;
4767 }
4768 }
4769 }
4770 }
4771}
4772
4773/// Generate formulae for each use.
4774void
4775LSRInstance::GenerateAllReuseFormulae() {
4776 // This is split into multiple loops so that hasRegsUsedByUsesOtherThan
4777 // queries are more precise.
4778 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4779 LSRUse &LU = Uses[LUIdx];
4780 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4781 GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
4782 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4783 GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
4784 }
4785 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4786 LSRUse &LU = Uses[LUIdx];
4787 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4788 GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]);
4789 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4790 GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]);
4791 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4792 GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]);
4793 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4794 GenerateScales(LU, LUIdx, LU.Formulae[i]);
4795 }
4796 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4797 LSRUse &LU = Uses[LUIdx];
4798 for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4799 GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
4800 }
4801
4802 GenerateCrossUseConstantOffsets();
4803
4804 LLVM_DEBUG(dbgs() << "\n"
4805 "After generating reuse formulae:\n";
4806 print_uses(dbgs()));
4807}
4808
4809/// If there are multiple formulae with the same set of registers used
4810/// by other uses, pick the best one and delete the others.
4811void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
4812 DenseSet<const SCEV *> VisitedRegs;
4813 SmallPtrSet<const SCEV *, 16> Regs;
4814 SmallPtrSet<const SCEV *, 16> LoserRegs;
4815#ifndef NDEBUG
4816 bool ChangedFormulae = false;
4817#endif
4818
4819 // Collect the best formula for each unique set of shared registers. This
4820 // is reset for each use.
4821 using BestFormulaeTy = DenseMap<SmallVector<const SCEV *, 4>, size_t>;
4822
4823 BestFormulaeTy BestFormulae;
4824
4825 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4826 LSRUse &LU = Uses[LUIdx];
4827 LLVM_DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs());
4828 dbgs() << '\n');
4829
4830 bool Any = false;
4831 for (size_t FIdx = 0, NumForms = LU.Formulae.size();
4832 FIdx != NumForms; ++FIdx) {
4833 Formula &F = LU.Formulae[FIdx];
4834
4835 // Some formulas are instant losers. For example, they may depend on
4836 // nonexistent AddRecs from other loops. These need to be filtered
4837 // immediately, otherwise heuristics could choose them over others leading
4838 // to an unsatisfactory solution. Passing LoserRegs into RateFormula here
4839 // avoids the need to recompute this information across formulae using the
4840 // same bad AddRec. Passing LoserRegs is also essential unless we remove
4841 // the corresponding bad register from the Regs set.
4842 Cost CostF(L, SE, TTI, AMK);
4843 Regs.clear();
4844 CostF.RateFormula(F, Regs, VisitedRegs, LU, HardwareLoopProfitable,
4845 &LoserRegs);
4846 if (CostF.isLoser()) {
4847 // During initial formula generation, undesirable formulae are generated
4848 // by uses within other loops that have some non-trivial address mode or
4849 // use the postinc form of the IV. LSR needs to provide these formulae
4850 // as the basis of rediscovering the desired formula that uses an AddRec
4851 // corresponding to the existing phi. Once all formulae have been
4852 // generated, these initial losers may be pruned.
4853 LLVM_DEBUG(dbgs() << " Filtering loser "; F.print(dbgs());
4854 dbgs() << "\n");
4855 }
4856 else {
4858 for (const SCEV *Reg : F.BaseRegs) {
4859 if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx))
4860 Key.push_back(Reg);
4861 }
4862 if (F.ScaledReg &&
4863 RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx))
4864 Key.push_back(F.ScaledReg);
4865 // Unstable sort by host order ok, because this is only used for
4866 // uniquifying.
4867 llvm::sort(Key);
4868
4869 std::pair<BestFormulaeTy::const_iterator, bool> P =
4870 BestFormulae.insert(std::make_pair(Key, FIdx));
4871 if (P.second)
4872 continue;
4873
4874 Formula &Best = LU.Formulae[P.first->second];
4875
4876 Cost CostBest(L, SE, TTI, AMK);
4877 Regs.clear();
4878 CostBest.RateFormula(Best, Regs, VisitedRegs, LU,
4879 HardwareLoopProfitable);
4880 if (CostF.isLess(CostBest))
4881 std::swap(F, Best);
4882 LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
4883 dbgs() << "\n"
4884 " in favor of formula ";
4885 Best.print(dbgs()); dbgs() << '\n');
4886 }
4887#ifndef NDEBUG
4888 ChangedFormulae = true;
4889#endif
4890 LU.DeleteFormula(F);
4891 --FIdx;
4892 --NumForms;
4893 Any = true;
4894 }
4895
4896 // Now that we've filtered out some formulae, recompute the Regs set.
4897 if (Any)
4898 LU.RecomputeRegs(LUIdx, RegUses);
4899
4900 // Reset this to prepare for the next use.
4901 BestFormulae.clear();
4902 }
4903
4904 LLVM_DEBUG(if (ChangedFormulae) {
4905 dbgs() << "\n"
4906 "After filtering out undesirable candidates:\n";
4907 print_uses(dbgs());
4908 });
4909}
4910
4911/// Estimate the worst-case number of solutions the solver might have to
4912/// consider. It almost never considers this many solutions because it prune the
4913/// search space, but the pruning isn't always sufficient.
4914size_t LSRInstance::EstimateSearchSpaceComplexity() const {
4915 size_t Power = 1;
4916 for (const LSRUse &LU : Uses) {
4917 size_t FSize = LU.Formulae.size();
4918 if (FSize >= ComplexityLimit) {
4919 Power = ComplexityLimit;
4920 break;
4921 }
4922 Power *= FSize;
4923 if (Power >= ComplexityLimit)
4924 break;
4925 }
4926 return Power;
4927}
4928
4929/// When one formula uses a superset of the registers of another formula, it
4930/// won't help reduce register pressure (though it may not necessarily hurt
4931/// register pressure); remove it to simplify the system.
4932void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
4933 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
4934 LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
4935
4936 LLVM_DEBUG(dbgs() << "Narrowing the search space by eliminating formulae "
4937 "which use a superset of registers used by other "
4938 "formulae.\n");
4939
4940 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4941 LSRUse &LU = Uses[LUIdx];
4942 bool Any = false;
4943 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
4944 Formula &F = LU.Formulae[i];
4945 if (F.BaseOffset.isNonZero() && F.BaseOffset.isScalable())
4946 continue;
4947 // Look for a formula with a constant or GV in a register. If the use
4948 // also has a formula with that same value in an immediate field,
4949 // delete the one that uses a register.
4951 I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
4952 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
4953 Formula NewF = F;
4954 //FIXME: Formulas should store bitwidth to do wrapping properly.
4955 // See PR41034.
4956 NewF.BaseOffset =
4957 Immediate::getFixed(NewF.BaseOffset.getFixedValue() +
4958 (uint64_t)C->getValue()->getSExtValue());
4959 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
4960 (I - F.BaseRegs.begin()));
4961 if (LU.HasFormulaWithSameRegs(NewF)) {
4962 LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs());
4963 dbgs() << '\n');
4964 LU.DeleteFormula(F);
4965 --i;
4966 --e;
4967 Any = true;
4968 break;
4969 }
4970 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) {
4971 if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue()))
4972 if (!F.BaseGV) {
4973 Formula NewF = F;
4974 NewF.BaseGV = GV;
4975 NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
4976 (I - F.BaseRegs.begin()));
4977 if (LU.HasFormulaWithSameRegs(NewF)) {
4978 LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs());
4979 dbgs() << '\n');
4980 LU.DeleteFormula(F);
4981 --i;
4982 --e;
4983 Any = true;
4984 break;
4985 }
4986 }
4987 }
4988 }
4989 }
4990 if (Any)
4991 LU.RecomputeRegs(LUIdx, RegUses);
4992 }
4993
4994 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4995 }
4996}
4997
4998/// When there are many registers for expressions like A, A+1, A+2, etc.,
4999/// allocate a single register for them.
5000void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
5001 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5002 return;
5003
5004 LLVM_DEBUG(
5005 dbgs() << "The search space is too complex.\n"
5006 "Narrowing the search space by assuming that uses separated "
5007 "by a constant offset will use the same registers.\n");
5008
5009 // This is especially useful for unrolled loops.
5010
5011 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5012 LSRUse &LU = Uses[LUIdx];
5013 for (const Formula &F : LU.Formulae) {
5014 if (F.BaseOffset.isZero() || (F.Scale != 0 && F.Scale != 1))
5015 continue;
5016 assert((LU.Kind == LSRUse::Address || LU.Kind == LSRUse::ICmpZero) &&
5017 "Only address and cmp uses expected to have nonzero BaseOffset");
5018
5019 LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU);
5020 if (!LUThatHas)
5021 continue;
5022
5023 if (!reconcileNewOffset(*LUThatHas, F.BaseOffset, /*HasBaseReg=*/ false,
5024 LU.Kind, LU.AccessTy))
5025 continue;
5026
5027 LLVM_DEBUG(dbgs() << " Deleting use "; LU.print(dbgs()); dbgs() << '\n');
5028
5029 LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
5030 LUThatHas->AllFixupsUnconditional &= LU.AllFixupsUnconditional;
5031
5032 // Transfer the fixups of LU to LUThatHas.
5033 for (LSRFixup &Fixup : LU.Fixups) {
5034 Fixup.Offset += F.BaseOffset;
5035 LUThatHas->pushFixup(Fixup);
5036 LLVM_DEBUG(dbgs() << "New fixup has offset " << Fixup.Offset << '\n');
5037 }
5038
5039#ifndef NDEBUG
5040 Type *FixupType = LUThatHas->Fixups[0].OperandValToReplace->getType();
5041 for (LSRFixup &Fixup : LUThatHas->Fixups)
5042 assert(Fixup.OperandValToReplace->getType() == FixupType &&
5043 "Expected all fixups to have the same type");
5044#endif
5045
5046 // Delete formulae from the new use which are no longer legal.
5047 bool Any = false;
5048 for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
5049 Formula &F = LUThatHas->Formulae[i];
5050 if (!isLegalUse(TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset,
5051 LUThatHas->Kind, LUThatHas->AccessTy, F)) {
5052 LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
5053 LUThatHas->DeleteFormula(F);
5054 --i;
5055 --e;
5056 Any = true;
5057 }
5058 }
5059
5060 if (Any)
5061 LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses);
5062
5063 // Delete the old use.
5064 DeleteUse(LU, LUIdx);
5065 --LUIdx;
5066 --NumUses;
5067 break;
5068 }
5069 }
5070
5071 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5072}
5073
5074/// Call FilterOutUndesirableDedicatedRegisters again, if necessary, now that
5075/// we've done more filtering, as it may be able to find more formulae to
5076/// eliminate.
5077void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
5078 if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
5079 LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
5080
5081 LLVM_DEBUG(dbgs() << "Narrowing the search space by re-filtering out "
5082 "undesirable dedicated registers.\n");
5083
5084 FilterOutUndesirableDedicatedRegisters();
5085
5086 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5087 }
5088}
5089
5090/// If a LSRUse has multiple formulae with the same ScaledReg and Scale.
5091/// Pick the best one and delete the others.
5092/// This narrowing heuristic is to keep as many formulae with different
5093/// Scale and ScaledReg pair as possible while narrowing the search space.
5094/// The benefit is that it is more likely to find out a better solution
5095/// from a formulae set with more Scale and ScaledReg variations than
5096/// a formulae set with the same Scale and ScaledReg. The picking winner
5097/// reg heuristic will often keep the formulae with the same Scale and
5098/// ScaledReg and filter others, and we want to avoid that if possible.
5099void LSRInstance::NarrowSearchSpaceByFilterFormulaWithSameScaledReg() {
5100 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5101 return;
5102
5103 LLVM_DEBUG(
5104 dbgs() << "The search space is too complex.\n"
5105 "Narrowing the search space by choosing the best Formula "
5106 "from the Formulae with the same Scale and ScaledReg.\n");
5107
5108 // Map the "Scale * ScaledReg" pair to the best formula of current LSRUse.
5109 using BestFormulaeTy = DenseMap<std::pair<const SCEV *, int64_t>, size_t>;
5110
5111 BestFormulaeTy BestFormulae;
5112#ifndef NDEBUG
5113 bool ChangedFormulae = false;
5114#endif
5115 DenseSet<const SCEV *> VisitedRegs;
5116 SmallPtrSet<const SCEV *, 16> Regs;
5117
5118 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5119 LSRUse &LU = Uses[LUIdx];
5120 LLVM_DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs());
5121 dbgs() << '\n');
5122
5123 // Return true if Formula FA is better than Formula FB.
5124 auto IsBetterThan = [&](Formula &FA, Formula &FB) {
5125 // First we will try to choose the Formula with fewer new registers.
5126 // For a register used by current Formula, the more the register is
5127 // shared among LSRUses, the less we increase the register number
5128 // counter of the formula.
5129 size_t FARegNum = 0;
5130 for (const SCEV *Reg : FA.BaseRegs) {
5131 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(Reg);
5132 FARegNum += (NumUses - UsedByIndices.count() + 1);
5133 }
5134 size_t FBRegNum = 0;
5135 for (const SCEV *Reg : FB.BaseRegs) {
5136 const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(Reg);
5137 FBRegNum += (NumUses - UsedByIndices.count() + 1);
5138 }
5139 if (FARegNum != FBRegNum)
5140 return FARegNum < FBRegNum;
5141
5142 // If the new register numbers are the same, choose the Formula with
5143 // less Cost.
5144 Cost CostFA(L, SE, TTI, AMK);
5145 Cost CostFB(L, SE, TTI, AMK);
5146 Regs.clear();
5147 CostFA.RateFormula(FA, Regs, VisitedRegs, LU, HardwareLoopProfitable);
5148 Regs.clear();
5149 CostFB.RateFormula(FB, Regs, VisitedRegs, LU, HardwareLoopProfitable);
5150 return CostFA.isLess(CostFB);
5151 };
5152
5153 bool Any = false;
5154 for (size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
5155 ++FIdx) {
5156 Formula &F = LU.Formulae[FIdx];
5157 if (!F.ScaledReg)
5158 continue;
5159 auto P = BestFormulae.insert({{F.ScaledReg, F.Scale}, FIdx});
5160 if (P.second)
5161 continue;
5162
5163 Formula &Best = LU.Formulae[P.first->second];
5164 if (IsBetterThan(F, Best))
5165 std::swap(F, Best);
5166 LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
5167 dbgs() << "\n"
5168 " in favor of formula ";
5169 Best.print(dbgs()); dbgs() << '\n');
5170#ifndef NDEBUG
5171 ChangedFormulae = true;
5172#endif
5173 LU.DeleteFormula(F);
5174 --FIdx;
5175 --NumForms;
5176 Any = true;
5177 }
5178 if (Any)
5179 LU.RecomputeRegs(LUIdx, RegUses);
5180
5181 // Reset this to prepare for the next use.
5182 BestFormulae.clear();
5183 }
5184
5185 LLVM_DEBUG(if (ChangedFormulae) {
5186 dbgs() << "\n"
5187 "After filtering out undesirable candidates:\n";
5188 print_uses(dbgs());
5189 });
5190}
5191
5192/// If we are over the complexity limit, filter out any post-inc prefering
5193/// variables to only post-inc values.
5194void LSRInstance::NarrowSearchSpaceByFilterPostInc() {
5195 if (AMK != TTI::AMK_PostIndexed)
5196 return;
5197 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5198 return;
5199
5200 LLVM_DEBUG(dbgs() << "The search space is too complex.\n"
5201 "Narrowing the search space by choosing the lowest "
5202 "register Formula for PostInc Uses.\n");
5203
5204 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5205 LSRUse &LU = Uses[LUIdx];
5206
5207 if (LU.Kind != LSRUse::Address)
5208 continue;
5209 if (!TTI.isIndexedLoadLegal(TTI.MIM_PostInc, LU.AccessTy.getType()) &&
5210 !TTI.isIndexedStoreLegal(TTI.MIM_PostInc, LU.AccessTy.getType()))
5211 continue;
5212
5213 size_t MinRegs = std::numeric_limits<size_t>::max();
5214 for (const Formula &F : LU.Formulae)
5215 MinRegs = std::min(F.getNumRegs(), MinRegs);
5216
5217 bool Any = false;
5218 for (size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
5219 ++FIdx) {
5220 Formula &F = LU.Formulae[FIdx];
5221 if (F.getNumRegs() > MinRegs) {
5222 LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
5223 dbgs() << "\n");
5224 LU.DeleteFormula(F);
5225 --FIdx;
5226 --NumForms;
5227 Any = true;
5228 }
5229 }
5230 if (Any)
5231 LU.RecomputeRegs(LUIdx, RegUses);
5232
5233 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5234 break;
5235 }
5236
5237 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5238}
5239
5240void LSRInstance::NarrowSearchSpaceByMergingUsesOutsideLoop() {
5241 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5242 return;
5243
5244 LLVM_DEBUG(
5245 dbgs() << "The search space is too complex.\n"
5246 "Narrowing the search space by merging uses with fixups "
5247 "entirely outside the loop with uses inside the loop.\n");
5248
5249 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5250 LSRUse &LU = Uses[LUIdx];
5251 // Don't merge ICmpZero uses outside the loop, as ICmpZero needs to be
5252 // handled specially when expanding.
5253 if (!LU.AllFixupsOutsideLoop || LU.Formulae.empty() ||
5254 LU.Kind == LSRUse::ICmpZero)
5255 continue;
5256
5257 LLVM_DEBUG(dbgs() << " Trying to eliminate use "; LU.print(dbgs());
5258 dbgs() << '\n');
5259
5260 // Find a compatible LSRUse inside the loop that we could merge LU with
5261 LSRUse *LUToMergeWith = nullptr;
5262 const Formula &ThisF = LU.Formulae[0];
5263 for (LSRUse &OtherLU : Uses) {
5264 // Only merge with uses inside the loop
5265 if (OtherLU.AllFixupsOutsideLoop)
5266 continue;
5267 // Can't merge with ICmpZero uses as they're handled specially when
5268 // expanding
5269 if (OtherLU.Kind == LSRUse::ICmpZero)
5270 continue;
5271 // Can't merge with uses without any formulae
5272 if (OtherLU.Formulae.empty())
5273 continue;
5274 // Can't merge if LU's offsets aren't legal for all of OtherLU's formulae
5275 if (any_of(OtherLU.Formulae, [&](const Formula &F) {
5276 return !isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, OtherLU.Kind,
5277 OtherLU.AccessTy, F);
5278 }))
5279 continue;
5280 // We can merge with uses that have the same initial formula. We allow
5281 // merging of uses with different Kind and AccessTy which means that the
5282 // cost may end up being inaccurate, but it's also what we would have
5283 // gotten if we'd ignored uses outside the loop entirely.
5284 const Formula &OtherF = OtherLU.Formulae[0];
5285 if (ThisF.BaseRegs == OtherF.BaseRegs &&
5286 ThisF.ScaledReg == OtherF.ScaledReg &&
5287 ThisF.BaseGV == OtherF.BaseGV && ThisF.Scale == OtherF.Scale &&
5288 ThisF.UnfoldedOffset == OtherF.UnfoldedOffset &&
5289 ThisF.BaseOffset == OtherF.BaseOffset) {
5290 LUToMergeWith = &OtherLU;
5291 break;
5292 }
5293 }
5294 if (!LUToMergeWith)
5295 continue;
5296
5297 LLVM_DEBUG(dbgs() << " Merging with "; LUToMergeWith->print(dbgs());
5298 dbgs() << '\n');
5299
5300 // Copy fixups
5301 for (LSRFixup &Fixup : LU.Fixups) {
5302 LUToMergeWith->pushFixup(Fixup);
5303 }
5304
5305 // Delete the old use.
5306 DeleteUse(LU, LUIdx);
5307 --LUIdx;
5308 --NumUses;
5309 }
5310
5311 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5312}
5313
5314/// The function delete formulas with high registers number expectation.
5315/// Assuming we don't know the value of each formula (already delete
5316/// all inefficient), generate probability of not selecting for each
5317/// register.
5318/// For example,
5319/// Use1:
5320/// reg(a) + reg({0,+,1})
5321/// reg(a) + reg({-1,+,1}) + 1
5322/// reg({a,+,1})
5323/// Use2:
5324/// reg(b) + reg({0,+,1})
5325/// reg(b) + reg({-1,+,1}) + 1
5326/// reg({b,+,1})
5327/// Use3:
5328/// reg(c) + reg(b) + reg({0,+,1})
5329/// reg(c) + reg({b,+,1})
5330///
5331/// Probability of not selecting
5332/// Use1 Use2 Use3
5333/// reg(a) (1/3) * 1 * 1
5334/// reg(b) 1 * (1/3) * (1/2)
5335/// reg({0,+,1}) (2/3) * (2/3) * (1/2)
5336/// reg({-1,+,1}) (2/3) * (2/3) * 1
5337/// reg({a,+,1}) (2/3) * 1 * 1
5338/// reg({b,+,1}) 1 * (2/3) * (2/3)
5339/// reg(c) 1 * 1 * 0
5340///
5341/// Now count registers number mathematical expectation for each formula:
5342/// Note that for each use we exclude probability if not selecting for the use.
5343/// For example for Use1 probability for reg(a) would be just 1 * 1 (excluding
5344/// probabilty 1/3 of not selecting for Use1).
5345/// Use1:
5346/// reg(a) + reg({0,+,1}) 1 + 1/3 -- to be deleted
5347/// reg(a) + reg({-1,+,1}) + 1 1 + 4/9 -- to be deleted
5348/// reg({a,+,1}) 1
5349/// Use2:
5350/// reg(b) + reg({0,+,1}) 1/2 + 1/3 -- to be deleted
5351/// reg(b) + reg({-1,+,1}) + 1 1/2 + 2/3 -- to be deleted
5352/// reg({b,+,1}) 2/3
5353/// Use3:
5354/// reg(c) + reg(b) + reg({0,+,1}) 1 + 1/3 + 4/9 -- to be deleted
5355/// reg(c) + reg({b,+,1}) 1 + 2/3
5356void LSRInstance::NarrowSearchSpaceByDeletingCostlyFormulas() {
5357 if (EstimateSearchSpaceComplexity() < ComplexityLimit)
5358 return;
5359 // Ok, we have too many of formulae on our hands to conveniently handle.
5360 // Use a rough heuristic to thin out the list.
5361
5362 // Set of Regs wich will be 100% used in final solution.
5363 // Used in each formula of a solution (in example above this is reg(c)).
5364 // We can skip them in calculations.
5365 SmallPtrSet<const SCEV *, 4> UniqRegs;
5366 LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
5367
5368 // Map each register to probability of not selecting
5369 DenseMap <const SCEV *, float> RegNumMap;
5370 for (const SCEV *Reg : RegUses) {
5371 if (UniqRegs.count(Reg))
5372 continue;
5373 float PNotSel = 1;
5374 for (const LSRUse &LU : Uses) {
5375 if (!LU.Regs.count(Reg))
5376 continue;
5377 float P = LU.getNotSelectedProbability(Reg);
5378 if (P != 0.0)
5379 PNotSel *= P;
5380 else
5381 UniqRegs.insert(Reg);
5382 }
5383 RegNumMap.insert(std::make_pair(Reg, PNotSel));
5384 }
5385
5386 LLVM_DEBUG(
5387 dbgs() << "Narrowing the search space by deleting costly formulas\n");
5388
5389 // Delete formulas where registers number expectation is high.
5390 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5391 LSRUse &LU = Uses[LUIdx];
5392 // If nothing to delete - continue.
5393 if (LU.Formulae.size() < 2)
5394 continue;
5395 // This is temporary solution to test performance. Float should be
5396 // replaced with round independent type (based on integers) to avoid
5397 // different results for different target builds.
5398 float FMinRegNum = LU.Formulae[0].getNumRegs();
5399 float FMinARegNum = LU.Formulae[0].getNumRegs();
5400 size_t MinIdx = 0;
5401 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
5402 Formula &F = LU.Formulae[i];
5403 float FRegNum = 0;
5404 float FARegNum = 0;
5405 for (const SCEV *BaseReg : F.BaseRegs) {
5406 if (UniqRegs.count(BaseReg))
5407 continue;
5408 FRegNum += RegNumMap[BaseReg] / LU.getNotSelectedProbability(BaseReg);
5409 if (isa<SCEVAddRecExpr>(BaseReg))
5410 FARegNum +=
5411 RegNumMap[BaseReg] / LU.getNotSelectedProbability(BaseReg);
5412 }
5413 if (const SCEV *ScaledReg = F.ScaledReg) {
5414 if (!UniqRegs.count(ScaledReg)) {
5415 FRegNum +=
5416 RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
5417 if (isa<SCEVAddRecExpr>(ScaledReg))
5418 FARegNum +=
5419 RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
5420 }
5421 }
5422 if (FMinRegNum > FRegNum ||
5423 (FMinRegNum == FRegNum && FMinARegNum > FARegNum)) {
5424 FMinRegNum = FRegNum;
5425 FMinARegNum = FARegNum;
5426 MinIdx = i;
5427 }
5428 }
5429 LLVM_DEBUG(dbgs() << " The formula "; LU.Formulae[MinIdx].print(dbgs());
5430 dbgs() << " with min reg num " << FMinRegNum << '\n');
5431 if (MinIdx != 0)
5432 std::swap(LU.Formulae[MinIdx], LU.Formulae[0]);
5433 while (LU.Formulae.size() != 1) {
5434 LLVM_DEBUG(dbgs() << " Deleting "; LU.Formulae.back().print(dbgs());
5435 dbgs() << '\n');
5436 LU.Formulae.pop_back();
5437 }
5438 LU.RecomputeRegs(LUIdx, RegUses);
5439 assert(LU.Formulae.size() == 1 && "Should be exactly 1 min regs formula");
5440 Formula &F = LU.Formulae[0];
5441 LLVM_DEBUG(dbgs() << " Leaving only "; F.print(dbgs()); dbgs() << '\n');
5442 // When we choose the formula, the regs become unique.
5443 UniqRegs.insert_range(F.BaseRegs);
5444 if (F.ScaledReg)
5445 UniqRegs.insert(F.ScaledReg);
5446 }
5447 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5448}
5449
5450// Check if Best and Reg are SCEVs separated by a constant amount C, and if so
5451// would the addressing offset +C would be legal where the negative offset -C is
5452// not.
5454 ScalarEvolution &SE, const SCEV *Best,
5455 const SCEV *Reg,
5456 MemAccessTy AccessType) {
5457 if (Best->getType() != Reg->getType() ||
5459 cast<SCEVAddRecExpr>(Best)->getLoop() !=
5460 cast<SCEVAddRecExpr>(Reg)->getLoop()))
5461 return false;
5462 std::optional<APInt> Diff = SE.computeConstantDifference(Best, Reg);
5463 if (!Diff)
5464 return false;
5465
5466 return TTI.isLegalAddressingMode(
5467 AccessType.MemTy, /*BaseGV=*/nullptr,
5468 /*BaseOffset=*/Diff->getSExtValue(),
5469 /*HasBaseReg=*/true, /*Scale=*/0, AccessType.AddrSpace) &&
5470 !TTI.isLegalAddressingMode(
5471 AccessType.MemTy, /*BaseGV=*/nullptr,
5472 /*BaseOffset=*/-Diff->getSExtValue(),
5473 /*HasBaseReg=*/true, /*Scale=*/0, AccessType.AddrSpace);
5474}
5475
5476/// Pick a register which seems likely to be profitable, and then in any use
5477/// which has any reference to that register, delete all formulae which do not
5478/// reference that register.
5479void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
5480 // With all other options exhausted, loop until the system is simple
5481 // enough to handle.
5482 SmallPtrSet<const SCEV *, 4> Taken;
5483 while (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
5484 // Ok, we have too many of formulae on our hands to conveniently handle.
5485 // Use a rough heuristic to thin out the list.
5486 LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
5487
5488 // Pick the register which is used by the most LSRUses, which is likely
5489 // to be a good reuse register candidate.
5490 const SCEV *Best = nullptr;
5491 unsigned BestNum = 0;
5492 for (const SCEV *Reg : RegUses) {
5493 if (Taken.count(Reg))
5494 continue;
5495 if (!Best) {
5496 Best = Reg;
5497 BestNum = RegUses.getUsedByIndices(Reg).count();
5498 } else {
5499 unsigned Count = RegUses.getUsedByIndices(Reg).count();
5500 if (Count > BestNum) {
5501 Best = Reg;
5502 BestNum = Count;
5503 }
5504
5505 // If the scores are the same, but the Reg is simpler for the target
5506 // (for example {x,+,1} as opposed to {x+C,+,1}, where the target can
5507 // handle +C but not -C), opt for the simpler formula.
5508 if (Count == BestNum) {
5509 int LUIdx = RegUses.getUsedByIndices(Reg).find_first();
5510 if (LUIdx >= 0 && Uses[LUIdx].Kind == LSRUse::Address &&
5512 Uses[LUIdx].AccessTy)) {
5513 Best = Reg;
5514 BestNum = Count;
5515 }
5516 }
5517 }
5518 }
5519 assert(Best && "Failed to find best LSRUse candidate");
5520
5521 LLVM_DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best
5522 << " will yield profitable reuse.\n");
5523 Taken.insert(Best);
5524
5525 // In any use with formulae which references this register, delete formulae
5526 // which don't reference it.
5527 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
5528 LSRUse &LU = Uses[LUIdx];
5529 if (!LU.Regs.count(Best)) continue;
5530
5531 bool Any = false;
5532 for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
5533 Formula &F = LU.Formulae[i];
5534 if (!F.referencesReg(Best)) {
5535 LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
5536 LU.DeleteFormula(F);
5537 --e;
5538 --i;
5539 Any = true;
5540 assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?");
5541 continue;
5542 }
5543 }
5544
5545 if (Any)
5546 LU.RecomputeRegs(LUIdx, RegUses);
5547 }
5548
5549 LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
5550 }
5551}
5552
5553/// If there are an extraordinary number of formulae to choose from, use some
5554/// rough heuristics to prune down the number of formulae. This keeps the main
5555/// solver from taking an extraordinary amount of time in some worst-case
5556/// scenarios.
5557void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
5558 NarrowSearchSpaceByDetectingSupersets();
5559 NarrowSearchSpaceByCollapsingUnrolledCode();
5560 NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
5562 NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
5563 NarrowSearchSpaceByFilterPostInc();
5564 NarrowSearchSpaceByMergingUsesOutsideLoop();
5565 if (LSRExpNarrow)
5566 NarrowSearchSpaceByDeletingCostlyFormulas();
5567 else
5568 NarrowSearchSpaceByPickingWinnerRegs();
5569}
5570
5571/// This is the recursive solver.
5572void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
5573 Cost &SolutionCost,
5574 SmallVectorImpl<const Formula *> &Workspace,
5575 const Cost &CurCost,
5576 const SmallPtrSet<const SCEV *, 16> &CurRegs,
5577 DenseSet<const SCEV *> &VisitedRegs) const {
5578 // Some ideas:
5579 // - prune more:
5580 // - use more aggressive filtering
5581 // - sort the formula so that the most profitable solutions are found first
5582 // - sort the uses too
5583 // - search faster:
5584 // - don't compute a cost, and then compare. compare while computing a cost
5585 // and bail early.
5586 // - track register sets with SmallBitVector
5587
5588 const LSRUse &LU = Uses[Workspace.size()];
5589
5590 // If this use references any register that's already a part of the
5591 // in-progress solution, consider it a requirement that a formula must
5592 // reference that register in order to be considered. This prunes out
5593 // unprofitable searching.
5594 SmallSetVector<const SCEV *, 4> ReqRegs;
5595 for (const SCEV *S : CurRegs)
5596 if (LU.Regs.count(S))
5597 ReqRegs.insert(S);
5598
5599 SmallPtrSet<const SCEV *, 16> NewRegs;
5600 Cost NewCost(L, SE, TTI, AMK);
5601 for (const Formula &F : LU.Formulae) {
5602 // Ignore formulae which may not be ideal in terms of register reuse of
5603 // ReqRegs. The formula should use all required registers before
5604 // introducing new ones.
5605 // This can sometimes (notably when trying to favour postinc) lead to
5606 // sub-optimial decisions. There it is best left to the cost modelling to
5607 // get correct.
5608 if (!(AMK & TTI::AMK_PostIndexed) || LU.Kind != LSRUse::Address) {
5609 int NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size());
5610 for (const SCEV *Reg : ReqRegs) {
5611 if ((F.ScaledReg && F.ScaledReg == Reg) ||
5612 is_contained(F.BaseRegs, Reg)) {
5613 --NumReqRegsToFind;
5614 if (NumReqRegsToFind == 0)
5615 break;
5616 }
5617 }
5618 if (NumReqRegsToFind != 0) {
5619 // If none of the formulae satisfied the required registers, then we could
5620 // clear ReqRegs and try again. Currently, we simply give up in this case.
5621 continue;
5622 }
5623 }
5624
5625 // Evaluate the cost of the current formula. If it's already worse than
5626 // the current best, prune the search at that point.
5627 NewCost = CurCost;
5628 NewRegs = CurRegs;
5629 NewCost.RateFormula(F, NewRegs, VisitedRegs, LU, HardwareLoopProfitable);
5630 if (NewCost.isLess(SolutionCost)) {
5631 Workspace.push_back(&F);
5632 if (Workspace.size() != Uses.size()) {
5633 SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
5634 NewRegs, VisitedRegs);
5635 if (F.getNumRegs() == 1 && Workspace.size() == 1)
5636 VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
5637 } else {
5638 LLVM_DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
5639 dbgs() << ".\nRegs:\n";
5640 for (const SCEV *S : NewRegs) dbgs()
5641 << "- " << *S << "\n";
5642 dbgs() << '\n');
5643
5644 SolutionCost = NewCost;
5645 Solution = Workspace;
5646 }
5647 Workspace.pop_back();
5648 }
5649 }
5650}
5651
5652/// Choose one formula from each use. Return the results in the given Solution
5653/// vector.
5654void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
5656 Cost SolutionCost(L, SE, TTI, AMK);
5657 SolutionCost.Lose();
5658 Cost CurCost(L, SE, TTI, AMK);
5659 SmallPtrSet<const SCEV *, 16> CurRegs;
5660 DenseSet<const SCEV *> VisitedRegs;
5661 Workspace.reserve(Uses.size());
5662
5663 // SolveRecurse does all the work.
5664 SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
5665 CurRegs, VisitedRegs);
5666 if (Solution.empty()) {
5667 LLVM_DEBUG(dbgs() << "\nNo Satisfactory Solution\n");
5668 return;
5669 }
5670
5671 // Ok, we've now made all our decisions.
5672 LLVM_DEBUG(dbgs() << "\n"
5673 "The chosen solution requires ";
5674 SolutionCost.print(dbgs()); dbgs() << ":\n";
5675 for (size_t i = 0, e = Uses.size(); i != e; ++i) {
5676 dbgs() << " ";
5677 Uses[i].print(dbgs());
5678 dbgs() << "\n"
5679 " ";
5680 Solution[i]->print(dbgs());
5681 dbgs() << '\n';
5682 });
5683
5684 assert(Solution.size() == Uses.size() && "Malformed solution!");
5685
5686 const bool EnableDropUnprofitableSolution = [&] {
5688 case cl::boolOrDefault::BOU_TRUE:
5689 return true;
5690 case cl::boolOrDefault::BOU_FALSE:
5691 return false;
5692 case cl::boolOrDefault::BOU_UNSET:
5694 }
5695 llvm_unreachable("Unhandled cl::boolOrDefault enum");
5696 }();
5697
5698 if (BaselineCost.isLess(SolutionCost)) {
5699 if (!EnableDropUnprofitableSolution)
5700 LLVM_DEBUG(
5701 dbgs() << "Baseline is more profitable than chosen solution, "
5702 "add option 'lsr-drop-solution' to drop LSR solution.\n");
5703 else {
5704 LLVM_DEBUG(dbgs() << "Baseline is more profitable than chosen "
5705 "solution, dropping LSR solution.\n";);
5706 Solution.clear();
5707 }
5708 }
5709}
5710
5711/// Helper for AdjustInsertPositionForExpand. Climb up the dominator tree far as
5712/// we can go while still being dominated by the input positions. This helps
5713/// canonicalize the insert position, which encourages sharing.
5715LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
5716 const SmallVectorImpl<Instruction *> &Inputs)
5717 const {
5718 Instruction *Tentative = &*IP;
5719 while (true) {
5720 bool AllDominate = true;
5721 Instruction *BetterPos = nullptr;
5722 // Don't bother attempting to insert before a catchswitch, their basic block
5723 // cannot have other non-PHI instructions.
5724 if (isa<CatchSwitchInst>(Tentative))
5725 return IP;
5726
5727 for (Instruction *Inst : Inputs) {
5728 if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
5729 AllDominate = false;
5730 break;
5731 }
5732 // Attempt to find an insert position in the middle of the block,
5733 // instead of at the end, so that it can be used for other expansions.
5734 if (Tentative->getParent() == Inst->getParent() &&
5735 (!BetterPos || !DT.dominates(Inst, BetterPos)))
5736 BetterPos = &*std::next(BasicBlock::iterator(Inst));
5737 }
5738 if (!AllDominate)
5739 break;
5740 if (BetterPos)
5741 IP = BetterPos->getIterator();
5742 else
5743 IP = Tentative->getIterator();
5744
5745 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
5746 unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
5747
5748 BasicBlock *IDom;
5749 for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) {
5750 if (!Rung) return IP;
5751 Rung = Rung->getIDom();
5752 if (!Rung) return IP;
5753 IDom = Rung->getBlock();
5754
5755 // Don't climb into a loop though.
5756 const Loop *IDomLoop = LI.getLoopFor(IDom);
5757 unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0;
5758 if (IDomDepth <= IPLoopDepth &&
5759 (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
5760 break;
5761 }
5762
5763 Tentative = IDom->getTerminator();
5764 }
5765
5766 return IP;
5767}
5768
5769/// Determine an input position which will be dominated by the operands and
5770/// which will dominate the result.
5771BasicBlock::iterator LSRInstance::AdjustInsertPositionForExpand(
5772 BasicBlock::iterator LowestIP, const LSRFixup &LF, const LSRUse &LU) const {
5773 // Collect some instructions which must be dominated by the
5774 // expanding replacement. These must be dominated by any operands that
5775 // will be required in the expansion.
5776 SmallVector<Instruction *, 4> Inputs;
5777 if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace))
5778 Inputs.push_back(I);
5779 if (LU.Kind == LSRUse::ICmpZero)
5780 if (Instruction *I =
5781 dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1)))
5782 Inputs.push_back(I);
5783 if (LF.PostIncLoops.count(L)) {
5784 if (LF.isUseFullyOutsideLoop(L))
5785 Inputs.push_back(L->getLoopLatch()->getTerminator());
5786 else
5787 Inputs.push_back(IVIncInsertPos);
5788 }
5789 // The expansion must also be dominated by the increment positions of any
5790 // loops it for which it is using post-inc mode.
5791 for (const Loop *PIL : LF.PostIncLoops) {
5792 if (PIL == L) continue;
5793
5794 // Be dominated by the loop exit.
5795 SmallVector<BasicBlock *, 4> ExitingBlocks;
5796 PIL->getExitingBlocks(ExitingBlocks);
5797 if (!ExitingBlocks.empty()) {
5798 BasicBlock *BB = ExitingBlocks[0];
5799 for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i)
5800 BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
5801 Inputs.push_back(BB->getTerminator());
5802 }
5803 }
5804
5805 assert(!isa<PHINode>(LowestIP) && !LowestIP->isEHPad() &&
5806 "Insertion point must be a normal instruction");
5807
5808 // Then, climb up the immediate dominator tree as far as we can go while
5809 // still being dominated by the input positions.
5810 BasicBlock::iterator IP = HoistInsertPosition(LowestIP, Inputs);
5811
5812 // Don't insert instructions before PHI nodes.
5813 while (isa<PHINode>(IP)) ++IP;
5814
5815 // Ignore landingpad instructions.
5816 while (IP->isEHPad()) ++IP;
5817
5818 // Set IP below instructions recently inserted by SCEVExpander. This keeps the
5819 // IP consistent across expansions and allows the previously inserted
5820 // instructions to be reused by subsequent expansion.
5821 while (Rewriter.isInsertedInstruction(&*IP) && IP != LowestIP)
5822 ++IP;
5823
5824 return IP;
5825}
5826
5827/// Emit instructions for the leading candidate expression for this LSRUse (this
5828/// is called "expanding").
5829Value *LSRInstance::Expand(const LSRUse &LU, const LSRFixup &LF,
5830 const Formula &F, BasicBlock::iterator IP,
5831 SmallVectorImpl<WeakTrackingVH> &DeadInsts) const {
5832 if (LU.RigidFormula)
5833 return LF.OperandValToReplace;
5834
5835 // Determine an input position which will be dominated by the operands and
5836 // which will dominate the result.
5837 IP = AdjustInsertPositionForExpand(IP, LF, LU);
5838 Rewriter.setInsertPoint(&*IP);
5839
5840 // Inform the Rewriter if we have a post-increment use, so that it can
5841 // perform an advantageous expansion.
5842 Rewriter.setPostInc(LF.PostIncLoops);
5843
5844 // This is the type that the user actually needs.
5845 Type *OpTy = LF.OperandValToReplace->getType();
5846 // This will be the type that we'll initially expand to.
5847 Type *Ty = F.getType();
5848 if (!Ty)
5849 // No type known; just expand directly to the ultimate type.
5850 Ty = OpTy;
5851 else if (SE.getEffectiveSCEVType(Ty) == SE.getEffectiveSCEVType(OpTy))
5852 // Expand directly to the ultimate type if it's the right size.
5853 Ty = OpTy;
5854 // This is the type to do integer arithmetic in.
5855 Type *IntTy = SE.getEffectiveSCEVType(Ty);
5856 // For ICmpZero with pointer-typed operands, keep the comparison in the
5857 // integer domain to avoid generating inttoptr casts. Use IntTy (the
5858 // formula's arithmetic width) so that both icmp operands match even when
5859 // the IV is wider than the pointer.
5860 if (LU.Kind == LSRUse::ICmpZero && OpTy->isPointerTy()) {
5861 OpTy = IntTy;
5862 Ty = IntTy;
5863 }
5864
5865 // Build up a list of operands to add together to form the full base.
5867
5868 // Expand the BaseRegs portion.
5869 for (const SCEV *Reg : F.BaseRegs) {
5870 assert(!Reg->isZero() && "Zero allocated in a base register!");
5871
5872 // If we're expanding for a post-inc user, make the post-inc adjustment.
5873 Reg = denormalizeForPostIncUse(Reg, LF.PostIncLoops, SE);
5874 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, nullptr)));
5875 }
5876
5877 // Expand the ScaledReg portion.
5878 Value *ICmpScaledV = nullptr;
5879 if (F.Scale != 0) {
5880 const SCEV *ScaledS = F.ScaledReg;
5881
5882 // If we're expanding for a post-inc user, make the post-inc adjustment.
5883 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(LF.PostIncLoops);
5884 ScaledS = denormalizeForPostIncUse(ScaledS, Loops, SE);
5885
5886 if (LU.Kind == LSRUse::ICmpZero) {
5887 // Expand ScaleReg as if it was part of the base regs.
5888 if (F.Scale == 1)
5889 Ops.push_back(
5890 SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr)));
5891 else {
5892 // An interesting way of "folding" with an icmp is to use a negated
5893 // scale, which we'll implement by inserting it into the other operand
5894 // of the icmp.
5895 assert(F.Scale == -1 &&
5896 "The only scale supported by ICmpZero uses is -1!");
5897 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, nullptr);
5898 }
5899 } else {
5900 // Otherwise just expand the scaled register and an explicit scale,
5901 // which is expected to be matched as part of the address.
5902
5903 // Flush the operand list to suppress SCEVExpander hoisting address modes.
5904 // Unless the addressing mode will not be folded.
5905 if (!Ops.empty() && LU.Kind == LSRUse::Address &&
5906 isAMCompletelyFolded(TTI, LU, F)) {
5907 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), nullptr);
5908 Ops.clear();
5909 Ops.push_back(SE.getUnknown(FullV));
5910 }
5911 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr));
5912 if (F.Scale != 1)
5913 ScaledS =
5914 SE.getMulExpr(ScaledS, SE.getConstant(ScaledS->getType(), F.Scale));
5915 Ops.push_back(ScaledS);
5916 }
5917 }
5918
5919 // Expand the GV portion.
5920 if (F.BaseGV) {
5921 // Flush the operand list to suppress SCEVExpander hoisting.
5922 if (!Ops.empty()) {
5923 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), IntTy);
5924 Ops.clear();
5925 Ops.push_back(SE.getUnknown(FullV));
5926 }
5927 Ops.push_back(SE.getUnknown(F.BaseGV));
5928 }
5929
5930 // Flush the operand list to suppress SCEVExpander hoisting of both folded and
5931 // unfolded offsets. LSR assumes they both live next to their uses.
5932 if (!Ops.empty()) {
5933 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty);
5934 Ops.clear();
5935 Ops.push_back(SE.getUnknown(FullV));
5936 }
5937
5938 // FIXME: Are we sure we won't get a mismatch here? Is there a way to bail
5939 // out at this point, or should we generate a SCEV adding together mixed
5940 // offsets?
5941 assert(F.BaseOffset.isCompatibleImmediate(LF.Offset) &&
5942 "Expanding mismatched offsets\n");
5943 // Expand the immediate portion.
5944 Immediate Offset = F.BaseOffset.addUnsigned(LF.Offset);
5945 if (Offset.isNonZero()) {
5946 if (LU.Kind == LSRUse::ICmpZero) {
5947 // The other interesting way of "folding" with an ICmpZero is to use a
5948 // negated immediate.
5949 if (!ICmpScaledV) {
5950 // TODO: Avoid implicit trunc?
5951 // See https://github.com/llvm/llvm-project/issues/112510.
5952 ICmpScaledV = ConstantInt::getSigned(
5953 IntTy, -(uint64_t)Offset.getFixedValue(), /*ImplicitTrunc=*/true);
5954 } else {
5955 Ops.push_back(SE.getUnknown(ICmpScaledV));
5956 ICmpScaledV = ConstantInt::getSigned(IntTy, Offset.getFixedValue(),
5957 /*ImplicitTrunc=*/true);
5958 }
5959 } else {
5960 // Just add the immediate values. These again are expected to be matched
5961 // as part of the address.
5962 Ops.push_back(Offset.getUnknownSCEV(SE, IntTy));
5963 }
5964 }
5965
5966 // Expand the unfolded offset portion.
5967 Immediate UnfoldedOffset = F.UnfoldedOffset;
5968 if (UnfoldedOffset.isNonZero()) {
5969 // Just add the immediate values.
5970 Ops.push_back(UnfoldedOffset.getUnknownSCEV(SE, IntTy));
5971 }
5972
5973 // Emit instructions summing all the operands.
5974 const SCEV *FullS = Ops.empty() ?
5975 SE.getConstant(IntTy, 0) :
5976 SE.getAddExpr(Ops);
5977 Value *FullV = Rewriter.expandCodeFor(FullS, Ty);
5978
5979 // We're done expanding now, so reset the rewriter.
5980 Rewriter.clearPostInc();
5981
5982 // An ICmpZero Formula represents an ICmp which we're handling as a
5983 // comparison against zero. Now that we've expanded an expression for that
5984 // form, update the ICmp's other operand.
5985 if (LU.Kind == LSRUse::ICmpZero) {
5986 ICmpInst *CI = cast<ICmpInst>(LF.UserInst);
5987 if (auto *OperandIsInstr = dyn_cast<Instruction>(CI->getOperand(1)))
5988 DeadInsts.emplace_back(OperandIsInstr);
5989 assert(!F.BaseGV && "ICmp does not support folding a global value and "
5990 "a scale at the same time!");
5991 if (F.Scale == -1) {
5992 if (ICmpScaledV->getType() != OpTy) {
5994 CastInst::getCastOpcode(ICmpScaledV, false, OpTy, false),
5995 ICmpScaledV, OpTy, "tmp", CI->getIterator());
5996 ICmpScaledV = Cast;
5997 }
5998 CI->setOperand(1, ICmpScaledV);
5999 } else {
6000 // A scale of 1 means that the scale has been expanded as part of the
6001 // base regs.
6002 assert((F.Scale == 0 || F.Scale == 1) &&
6003 "ICmp does not support folding a global value and "
6004 "a scale at the same time!");
6005 // TODO: Avoid implicit trunc?
6006 // See https://github.com/llvm/llvm-project/issues/112510.
6008 -(uint64_t)Offset.getFixedValue(),
6009 /*ImplicitTrunc=*/true);
6010 if (C->getType() != OpTy) {
6012 CastInst::getCastOpcode(C, false, OpTy, false), C, OpTy,
6013 CI->getDataLayout());
6014 assert(C && "Cast of ConstantInt should have folded");
6015 }
6016
6017 CI->setOperand(1, C);
6018 }
6019 }
6020
6021 return FullV;
6022}
6023
6024/// Helper for Rewrite. PHI nodes are special because the use of their operands
6025/// effectively happens in their predecessor blocks, so the expression may need
6026/// to be expanded in multiple places.
6027void LSRInstance::RewriteForPHI(PHINode *PN, const LSRUse &LU,
6028 const LSRFixup &LF, const Formula &F,
6029 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
6030 DenseMap<BasicBlock *, Value *> Inserted;
6031
6032 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
6033 if (PN->getIncomingValue(i) == LF.OperandValToReplace) {
6034 bool needUpdateFixups = false;
6035 BasicBlock *BB = PN->getIncomingBlock(i);
6036
6037 // If this is a critical edge, split the edge so that we do not insert
6038 // the code on all predecessor/successor paths. We do this unless this
6039 // is the canonical backedge for this loop, which complicates post-inc
6040 // users.
6041 if (e != 1 && BB->getTerminator()->getNumSuccessors() > 1 &&
6044 BasicBlock *Parent = PN->getParent();
6045 Loop *PNLoop = LI.getLoopFor(Parent);
6046 if (!PNLoop || Parent != PNLoop->getHeader()) {
6047 // Split the critical edge.
6048 BasicBlock *NewBB = nullptr;
6049 if (!Parent->isLandingPad()) {
6050 CriticalEdgeSplittingOptions SplitOptions(&DT, &LI, MSSAU);
6051 SplitOptions =
6052 SplitOptions.setMergeIdenticalEdges().setKeepOneInputPHIs();
6053 if (ShouldPreserveLCSSA)
6054 SplitOptions = SplitOptions.setPreserveLCSSA();
6055 NewBB = SplitCriticalEdge(BB, Parent, SplitOptions);
6056 } else {
6058 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
6059 SplitLandingPadPredecessors(Parent, BB, "", "", NewBBs, &DTU, &LI);
6060 NewBB = NewBBs[0];
6061 }
6062 // If NewBB==NULL, then SplitCriticalEdge refused to split because all
6063 // phi predecessors are identical. The simple thing to do is skip
6064 // splitting in this case rather than complicate the API.
6065 if (NewBB) {
6066 // If PN is outside of the loop and BB is in the loop, we want to
6067 // move the block to be immediately before the PHI block, not
6068 // immediately after BB.
6069 if (L->contains(BB) && !L->contains(PN))
6070 NewBB->moveBefore(PN->getParent());
6071
6072 // Splitting the edge can reduce the number of PHI entries we have.
6073 e = PN->getNumIncomingValues();
6074 BB = NewBB;
6075 i = PN->getBasicBlockIndex(BB);
6076
6077 needUpdateFixups = true;
6078 }
6079 }
6080 }
6081
6082 std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair =
6083 Inserted.try_emplace(BB);
6084 if (!Pair.second)
6085 PN->setIncomingValue(i, Pair.first->second);
6086 else {
6087 Value *FullV =
6088 Expand(LU, LF, F, BB->getTerminator()->getIterator(), DeadInsts);
6089
6090 // If this is reuse-by-noop-cast, insert the noop cast.
6091 Type *OpTy = LF.OperandValToReplace->getType();
6092 if (FullV->getType() != OpTy)
6093 FullV = CastInst::Create(
6094 CastInst::getCastOpcode(FullV, false, OpTy, false), FullV,
6095 LF.OperandValToReplace->getType(), "tmp",
6096 BB->getTerminator()->getIterator());
6097
6098 // If the incoming block for this value is not in the loop, it means the
6099 // current PHI is not in a loop exit, so we must create a LCSSA PHI for
6100 // the inserted value.
6101 if (auto *I = dyn_cast<Instruction>(FullV))
6102 if (L->contains(I) && !L->contains(BB))
6103 InsertedNonLCSSAInsts.insert(I);
6104
6105 PN->setIncomingValue(i, FullV);
6106 Pair.first->second = FullV;
6107 }
6108
6109 // If LSR splits critical edge and phi node has other pending
6110 // fixup operands, we need to update those pending fixups. Otherwise
6111 // formulae will not be implemented completely and some instructions
6112 // will not be eliminated.
6113 if (needUpdateFixups) {
6114 for (LSRUse &LU : Uses)
6115 for (LSRFixup &Fixup : LU.Fixups)
6116 // If fixup is supposed to rewrite some operand in the phi
6117 // that was just updated, it may be already moved to
6118 // another phi node. Such fixup requires update.
6119 if (Fixup.UserInst == PN) {
6120 // Check if the operand we try to replace still exists in the
6121 // original phi.
6122 bool foundInOriginalPHI = false;
6123 for (const auto &val : PN->incoming_values())
6124 if (val == Fixup.OperandValToReplace) {
6125 foundInOriginalPHI = true;
6126 break;
6127 }
6128
6129 // If fixup operand found in original PHI - nothing to do.
6130 if (foundInOriginalPHI)
6131 continue;
6132
6133 // Otherwise it might be moved to another PHI and requires update.
6134 // If fixup operand not found in any of the incoming blocks that
6135 // means we have already rewritten it - nothing to do.
6136 for (const auto &Block : PN->blocks())
6137 for (BasicBlock::iterator I = Block->begin(); isa<PHINode>(I);
6138 ++I) {
6139 PHINode *NewPN = cast<PHINode>(I);
6140 for (const auto &val : NewPN->incoming_values())
6141 if (val == Fixup.OperandValToReplace)
6142 Fixup.UserInst = NewPN;
6143 }
6144 }
6145 }
6146 }
6147}
6148
6149/// Emit instructions for the leading candidate expression for this LSRUse (this
6150/// is called "expanding"), and update the UserInst to reference the newly
6151/// expanded value.
6152void LSRInstance::Rewrite(const LSRUse &LU, const LSRFixup &LF,
6153 const Formula &F,
6154 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
6155 // First, find an insertion point that dominates UserInst. For PHI nodes,
6156 // find the nearest block which dominates all the relevant uses.
6157 if (PHINode *PN = dyn_cast<PHINode>(LF.UserInst)) {
6158 RewriteForPHI(PN, LU, LF, F, DeadInsts);
6159 } else {
6160 Value *FullV = Expand(LU, LF, F, LF.UserInst->getIterator(), DeadInsts);
6161
6162 // If this is reuse-by-noop-cast, insert the noop cast.
6163 // For ICmpZero with pointer operands, Expand() already set both operands
6164 // in integer domain, so no cast is needed here.
6165 Type *OpTy = LF.OperandValToReplace->getType();
6166 if (FullV->getType() != OpTy &&
6167 !(LU.Kind == LSRUse::ICmpZero && OpTy->isPointerTy())) {
6168 Instruction *Cast =
6169 CastInst::Create(CastInst::getCastOpcode(FullV, false, OpTy, false),
6170 FullV, OpTy, "tmp", LF.UserInst->getIterator());
6171 FullV = Cast;
6172 }
6173
6174 // Update the user. ICmpZero is handled specially here (for now) because
6175 // Expand may have updated one of the operands of the icmp already, and
6176 // its new value may happen to be equal to LF.OperandValToReplace, in
6177 // which case doing replaceUsesOfWith leads to replacing both operands
6178 // with the same value. TODO: Reorganize this.
6179 if (LU.Kind == LSRUse::ICmpZero)
6180 LF.UserInst->setOperand(0, FullV);
6181 else
6182 LF.UserInst->replaceUsesOfWith(LF.OperandValToReplace, FullV);
6183 }
6184
6185 if (auto *OperandIsInstr = dyn_cast<Instruction>(LF.OperandValToReplace))
6186 DeadInsts.emplace_back(OperandIsInstr);
6187}
6188
6189// Determine where to insert the transformed IV increment instruction for this
6190// fixup. By default this is the default insert position, but if this is a
6191// postincrement opportunity then we try to insert it in the same block as the
6192// fixup user instruction, as this is needed for a postincrement instruction to
6193// be generated.
6195 const LSRFixup &Fixup, const LSRUse &LU,
6196 Instruction *IVIncInsertPos,
6197 DominatorTree &DT) {
6198 // Only address uses can be postincremented
6199 if (LU.Kind != LSRUse::Address)
6200 return IVIncInsertPos;
6201
6202 // Don't try to postincrement if it's not legal
6203 Instruction *I = Fixup.UserInst;
6204 Type *Ty = I->getType();
6205 if (!(isa<LoadInst>(I) && TTI.isIndexedLoadLegal(TTI.MIM_PostInc, Ty)) &&
6206 !(isa<StoreInst>(I) && TTI.isIndexedStoreLegal(TTI.MIM_PostInc, Ty)))
6207 return IVIncInsertPos;
6208
6209 // It's only legal to hoist to the user block if it dominates the default
6210 // insert position.
6211 BasicBlock *HoistBlock = I->getParent();
6212 BasicBlock *IVIncBlock = IVIncInsertPos->getParent();
6213 if (!DT.dominates(I, IVIncBlock))
6214 return IVIncInsertPos;
6215
6216 return HoistBlock->getTerminator();
6217}
6218
6219/// Rewrite all the fixup locations with new values, following the chosen
6220/// solution.
6221void LSRInstance::ImplementSolution(
6222 const SmallVectorImpl<const Formula *> &Solution) {
6223 // Keep track of instructions we may have made dead, so that
6224 // we can remove them after we are done working.
6226
6227 // Mark phi nodes that terminate chains so the expander tries to reuse them.
6228 for (const IVChain &Chain : IVChainVec) {
6229 if (PHINode *PN = dyn_cast<PHINode>(Chain.tailUserInst()))
6230 Rewriter.setChainedPhi(PN);
6231 }
6232
6233 // Expand the new value definitions and update the users.
6234 for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx)
6235 for (const LSRFixup &Fixup : Uses[LUIdx].Fixups) {
6236 Instruction *InsertPos =
6237 getFixupInsertPos(TTI, Fixup, Uses[LUIdx], IVIncInsertPos, DT);
6238 Rewriter.setIVIncInsertPos(L, InsertPos);
6239 Rewrite(Uses[LUIdx], Fixup, *Solution[LUIdx], DeadInsts);
6240 Changed = true;
6241 }
6242
6243 auto InsertedInsts = InsertedNonLCSSAInsts.takeVector();
6244 formLCSSAForInstructions(InsertedInsts, DT, LI, &SE);
6245
6246 for (const IVChain &Chain : IVChainVec) {
6247 GenerateIVChain(Chain, DeadInsts);
6248 Changed = true;
6249 }
6250
6251 for (const WeakVH &IV : Rewriter.getInsertedIVs())
6252 if (IV && dyn_cast<Instruction>(&*IV)->getParent())
6253 ScalarEvolutionIVs.push_back(IV);
6254
6255 // Clean up after ourselves. This must be done before deleting any
6256 // instructions.
6257 Rewriter.clear();
6258
6260 &TLI, MSSAU);
6261
6262 // In our cost analysis above, we assume that each addrec consumes exactly
6263 // one register, and arrange to have increments inserted just before the
6264 // latch to maximimize the chance this is true. However, if we reused
6265 // existing IVs, we now need to move the increments to match our
6266 // expectations. Otherwise, our cost modeling results in us having a
6267 // chosen a non-optimal result for the actual schedule. (And yes, this
6268 // scheduling decision does impact later codegen.)
6269 for (PHINode &PN : L->getHeader()->phis()) {
6270 BinaryOperator *BO = nullptr;
6271 Value *Start = nullptr, *Step = nullptr;
6272 if (!matchSimpleRecurrence(&PN, BO, Start, Step))
6273 continue;
6274
6275 switch (BO->getOpcode()) {
6276 case Instruction::Sub:
6277 if (BO->getOperand(0) != &PN)
6278 // sub is non-commutative - match handling elsewhere in LSR
6279 continue;
6280 break;
6281 case Instruction::Add:
6282 break;
6283 default:
6284 continue;
6285 };
6286
6287 if (!isa<Constant>(Step))
6288 // If not a constant step, might increase register pressure
6289 // (We assume constants have been canonicalized to RHS)
6290 continue;
6291
6292 if (BO->getParent() == IVIncInsertPos->getParent())
6293 // Only bother moving across blocks. Isel can handle block local case.
6294 continue;
6295
6296 // Can we legally schedule inc at the desired point?
6297 if (!llvm::all_of(BO->uses(),
6298 [&](Use &U) {return DT.dominates(IVIncInsertPos, U);}))
6299 continue;
6300 BO->moveBefore(IVIncInsertPos->getIterator());
6301 Changed = true;
6302 }
6303
6304
6305}
6306
6307LSRInstance::LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE,
6308 DominatorTree &DT, LoopInfo &LI,
6309 const TargetTransformInfo &TTI, AssumptionCache &AC,
6310 TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU,
6311 bool PreserveLCSSA)
6312 : IU(IU), SE(SE), DT(DT), LI(LI), AC(AC), TLI(TLI), TTI(TTI), L(L),
6313 MSSAU(MSSAU), AMK(PreferredAddresingMode.getNumOccurrences() > 0
6315 : TTI.getPreferredAddressingMode(L, &SE)),
6316 Rewriter(SE, "lsr", PreserveLCSSA), ShouldPreserveLCSSA(PreserveLCSSA),
6317 BaselineCost(L, SE, TTI, AMK) {
6318 // If LoopSimplify form is not available, stay out of trouble.
6319 if (!L->isLoopSimplifyForm())
6320 return;
6321
6322 // If there's no interesting work to be done, bail early.
6323 if (IU.empty()) return;
6324
6325 // If there's too much analysis to be done, bail early. We won't be able to
6326 // model the problem anyway.
6327 unsigned NumUsers = 0;
6328 for (const IVStrideUse &U : IU) {
6329 if (++NumUsers > MaxIVUsers) {
6330 (void)U;
6331 LLVM_DEBUG(dbgs() << "LSR skipping loop, too many IV Users in " << U
6332 << "\n");
6333 return;
6334 }
6335 // Bail out if we have a PHI on an EHPad that gets a value from a
6336 // CatchSwitchInst. Because the CatchSwitchInst cannot be split, there is
6337 // no good place to stick any instructions.
6338 if (auto *PN = dyn_cast<PHINode>(U.getUser())) {
6339 auto FirstNonPHI = PN->getParent()->getFirstNonPHIIt();
6340 if (isa<FuncletPadInst>(FirstNonPHI) ||
6341 isa<CatchSwitchInst>(FirstNonPHI))
6342 for (BasicBlock *PredBB : PN->blocks())
6343 if (isa<CatchSwitchInst>(PredBB->getFirstNonPHIIt()))
6344 return;
6345 }
6346 }
6347
6348 LLVM_DEBUG(dbgs() << "\nLSR on loop ";
6349 L->getHeader()->printAsOperand(dbgs(), /*PrintType=*/false);
6350 dbgs() << ":\n");
6351
6352 // Check if we expect this loop to use a hardware loop instruction, which will
6353 // be used when calculating the costs of formulas.
6354 HardwareLoopInfo HWLoopInfo(L);
6355 HardwareLoopProfitable =
6356 TTI.isHardwareLoopProfitable(L, SE, AC, &TLI, HWLoopInfo);
6357
6358 // Configure SCEVExpander already now, so the correct mode is used for
6359 // isSafeToExpand() checks.
6360#if LLVM_ENABLE_ABI_BREAKING_CHECKS
6361 Rewriter.setDebugType(DEBUG_TYPE);
6362#endif
6363 Rewriter.disableCanonicalMode();
6364 Rewriter.enableLSRMode();
6365
6366 // First, perform some low-level loop optimizations.
6367 OptimizeShadowIV();
6368 OptimizeLoopTermCond();
6369
6370 // If loop preparation eliminates all interesting IV users, bail.
6371 if (IU.empty()) return;
6372
6373 // Skip nested loops until we can model them better with formulae.
6374 if (!L->isInnermost()) {
6375 LLVM_DEBUG(dbgs() << "LSR skipping outer loop " << *L << "\n");
6376 return;
6377 }
6378
6379 // Start collecting data and preparing for the solver.
6380 // If number of registers is not the major cost, we cannot benefit from the
6381 // current profitable chain optimization which is based on number of
6382 // registers.
6383 // FIXME: add profitable chain optimization for other kinds major cost, for
6384 // example number of instructions.
6385 if (TTI.isNumRegsMajorCostOfLSR() || StressIVChain)
6386 CollectChains();
6387 CollectInterestingTypesAndFactors();
6388 CollectFixupsAndInitialFormulae();
6389 CollectLoopInvariantFixupsAndFormulae();
6390
6391 if (Uses.empty())
6392 return;
6393
6394 LLVM_DEBUG(dbgs() << "LSR found " << Uses.size() << " uses:\n";
6395 print_uses(dbgs()));
6396 LLVM_DEBUG(dbgs() << "The baseline solution requires ";
6397 BaselineCost.print(dbgs()); dbgs() << "\n");
6398
6399 // Now use the reuse data to generate a bunch of interesting ways
6400 // to formulate the values needed for the uses.
6401 GenerateAllReuseFormulae();
6402
6403 FilterOutUndesirableDedicatedRegisters();
6404 NarrowSearchSpaceUsingHeuristics();
6405
6407 Solve(Solution);
6408
6409 // Release memory that is no longer needed.
6410 Factors.clear();
6411 Types.clear();
6412 RegUses.clear();
6413
6414 if (Solution.empty())
6415 return;
6416
6417#ifndef NDEBUG
6418 // Formulae should be legal.
6419 for (const LSRUse &LU : Uses) {
6420 for (const Formula &F : LU.Formulae)
6421 assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
6422 F) && "Illegal formula generated!");
6423 };
6424#endif
6425
6426 // Now that we've decided what we want, make it so.
6427 ImplementSolution(Solution);
6428}
6429
6430#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6431void LSRInstance::print_factors_and_types(raw_ostream &OS) const {
6432 if (Factors.empty() && Types.empty()) return;
6433
6434 OS << "LSR has identified the following interesting factors and types: ";
6435 ListSeparator LS;
6436
6437 for (int64_t Factor : Factors)
6438 OS << LS << '*' << Factor;
6439
6440 for (Type *Ty : Types)
6441 OS << LS << '(' << *Ty << ')';
6442 OS << '\n';
6443}
6444
6445void LSRInstance::print_fixups(raw_ostream &OS) const {
6446 OS << "LSR is examining the following fixup sites:\n";
6447 for (const LSRUse &LU : Uses)
6448 for (const LSRFixup &LF : LU.Fixups) {
6449 dbgs() << " ";
6450 LF.print(OS);
6451 OS << '\n';
6452 }
6453}
6454
6455void LSRInstance::print_uses(raw_ostream &OS) const {
6456 OS << "LSR is examining the following uses:\n";
6457 for (const LSRUse &LU : Uses) {
6458 dbgs() << " ";
6459 LU.print(OS);
6460 OS << '\n';
6461 for (const Formula &F : LU.Formulae) {
6462 OS << " ";
6463 F.print(OS);
6464 OS << '\n';
6465 }
6466 }
6467}
6468
6469void LSRInstance::print(raw_ostream &OS) const {
6470 print_factors_and_types(OS);
6471 print_fixups(OS);
6472 print_uses(OS);
6473}
6474
6475LLVM_DUMP_METHOD void LSRInstance::dump() const {
6476 print(errs()); errs() << '\n';
6477}
6478#endif
6479
6480namespace {
6481
6482class LoopStrengthReduce : public LoopPass {
6483public:
6484 static char ID; // Pass ID, replacement for typeid
6485
6486 LoopStrengthReduce();
6487
6488private:
6489 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
6490 void getAnalysisUsage(AnalysisUsage &AU) const override;
6491};
6492
6493} // end anonymous namespace
6494
6495LoopStrengthReduce::LoopStrengthReduce() : LoopPass(ID) {
6497}
6498
6499void LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
6500 // We split critical edges, so we change the CFG. However, we do update
6501 // many analyses if they are around.
6503
6504 AU.addRequired<LoopInfoWrapperPass>();
6505 AU.addPreserved<LoopInfoWrapperPass>();
6507 AU.addRequired<DominatorTreeWrapperPass>();
6508 AU.addPreserved<DominatorTreeWrapperPass>();
6509 AU.addRequired<ScalarEvolutionWrapperPass>();
6510 AU.addPreserved<ScalarEvolutionWrapperPass>();
6511 AU.addRequired<AssumptionCacheTracker>();
6512 AU.addRequired<TargetLibraryInfoWrapperPass>();
6513 // Requiring LoopSimplify a second time here prevents IVUsers from running
6514 // twice, since LoopSimplify was invalidated by running ScalarEvolution.
6516 AU.addRequired<IVUsersWrapperPass>();
6517 AU.addPreserved<IVUsersWrapperPass>();
6518 AU.addRequired<TargetTransformInfoWrapperPass>();
6519 AU.addPreserved<MemorySSAWrapperPass>();
6520}
6521
6522namespace {
6523
6524/// Enables more convenient iteration over a DWARF expression vector.
6526ToDwarfOpIter(SmallVectorImpl<uint64_t> &Expr) {
6527 llvm::DIExpression::expr_op_iterator Begin =
6528 llvm::DIExpression::expr_op_iterator(Expr.begin());
6529 llvm::DIExpression::expr_op_iterator End =
6530 llvm::DIExpression::expr_op_iterator(Expr.end());
6531 return {Begin, End};
6532}
6533
6534struct SCEVDbgValueBuilder {
6535 SCEVDbgValueBuilder() = default;
6536 SCEVDbgValueBuilder(const SCEVDbgValueBuilder &Base) { clone(Base); }
6537
6538 void clone(const SCEVDbgValueBuilder &Base) {
6539 LocationOps = Base.LocationOps;
6540 Expr = Base.Expr;
6541 }
6542
6543 void clear() {
6544 LocationOps.clear();
6545 Expr.clear();
6546 }
6547
6548 /// The DIExpression as we translate the SCEV.
6550 /// The location ops of the DIExpression.
6551 SmallVector<Value *, 2> LocationOps;
6552
6553 void pushOperator(uint64_t Op) { Expr.push_back(Op); }
6554 void pushUInt(uint64_t Operand) { Expr.push_back(Operand); }
6555
6556 /// Add a DW_OP_LLVM_arg to the expression, followed by the index of the value
6557 /// in the set of values referenced by the expression.
6558 void pushLocation(llvm::Value *V) {
6560 auto *It = llvm::find(LocationOps, V);
6561 unsigned ArgIndex = 0;
6562 if (It != LocationOps.end()) {
6563 ArgIndex = std::distance(LocationOps.begin(), It);
6564 } else {
6565 ArgIndex = LocationOps.size();
6566 LocationOps.push_back(V);
6567 }
6568 Expr.push_back(ArgIndex);
6569 }
6570
6571 void pushValue(const SCEVUnknown *U) {
6572 llvm::Value *V = cast<SCEVUnknown>(U)->getValue();
6573 pushLocation(V);
6574 }
6575
6576 bool pushConst(const SCEVConstant *C) {
6577 if (C->getAPInt().getSignificantBits() > 64)
6578 return false;
6579 Expr.push_back(llvm::dwarf::DW_OP_consts);
6580 Expr.push_back(C->getAPInt().getSExtValue());
6581 return true;
6582 }
6583
6584 // Iterating the expression as DWARF ops is convenient when updating
6585 // DWARF_OP_LLVM_args.
6587 return ToDwarfOpIter(Expr);
6588 }
6589
6590 /// Several SCEV types are sequences of the same arithmetic operator applied
6591 /// to constants and values that may be extended or truncated.
6592 bool pushArithmeticExpr(const llvm::SCEVCommutativeExpr *CommExpr,
6593 uint64_t DwarfOp) {
6594 assert((isa<llvm::SCEVAddExpr>(CommExpr) || isa<SCEVMulExpr>(CommExpr)) &&
6595 "Expected arithmetic SCEV type");
6596 bool Success = true;
6597 unsigned EmitOperator = 0;
6598 for (const auto &Op : CommExpr->operands()) {
6599 Success &= pushSCEV(Op);
6600
6601 if (EmitOperator >= 1)
6602 pushOperator(DwarfOp);
6603 ++EmitOperator;
6604 }
6605 return Success;
6606 }
6607
6608 // TODO: Identify and omit noop casts.
6609 bool pushCast(const llvm::SCEVCastExpr *C, bool IsSigned) {
6610 const llvm::SCEV *Inner = C->getOperand(0);
6611 const llvm::Type *Type = C->getType();
6612 uint64_t ToWidth = Type->getIntegerBitWidth();
6613 bool Success = pushSCEV(Inner);
6614 uint64_t CastOps[] = {dwarf::DW_OP_LLVM_convert, ToWidth,
6615 IsSigned ? llvm::dwarf::DW_ATE_signed
6616 : llvm::dwarf::DW_ATE_unsigned};
6617 for (const auto &Op : CastOps)
6618 pushOperator(Op);
6619 return Success;
6620 }
6621
6622 // TODO: MinMax - although these haven't been encountered in the test suite.
6623 bool pushSCEV(const llvm::SCEV *S) {
6624 bool Success = true;
6625 if (const SCEVConstant *StartInt = dyn_cast<SCEVConstant>(S)) {
6626 Success &= pushConst(StartInt);
6627
6628 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
6629 if (!U->getValue())
6630 return false;
6631 pushLocation(U->getValue());
6632
6633 } else if (const SCEVMulExpr *MulRec = dyn_cast<SCEVMulExpr>(S)) {
6634 Success &= pushArithmeticExpr(MulRec, llvm::dwarf::DW_OP_mul);
6635
6636 } else if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
6637 Success &= pushSCEV(UDiv->getLHS());
6638 Success &= pushSCEV(UDiv->getRHS());
6639 pushOperator(llvm::dwarf::DW_OP_div);
6640
6641 } else if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(S)) {
6642 // Assert if a new and unknown SCEVCastEXpr type is encountered.
6645 isa<SCEVSignExtendExpr>(Cast)) &&
6646 "Unexpected cast type in SCEV.");
6647 Success &= pushCast(Cast, (isa<SCEVSignExtendExpr>(Cast)));
6648
6649 } else if (const SCEVAddExpr *AddExpr = dyn_cast<SCEVAddExpr>(S)) {
6650 Success &= pushArithmeticExpr(AddExpr, llvm::dwarf::DW_OP_plus);
6651
6652 } else if (isa<SCEVAddRecExpr>(S)) {
6653 // Nested SCEVAddRecExpr are generated by nested loops and are currently
6654 // unsupported.
6655 return false;
6656
6657 } else {
6658 return false;
6659 }
6660 return Success;
6661 }
6662
6663 /// Return true if the combination of arithmetic operator and underlying
6664 /// SCEV constant value is an identity function.
6665 bool isIdentityFunction(uint64_t Op, const SCEV *S) {
6666 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
6667 if (C->getAPInt().getSignificantBits() > 64)
6668 return false;
6669 int64_t I = C->getAPInt().getSExtValue();
6670 switch (Op) {
6671 case llvm::dwarf::DW_OP_plus:
6672 case llvm::dwarf::DW_OP_minus:
6673 return I == 0;
6674 case llvm::dwarf::DW_OP_mul:
6675 case llvm::dwarf::DW_OP_div:
6676 return I == 1;
6677 }
6678 }
6679 return false;
6680 }
6681
6682 /// Convert a SCEV of a value to a DIExpression that is pushed onto the
6683 /// builder's expression stack. The stack should already contain an
6684 /// expression for the iteration count, so that it can be multiplied by
6685 /// the stride and added to the start.
6686 /// Components of the expression are omitted if they are an identity function.
6687 /// Chain (non-affine) SCEVs are not supported.
6688 bool SCEVToValueExpr(const llvm::SCEVAddRecExpr &SAR, ScalarEvolution &SE) {
6689 assert(SAR.isAffine() && "Expected affine SCEV");
6690 const SCEV *Start = SAR.getStart();
6691 const SCEV *Stride = SAR.getStepRecurrence(SE);
6692
6693 // Skip pushing arithmetic noops.
6694 if (!isIdentityFunction(llvm::dwarf::DW_OP_mul, Stride)) {
6695 if (!pushSCEV(Stride))
6696 return false;
6697 pushOperator(llvm::dwarf::DW_OP_mul);
6698 }
6699 if (!isIdentityFunction(llvm::dwarf::DW_OP_plus, Start)) {
6700 if (!pushSCEV(Start))
6701 return false;
6702 pushOperator(llvm::dwarf::DW_OP_plus);
6703 }
6704 return true;
6705 }
6706
6707 /// Create an expression that is an offset from a value (usually the IV).
6708 void createOffsetExpr(int64_t Offset, Value *OffsetValue) {
6709 pushLocation(OffsetValue);
6711 LLVM_DEBUG(
6712 dbgs() << "scev-salvage: Generated IV offset expression. Offset: "
6713 << std::to_string(Offset) << "\n");
6714 }
6715
6716 /// Combine a translation of the SCEV and the IV to create an expression that
6717 /// recovers a location's value.
6718 /// returns true if an expression was created.
6719 bool createIterCountExpr(const SCEV *S,
6720 const SCEVDbgValueBuilder &IterationCount,
6721 ScalarEvolution &SE) {
6722 // SCEVs for SSA values are most frquently of the form
6723 // {start,+,stride}, but sometimes they are ({start,+,stride} + %a + ..).
6724 // This is because %a is a PHI node that is not the IV. However, these
6725 // SCEVs have not been observed to result in debuginfo-lossy optimisations,
6726 // so its not expected this point will be reached.
6727 if (!isa<SCEVAddRecExpr>(S))
6728 return false;
6729
6730 LLVM_DEBUG(dbgs() << "scev-salvage: Location to salvage SCEV: " << *S
6731 << '\n');
6732
6733 const auto *Rec = cast<SCEVAddRecExpr>(S);
6734 if (!Rec->isAffine())
6735 return false;
6736
6738 return false;
6739
6740 // Initialise a new builder with the iteration count expression. In
6741 // combination with the value's SCEV this enables recovery.
6742 clone(IterationCount);
6743 if (!SCEVToValueExpr(*Rec, SE))
6744 return false;
6745
6746 return true;
6747 }
6748
6749 /// Convert a SCEV of a value to a DIExpression that is pushed onto the
6750 /// builder's expression stack. The stack should already contain an
6751 /// expression for the iteration count, so that it can be multiplied by
6752 /// the stride and added to the start.
6753 /// Components of the expression are omitted if they are an identity function.
6754 bool SCEVToIterCountExpr(const llvm::SCEVAddRecExpr &SAR,
6755 ScalarEvolution &SE) {
6756 assert(SAR.isAffine() && "Expected affine SCEV");
6757 const SCEV *Start = SAR.getStart();
6758 const SCEV *Stride = SAR.getStepRecurrence(SE);
6759
6760 // Skip pushing arithmetic noops.
6761 if (!isIdentityFunction(llvm::dwarf::DW_OP_minus, Start)) {
6762 if (!pushSCEV(Start))
6763 return false;
6764 pushOperator(llvm::dwarf::DW_OP_minus);
6765 }
6766 if (!isIdentityFunction(llvm::dwarf::DW_OP_div, Stride)) {
6767 if (!pushSCEV(Stride))
6768 return false;
6769 pushOperator(llvm::dwarf::DW_OP_div);
6770 }
6771 return true;
6772 }
6773
6774 // Append the current expression and locations to a location list and an
6775 // expression list. Modify the DW_OP_LLVM_arg indexes to account for
6776 // the locations already present in the destination list.
6777 void appendToVectors(SmallVectorImpl<uint64_t> &DestExpr,
6778 SmallVectorImpl<Value *> &DestLocations) {
6779 assert(!DestLocations.empty() &&
6780 "Expected the locations vector to contain the IV");
6781 // The DWARF_OP_LLVM_arg arguments of the expression being appended must be
6782 // modified to account for the locations already in the destination vector.
6783 // All builders contain the IV as the first location op.
6784 assert(!LocationOps.empty() &&
6785 "Expected the location ops to contain the IV.");
6786 // DestIndexMap[n] contains the index in DestLocations for the nth
6787 // location in this SCEVDbgValueBuilder.
6788 SmallVector<uint64_t, 2> DestIndexMap;
6789 for (const auto &Op : LocationOps) {
6790 auto It = find(DestLocations, Op);
6791 if (It != DestLocations.end()) {
6792 // Location already exists in DestLocations, reuse existing ArgIndex.
6793 DestIndexMap.push_back(std::distance(DestLocations.begin(), It));
6794 continue;
6795 }
6796 // Location is not in DestLocations, add it.
6797 DestIndexMap.push_back(DestLocations.size());
6798 DestLocations.push_back(Op);
6799 }
6800
6801 for (const auto &Op : expr_ops()) {
6802 if (Op.getOp() != dwarf::DW_OP_LLVM_arg) {
6803 Op.appendToVector(DestExpr);
6804 continue;
6805 }
6806
6808 // `DW_OP_LLVM_arg n` represents the nth LocationOp in this SCEV,
6809 // DestIndexMap[n] contains its new index in DestLocations.
6810 uint64_t NewIndex = DestIndexMap[Op.getArg(0)];
6811 DestExpr.push_back(NewIndex);
6812 }
6813 }
6814};
6815
6816/// Holds all the required data to salvage a dbg.value using the pre-LSR SCEVs
6817/// and DIExpression.
6818struct DVIRecoveryRec {
6819 DVIRecoveryRec(DbgVariableRecord *DVR)
6820 : DbgRef(DVR), Expr(DVR->getExpression()), HadLocationArgList(false) {}
6821
6822 DbgVariableRecord *DbgRef;
6823 DIExpression *Expr;
6824 bool HadLocationArgList;
6825 SmallVector<WeakVH, 2> LocationOps;
6828
6829 void clear() {
6830 for (auto &RE : RecoveryExprs)
6831 RE.reset();
6832 RecoveryExprs.clear();
6833 }
6834
6835 ~DVIRecoveryRec() { clear(); }
6836};
6837} // namespace
6838
6839/// Returns the total number of DW_OP_llvm_arg operands in the expression.
6840/// This helps in determining if a DIArglist is necessary or can be omitted from
6841/// the dbg.value.
6843 auto expr_ops = ToDwarfOpIter(Expr);
6844 unsigned Count = 0;
6845 for (auto Op : expr_ops)
6846 if (Op.getOp() == dwarf::DW_OP_LLVM_arg)
6847 Count++;
6848 return Count;
6849}
6850
6851/// Overwrites DVI with the location and Ops as the DIExpression. This will
6852/// create an invalid expression if Ops has any dwarf::DW_OP_llvm_arg operands,
6853/// because a DIArglist is not created for the first argument of the dbg.value.
6854template <typename T>
6855static void updateDVIWithLocation(T &DbgVal, Value *Location,
6857 assert(numLLVMArgOps(Ops) == 0 && "Expected expression that does not "
6858 "contain any DW_OP_llvm_arg operands.");
6859 DbgVal.setRawLocation(ValueAsMetadata::get(Location));
6860 DbgVal.setExpression(DIExpression::get(DbgVal.getContext(), Ops));
6861 DbgVal.setExpression(DIExpression::get(DbgVal.getContext(), Ops));
6862}
6863
6864/// Overwrite DVI with locations placed into a DIArglist.
6865template <typename T>
6866static void updateDVIWithLocations(T &DbgVal,
6867 SmallVectorImpl<Value *> &Locations,
6869 assert(numLLVMArgOps(Ops) != 0 &&
6870 "Expected expression that references DIArglist locations using "
6871 "DW_OP_llvm_arg operands.");
6873 for (Value *V : Locations)
6874 MetadataLocs.push_back(ValueAsMetadata::get(V));
6875 auto ValArrayRef = llvm::ArrayRef<llvm::ValueAsMetadata *>(MetadataLocs);
6876 DbgVal.setRawLocation(llvm::DIArgList::get(DbgVal.getContext(), ValArrayRef));
6877 DbgVal.setExpression(DIExpression::get(DbgVal.getContext(), Ops));
6878}
6879
6880/// Write the new expression and new location ops for the dbg.value. If possible
6881/// reduce the szie of the dbg.value by omitting DIArglist. This
6882/// can be omitted if:
6883/// 1. There is only a single location, refenced by a single DW_OP_llvm_arg.
6884/// 2. The DW_OP_LLVM_arg is the first operand in the expression.
6885static void UpdateDbgValue(DVIRecoveryRec &DVIRec,
6886 SmallVectorImpl<Value *> &NewLocationOps,
6888 DbgVariableRecord *DbgVal = DVIRec.DbgRef;
6889 unsigned NumLLVMArgs = numLLVMArgOps(NewExpr);
6890 if (NumLLVMArgs == 0) {
6891 // Location assumed to be on the stack.
6892 updateDVIWithLocation(*DbgVal, NewLocationOps[0], NewExpr);
6893 } else if (NumLLVMArgs == 1 && NewExpr[0] == dwarf::DW_OP_LLVM_arg) {
6894 // There is only a single DW_OP_llvm_arg at the start of the expression,
6895 // so it can be omitted along with DIArglist.
6896 assert(NewExpr[1] == 0 &&
6897 "Lone LLVM_arg in a DIExpression should refer to location-op 0.");
6899 updateDVIWithLocation(*DbgVal, NewLocationOps[0], ShortenedOps);
6900 } else {
6901 // Multiple DW_OP_llvm_arg, so DIArgList is strictly necessary.
6902 updateDVIWithLocations(*DbgVal, NewLocationOps, NewExpr);
6903 }
6904
6905 // If the DIExpression was previously empty then add the stack terminator.
6906 // Non-empty expressions have only had elements inserted into them and so
6907 // the terminator should already be present e.g. stack_value or fragment.
6908 DIExpression *SalvageExpr = DbgVal->getExpression();
6909 if (!DVIRec.Expr->isComplex() && SalvageExpr->isComplex()) {
6910 SalvageExpr = DIExpression::append(SalvageExpr, {dwarf::DW_OP_stack_value});
6911 DbgVal->setExpression(SalvageExpr);
6912 }
6913}
6914
6915/// Cached location ops may be erased during LSR, in which case a poison is
6916/// required when restoring from the cache. The type of that location is no
6917/// longer available, so just use int8. The poison will be replaced by one or
6918/// more locations later when a SCEVDbgValueBuilder selects alternative
6919/// locations to use for the salvage.
6921 return (VH) ? VH : PoisonValue::get(llvm::Type::getInt8Ty(C));
6922}
6923
6924/// Restore the DVI's pre-LSR arguments. Substitute undef for any erased values.
6925static void restorePreTransformState(DVIRecoveryRec &DVIRec) {
6926 DbgVariableRecord *DbgVal = DVIRec.DbgRef;
6927 LLVM_DEBUG(dbgs() << "scev-salvage: restore dbg.value to pre-LSR state\n"
6928 << "scev-salvage: post-LSR: " << *DbgVal << '\n');
6929 assert(DVIRec.Expr && "Expected an expression");
6930 DbgVal->setExpression(DVIRec.Expr);
6931
6932 // Even a single location-op may be inside a DIArgList and referenced with
6933 // DW_OP_LLVM_arg, which is valid only with a DIArgList.
6934 if (!DVIRec.HadLocationArgList) {
6935 assert(DVIRec.LocationOps.size() == 1 &&
6936 "Unexpected number of location ops.");
6937 // LSR's unsuccessful salvage attempt may have added DIArgList, which in
6938 // this case was not present before, so force the location back to a
6939 // single uncontained Value.
6940 Value *CachedValue =
6941 getValueOrPoison(DVIRec.LocationOps[0], DbgVal->getContext());
6942 DbgVal->setRawLocation(ValueAsMetadata::get(CachedValue));
6943 } else {
6945 for (WeakVH VH : DVIRec.LocationOps) {
6946 Value *CachedValue = getValueOrPoison(VH, DbgVal->getContext());
6947 MetadataLocs.push_back(ValueAsMetadata::get(CachedValue));
6948 }
6949 auto ValArrayRef = llvm::ArrayRef<llvm::ValueAsMetadata *>(MetadataLocs);
6950 DbgVal->setRawLocation(
6951 llvm::DIArgList::get(DbgVal->getContext(), ValArrayRef));
6952 }
6953 LLVM_DEBUG(dbgs() << "scev-salvage: pre-LSR: " << *DbgVal << '\n');
6954}
6955
6957 llvm::PHINode *LSRInductionVar, DVIRecoveryRec &DVIRec,
6958 const SCEV *SCEVInductionVar,
6959 SCEVDbgValueBuilder IterCountExpr) {
6960
6961 if (!DVIRec.DbgRef->isKillLocation())
6962 return false;
6963
6964 // LSR may have caused several changes to the dbg.value in the failed salvage
6965 // attempt. So restore the DIExpression, the location ops and also the
6966 // location ops format, which is always DIArglist for multiple ops, but only
6967 // sometimes for a single op.
6969
6970 // LocationOpIndexMap[i] will store the post-LSR location index of
6971 // the non-optimised out location at pre-LSR index i.
6972 SmallVector<int64_t, 2> LocationOpIndexMap;
6973 LocationOpIndexMap.assign(DVIRec.LocationOps.size(), -1);
6974 SmallVector<Value *, 2> NewLocationOps;
6975 NewLocationOps.push_back(LSRInductionVar);
6976
6977 for (unsigned i = 0; i < DVIRec.LocationOps.size(); i++) {
6978 WeakVH VH = DVIRec.LocationOps[i];
6979 // Place the locations not optimised out in the list first, avoiding
6980 // inserts later. The map is used to update the DIExpression's
6981 // DW_OP_LLVM_arg arguments as the expression is updated.
6982 if (VH && !isa<UndefValue>(VH)) {
6983 NewLocationOps.push_back(VH);
6984 LocationOpIndexMap[i] = NewLocationOps.size() - 1;
6985 LLVM_DEBUG(dbgs() << "scev-salvage: Location index " << i
6986 << " now at index " << LocationOpIndexMap[i] << "\n");
6987 continue;
6988 }
6989
6990 // It's possible that a value referred to in the SCEV may have been
6991 // optimised out by LSR.
6992 if (SE.containsErasedValue(DVIRec.SCEVs[i]) ||
6993 SE.containsUndefs(DVIRec.SCEVs[i])) {
6994 LLVM_DEBUG(dbgs() << "scev-salvage: SCEV for location at index: " << i
6995 << " refers to a location that is now undef or erased. "
6996 "Salvage abandoned.\n");
6997 return false;
6998 }
6999
7000 LLVM_DEBUG(dbgs() << "scev-salvage: salvaging location at index " << i
7001 << " with SCEV: " << *DVIRec.SCEVs[i] << "\n");
7002
7003 DVIRec.RecoveryExprs[i] = std::make_unique<SCEVDbgValueBuilder>();
7004 SCEVDbgValueBuilder *SalvageExpr = DVIRec.RecoveryExprs[i].get();
7005
7006 // Create an offset-based salvage expression if possible, as it requires
7007 // less DWARF ops than an iteration count-based expression.
7008 if (std::optional<APInt> Offset =
7009 SE.computeConstantDifference(DVIRec.SCEVs[i], SCEVInductionVar)) {
7010 if (Offset->getSignificantBits() <= 64)
7011 SalvageExpr->createOffsetExpr(Offset->getSExtValue(), LSRInductionVar);
7012 else
7013 return false;
7014 } else if (!SalvageExpr->createIterCountExpr(DVIRec.SCEVs[i], IterCountExpr,
7015 SE))
7016 return false;
7017 }
7018
7019 // Merge the DbgValueBuilder generated expressions and the original
7020 // DIExpression, place the result into an new vector.
7022 if (DVIRec.Expr->getNumElements() == 0) {
7023 assert(DVIRec.RecoveryExprs.size() == 1 &&
7024 "Expected only a single recovery expression for an empty "
7025 "DIExpression.");
7026 assert(DVIRec.RecoveryExprs[0] &&
7027 "Expected a SCEVDbgSalvageBuilder for location 0");
7028 SCEVDbgValueBuilder *B = DVIRec.RecoveryExprs[0].get();
7029 B->appendToVectors(NewExpr, NewLocationOps);
7030 }
7031 for (const auto &Op : DVIRec.Expr->expr_ops()) {
7032 // Most Ops needn't be updated.
7033 if (Op.getOp() != dwarf::DW_OP_LLVM_arg) {
7034 Op.appendToVector(NewExpr);
7035 continue;
7036 }
7037
7038 uint64_t LocationArgIndex = Op.getArg(0);
7039 SCEVDbgValueBuilder *DbgBuilder =
7040 DVIRec.RecoveryExprs[LocationArgIndex].get();
7041 // The location doesn't have s SCEVDbgValueBuilder, so LSR did not
7042 // optimise it away. So just translate the argument to the updated
7043 // location index.
7044 if (!DbgBuilder) {
7045 NewExpr.push_back(dwarf::DW_OP_LLVM_arg);
7046 assert(LocationOpIndexMap[Op.getArg(0)] != -1 &&
7047 "Expected a positive index for the location-op position.");
7048 NewExpr.push_back(LocationOpIndexMap[Op.getArg(0)]);
7049 continue;
7050 }
7051 // The location has a recovery expression.
7052 DbgBuilder->appendToVectors(NewExpr, NewLocationOps);
7053 }
7054
7055 UpdateDbgValue(DVIRec, NewLocationOps, NewExpr);
7056 LLVM_DEBUG(dbgs() << "scev-salvage: Updated DVI: " << *DVIRec.DbgRef << "\n");
7057 return true;
7058}
7059
7060/// Obtain an expression for the iteration count, then attempt to salvage the
7061/// dbg.value intrinsics.
7063 llvm::Loop *L, ScalarEvolution &SE, llvm::PHINode *LSRInductionVar,
7064 SmallVector<std::unique_ptr<DVIRecoveryRec>, 2> &DVIToUpdate) {
7065 if (DVIToUpdate.empty())
7066 return;
7067
7068 const llvm::SCEV *SCEVInductionVar = SE.getSCEV(LSRInductionVar);
7069 assert(SCEVInductionVar &&
7070 "Anticipated a SCEV for the post-LSR induction variable");
7071
7072 if (const SCEVAddRecExpr *IVAddRec =
7073 dyn_cast<SCEVAddRecExpr>(SCEVInductionVar)) {
7074 if (!IVAddRec->isAffine())
7075 return;
7076
7077 // Prevent translation using excessive resources.
7078 if (IVAddRec->getExpressionSize() > MaxSCEVSalvageExpressionSize)
7079 return;
7080
7081 // The iteration count is required to recover location values.
7082 SCEVDbgValueBuilder IterCountExpr;
7083 IterCountExpr.pushLocation(LSRInductionVar);
7084 if (!IterCountExpr.SCEVToIterCountExpr(*IVAddRec, SE))
7085 return;
7086
7087 LLVM_DEBUG(dbgs() << "scev-salvage: IV SCEV: " << *SCEVInductionVar
7088 << '\n');
7089
7090 for (auto &DVIRec : DVIToUpdate) {
7091 SalvageDVI(L, SE, LSRInductionVar, *DVIRec, SCEVInductionVar,
7092 IterCountExpr);
7093 }
7094 }
7095}
7096
7097/// Identify and cache salvageable DVI locations and expressions along with the
7098/// corresponding SCEV(s). Also ensure that the DVI is not deleted between
7099/// cacheing and salvaging.
7101 Loop *L, ScalarEvolution &SE,
7102 SmallVector<std::unique_ptr<DVIRecoveryRec>, 2> &SalvageableDVISCEVs) {
7103 for (const auto &B : L->getBlocks()) {
7104 for (auto &I : *B) {
7105 for (DbgVariableRecord &DbgVal : filterDbgVars(I.getDbgRecordRange())) {
7106 if (!DbgVal.isDbgValue() && !DbgVal.isDbgAssign())
7107 continue;
7108
7109 // Ensure that if any location op is undef that the dbg.vlue is not
7110 // cached.
7111 if (DbgVal.isKillLocation())
7112 continue;
7113
7114 // Check that the location op SCEVs are suitable for translation to
7115 // DIExpression.
7116 const auto &HasTranslatableLocationOps =
7117 [&](const DbgVariableRecord &DbgValToTranslate) -> bool {
7118 for (const auto LocOp : DbgValToTranslate.location_ops()) {
7119 if (!LocOp)
7120 return false;
7121
7122 if (!SE.isSCEVable(LocOp->getType()))
7123 return false;
7124
7125 const SCEV *S = SE.getSCEV(LocOp);
7126 if (SE.containsUndefs(S))
7127 return false;
7128 }
7129 return true;
7130 };
7131
7132 if (!HasTranslatableLocationOps(DbgVal))
7133 continue;
7134
7135 std::unique_ptr<DVIRecoveryRec> NewRec =
7136 std::make_unique<DVIRecoveryRec>(&DbgVal);
7137 // Each location Op may need a SCEVDbgValueBuilder in order to recover
7138 // it. Pre-allocating a vector will enable quick lookups of the builder
7139 // later during the salvage.
7140 NewRec->RecoveryExprs.resize(DbgVal.getNumVariableLocationOps());
7141 for (const auto LocOp : DbgVal.location_ops()) {
7142 NewRec->SCEVs.push_back(SE.getSCEV(LocOp));
7143 NewRec->LocationOps.push_back(LocOp);
7144 NewRec->HadLocationArgList = DbgVal.hasArgList();
7145 }
7146 SalvageableDVISCEVs.push_back(std::move(NewRec));
7147 }
7148 }
7149 }
7150}
7151
7152/// Ideally pick the PHI IV inserted by ScalarEvolutionExpander. As a fallback
7153/// any PHi from the loop header is usable, but may have less chance of
7154/// surviving subsequent transforms.
7156 const LSRInstance &LSR) {
7157
7158 auto IsSuitableIV = [&](PHINode *P) {
7159 if (!SE.isSCEVable(P->getType()))
7160 return false;
7161 if (const SCEVAddRecExpr *Rec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(P)))
7162 return Rec->isAffine() && !SE.containsUndefs(SE.getSCEV(P));
7163 return false;
7164 };
7165
7166 // For now, just pick the first IV that was generated and inserted by
7167 // ScalarEvolution. Ideally pick an IV that is unlikely to be optimised away
7168 // by subsequent transforms.
7169 for (const WeakVH &IV : LSR.getScalarEvolutionIVs()) {
7170 if (!IV)
7171 continue;
7172
7173 // There should only be PHI node IVs.
7174 PHINode *P = cast<PHINode>(&*IV);
7175
7176 if (IsSuitableIV(P))
7177 return P;
7178 }
7179
7180 for (PHINode &P : L.getHeader()->phis()) {
7181 if (IsSuitableIV(&P))
7182 return &P;
7183 }
7184 return nullptr;
7185}
7186
7188 DominatorTree &DT, LoopInfo &LI,
7189 const TargetTransformInfo &TTI,
7191 MemorySSA *MSSA, bool PreserveLCSSA) {
7192
7193 // Debug preservation - before we start removing anything identify which DVI
7194 // meet the salvageable criteria and store their DIExpression and SCEVs.
7195 SmallVector<std::unique_ptr<DVIRecoveryRec>, 2> SalvageableDVIRecords;
7196 DbgGatherSalvagableDVI(L, SE, SalvageableDVIRecords);
7197
7198 bool Changed = false;
7199 std::unique_ptr<MemorySSAUpdater> MSSAU;
7200 if (MSSA)
7201 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
7202
7203 // Run the main LSR transformation.
7204 const LSRInstance &Reducer =
7205 LSRInstance(L, IU, SE, DT, LI, TTI, AC, TLI, MSSAU.get(), PreserveLCSSA);
7206 Changed |= Reducer.getChanged();
7207
7208 // Remove any extra phis created by processing inner loops.
7209 Changed |= DeleteDeadPHIs(L->getHeader(), &TLI, MSSAU.get());
7210 if (EnablePhiElim && L->isLoopSimplifyForm()) {
7212 SCEVExpander Rewriter(SE, "lsr", false);
7213#if LLVM_ENABLE_ABI_BREAKING_CHECKS
7214 Rewriter.setDebugType(DEBUG_TYPE);
7215#endif
7216 unsigned numFolded = Rewriter.replaceCongruentIVs(L, &DT, DeadInsts, &TTI);
7217 Rewriter.clear();
7218 if (numFolded) {
7219 Changed = true;
7221 MSSAU.get());
7222 DeleteDeadPHIs(L->getHeader(), &TLI, MSSAU.get());
7223 }
7224 }
7225 // LSR may at times remove all uses of an induction variable from a loop.
7226 // The only remaining use is the PHI in the exit block.
7227 // When this is the case, if the exit value of the IV can be calculated using
7228 // SCEV, we can replace the exit block PHI with the final value of the IV and
7229 // skip the updates in each loop iteration.
7230 if (L->isRecursivelyLCSSAForm(DT, LI) && L->getExitBlock()) {
7232 SCEVExpander Rewriter(SE, "lsr", true);
7233 int Rewrites = rewriteLoopExitValues(L, &LI, &TLI, &SE, &TTI, Rewriter, &DT,
7234 UnusedIndVarInLoop, DeadInsts);
7235 Rewriter.clear();
7236 if (Rewrites) {
7237 Changed = true;
7239 MSSAU.get());
7240 DeleteDeadPHIs(L->getHeader(), &TLI, MSSAU.get());
7241 }
7242 }
7243
7244 if (SalvageableDVIRecords.empty())
7245 return Changed;
7246
7247 // Obtain relevant IVs and attempt to rewrite the salvageable DVIs with
7248 // expressions composed using the derived iteration count.
7249 // TODO: Allow for multiple IV references for nested AddRecSCEVs
7250 for (const auto &L : LI) {
7251 if (llvm::PHINode *IV = GetInductionVariable(*L, SE, Reducer))
7252 DbgRewriteSalvageableDVIs(L, SE, IV, SalvageableDVIRecords);
7253 else {
7254 LLVM_DEBUG(dbgs() << "scev-salvage: SCEV salvaging not possible. An IV "
7255 "could not be identified.\n");
7256 }
7257 }
7258
7259 for (auto &Rec : SalvageableDVIRecords)
7260 Rec->clear();
7261 SalvageableDVIRecords.clear();
7262 return Changed;
7263}
7264
7265bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager & /*LPM*/) {
7266 if (skipLoop(L))
7267 return false;
7268
7269 auto &IU = getAnalysis<IVUsersWrapperPass>().getIU();
7270 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
7271 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
7272 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
7273 const auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
7274 *L->getHeader()->getParent());
7275 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
7276 *L->getHeader()->getParent());
7277 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
7278 *L->getHeader()->getParent());
7279 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
7280 MemorySSA *MSSA = nullptr;
7281 if (MSSAAnalysis)
7282 MSSA = &MSSAAnalysis->getMSSA();
7283 return ReduceLoopStrength(L, IU, SE, DT, LI, TTI, AC, TLI, MSSA,
7284 /*PreserveLCSSA=*/false);
7285}
7286
7289 LPMUpdater &) {
7290 if (!ReduceLoopStrength(&L, AM.getResult<IVUsersAnalysis>(L, AR), AR.SE,
7291 AR.DT, AR.LI, AR.TTI, AR.AC, AR.TLI, AR.MSSA,
7292 /*PreserveLCSSA=*/true))
7293 return PreservedAnalyses::all();
7294
7295 auto PA = getLoopPassPreservedAnalyses();
7296 if (AR.MSSA)
7297 PA.preserve<MemorySSAAnalysis>();
7298 return PA;
7299}
7300
7301char LoopStrengthReduce::ID = 0;
7302
7303INITIALIZE_PASS_BEGIN(LoopStrengthReduce, "loop-reduce",
7304 "Loop Strength Reduction", false, false)
7310INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
7311INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce",
7312 "Loop Strength Reduction", false, false)
7313
7314Pass *llvm::createLoopStrengthReducePass() { return new LoopStrengthReduce(); }
#define Success
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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 const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
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 clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:672
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool isCanonical(const MDString *S)
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
#define DEBUG_TYPE
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
This defines the Use class.
iv Induction Variable Users
Definition IVUsers.cpp:48
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
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 cl::opt< bool > DropScaledForVScale("lsr-drop-scaled-reg-for-vscale", cl::Hidden, cl::init(true), cl::desc("Avoid using scaled registers with vscale-relative addressing"))
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 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 cl::opt< TTI::AddressingModeKind > PreferredAddresingMode("lsr-preferred-addressing-mode", cl::Hidden, cl::init(TTI::AMK_None), cl::desc("A flag that overrides the target's preferred addressing mode."), cl::values(clEnumValN(TTI::AMK_None, "none", "Don't prefer any addressing mode"), clEnumValN(TTI::AMK_PreIndexed, "preindexed", "Prefer pre-indexed addressing mode"), clEnumValN(TTI::AMK_PostIndexed, "postindexed", "Prefer post-indexed addressing mode"), clEnumValN(TTI::AMK_All, "all", "Consider all addressing modes")))
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 cl::opt< bool > InsnsCost("lsr-insns-cost", cl::Hidden, cl::init(true), cl::desc("Add instruction count to a LSR cost model"))
static cl::opt< bool > StressIVChain("stress-ivchain", cl::Hidden, cl::init(false), cl::desc("Stress test LSR IV chains"))
static bool isAddressUse(const TargetTransformInfo &TTI, Instruction *Inst, Value *OperandVal)
Returns true if the specified instruction is using the specified value as an address.
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 cl::opt< cl::boolOrDefault > AllowDropSolutionIfLessProfitable("lsr-drop-solution", cl::Hidden, cl::desc("Attempt to drop solution if it is less profitable"))
static cl::opt< bool > EnableVScaleImmediates("lsr-enable-vscale-immediates", cl::Hidden, cl::init(true), cl::desc("Enable analysis of vscale-relative immediates in LSR"))
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 bool isAlwaysFoldable(const TargetTransformInfo &TTI, LSRUse::KindType Kind, MemAccessTy AccessTy, GlobalValue *BaseGV, Immediate BaseOffset, bool HasBaseReg)
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 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 Immediate ExtractImmediate(SCEVUse &S, ScalarEvolution &SE, bool PreferScalable=false)
If S involves the addition of a constant integer value, return that integer value,...
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 cl::opt< bool > EnablePhiElim("enable-lsr-phielim", cl::Hidden, cl::init(true), cl::desc("Enable LSR phi elimination"))
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 Immediate ExtractImmediateOperand(MutableArrayRef< SCEVUse > Ops, ScalarEvolution &SE, bool PreferScalable)
Extracts an immediate operand from Ops and replaces the operand with zero.
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 cl::opt< bool > LSRExpNarrow("lsr-exp-narrow", cl::Hidden, cl::init(false), cl::desc("Narrow LSR complex solution using" " expectation of registers number"))
static cl::opt< bool > FilterSameScaledReg("lsr-filter-same-scaled-reg", cl::Hidden, cl::init(true), cl::desc("Narrow LSR search space by filtering non-optimal formulae" " with the same ScaledReg and Scale"))
static void updateDVIWithLocations(T &DbgVal, SmallVectorImpl< Value * > &Locations, SmallVectorImpl< uint64_t > &Ops)
Overwrite DVI with locations placed into a DIArglist.
static cl::opt< unsigned > ComplexityLimit("lsr-complexity-limit", cl::Hidden, cl::init(std::numeric_limits< uint16_t >::max()), cl::desc("LSR search space complexity limit"))
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 bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst, Value *Operand, const TargetTransformInfo &TTI)
Return true if the IVInc can be folded into an addressing mode.
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)
static cl::opt< unsigned > SetupCostDepthLimit("lsr-setupcost-depth-limit", cl::Hidden, cl::init(7), cl::desc("The limit on recursion depth for LSRs setup cost"))
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Register Reg
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
#define T
uint64_t IntrinsicInst * II
#define P(N)
PowerPC TLS Dynamic Call Fixup
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the PointerIntPair class.
const SmallVectorImpl< MachineOperand > & Cond
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 contains some templates that are useful if you are working with the STL at all.
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 const unsigned UnknownAddressSpace
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Virtual Register Rewriter
Value * RHS
Value * LHS
BinaryOperator * Mul
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1565
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1670
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
Definition APInt.h:1556
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1771
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1587
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)
Definition Pass.cpp:289
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),...
Definition ArrayRef.h:40
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.
Definition BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:530
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition BasicBlock.h:388
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.
Definition BasicBlock.h:237
BinaryOps getOpcode() const
Definition InstrTypes.h:409
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.
Definition InstrTypes.h:740
@ ICMP_NE
not equal
Definition InstrTypes.h:762
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
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.
Definition Constants.h:135
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition Constants.h:174
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
DWARF expression.
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 > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:284
NodeT * getBlock() const
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.
Definition Dominators.h:306
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
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.
Definition IVUsers.h:36
void transformToPostInc(const Loop *L)
transformToPostInc - Transform the expression to post-inc form for the given loop.
Definition IVUsers.cpp:365
Value * getOperandValToReplace() const
getOperandValToReplace - Return the Value of the operand in the user instruction that this IVStrideUs...
Definition IVUsers.h:55
void setUser(Instruction *NewUser)
setUser - Assign a new user instruction for this use.
Definition IVUsers.h:49
Analysis pass that exposes the IVUsers for a loop.
Definition IVUsers.h:187
ilist< IVStrideUse >::const_iterator const_iterator
Definition IVUsers.h:143
iterator end()
Definition IVUsers.h:145
iterator begin()
Definition IVUsers.h:144
bool empty() const
Definition IVUsers.h:148
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.
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.
Definition Type.cpp:348
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
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.
Definition LoopInfo.h:612
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1565
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:922
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
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'.
Definition Pass.h:99
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.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
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.
ArrayRef< SCEVUse > operands() 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.
static constexpr auto FlagAnyWrap
LLVM_ABI ArrayRef< SCEVUse > operands() const
Return operands of this SCEV expression.
SCEVTypes getSCEVType() const
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
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 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 * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
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 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 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(const SCEV *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 * getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getVScale(Type *Ty)
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 const SCEV * getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
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.
Definition SetVector.h:103
iterator end()
Get an iterator to the end of the SetVector.
Definition SetVector.h:112
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
SmallBitVector & 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.
SmallBitVector & reset()
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.
Definition SmallSet.h:184
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 resize(size_type N)
void push_back(const T &Elt)
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)
Definition TypeSize.h:41
An instruction for storing to memory.
Provides information about what library functions are available for the current target.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool shouldDropLSRSolutionIfLessProfitable() const
Return true if LSR should drop a found solution if it's calculated to be less profitable than the bas...
LLVM_ABI bool isLSRCostLess(const TargetTransformInfo::LSRCost &C1, const TargetTransformInfo::LSRCost &C2) const
Return true if LSR cost of C1 is lower than C2.
LLVM_ABI bool isIndexedStoreLegal(enum MemIndexedMode Mode, Type *Ty) const
LLVM_ABI unsigned getRegisterClassForType(bool Vector, Type *Ty=nullptr) const
LLVM_ABI bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace=0, Instruction *I=nullptr, int64_t ScalableOffset=0) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
@ TCK_RecipThroughput
Reciprocal throughput.
LLVM_ABI bool isIndexedLoadLegal(enum MemIndexedMode Mode, Type *Ty) const
LLVM_ABI bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
LLVM_ABI bool isLegalAddImmediate(int64_t Imm) const
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
LLVM_ABI bool canSaveCmp(Loop *L, CondBrInst **BI, ScalarEvolution *SE, LoopInfo *LI, DominatorTree *DT, AssumptionCache *AC, TargetLibraryInfo *LibInfo) const
Return true if the target can save a compare for loop count, for example hardware loop saves a compar...
LLVM_ABI unsigned getNumberOfRegisters(unsigned ClassID) const
LLVM_ABI bool isLegalAddScalableImmediate(int64_t Imm) const
Return true if adding the specified scalable immediate is legal, that is the target has add instructi...
@ TCC_Free
Expected to fold away in lowering.
LLVM_ABI bool canMacroFuseCmp() const
Return true if the target can fuse a compare and branch.
AddressingModeKind
Which addressing mode Loop Strength Reduction will try to generate.
@ AMK_PostIndexed
Prefer post-indexed addressing mode.
@ AMK_All
Consider all addressing modes.
@ AMK_PreIndexed
Prefer pre-indexed addressing mode.
@ AMK_None
Don't prefer any addressing mode.
LLVM_ABI bool isTruncateFree(Type *Ty1, Type *Ty2) const
Return true if it's free to truncate a value of type Ty1 to type Ty2.
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
Definition Type.cpp:61
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:237
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
Use * op_iterator
Definition User.h:254
op_range operands()
Definition User.h:267
op_iterator op_begin()
Definition User.h:259
void setOperand(unsigned i, Value *Val)
Definition User.h:212
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Value * getOperand(unsigned i) const
Definition User.h:207
op_iterator op_end()
Definition User.h:261
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:509
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
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()
Definition Value.h:380
A nullable Value handle that is nullable.
int getNumOccurrences() const
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:187
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
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.
@ Entry
Definition COFF.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
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.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
@ DW_OP_LLVM_convert
Only used in LLVM metadata.
Definition Dwarf.h:145
constexpr double e
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
Definition PtrState.h:41
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:392
NodeAddr< UseNode * > Use
Definition RDFGraph.h:387
iterator end() const
Definition BasicBlock.h:89
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
LLVM_ABI iterator begin() const
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
unsigned KindType
For isa, dyn_cast, etc operations on TelemetryInfo.
Definition Telemetry.h:83
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.
Definition STLExtras.h:315
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Offset
Definition DWP.cpp:573
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1765
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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...
Definition Utils.cpp:1690
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2144
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.
Definition STLExtras.h:2208
LLVM_ABI char & LoopSimplifyID
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
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)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI void initializeLoopStrengthReducePass(PassRegistry &)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
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)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
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...
Definition Casting.h:547
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 > >
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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...
Definition STLExtras.h:2012
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...
Definition Local.cpp:550
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.
Definition LCSSA.cpp:308
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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.
Definition STLExtras.h:1772
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.
Definition STLExtras.h:1947
@ UnusedIndVarInLoop
Definition LoopUtils.h:600
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
SCEVUseT< const SCEV * > SCEVUse
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.
Definition BitVector.h:862
#define N
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...
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.