LLVM  14.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"
59 #include "llvm/ADT/Hashing.h"
61 #include "llvm/ADT/STLExtras.h"
62 #include "llvm/ADT/SetVector.h"
64 #include "llvm/ADT/SmallPtrSet.h"
65 #include "llvm/ADT/SmallSet.h"
66 #include "llvm/ADT/SmallVector.h"
69 #include "llvm/Analysis/IVUsers.h"
71 #include "llvm/Analysis/LoopInfo.h"
72 #include "llvm/Analysis/LoopPass.h"
81 #include "llvm/Config/llvm-config.h"
82 #include "llvm/IR/BasicBlock.h"
83 #include "llvm/IR/Constant.h"
84 #include "llvm/IR/Constants.h"
86 #include "llvm/IR/DerivedTypes.h"
87 #include "llvm/IR/Dominators.h"
88 #include "llvm/IR/GlobalValue.h"
89 #include "llvm/IR/IRBuilder.h"
90 #include "llvm/IR/InstrTypes.h"
91 #include "llvm/IR/Instruction.h"
92 #include "llvm/IR/Instructions.h"
93 #include "llvm/IR/IntrinsicInst.h"
94 #include "llvm/IR/Intrinsics.h"
95 #include "llvm/IR/Module.h"
96 #include "llvm/IR/OperandTraits.h"
97 #include "llvm/IR/Operator.h"
98 #include "llvm/IR/PassManager.h"
99 #include "llvm/IR/Type.h"
100 #include "llvm/IR/Use.h"
101 #include "llvm/IR/User.h"
102 #include "llvm/IR/Value.h"
103 #include "llvm/IR/ValueHandle.h"
104 #include "llvm/InitializePasses.h"
105 #include "llvm/Pass.h"
106 #include "llvm/Support/Casting.h"
108 #include "llvm/Support/Compiler.h"
109 #include "llvm/Support/Debug.h"
111 #include "llvm/Support/MathExtras.h"
113 #include "llvm/Transforms/Scalar.h"
114 #include "llvm/Transforms/Utils.h"
118 #include <algorithm>
119 #include <cassert>
120 #include <cstddef>
121 #include <cstdint>
122 #include <cstdlib>
123 #include <iterator>
124 #include <limits>
125 #include <map>
126 #include <numeric>
127 #include <utility>
128 
129 using namespace llvm;
130 
131 #define DEBUG_TYPE "loop-reduce"
132 
133 /// MaxIVUsers is an arbitrary threshold that provides an early opportunity for
134 /// bail out. This threshold is far beyond the number of users that LSR can
135 /// conceivably solve, so it should not affect generated code, but catches the
136 /// worst cases before LSR burns too much compile time and stack space.
137 static const unsigned MaxIVUsers = 200;
138 
139 // Temporary flag to cleanup congruent phis after LSR phi expansion.
140 // It's currently disabled until we can determine whether it's truly useful or
141 // not. The flag should be removed after the v3.0 release.
142 // This is now needed for ivchains.
144  "enable-lsr-phielim", cl::Hidden, cl::init(true),
145  cl::desc("Enable LSR phi elimination"));
146 
147 // The flag adds instruction count to solutions cost comparision.
148 static cl::opt<bool> InsnsCost(
149  "lsr-insns-cost", cl::Hidden, cl::init(true),
150  cl::desc("Add instruction count to a LSR cost model"));
151 
152 // Flag to choose how to narrow complex lsr solution
154  "lsr-exp-narrow", cl::Hidden, cl::init(false),
155  cl::desc("Narrow LSR complex solution using"
156  " expectation of registers number"));
157 
158 // Flag to narrow search space by filtering non-optimal formulae with
159 // the same ScaledReg and Scale.
161  "lsr-filter-same-scaled-reg", cl::Hidden, cl::init(true),
162  cl::desc("Narrow LSR search space by filtering non-optimal formulae"
163  " with the same ScaledReg and Scale"));
164 
166  "lsr-preferred-addressing-mode", cl::Hidden, cl::init(TTI::AMK_None),
167  cl::desc("A flag that overrides the target's preferred addressing mode."),
169  "none",
170  "Don't prefer any addressing mode"),
172  "preindexed",
173  "Prefer pre-indexed addressing mode"),
175  "postindexed",
176  "Prefer post-indexed addressing mode")));
177 
179  "lsr-complexity-limit", cl::Hidden,
181  cl::desc("LSR search space complexity limit"));
182 
184  "lsr-setupcost-depth-limit", cl::Hidden, cl::init(7),
185  cl::desc("The limit on recursion depth for LSRs setup cost"));
186 
187 #ifndef NDEBUG
188 // Stress test IV chain generation.
190  "stress-ivchain", cl::Hidden, cl::init(false),
191  cl::desc("Stress test LSR IV chains"));
192 #else
193 static bool StressIVChain = false;
194 #endif
195 
196 namespace {
197 
198 struct MemAccessTy {
199  /// Used in situations where the accessed memory type is unknown.
200  static const unsigned UnknownAddressSpace =
202 
203  Type *MemTy = nullptr;
204  unsigned AddrSpace = UnknownAddressSpace;
205 
206  MemAccessTy() = default;
207  MemAccessTy(Type *Ty, unsigned AS) : MemTy(Ty), AddrSpace(AS) {}
208 
209  bool operator==(MemAccessTy Other) const {
210  return MemTy == Other.MemTy && AddrSpace == Other.AddrSpace;
211  }
212 
213  bool operator!=(MemAccessTy Other) const { return !(*this == Other); }
214 
215  static MemAccessTy getUnknown(LLVMContext &Ctx,
216  unsigned AS = UnknownAddressSpace) {
217  return MemAccessTy(Type::getVoidTy(Ctx), AS);
218  }
219 
220  Type *getType() { return MemTy; }
221 };
222 
223 /// This class holds data which is used to order reuse candidates.
224 class RegSortData {
225 public:
226  /// This represents the set of LSRUse indices which reference
227  /// a particular register.
228  SmallBitVector UsedByIndices;
229 
230  void print(raw_ostream &OS) const;
231  void dump() const;
232 };
233 
234 } // end anonymous namespace
235 
236 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
237 void RegSortData::print(raw_ostream &OS) const {
238  OS << "[NumUses=" << UsedByIndices.count() << ']';
239 }
240 
241 LLVM_DUMP_METHOD void RegSortData::dump() const {
242  print(errs()); errs() << '\n';
243 }
244 #endif
245 
246 namespace {
247 
248 /// Map register candidates to information about how they are used.
249 class RegUseTracker {
250  using RegUsesTy = DenseMap<const SCEV *, RegSortData>;
251 
252  RegUsesTy RegUsesMap;
254 
255 public:
256  void countRegister(const SCEV *Reg, size_t LUIdx);
257  void dropRegister(const SCEV *Reg, size_t LUIdx);
258  void swapAndDropUse(size_t LUIdx, size_t LastLUIdx);
259 
260  bool isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const;
261 
262  const SmallBitVector &getUsedByIndices(const SCEV *Reg) const;
263 
264  void clear();
265 
267  using const_iterator = SmallVectorImpl<const SCEV *>::const_iterator;
268 
269  iterator begin() { return RegSequence.begin(); }
270  iterator end() { return RegSequence.end(); }
271  const_iterator begin() const { return RegSequence.begin(); }
272  const_iterator end() const { return RegSequence.end(); }
273 };
274 
275 } // end anonymous namespace
276 
277 void
278 RegUseTracker::countRegister(const SCEV *Reg, size_t LUIdx) {
279  std::pair<RegUsesTy::iterator, bool> Pair =
280  RegUsesMap.insert(std::make_pair(Reg, RegSortData()));
281  RegSortData &RSD = Pair.first->second;
282  if (Pair.second)
283  RegSequence.push_back(Reg);
284  RSD.UsedByIndices.resize(std::max(RSD.UsedByIndices.size(), LUIdx + 1));
285  RSD.UsedByIndices.set(LUIdx);
286 }
287 
288 void
289 RegUseTracker::dropRegister(const SCEV *Reg, size_t LUIdx) {
290  RegUsesTy::iterator It = RegUsesMap.find(Reg);
291  assert(It != RegUsesMap.end());
292  RegSortData &RSD = It->second;
293  assert(RSD.UsedByIndices.size() > LUIdx);
294  RSD.UsedByIndices.reset(LUIdx);
295 }
296 
297 void
298 RegUseTracker::swapAndDropUse(size_t LUIdx, size_t LastLUIdx) {
299  assert(LUIdx <= LastLUIdx);
300 
301  // Update RegUses. The data structure is not optimized for this purpose;
302  // we must iterate through it and update each of the bit vectors.
303  for (auto &Pair : RegUsesMap) {
304  SmallBitVector &UsedByIndices = Pair.second.UsedByIndices;
305  if (LUIdx < UsedByIndices.size())
306  UsedByIndices[LUIdx] =
307  LastLUIdx < UsedByIndices.size() ? UsedByIndices[LastLUIdx] : false;
308  UsedByIndices.resize(std::min(UsedByIndices.size(), LastLUIdx));
309  }
310 }
311 
312 bool
313 RegUseTracker::isRegUsedByUsesOtherThan(const SCEV *Reg, size_t LUIdx) const {
314  RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
315  if (I == RegUsesMap.end())
316  return false;
317  const SmallBitVector &UsedByIndices = I->second.UsedByIndices;
318  int i = UsedByIndices.find_first();
319  if (i == -1) return false;
320  if ((size_t)i != LUIdx) return true;
321  return UsedByIndices.find_next(i) != -1;
322 }
323 
324 const SmallBitVector &RegUseTracker::getUsedByIndices(const SCEV *Reg) const {
325  RegUsesTy::const_iterator I = RegUsesMap.find(Reg);
326  assert(I != RegUsesMap.end() && "Unknown register!");
327  return I->second.UsedByIndices;
328 }
329 
330 void RegUseTracker::clear() {
331  RegUsesMap.clear();
332  RegSequence.clear();
333 }
334 
335 namespace {
336 
337 /// This class holds information that describes a formula for computing
338 /// satisfying a use. It may include broken-out immediates and scaled registers.
339 struct Formula {
340  /// Global base address used for complex addressing.
341  GlobalValue *BaseGV = nullptr;
342 
343  /// Base offset for complex addressing.
344  int64_t BaseOffset = 0;
345 
346  /// Whether any complex addressing has a base register.
347  bool HasBaseReg = false;
348 
349  /// The scale of any complex addressing.
350  int64_t Scale = 0;
351 
352  /// The list of "base" registers for this use. When this is non-empty. The
353  /// canonical representation of a formula is
354  /// 1. BaseRegs.size > 1 implies ScaledReg != NULL and
355  /// 2. ScaledReg != NULL implies Scale != 1 || !BaseRegs.empty().
356  /// 3. The reg containing recurrent expr related with currect loop in the
357  /// formula should be put in the ScaledReg.
358  /// #1 enforces that the scaled register is always used when at least two
359  /// registers are needed by the formula: e.g., reg1 + reg2 is reg1 + 1 * reg2.
360  /// #2 enforces that 1 * reg is reg.
361  /// #3 ensures invariant regs with respect to current loop can be combined
362  /// together in LSR codegen.
363  /// This invariant can be temporarily broken while building a formula.
364  /// However, every formula inserted into the LSRInstance must be in canonical
365  /// form.
367 
368  /// The 'scaled' register for this use. This should be non-null when Scale is
369  /// not zero.
370  const SCEV *ScaledReg = nullptr;
371 
372  /// An additional constant offset which added near the use. This requires a
373  /// temporary register, but the offset itself can live in an add immediate
374  /// field rather than a register.
375  int64_t UnfoldedOffset = 0;
376 
377  Formula() = default;
378 
379  void initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE);
380 
381  bool isCanonical(const Loop &L) const;
382 
383  void canonicalize(const Loop &L);
384 
385  bool unscale();
386 
387  bool hasZeroEnd() const;
388 
389  size_t getNumRegs() const;
390  Type *getType() const;
391 
392  void deleteBaseReg(const SCEV *&S);
393 
394  bool referencesReg(const SCEV *S) const;
395  bool hasRegsUsedByUsesOtherThan(size_t LUIdx,
396  const RegUseTracker &RegUses) const;
397 
398  void print(raw_ostream &OS) const;
399  void dump() const;
400 };
401 
402 } // end anonymous namespace
403 
404 /// Recursion helper for initialMatch.
405 static void DoInitialMatch(const SCEV *S, Loop *L,
408  ScalarEvolution &SE) {
409  // Collect expressions which properly dominate the loop header.
410  if (SE.properlyDominates(S, L->getHeader())) {
411  Good.push_back(S);
412  return;
413  }
414 
415  // Look at add operands.
416  if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
417  for (const SCEV *S : Add->operands())
418  DoInitialMatch(S, L, Good, Bad, SE);
419  return;
420  }
421 
422  // Look at addrec operands.
423  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
424  if (!AR->getStart()->isZero() && AR->isAffine()) {
425  DoInitialMatch(AR->getStart(), L, Good, Bad, SE);
426  DoInitialMatch(SE.getAddRecExpr(SE.getConstant(AR->getType(), 0),
427  AR->getStepRecurrence(SE),
428  // FIXME: AR->getNoWrapFlags()
429  AR->getLoop(), SCEV::FlagAnyWrap),
430  L, Good, Bad, SE);
431  return;
432  }
433 
434  // Handle a multiplication by -1 (negation) if it didn't fold.
435  if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S))
436  if (Mul->getOperand(0)->isAllOnesValue()) {
437  SmallVector<const SCEV *, 4> Ops(drop_begin(Mul->operands()));
438  const SCEV *NewMul = SE.getMulExpr(Ops);
439 
442  DoInitialMatch(NewMul, L, MyGood, MyBad, SE);
443  const SCEV *NegOne = SE.getSCEV(ConstantInt::getAllOnesValue(
444  SE.getEffectiveSCEVType(NewMul->getType())));
445  for (const SCEV *S : MyGood)
446  Good.push_back(SE.getMulExpr(NegOne, S));
447  for (const SCEV *S : MyBad)
448  Bad.push_back(SE.getMulExpr(NegOne, S));
449  return;
450  }
451 
452  // Ok, we can't do anything interesting. Just stuff the whole thing into a
453  // register and hope for the best.
454  Bad.push_back(S);
455 }
456 
457 /// Incorporate loop-variant parts of S into this Formula, attempting to keep
458 /// all loop-invariant and loop-computable values in a single base register.
459 void Formula::initialMatch(const SCEV *S, Loop *L, ScalarEvolution &SE) {
462  DoInitialMatch(S, L, Good, Bad, SE);
463  if (!Good.empty()) {
464  const SCEV *Sum = SE.getAddExpr(Good);
465  if (!Sum->isZero())
466  BaseRegs.push_back(Sum);
467  HasBaseReg = true;
468  }
469  if (!Bad.empty()) {
470  const SCEV *Sum = SE.getAddExpr(Bad);
471  if (!Sum->isZero())
472  BaseRegs.push_back(Sum);
473  HasBaseReg = true;
474  }
475  canonicalize(*L);
476 }
477 
478 /// Check whether or not this formula satisfies the canonical
479 /// representation.
480 /// \see Formula::BaseRegs.
481 bool Formula::isCanonical(const Loop &L) const {
482  if (!ScaledReg)
483  return BaseRegs.size() <= 1;
484 
485  if (Scale != 1)
486  return true;
487 
488  if (Scale == 1 && BaseRegs.empty())
489  return false;
490 
491  const SCEVAddRecExpr *SAR = dyn_cast<const SCEVAddRecExpr>(ScaledReg);
492  if (SAR && SAR->getLoop() == &L)
493  return true;
494 
495  // If ScaledReg is not a recurrent expr, or it is but its loop is not current
496  // loop, meanwhile BaseRegs contains a recurrent expr reg related with current
497  // loop, we want to swap the reg in BaseRegs with ScaledReg.
498  auto I = find_if(BaseRegs, [&](const SCEV *S) {
499  return isa<const SCEVAddRecExpr>(S) &&
500  (cast<SCEVAddRecExpr>(S)->getLoop() == &L);
501  });
502  return I == BaseRegs.end();
503 }
504 
505 /// Helper method to morph a formula into its canonical representation.
506 /// \see Formula::BaseRegs.
507 /// Every formula having more than one base register, must use the ScaledReg
508 /// field. Otherwise, we would have to do special cases everywhere in LSR
509 /// to treat reg1 + reg2 + ... the same way as reg1 + 1*reg2 + ...
510 /// On the other hand, 1*reg should be canonicalized into reg.
511 void Formula::canonicalize(const Loop &L) {
512  if (isCanonical(L))
513  return;
514 
515  if (BaseRegs.empty()) {
516  // No base reg? Use scale reg with scale = 1 as such.
517  assert(ScaledReg && "Expected 1*reg => reg");
518  assert(Scale == 1 && "Expected 1*reg => reg");
519  BaseRegs.push_back(ScaledReg);
520  Scale = 0;
521  ScaledReg = nullptr;
522  return;
523  }
524 
525  // Keep the invariant sum in BaseRegs and one of the variant sum in ScaledReg.
526  if (!ScaledReg) {
527  ScaledReg = BaseRegs.pop_back_val();
528  Scale = 1;
529  }
530 
531  // If ScaledReg is an invariant with respect to L, find the reg from
532  // BaseRegs containing the recurrent expr related with Loop L. Swap the
533  // reg with ScaledReg.
534  const SCEVAddRecExpr *SAR = dyn_cast<const SCEVAddRecExpr>(ScaledReg);
535  if (!SAR || SAR->getLoop() != &L) {
536  auto I = find_if(BaseRegs, [&](const SCEV *S) {
537  return isa<const SCEVAddRecExpr>(S) &&
538  (cast<SCEVAddRecExpr>(S)->getLoop() == &L);
539  });
540  if (I != BaseRegs.end())
541  std::swap(ScaledReg, *I);
542  }
543  assert(isCanonical(L) && "Failed to canonicalize?");
544 }
545 
546 /// Get rid of the scale in the formula.
547 /// In other words, this method morphes reg1 + 1*reg2 into reg1 + reg2.
548 /// \return true if it was possible to get rid of the scale, false otherwise.
549 /// \note After this operation the formula may not be in the canonical form.
550 bool Formula::unscale() {
551  if (Scale != 1)
552  return false;
553  Scale = 0;
554  BaseRegs.push_back(ScaledReg);
555  ScaledReg = nullptr;
556  return true;
557 }
558 
559 bool Formula::hasZeroEnd() const {
560  if (UnfoldedOffset || BaseOffset)
561  return false;
562  if (BaseRegs.size() != 1 || ScaledReg)
563  return false;
564  return true;
565 }
566 
567 /// Return the total number of register operands used by this formula. This does
568 /// not include register uses implied by non-constant addrec strides.
569 size_t Formula::getNumRegs() const {
570  return !!ScaledReg + BaseRegs.size();
571 }
572 
573 /// Return the type of this formula, if it has one, or null otherwise. This type
574 /// is meaningless except for the bit size.
575 Type *Formula::getType() const {
576  return !BaseRegs.empty() ? BaseRegs.front()->getType() :
577  ScaledReg ? ScaledReg->getType() :
578  BaseGV ? BaseGV->getType() :
579  nullptr;
580 }
581 
582 /// Delete the given base reg from the BaseRegs list.
583 void Formula::deleteBaseReg(const SCEV *&S) {
584  if (&S != &BaseRegs.back())
585  std::swap(S, BaseRegs.back());
586  BaseRegs.pop_back();
587 }
588 
589 /// Test if this formula references the given register.
590 bool Formula::referencesReg(const SCEV *S) const {
591  return S == ScaledReg || is_contained(BaseRegs, S);
592 }
593 
594 /// Test whether this formula uses registers which are used by uses other than
595 /// the use with the given index.
596 bool Formula::hasRegsUsedByUsesOtherThan(size_t LUIdx,
597  const RegUseTracker &RegUses) const {
598  if (ScaledReg)
599  if (RegUses.isRegUsedByUsesOtherThan(ScaledReg, LUIdx))
600  return true;
601  for (const SCEV *BaseReg : BaseRegs)
602  if (RegUses.isRegUsedByUsesOtherThan(BaseReg, LUIdx))
603  return true;
604  return false;
605 }
606 
607 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
608 void Formula::print(raw_ostream &OS) const {
609  bool First = true;
610  if (BaseGV) {
611  if (!First) OS << " + "; else First = false;
612  BaseGV->printAsOperand(OS, /*PrintType=*/false);
613  }
614  if (BaseOffset != 0) {
615  if (!First) OS << " + "; else First = false;
616  OS << BaseOffset;
617  }
618  for (const SCEV *BaseReg : BaseRegs) {
619  if (!First) OS << " + "; else First = false;
620  OS << "reg(" << *BaseReg << ')';
621  }
622  if (HasBaseReg && BaseRegs.empty()) {
623  if (!First) OS << " + "; else First = false;
624  OS << "**error: HasBaseReg**";
625  } else if (!HasBaseReg && !BaseRegs.empty()) {
626  if (!First) OS << " + "; else First = false;
627  OS << "**error: !HasBaseReg**";
628  }
629  if (Scale != 0) {
630  if (!First) OS << " + "; else First = false;
631  OS << Scale << "*reg(";
632  if (ScaledReg)
633  OS << *ScaledReg;
634  else
635  OS << "<unknown>";
636  OS << ')';
637  }
638  if (UnfoldedOffset != 0) {
639  if (!First) OS << " + ";
640  OS << "imm(" << UnfoldedOffset << ')';
641  }
642 }
643 
644 LLVM_DUMP_METHOD void Formula::dump() const {
645  print(errs()); errs() << '\n';
646 }
647 #endif
648 
649 /// Return true if the given addrec can be sign-extended without changing its
650 /// value.
651 static bool isAddRecSExtable(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
652  Type *WideTy =
654  return isa<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
655 }
656 
657 /// Return true if the given add can be sign-extended without changing its
658 /// value.
659 static bool isAddSExtable(const SCEVAddExpr *A, ScalarEvolution &SE) {
660  Type *WideTy =
661  IntegerType::get(SE.getContext(), SE.getTypeSizeInBits(A->getType()) + 1);
662  return isa<SCEVAddExpr>(SE.getSignExtendExpr(A, WideTy));
663 }
664 
665 /// Return true if the given mul can be sign-extended without changing its
666 /// value.
667 static bool isMulSExtable(const SCEVMulExpr *M, ScalarEvolution &SE) {
668  Type *WideTy =
670  SE.getTypeSizeInBits(M->getType()) * M->getNumOperands());
671  return isa<SCEVMulExpr>(SE.getSignExtendExpr(M, WideTy));
672 }
673 
674 /// Return an expression for LHS /s RHS, if it can be determined and if the
675 /// remainder is known to be zero, or null otherwise. If IgnoreSignificantBits
676 /// is true, expressions like (X * Y) /s Y are simplified to X, ignoring that
677 /// the multiplication may overflow, which is useful when the result will be
678 /// used in a context where the most significant bits are ignored.
679 static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
680  ScalarEvolution &SE,
681  bool IgnoreSignificantBits = false) {
682  // Handle the trivial case, which works for any SCEV type.
683  if (LHS == RHS)
684  return SE.getConstant(LHS->getType(), 1);
685 
686  // Handle a few RHS special cases.
687  const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS);
688  if (RC) {
689  const APInt &RA = RC->getAPInt();
690  // Handle x /s -1 as x * -1, to give ScalarEvolution a chance to do
691  // some folding.
692  if (RA.isAllOnesValue()) {
693  if (LHS->getType()->isPointerTy())
694  return nullptr;
695  return SE.getMulExpr(LHS, RC);
696  }
697  // Handle x /s 1 as x.
698  if (RA == 1)
699  return LHS;
700  }
701 
702  // Check for a division of a constant by a constant.
703  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(LHS)) {
704  if (!RC)
705  return nullptr;
706  const APInt &LA = C->getAPInt();
707  const APInt &RA = RC->getAPInt();
708  if (LA.srem(RA) != 0)
709  return nullptr;
710  return SE.getConstant(LA.sdiv(RA));
711  }
712 
713  // Distribute the sdiv over addrec operands, if the addrec doesn't overflow.
714  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) {
715  if ((IgnoreSignificantBits || isAddRecSExtable(AR, SE)) && AR->isAffine()) {
716  const SCEV *Step = getExactSDiv(AR->getStepRecurrence(SE), RHS, SE,
717  IgnoreSignificantBits);
718  if (!Step) return nullptr;
719  const SCEV *Start = getExactSDiv(AR->getStart(), RHS, SE,
720  IgnoreSignificantBits);
721  if (!Start) return nullptr;
722  // FlagNW is independent of the start value, step direction, and is
723  // preserved with smaller magnitude steps.
724  // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
725  return SE.getAddRecExpr(Start, Step, AR->getLoop(), SCEV::FlagAnyWrap);
726  }
727  return nullptr;
728  }
729 
730  // Distribute the sdiv over add operands, if the add doesn't overflow.
731  if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(LHS)) {
732  if (IgnoreSignificantBits || isAddSExtable(Add, SE)) {
734  for (const SCEV *S : Add->operands()) {
735  const SCEV *Op = getExactSDiv(S, RHS, SE, IgnoreSignificantBits);
736  if (!Op) return nullptr;
737  Ops.push_back(Op);
738  }
739  return SE.getAddExpr(Ops);
740  }
741  return nullptr;
742  }
743 
744  // Check for a multiply operand that we can pull RHS out of.
745  if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS)) {
746  if (IgnoreSignificantBits || isMulSExtable(Mul, SE)) {
747  // Handle special case C1*X*Y /s C2*X*Y.
748  if (const SCEVMulExpr *MulRHS = dyn_cast<SCEVMulExpr>(RHS)) {
749  if (IgnoreSignificantBits || isMulSExtable(MulRHS, SE)) {
750  const SCEVConstant *LC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
751  const SCEVConstant *RC =
752  dyn_cast<SCEVConstant>(MulRHS->getOperand(0));
753  if (LC && RC) {
754  SmallVector<const SCEV *, 4> LOps(drop_begin(Mul->operands()));
755  SmallVector<const SCEV *, 4> ROps(drop_begin(MulRHS->operands()));
756  if (LOps == ROps)
757  return getExactSDiv(LC, RC, SE, IgnoreSignificantBits);
758  }
759  }
760  }
761 
763  bool Found = false;
764  for (const SCEV *S : Mul->operands()) {
765  if (!Found)
766  if (const SCEV *Q = getExactSDiv(S, RHS, SE,
767  IgnoreSignificantBits)) {
768  S = Q;
769  Found = true;
770  }
771  Ops.push_back(S);
772  }
773  return Found ? SE.getMulExpr(Ops) : nullptr;
774  }
775  return nullptr;
776  }
777 
778  // Otherwise we don't know.
779  return nullptr;
780 }
781 
782 /// If S involves the addition of a constant integer value, return that integer
783 /// value, and mutate S to point to a new SCEV with that value excluded.
784 static int64_t ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
785  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
786  if (C->getAPInt().getMinSignedBits() <= 64) {
787  S = SE.getConstant(C->getType(), 0);
788  return C->getValue()->getSExtValue();
789  }
790  } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
791  SmallVector<const SCEV *, 8> NewOps(Add->operands());
792  int64_t Result = ExtractImmediate(NewOps.front(), SE);
793  if (Result != 0)
794  S = SE.getAddExpr(NewOps);
795  return Result;
796  } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
797  SmallVector<const SCEV *, 8> NewOps(AR->operands());
798  int64_t Result = ExtractImmediate(NewOps.front(), SE);
799  if (Result != 0)
800  S = SE.getAddRecExpr(NewOps, AR->getLoop(),
801  // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
803  return Result;
804  }
805  return 0;
806 }
807 
808 /// If S involves the addition of a GlobalValue address, return that symbol, and
809 /// mutate S to point to a new SCEV with that value excluded.
811  if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
812  if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue())) {
813  S = SE.getConstant(GV->getType(), 0);
814  return GV;
815  }
816  } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
817  SmallVector<const SCEV *, 8> NewOps(Add->operands());
818  GlobalValue *Result = ExtractSymbol(NewOps.back(), SE);
819  if (Result)
820  S = SE.getAddExpr(NewOps);
821  return Result;
822  } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
823  SmallVector<const SCEV *, 8> NewOps(AR->operands());
824  GlobalValue *Result = ExtractSymbol(NewOps.front(), SE);
825  if (Result)
826  S = SE.getAddRecExpr(NewOps, AR->getLoop(),
827  // FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
829  return Result;
830  }
831  return nullptr;
832 }
833 
834 /// Returns true if the specified instruction is using the specified value as an
835 /// address.
837  Instruction *Inst, Value *OperandVal) {
838  bool isAddress = isa<LoadInst>(Inst);
839  if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
840  if (SI->getPointerOperand() == OperandVal)
841  isAddress = true;
842  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
843  // Addressing modes can also be folded into prefetches and a variety
844  // of intrinsics.
845  switch (II->getIntrinsicID()) {
846  case Intrinsic::memset:
847  case Intrinsic::prefetch:
848  case Intrinsic::masked_load:
849  if (II->getArgOperand(0) == OperandVal)
850  isAddress = true;
851  break;
852  case Intrinsic::masked_store:
853  if (II->getArgOperand(1) == OperandVal)
854  isAddress = true;
855  break;
856  case Intrinsic::memmove:
857  case Intrinsic::memcpy:
858  if (II->getArgOperand(0) == OperandVal ||
859  II->getArgOperand(1) == OperandVal)
860  isAddress = true;
861  break;
862  default: {
863  MemIntrinsicInfo IntrInfo;
864  if (TTI.getTgtMemIntrinsic(II, IntrInfo)) {
865  if (IntrInfo.PtrVal == OperandVal)
866  isAddress = true;
867  }
868  }
869  }
870  } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(Inst)) {
871  if (RMW->getPointerOperand() == OperandVal)
872  isAddress = true;
873  } else if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst)) {
874  if (CmpX->getPointerOperand() == OperandVal)
875  isAddress = true;
876  }
877  return isAddress;
878 }
879 
880 /// Return the type of the memory being accessed.
881 static MemAccessTy getAccessType(const TargetTransformInfo &TTI,
882  Instruction *Inst, Value *OperandVal) {
883  MemAccessTy AccessTy(Inst->getType(), MemAccessTy::UnknownAddressSpace);
884  if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
885  AccessTy.MemTy = SI->getOperand(0)->getType();
886  AccessTy.AddrSpace = SI->getPointerAddressSpace();
887  } else if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
888  AccessTy.AddrSpace = LI->getPointerAddressSpace();
889  } else if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(Inst)) {
890  AccessTy.AddrSpace = RMW->getPointerAddressSpace();
891  } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst)) {
892  AccessTy.AddrSpace = CmpX->getPointerAddressSpace();
893  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
894  switch (II->getIntrinsicID()) {
895  case Intrinsic::prefetch:
896  case Intrinsic::memset:
897  AccessTy.AddrSpace = II->getArgOperand(0)->getType()->getPointerAddressSpace();
898  AccessTy.MemTy = OperandVal->getType();
899  break;
900  case Intrinsic::memmove:
901  case Intrinsic::memcpy:
902  AccessTy.AddrSpace = OperandVal->getType()->getPointerAddressSpace();
903  AccessTy.MemTy = OperandVal->getType();
904  break;
905  case Intrinsic::masked_load:
906  AccessTy.AddrSpace =
907  II->getArgOperand(0)->getType()->getPointerAddressSpace();
908  break;
909  case Intrinsic::masked_store:
910  AccessTy.MemTy = II->getOperand(0)->getType();
911  AccessTy.AddrSpace =
912  II->getArgOperand(1)->getType()->getPointerAddressSpace();
913  break;
914  default: {
915  MemIntrinsicInfo IntrInfo;
916  if (TTI.getTgtMemIntrinsic(II, IntrInfo) && IntrInfo.PtrVal) {
917  AccessTy.AddrSpace
918  = IntrInfo.PtrVal->getType()->getPointerAddressSpace();
919  }
920 
921  break;
922  }
923  }
924  }
925 
926  // All pointers have the same requirements, so canonicalize them to an
927  // arbitrary pointer type to minimize variation.
928  if (PointerType *PTy = dyn_cast<PointerType>(AccessTy.MemTy))
929  AccessTy.MemTy = PointerType::get(IntegerType::get(PTy->getContext(), 1),
930  PTy->getAddressSpace());
931 
932  return AccessTy;
933 }
934 
935 /// Return true if this AddRec is already a phi in its loop.
936 static bool isExistingPhi(const SCEVAddRecExpr *AR, ScalarEvolution &SE) {
937  for (PHINode &PN : AR->getLoop()->getHeader()->phis()) {
938  if (SE.isSCEVable(PN.getType()) &&
939  (SE.getEffectiveSCEVType(PN.getType()) ==
940  SE.getEffectiveSCEVType(AR->getType())) &&
941  SE.getSCEV(&PN) == AR)
942  return true;
943  }
944  return false;
945 }
946 
947 /// Check if expanding this expression is likely to incur significant cost. This
948 /// is tricky because SCEV doesn't track which expressions are actually computed
949 /// by the current IR.
950 ///
951 /// We currently allow expansion of IV increments that involve adds,
952 /// multiplication by constants, and AddRecs from existing phis.
953 ///
954 /// TODO: Allow UDivExpr if we can find an existing IV increment that is an
955 /// obvious multiple of the UDivExpr.
956 static bool isHighCostExpansion(const SCEV *S,
957  SmallPtrSetImpl<const SCEV*> &Processed,
958  ScalarEvolution &SE) {
959  // Zero/One operand expressions
960  switch (S->getSCEVType()) {
961  case scUnknown:
962  case scConstant:
963  return false;
964  case scTruncate:
965  return isHighCostExpansion(cast<SCEVTruncateExpr>(S)->getOperand(),
966  Processed, SE);
967  case scZeroExtend:
968  return isHighCostExpansion(cast<SCEVZeroExtendExpr>(S)->getOperand(),
969  Processed, SE);
970  case scSignExtend:
971  return isHighCostExpansion(cast<SCEVSignExtendExpr>(S)->getOperand(),
972  Processed, SE);
973  default:
974  break;
975  }
976 
977  if (!Processed.insert(S).second)
978  return false;
979 
980  if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
981  for (const SCEV *S : Add->operands()) {
982  if (isHighCostExpansion(S, Processed, SE))
983  return true;
984  }
985  return false;
986  }
987 
988  if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
989  if (Mul->getNumOperands() == 2) {
990  // Multiplication by a constant is ok
991  if (isa<SCEVConstant>(Mul->getOperand(0)))
992  return isHighCostExpansion(Mul->getOperand(1), Processed, SE);
993 
994  // If we have the value of one operand, check if an existing
995  // multiplication already generates this expression.
996  if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Mul->getOperand(1))) {
997  Value *UVal = U->getValue();
998  for (User *UR : UVal->users()) {
999  // If U is a constant, it may be used by a ConstantExpr.
1000  Instruction *UI = dyn_cast<Instruction>(UR);
1001  if (UI && UI->getOpcode() == Instruction::Mul &&
1002  SE.isSCEVable(UI->getType())) {
1003  return SE.getSCEV(UI) == Mul;
1004  }
1005  }
1006  }
1007  }
1008  }
1009 
1010  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
1011  if (isExistingPhi(AR, SE))
1012  return false;
1013  }
1014 
1015  // Fow now, consider any other type of expression (div/mul/min/max) high cost.
1016  return true;
1017 }
1018 
1019 namespace {
1020 
1021 class LSRUse;
1022 
1023 } // end anonymous namespace
1024 
1025 /// Check if the addressing mode defined by \p F is completely
1026 /// folded in \p LU at isel time.
1027 /// This includes address-mode folding and special icmp tricks.
1028 /// This function returns true if \p LU can accommodate what \p F
1029 /// defines and up to 1 base + 1 scaled + offset.
1030 /// In other words, if \p F has several base registers, this function may
1031 /// still return true. Therefore, users still need to account for
1032 /// additional base registers and/or unfolded offsets to derive an
1033 /// accurate cost model.
1034 static bool isAMCompletelyFolded(const TargetTransformInfo &TTI,
1035  const LSRUse &LU, const Formula &F);
1036 
1037 // Get the cost of the scaling factor used in F for LU.
1039  const LSRUse &LU, const Formula &F,
1040  const Loop &L);
1041 
1042 namespace {
1043 
1044 /// This class is used to measure and compare candidate formulae.
1045 class Cost {
1046  const Loop *L = nullptr;
1047  ScalarEvolution *SE = nullptr;
1048  const TargetTransformInfo *TTI = nullptr;
1051 
1052 public:
1053  Cost() = delete;
1054  Cost(const Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI,
1056  L(L), SE(&SE), TTI(&TTI), AMK(AMK) {
1057  C.Insns = 0;
1058  C.NumRegs = 0;
1059  C.AddRecCost = 0;
1060  C.NumIVMuls = 0;
1061  C.NumBaseAdds = 0;
1062  C.ImmCost = 0;
1063  C.SetupCost = 0;
1064  C.ScaleCost = 0;
1065  }
1066 
1067  bool isLess(Cost &Other);
1068 
1069  void Lose();
1070 
1071 #ifndef NDEBUG
1072  // Once any of the metrics loses, they must all remain losers.
1073  bool isValid() {
1074  return ((C.Insns | C.NumRegs | C.AddRecCost | C.NumIVMuls | C.NumBaseAdds
1075  | C.ImmCost | C.SetupCost | C.ScaleCost) != ~0u)
1076  || ((C.Insns & C.NumRegs & C.AddRecCost & C.NumIVMuls & C.NumBaseAdds
1077  & C.ImmCost & C.SetupCost & C.ScaleCost) == ~0u);
1078  }
1079 #endif
1080 
1081  bool isLoser() {
1082  assert(isValid() && "invalid cost");
1083  return C.NumRegs == ~0u;
1084  }
1085 
1086  void RateFormula(const Formula &F,
1088  const DenseSet<const SCEV *> &VisitedRegs,
1089  const LSRUse &LU,
1090  SmallPtrSetImpl<const SCEV *> *LoserRegs = nullptr);
1091 
1092  void print(raw_ostream &OS) const;
1093  void dump() const;
1094 
1095 private:
1096  void RateRegister(const Formula &F, const SCEV *Reg,
1098  void RatePrimaryRegister(const Formula &F, const SCEV *Reg,
1100  SmallPtrSetImpl<const SCEV *> *LoserRegs);
1101 };
1102 
1103 /// An operand value in an instruction which is to be replaced with some
1104 /// equivalent, possibly strength-reduced, replacement.
1105 struct LSRFixup {
1106  /// The instruction which will be updated.
1107  Instruction *UserInst = nullptr;
1108 
1109  /// The operand of the instruction which will be replaced. The operand may be
1110  /// used more than once; every instance will be replaced.
1111  Value *OperandValToReplace = nullptr;
1112 
1113  /// If this user is to use the post-incremented value of an induction
1114  /// variable, this set is non-empty and holds the loops associated with the
1115  /// induction variable.
1116  PostIncLoopSet PostIncLoops;
1117 
1118  /// A constant offset to be added to the LSRUse expression. This allows
1119  /// multiple fixups to share the same LSRUse with different offsets, for
1120  /// example in an unrolled loop.
1121  int64_t Offset = 0;
1122 
1123  LSRFixup() = default;
1124 
1125  bool isUseFullyOutsideLoop(const Loop *L) const;
1126 
1127  void print(raw_ostream &OS) const;
1128  void dump() const;
1129 };
1130 
1131 /// A DenseMapInfo implementation for holding DenseMaps and DenseSets of sorted
1132 /// SmallVectors of const SCEV*.
1133 struct UniquifierDenseMapInfo {
1134  static SmallVector<const SCEV *, 4> getEmptyKey() {
1136  V.push_back(reinterpret_cast<const SCEV *>(-1));
1137  return V;
1138  }
1139 
1140  static SmallVector<const SCEV *, 4> getTombstoneKey() {
1142  V.push_back(reinterpret_cast<const SCEV *>(-2));
1143  return V;
1144  }
1145 
1146  static unsigned getHashValue(const SmallVector<const SCEV *, 4> &V) {
1147  return static_cast<unsigned>(hash_combine_range(V.begin(), V.end()));
1148  }
1149 
1150  static bool isEqual(const SmallVector<const SCEV *, 4> &LHS,
1151  const SmallVector<const SCEV *, 4> &RHS) {
1152  return LHS == RHS;
1153  }
1154 };
1155 
1156 /// This class holds the state that LSR keeps for each use in IVUsers, as well
1157 /// as uses invented by LSR itself. It includes information about what kinds of
1158 /// things can be folded into the user, information about the user itself, and
1159 /// information about how the use may be satisfied. TODO: Represent multiple
1160 /// users of the same expression in common?
1161 class LSRUse {
1162  DenseSet<SmallVector<const SCEV *, 4>, UniquifierDenseMapInfo> Uniquifier;
1163 
1164 public:
1165  /// An enum for a kind of use, indicating what types of scaled and immediate
1166  /// operands it might support.
1167  enum KindType {
1168  Basic, ///< A normal use, with no folding.
1169  Special, ///< A special case of basic, allowing -1 scales.
1170  Address, ///< An address use; folding according to TargetLowering
1171  ICmpZero ///< An equality icmp with both operands folded into one.
1172  // TODO: Add a generic icmp too?
1173  };
1174 
1175  using SCEVUseKindPair = PointerIntPair<const SCEV *, 2, KindType>;
1176 
1177  KindType Kind;
1178  MemAccessTy AccessTy;
1179 
1180  /// The list of operands which are to be replaced.
1182 
1183  /// Keep track of the min and max offsets of the fixups.
1184  int64_t MinOffset = std::numeric_limits<int64_t>::max();
1185  int64_t MaxOffset = std::numeric_limits<int64_t>::min();
1186 
1187  /// This records whether all of the fixups using this LSRUse are outside of
1188  /// the loop, in which case some special-case heuristics may be used.
1189  bool AllFixupsOutsideLoop = true;
1190 
1191  /// RigidFormula is set to true to guarantee that this use will be associated
1192  /// with a single formula--the one that initially matched. Some SCEV
1193  /// expressions cannot be expanded. This allows LSR to consider the registers
1194  /// used by those expressions without the need to expand them later after
1195  /// changing the formula.
1196  bool RigidFormula = false;
1197 
1198  /// This records the widest use type for any fixup using this
1199  /// LSRUse. FindUseWithSimilarFormula can't consider uses with different max
1200  /// fixup widths to be equivalent, because the narrower one may be relying on
1201  /// the implicit truncation to truncate away bogus bits.
1202  Type *WidestFixupType = nullptr;
1203 
1204  /// A list of ways to build a value that can satisfy this user. After the
1205  /// list is populated, one of these is selected heuristically and used to
1206  /// formulate a replacement for OperandValToReplace in UserInst.
1207  SmallVector<Formula, 12> Formulae;
1208 
1209  /// The set of register candidates used by all formulae in this LSRUse.
1211 
1212  LSRUse(KindType K, MemAccessTy AT) : Kind(K), AccessTy(AT) {}
1213 
1214  LSRFixup &getNewFixup() {
1215  Fixups.push_back(LSRFixup());
1216  return Fixups.back();
1217  }
1218 
1219  void pushFixup(LSRFixup &f) {
1220  Fixups.push_back(f);
1221  if (f.Offset > MaxOffset)
1222  MaxOffset = f.Offset;
1223  if (f.Offset < MinOffset)
1224  MinOffset = f.Offset;
1225  }
1226 
1227  bool HasFormulaWithSameRegs(const Formula &F) const;
1228  float getNotSelectedProbability(const SCEV *Reg) const;
1229  bool InsertFormula(const Formula &F, const Loop &L);
1230  void DeleteFormula(Formula &F);
1231  void RecomputeRegs(size_t LUIdx, RegUseTracker &Reguses);
1232 
1233  void print(raw_ostream &OS) const;
1234  void dump() const;
1235 };
1236 
1237 } // end anonymous namespace
1238 
1239 static bool isAMCompletelyFolded(const TargetTransformInfo &TTI,
1240  LSRUse::KindType Kind, MemAccessTy AccessTy,
1241  GlobalValue *BaseGV, int64_t BaseOffset,
1242  bool HasBaseReg, int64_t Scale,
1243  Instruction *Fixup = nullptr);
1244 
1245 static unsigned getSetupCost(const SCEV *Reg, unsigned Depth) {
1246  if (isa<SCEVUnknown>(Reg) || isa<SCEVConstant>(Reg))
1247  return 1;
1248  if (Depth == 0)
1249  return 0;
1250  if (const auto *S = dyn_cast<SCEVAddRecExpr>(Reg))
1251  return getSetupCost(S->getStart(), Depth - 1);
1252  if (auto S = dyn_cast<SCEVIntegralCastExpr>(Reg))
1253  return getSetupCost(S->getOperand(), Depth - 1);
1254  if (auto S = dyn_cast<SCEVNAryExpr>(Reg))
1255  return std::accumulate(S->op_begin(), S->op_end(), 0,
1256  [&](unsigned i, const SCEV *Reg) {
1257  return i + getSetupCost(Reg, Depth - 1);
1258  });
1259  if (auto S = dyn_cast<SCEVUDivExpr>(Reg))
1260  return getSetupCost(S->getLHS(), Depth - 1) +
1261  getSetupCost(S->getRHS(), Depth - 1);
1262  return 0;
1263 }
1264 
1265 /// Tally up interesting quantities from the given register.
1266 void Cost::RateRegister(const Formula &F, const SCEV *Reg,
1268  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Reg)) {
1269  // If this is an addrec for another loop, it should be an invariant
1270  // with respect to L since L is the innermost loop (at least
1271  // for now LSR only handles innermost loops).
1272  if (AR->getLoop() != L) {
1273  // If the AddRec exists, consider it's register free and leave it alone.
1274  if (isExistingPhi(AR, *SE) && AMK != TTI::AMK_PostIndexed)
1275  return;
1276 
1277  // It is bad to allow LSR for current loop to add induction variables
1278  // for its sibling loops.
1279  if (!AR->getLoop()->contains(L)) {
1280  Lose();
1281  return;
1282  }
1283 
1284  // Otherwise, it will be an invariant with respect to Loop L.
1285  ++C.NumRegs;
1286  return;
1287  }
1288 
1289  unsigned LoopCost = 1;
1290  if (TTI->isIndexedLoadLegal(TTI->MIM_PostInc, AR->getType()) ||
1291  TTI->isIndexedStoreLegal(TTI->MIM_PostInc, AR->getType())) {
1292 
1293  // If the step size matches the base offset, we could use pre-indexed
1294  // addressing.
1295  if (AMK == TTI::AMK_PreIndexed) {
1296  if (auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*SE)))
1297  if (Step->getAPInt() == F.BaseOffset)
1298  LoopCost = 0;
1299  } else if (AMK == TTI::AMK_PostIndexed) {
1300  const SCEV *LoopStep = AR->getStepRecurrence(*SE);
1301  if (isa<SCEVConstant>(LoopStep)) {
1302  const SCEV *LoopStart = AR->getStart();
1303  if (!isa<SCEVConstant>(LoopStart) &&
1304  SE->isLoopInvariant(LoopStart, L))
1305  LoopCost = 0;
1306  }
1307  }
1308  }
1309  C.AddRecCost += LoopCost;
1310 
1311  // Add the step value register, if it needs one.
1312  // TODO: The non-affine case isn't precisely modeled here.
1313  if (!AR->isAffine() || !isa<SCEVConstant>(AR->getOperand(1))) {
1314  if (!Regs.count(AR->getOperand(1))) {
1315  RateRegister(F, AR->getOperand(1), Regs);
1316  if (isLoser())
1317  return;
1318  }
1319  }
1320  }
1321  ++C.NumRegs;
1322 
1323  // Rough heuristic; favor registers which don't require extra setup
1324  // instructions in the preheader.
1325  C.SetupCost += getSetupCost(Reg, SetupCostDepthLimit);
1326  // Ensure we don't, even with the recusion limit, produce invalid costs.
1327  C.SetupCost = std::min<unsigned>(C.SetupCost, 1 << 16);
1328 
1329  C.NumIVMuls += isa<SCEVMulExpr>(Reg) &&
1331 }
1332 
1333 /// Record this register in the set. If we haven't seen it before, rate
1334 /// it. Optional LoserRegs provides a way to declare any formula that refers to
1335 /// one of those regs an instant loser.
1336 void Cost::RatePrimaryRegister(const Formula &F, const SCEV *Reg,
1338  SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1339  if (LoserRegs && LoserRegs->count(Reg)) {
1340  Lose();
1341  return;
1342  }
1343  if (Regs.insert(Reg).second) {
1344  RateRegister(F, Reg, Regs);
1345  if (LoserRegs && isLoser())
1346  LoserRegs->insert(Reg);
1347  }
1348 }
1349 
1350 void Cost::RateFormula(const Formula &F,
1352  const DenseSet<const SCEV *> &VisitedRegs,
1353  const LSRUse &LU,
1354  SmallPtrSetImpl<const SCEV *> *LoserRegs) {
1355  assert(F.isCanonical(*L) && "Cost is accurate only for canonical formula");
1356  // Tally up the registers.
1357  unsigned PrevAddRecCost = C.AddRecCost;
1358  unsigned PrevNumRegs = C.NumRegs;
1359  unsigned PrevNumBaseAdds = C.NumBaseAdds;
1360  if (const SCEV *ScaledReg = F.ScaledReg) {
1361  if (VisitedRegs.count(ScaledReg)) {
1362  Lose();
1363  return;
1364  }
1365  RatePrimaryRegister(F, ScaledReg, Regs, LoserRegs);
1366  if (isLoser())
1367  return;
1368  }
1369  for (const SCEV *BaseReg : F.BaseRegs) {
1370  if (VisitedRegs.count(BaseReg)) {
1371  Lose();
1372  return;
1373  }
1374  RatePrimaryRegister(F, BaseReg, Regs, LoserRegs);
1375  if (isLoser())
1376  return;
1377  }
1378 
1379  // Determine how many (unfolded) adds we'll need inside the loop.
1380  size_t NumBaseParts = F.getNumRegs();
1381  if (NumBaseParts > 1)
1382  // Do not count the base and a possible second register if the target
1383  // allows to fold 2 registers.
1384  C.NumBaseAdds +=
1385  NumBaseParts - (1 + (F.Scale && isAMCompletelyFolded(*TTI, LU, F)));
1386  C.NumBaseAdds += (F.UnfoldedOffset != 0);
1387 
1388  // Accumulate non-free scaling amounts.
1389  C.ScaleCost += *getScalingFactorCost(*TTI, LU, F, *L).getValue();
1390 
1391  // Tally up the non-zero immediates.
1392  for (const LSRFixup &Fixup : LU.Fixups) {
1393  int64_t O = Fixup.Offset;
1394  int64_t Offset = (uint64_t)O + F.BaseOffset;
1395  if (F.BaseGV)
1396  C.ImmCost += 64; // Handle symbolic values conservatively.
1397  // TODO: This should probably be the pointer size.
1398  else if (Offset != 0)
1399  C.ImmCost += APInt(64, Offset, true).getMinSignedBits();
1400 
1401  // Check with target if this offset with this instruction is
1402  // specifically not supported.
1403  if (LU.Kind == LSRUse::Address && Offset != 0 &&
1404  !isAMCompletelyFolded(*TTI, LSRUse::Address, LU.AccessTy, F.BaseGV,
1405  Offset, F.HasBaseReg, F.Scale, Fixup.UserInst))
1406  C.NumBaseAdds++;
1407  }
1408 
1409  // If we don't count instruction cost exit here.
1410  if (!InsnsCost) {
1411  assert(isValid() && "invalid cost");
1412  return;
1413  }
1414 
1415  // Treat every new register that exceeds TTI.getNumberOfRegisters() - 1 as
1416  // additional instruction (at least fill).
1417  // TODO: Need distinguish register class?
1418  unsigned TTIRegNum = TTI->getNumberOfRegisters(
1419  TTI->getRegisterClassForType(false, F.getType())) - 1;
1420  if (C.NumRegs > TTIRegNum) {
1421  // Cost already exceeded TTIRegNum, then only newly added register can add
1422  // new instructions.
1423  if (PrevNumRegs > TTIRegNum)
1424  C.Insns += (C.NumRegs - PrevNumRegs);
1425  else
1426  C.Insns += (C.NumRegs - TTIRegNum);
1427  }
1428 
1429  // If ICmpZero formula ends with not 0, it could not be replaced by
1430  // just add or sub. We'll need to compare final result of AddRec.
1431  // That means we'll need an additional instruction. But if the target can
1432  // macro-fuse a compare with a branch, don't count this extra instruction.
1433  // For -10 + {0, +, 1}:
1434  // i = i + 1;
1435  // cmp i, 10
1436  //
1437  // For {-10, +, 1}:
1438  // i = i + 1;
1439  if (LU.Kind == LSRUse::ICmpZero && !F.hasZeroEnd() &&
1440  !TTI->canMacroFuseCmp())
1441  C.Insns++;
1442  // Each new AddRec adds 1 instruction to calculation.
1443  C.Insns += (C.AddRecCost - PrevAddRecCost);
1444 
1445  // BaseAdds adds instructions for unfolded registers.
1446  if (LU.Kind != LSRUse::ICmpZero)
1447  C.Insns += C.NumBaseAdds - PrevNumBaseAdds;
1448  assert(isValid() && "invalid cost");
1449 }
1450 
1451 /// Set this cost to a losing value.
1452 void Cost::Lose() {
1455  C.AddRecCost = std::numeric_limits<unsigned>::max();
1456  C.NumIVMuls = std::numeric_limits<unsigned>::max();
1457  C.NumBaseAdds = std::numeric_limits<unsigned>::max();
1459  C.SetupCost = std::numeric_limits<unsigned>::max();
1460  C.ScaleCost = std::numeric_limits<unsigned>::max();
1461 }
1462 
1463 /// Choose the lower cost.
1464 bool Cost::isLess(Cost &Other) {
1465  if (InsnsCost.getNumOccurrences() > 0 && InsnsCost &&
1466  C.Insns != Other.C.Insns)
1467  return C.Insns < Other.C.Insns;
1468  return TTI->isLSRCostLess(C, Other.C);
1469 }
1470 
1471 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1472 void Cost::print(raw_ostream &OS) const {
1473  if (InsnsCost)
1474  OS << C.Insns << " instruction" << (C.Insns == 1 ? " " : "s ");
1475  OS << C.NumRegs << " reg" << (C.NumRegs == 1 ? "" : "s");
1476  if (C.AddRecCost != 0)
1477  OS << ", with addrec cost " << C.AddRecCost;
1478  if (C.NumIVMuls != 0)
1479  OS << ", plus " << C.NumIVMuls << " IV mul"
1480  << (C.NumIVMuls == 1 ? "" : "s");
1481  if (C.NumBaseAdds != 0)
1482  OS << ", plus " << C.NumBaseAdds << " base add"
1483  << (C.NumBaseAdds == 1 ? "" : "s");
1484  if (C.ScaleCost != 0)
1485  OS << ", plus " << C.ScaleCost << " scale cost";
1486  if (C.ImmCost != 0)
1487  OS << ", plus " << C.ImmCost << " imm cost";
1488  if (C.SetupCost != 0)
1489  OS << ", plus " << C.SetupCost << " setup cost";
1490 }
1491 
1492 LLVM_DUMP_METHOD void Cost::dump() const {
1493  print(errs()); errs() << '\n';
1494 }
1495 #endif
1496 
1497 /// Test whether this fixup always uses its value outside of the given loop.
1498 bool LSRFixup::isUseFullyOutsideLoop(const Loop *L) const {
1499  // PHI nodes use their value in their incoming blocks.
1500  if (const PHINode *PN = dyn_cast<PHINode>(UserInst)) {
1501  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1502  if (PN->getIncomingValue(i) == OperandValToReplace &&
1503  L->contains(PN->getIncomingBlock(i)))
1504  return false;
1505  return true;
1506  }
1507 
1508  return !L->contains(UserInst);
1509 }
1510 
1511 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1512 void LSRFixup::print(raw_ostream &OS) const {
1513  OS << "UserInst=";
1514  // Store is common and interesting enough to be worth special-casing.
1515  if (StoreInst *Store = dyn_cast<StoreInst>(UserInst)) {
1516  OS << "store ";
1517  Store->getOperand(0)->printAsOperand(OS, /*PrintType=*/false);
1518  } else if (UserInst->getType()->isVoidTy())
1519  OS << UserInst->getOpcodeName();
1520  else
1521  UserInst->printAsOperand(OS, /*PrintType=*/false);
1522 
1523  OS << ", OperandValToReplace=";
1524  OperandValToReplace->printAsOperand(OS, /*PrintType=*/false);
1525 
1526  for (const Loop *PIL : PostIncLoops) {
1527  OS << ", PostIncLoop=";
1528  PIL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
1529  }
1530 
1531  if (Offset != 0)
1532  OS << ", Offset=" << Offset;
1533 }
1534 
1535 LLVM_DUMP_METHOD void LSRFixup::dump() const {
1536  print(errs()); errs() << '\n';
1537 }
1538 #endif
1539 
1540 /// Test whether this use as a formula which has the same registers as the given
1541 /// formula.
1542 bool LSRUse::HasFormulaWithSameRegs(const Formula &F) const {
1543  SmallVector<const SCEV *, 4> Key = F.BaseRegs;
1544  if (F.ScaledReg) Key.push_back(F.ScaledReg);
1545  // Unstable sort by host order ok, because this is only used for uniquifying.
1546  llvm::sort(Key);
1547  return Uniquifier.count(Key);
1548 }
1549 
1550 /// The function returns a probability of selecting formula without Reg.
1551 float LSRUse::getNotSelectedProbability(const SCEV *Reg) const {
1552  unsigned FNum = 0;
1553  for (const Formula &F : Formulae)
1554  if (F.referencesReg(Reg))
1555  FNum++;
1556  return ((float)(Formulae.size() - FNum)) / Formulae.size();
1557 }
1558 
1559 /// If the given formula has not yet been inserted, add it to the list, and
1560 /// return true. Return false otherwise. The formula must be in canonical form.
1561 bool LSRUse::InsertFormula(const Formula &F, const Loop &L) {
1562  assert(F.isCanonical(L) && "Invalid canonical representation");
1563 
1564  if (!Formulae.empty() && RigidFormula)
1565  return false;
1566 
1567  SmallVector<const SCEV *, 4> Key = F.BaseRegs;
1568  if (F.ScaledReg) Key.push_back(F.ScaledReg);
1569  // Unstable sort by host order ok, because this is only used for uniquifying.
1570  llvm::sort(Key);
1571 
1572  if (!Uniquifier.insert(Key).second)
1573  return false;
1574 
1575  // Using a register to hold the value of 0 is not profitable.
1576  assert((!F.ScaledReg || !F.ScaledReg->isZero()) &&
1577  "Zero allocated in a scaled register!");
1578 #ifndef NDEBUG
1579  for (const SCEV *BaseReg : F.BaseRegs)
1580  assert(!BaseReg->isZero() && "Zero allocated in a base register!");
1581 #endif
1582 
1583  // Add the formula to the list.
1584  Formulae.push_back(F);
1585 
1586  // Record registers now being used by this use.
1587  Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1588  if (F.ScaledReg)
1589  Regs.insert(F.ScaledReg);
1590 
1591  return true;
1592 }
1593 
1594 /// Remove the given formula from this use's list.
1595 void LSRUse::DeleteFormula(Formula &F) {
1596  if (&F != &Formulae.back())
1597  std::swap(F, Formulae.back());
1598  Formulae.pop_back();
1599 }
1600 
1601 /// Recompute the Regs field, and update RegUses.
1602 void LSRUse::RecomputeRegs(size_t LUIdx, RegUseTracker &RegUses) {
1603  // Now that we've filtered out some formulae, recompute the Regs set.
1604  SmallPtrSet<const SCEV *, 4> OldRegs = std::move(Regs);
1605  Regs.clear();
1606  for (const Formula &F : Formulae) {
1607  if (F.ScaledReg) Regs.insert(F.ScaledReg);
1608  Regs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
1609  }
1610 
1611  // Update the RegTracker.
1612  for (const SCEV *S : OldRegs)
1613  if (!Regs.count(S))
1614  RegUses.dropRegister(S, LUIdx);
1615 }
1616 
1617 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1618 void LSRUse::print(raw_ostream &OS) const {
1619  OS << "LSR Use: Kind=";
1620  switch (Kind) {
1621  case Basic: OS << "Basic"; break;
1622  case Special: OS << "Special"; break;
1623  case ICmpZero: OS << "ICmpZero"; break;
1624  case Address:
1625  OS << "Address of ";
1626  if (AccessTy.MemTy->isPointerTy())
1627  OS << "pointer"; // the full pointer type could be really verbose
1628  else {
1629  OS << *AccessTy.MemTy;
1630  }
1631 
1632  OS << " in addrspace(" << AccessTy.AddrSpace << ')';
1633  }
1634 
1635  OS << ", Offsets={";
1636  bool NeedComma = false;
1637  for (const LSRFixup &Fixup : Fixups) {
1638  if (NeedComma) OS << ',';
1639  OS << Fixup.Offset;
1640  NeedComma = true;
1641  }
1642  OS << '}';
1643 
1644  if (AllFixupsOutsideLoop)
1645  OS << ", all-fixups-outside-loop";
1646 
1647  if (WidestFixupType)
1648  OS << ", widest fixup type: " << *WidestFixupType;
1649 }
1650 
1651 LLVM_DUMP_METHOD void LSRUse::dump() const {
1652  print(errs()); errs() << '\n';
1653 }
1654 #endif
1655 
1657  LSRUse::KindType Kind, MemAccessTy AccessTy,
1658  GlobalValue *BaseGV, int64_t BaseOffset,
1659  bool HasBaseReg, int64_t Scale,
1660  Instruction *Fixup/*= nullptr*/) {
1661  switch (Kind) {
1662  case LSRUse::Address:
1663  return TTI.isLegalAddressingMode(AccessTy.MemTy, BaseGV, BaseOffset,
1664  HasBaseReg, Scale, AccessTy.AddrSpace, Fixup);
1665 
1666  case LSRUse::ICmpZero:
1667  // There's not even a target hook for querying whether it would be legal to
1668  // fold a GV into an ICmp.
1669  if (BaseGV)
1670  return false;
1671 
1672  // ICmp only has two operands; don't allow more than two non-trivial parts.
1673  if (Scale != 0 && HasBaseReg && BaseOffset != 0)
1674  return false;
1675 
1676  // ICmp only supports no scale or a -1 scale, as we can "fold" a -1 scale by
1677  // putting the scaled register in the other operand of the icmp.
1678  if (Scale != 0 && Scale != -1)
1679  return false;
1680 
1681  // If we have low-level target information, ask the target if it can fold an
1682  // integer immediate on an icmp.
1683  if (BaseOffset != 0) {
1684  // We have one of:
1685  // ICmpZero BaseReg + BaseOffset => ICmp BaseReg, -BaseOffset
1686  // ICmpZero -1*ScaleReg + BaseOffset => ICmp ScaleReg, BaseOffset
1687  // Offs is the ICmp immediate.
1688  if (Scale == 0)
1689  // The cast does the right thing with
1690  // std::numeric_limits<int64_t>::min().
1691  BaseOffset = -(uint64_t)BaseOffset;
1692  return TTI.isLegalICmpImmediate(BaseOffset);
1693  }
1694 
1695  // ICmpZero BaseReg + -1*ScaleReg => ICmp BaseReg, ScaleReg
1696  return true;
1697 
1698  case LSRUse::Basic:
1699  // Only handle single-register values.
1700  return !BaseGV && Scale == 0 && BaseOffset == 0;
1701 
1702  case LSRUse::Special:
1703  // Special case Basic to handle -1 scales.
1704  return !BaseGV && (Scale == 0 || Scale == -1) && BaseOffset == 0;
1705  }
1706 
1707  llvm_unreachable("Invalid LSRUse Kind!");
1708 }
1709 
1711  int64_t MinOffset, int64_t MaxOffset,
1712  LSRUse::KindType Kind, MemAccessTy AccessTy,
1713  GlobalValue *BaseGV, int64_t BaseOffset,
1714  bool HasBaseReg, int64_t Scale) {
1715  // Check for overflow.
1716  if (((int64_t)((uint64_t)BaseOffset + MinOffset) > BaseOffset) !=
1717  (MinOffset > 0))
1718  return false;
1719  MinOffset = (uint64_t)BaseOffset + MinOffset;
1720  if (((int64_t)((uint64_t)BaseOffset + MaxOffset) > BaseOffset) !=
1721  (MaxOffset > 0))
1722  return false;
1723  MaxOffset = (uint64_t)BaseOffset + MaxOffset;
1724 
1725  return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MinOffset,
1726  HasBaseReg, Scale) &&
1727  isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, MaxOffset,
1728  HasBaseReg, Scale);
1729 }
1730 
1732  int64_t MinOffset, int64_t MaxOffset,
1733  LSRUse::KindType Kind, MemAccessTy AccessTy,
1734  const Formula &F, const Loop &L) {
1735  // For the purpose of isAMCompletelyFolded either having a canonical formula
1736  // or a scale not equal to zero is correct.
1737  // Problems may arise from non canonical formulae having a scale == 0.
1738  // Strictly speaking it would best to just rely on canonical formulae.
1739  // However, when we generate the scaled formulae, we first check that the
1740  // scaling factor is profitable before computing the actual ScaledReg for
1741  // compile time sake.
1742  assert((F.isCanonical(L) || F.Scale != 0));
1743  return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy,
1744  F.BaseGV, F.BaseOffset, F.HasBaseReg, F.Scale);
1745 }
1746 
1747 /// Test whether we know how to expand the current formula.
1748 static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1749  int64_t MaxOffset, LSRUse::KindType Kind,
1750  MemAccessTy AccessTy, GlobalValue *BaseGV,
1751  int64_t BaseOffset, bool HasBaseReg, int64_t Scale) {
1752  // We know how to expand completely foldable formulae.
1753  return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
1754  BaseOffset, HasBaseReg, Scale) ||
1755  // Or formulae that use a base register produced by a sum of base
1756  // registers.
1757  (Scale == 1 &&
1758  isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy,
1759  BaseGV, BaseOffset, true, 0));
1760 }
1761 
1762 static bool isLegalUse(const TargetTransformInfo &TTI, int64_t MinOffset,
1763  int64_t MaxOffset, LSRUse::KindType Kind,
1764  MemAccessTy AccessTy, const Formula &F) {
1765  return isLegalUse(TTI, MinOffset, MaxOffset, Kind, AccessTy, F.BaseGV,
1766  F.BaseOffset, F.HasBaseReg, F.Scale);
1767 }
1768 
1770  const LSRUse &LU, const Formula &F) {
1771  // Target may want to look at the user instructions.
1772  if (LU.Kind == LSRUse::Address && TTI.LSRWithInstrQueries()) {
1773  for (const LSRFixup &Fixup : LU.Fixups)
1774  if (!isAMCompletelyFolded(TTI, LSRUse::Address, LU.AccessTy, F.BaseGV,
1775  (F.BaseOffset + Fixup.Offset), F.HasBaseReg,
1776  F.Scale, Fixup.UserInst))
1777  return false;
1778  return true;
1779  }
1780 
1781  return isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind,
1782  LU.AccessTy, F.BaseGV, F.BaseOffset, F.HasBaseReg,
1783  F.Scale);
1784 }
1785 
1787  const LSRUse &LU, const Formula &F,
1788  const Loop &L) {
1789  if (!F.Scale)
1790  return 0;
1791 
1792  // If the use is not completely folded in that instruction, we will have to
1793  // pay an extra cost only for scale != 1.
1794  if (!isAMCompletelyFolded(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind,
1795  LU.AccessTy, F, L))
1796  return F.Scale != 1;
1797 
1798  switch (LU.Kind) {
1799  case LSRUse::Address: {
1800  // Check the scaling factor cost with both the min and max offsets.
1801  InstructionCost ScaleCostMinOffset = TTI.getScalingFactorCost(
1802  LU.AccessTy.MemTy, F.BaseGV, F.BaseOffset + LU.MinOffset, F.HasBaseReg,
1803  F.Scale, LU.AccessTy.AddrSpace);
1804  InstructionCost ScaleCostMaxOffset = TTI.getScalingFactorCost(
1805  LU.AccessTy.MemTy, F.BaseGV, F.BaseOffset + LU.MaxOffset, F.HasBaseReg,
1806  F.Scale, LU.AccessTy.AddrSpace);
1807 
1808  assert(ScaleCostMinOffset.isValid() && ScaleCostMaxOffset.isValid() &&
1809  "Legal addressing mode has an illegal cost!");
1810  return std::max(ScaleCostMinOffset, ScaleCostMaxOffset);
1811  }
1812  case LSRUse::ICmpZero:
1813  case LSRUse::Basic:
1814  case LSRUse::Special:
1815  // The use is completely folded, i.e., everything is folded into the
1816  // instruction.
1817  return 0;
1818  }
1819 
1820  llvm_unreachable("Invalid LSRUse Kind!");
1821 }
1822 
1824  LSRUse::KindType Kind, MemAccessTy AccessTy,
1825  GlobalValue *BaseGV, int64_t BaseOffset,
1826  bool HasBaseReg) {
1827  // Fast-path: zero is always foldable.
1828  if (BaseOffset == 0 && !BaseGV) return true;
1829 
1830  // Conservatively, create an address with an immediate and a
1831  // base and a scale.
1832  int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1833 
1834  // Canonicalize a scale of 1 to a base register if the formula doesn't
1835  // already have a base register.
1836  if (!HasBaseReg && Scale == 1) {
1837  Scale = 0;
1838  HasBaseReg = true;
1839  }
1840 
1841  return isAMCompletelyFolded(TTI, Kind, AccessTy, BaseGV, BaseOffset,
1842  HasBaseReg, Scale);
1843 }
1844 
1846  ScalarEvolution &SE, int64_t MinOffset,
1847  int64_t MaxOffset, LSRUse::KindType Kind,
1848  MemAccessTy AccessTy, const SCEV *S,
1849  bool HasBaseReg) {
1850  // Fast-path: zero is always foldable.
1851  if (S->isZero()) return true;
1852 
1853  // Conservatively, create an address with an immediate and a
1854  // base and a scale.
1855  int64_t BaseOffset = ExtractImmediate(S, SE);
1856  GlobalValue *BaseGV = ExtractSymbol(S, SE);
1857 
1858  // If there's anything else involved, it's not foldable.
1859  if (!S->isZero()) return false;
1860 
1861  // Fast-path: zero is always foldable.
1862  if (BaseOffset == 0 && !BaseGV) return true;
1863 
1864  // Conservatively, create an address with an immediate and a
1865  // base and a scale.
1866  int64_t Scale = Kind == LSRUse::ICmpZero ? -1 : 1;
1867 
1868  return isAMCompletelyFolded(TTI, MinOffset, MaxOffset, Kind, AccessTy, BaseGV,
1869  BaseOffset, HasBaseReg, Scale);
1870 }
1871 
1872 namespace {
1873 
1874 /// An individual increment in a Chain of IV increments. Relate an IV user to
1875 /// an expression that computes the IV it uses from the IV used by the previous
1876 /// link in the Chain.
1877 ///
1878 /// For the head of a chain, IncExpr holds the absolute SCEV expression for the
1879 /// original IVOperand. The head of the chain's IVOperand is only valid during
1880 /// chain collection, before LSR replaces IV users. During chain generation,
1881 /// IncExpr can be used to find the new IVOperand that computes the same
1882 /// expression.
1883 struct IVInc {
1884  Instruction *UserInst;
1885  Value* IVOperand;
1886  const SCEV *IncExpr;
1887 
1888  IVInc(Instruction *U, Value *O, const SCEV *E)
1889  : UserInst(U), IVOperand(O), IncExpr(E) {}
1890 };
1891 
1892 // The list of IV increments in program order. We typically add the head of a
1893 // chain without finding subsequent links.
1894 struct IVChain {
1895  SmallVector<IVInc, 1> Incs;
1896  const SCEV *ExprBase = nullptr;
1897 
1898  IVChain() = default;
1899  IVChain(const IVInc &Head, const SCEV *Base)
1900  : Incs(1, Head), ExprBase(Base) {}
1901 
1902  using const_iterator = SmallVectorImpl<IVInc>::const_iterator;
1903 
1904  // Return the first increment in the chain.
1905  const_iterator begin() const {
1906  assert(!Incs.empty());
1907  return std::next(Incs.begin());
1908  }
1909  const_iterator end() const {
1910  return Incs.end();
1911  }
1912 
1913  // Returns true if this chain contains any increments.
1914  bool hasIncs() const { return Incs.size() >= 2; }
1915 
1916  // Add an IVInc to the end of this chain.
1917  void add(const IVInc &X) { Incs.push_back(X); }
1918 
1919  // Returns the last UserInst in the chain.
1920  Instruction *tailUserInst() const { return Incs.back().UserInst; }
1921 
1922  // Returns true if IncExpr can be profitably added to this chain.
1923  bool isProfitableIncrement(const SCEV *OperExpr,
1924  const SCEV *IncExpr,
1925  ScalarEvolution&);
1926 };
1927 
1928 /// Helper for CollectChains to track multiple IV increment uses. Distinguish
1929 /// between FarUsers that definitely cross IV increments and NearUsers that may
1930 /// be used between IV increments.
1931 struct ChainUsers {
1933  SmallPtrSet<Instruction*, 4> NearUsers;
1934 };
1935 
1936 /// This class holds state for the main loop strength reduction logic.
1937 class LSRInstance {
1938  IVUsers &IU;
1939  ScalarEvolution &SE;
1940  DominatorTree &DT;
1941  LoopInfo &LI;
1942  AssumptionCache &AC;
1943  TargetLibraryInfo &TLI;
1944  const TargetTransformInfo &TTI;
1945  Loop *const L;
1946  MemorySSAUpdater *MSSAU;
1948  bool Changed = false;
1949 
1950  /// This is the insert position that the current loop's induction variable
1951  /// increment should be placed. In simple loops, this is the latch block's
1952  /// terminator. But in more complicated cases, this is a position which will
1953  /// dominate all the in-loop post-increment users.
1954  Instruction *IVIncInsertPos = nullptr;
1955 
1956  /// Interesting factors between use strides.
1957  ///
1958  /// We explicitly use a SetVector which contains a SmallSet, instead of the
1959  /// default, a SmallDenseSet, because we need to use the full range of
1960  /// int64_ts, and there's currently no good way of doing that with
1961  /// SmallDenseSet.
1963 
1964  /// Interesting use types, to facilitate truncation reuse.
1966 
1967  /// The list of interesting uses.
1968  mutable SmallVector<LSRUse, 16> Uses;
1969 
1970  /// Track which uses use which register candidates.
1971  RegUseTracker RegUses;
1972 
1973  // Limit the number of chains to avoid quadratic behavior. We don't expect to
1974  // have more than a few IV increment chains in a loop. Missing a Chain falls
1975  // back to normal LSR behavior for those uses.
1976  static const unsigned MaxChains = 8;
1977 
1978  /// IV users can form a chain of IV increments.
1980 
1981  /// IV users that belong to profitable IVChains.
1983 
1984  /// Induction variables that were generated and inserted by the SCEV Expander.
1985  SmallVector<llvm::WeakVH, 2> ScalarEvolutionIVs;
1986 
1987  void OptimizeShadowIV();
1988  bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
1989  ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
1990  void OptimizeLoopTermCond();
1991 
1992  void ChainInstruction(Instruction *UserInst, Instruction *IVOper,
1993  SmallVectorImpl<ChainUsers> &ChainUsersVec);
1994  void FinalizeChain(IVChain &Chain);
1995  void CollectChains();
1996  void GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
1997  SmallVectorImpl<WeakTrackingVH> &DeadInsts);
1998 
1999  void CollectInterestingTypesAndFactors();
2000  void CollectFixupsAndInitialFormulae();
2001 
2002  // Support for sharing of LSRUses between LSRFixups.
2004  UseMapTy UseMap;
2005 
2006  bool reconcileNewOffset(LSRUse &LU, int64_t NewOffset, bool HasBaseReg,
2007  LSRUse::KindType Kind, MemAccessTy AccessTy);
2008 
2009  std::pair<size_t, int64_t> getUse(const SCEV *&Expr, LSRUse::KindType Kind,
2010  MemAccessTy AccessTy);
2011 
2012  void DeleteUse(LSRUse &LU, size_t LUIdx);
2013 
2014  LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU);
2015 
2016  void InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
2017  void InsertSupplementalFormula(const SCEV *S, LSRUse &LU, size_t LUIdx);
2018  void CountRegisters(const Formula &F, size_t LUIdx);
2019  bool InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F);
2020 
2021  void CollectLoopInvariantFixupsAndFormulae();
2022 
2023  void GenerateReassociations(LSRUse &LU, unsigned LUIdx, Formula Base,
2024  unsigned Depth = 0);
2025 
2026  void GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx,
2027  const Formula &Base, unsigned Depth,
2028  size_t Idx, bool IsScaledReg = false);
2029  void GenerateCombinations(LSRUse &LU, unsigned LUIdx, Formula Base);
2030  void GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx,
2031  const Formula &Base, size_t Idx,
2032  bool IsScaledReg = false);
2033  void GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
2034  void GenerateConstantOffsetsImpl(LSRUse &LU, unsigned LUIdx,
2035  const Formula &Base,
2036  const SmallVectorImpl<int64_t> &Worklist,
2037  size_t Idx, bool IsScaledReg = false);
2038  void GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx, Formula Base);
2039  void GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx, Formula Base);
2040  void GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base);
2041  void GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base);
2042  void GenerateCrossUseConstantOffsets();
2043  void GenerateAllReuseFormulae();
2044 
2045  void FilterOutUndesirableDedicatedRegisters();
2046 
2047  size_t EstimateSearchSpaceComplexity() const;
2048  void NarrowSearchSpaceByDetectingSupersets();
2049  void NarrowSearchSpaceByCollapsingUnrolledCode();
2050  void NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
2051  void NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
2052  void NarrowSearchSpaceByFilterPostInc();
2053  void NarrowSearchSpaceByDeletingCostlyFormulas();
2054  void NarrowSearchSpaceByPickingWinnerRegs();
2055  void NarrowSearchSpaceUsingHeuristics();
2056 
2057  void SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
2058  Cost &SolutionCost,
2060  const Cost &CurCost,
2061  const SmallPtrSet<const SCEV *, 16> &CurRegs,
2062  DenseSet<const SCEV *> &VisitedRegs) const;
2063  void Solve(SmallVectorImpl<const Formula *> &Solution) const;
2064 
2066  HoistInsertPosition(BasicBlock::iterator IP,
2067  const SmallVectorImpl<Instruction *> &Inputs) const;
2069  AdjustInsertPositionForExpand(BasicBlock::iterator IP,
2070  const LSRFixup &LF,
2071  const LSRUse &LU,
2072  SCEVExpander &Rewriter) const;
2073 
2074  Value *Expand(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
2076  SmallVectorImpl<WeakTrackingVH> &DeadInsts) const;
2077  void RewriteForPHI(PHINode *PN, const LSRUse &LU, const LSRFixup &LF,
2078  const Formula &F, SCEVExpander &Rewriter,
2079  SmallVectorImpl<WeakTrackingVH> &DeadInsts) const;
2080  void Rewrite(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
2082  SmallVectorImpl<WeakTrackingVH> &DeadInsts) const;
2083  void ImplementSolution(const SmallVectorImpl<const Formula *> &Solution);
2084 
2085 public:
2086  LSRInstance(Loop *L, IVUsers &IU, ScalarEvolution &SE, DominatorTree &DT,
2088  TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU);
2089 
2090  bool getChanged() const { return Changed; }
2091  const SmallVectorImpl<WeakVH> &getScalarEvolutionIVs() const {
2092  return ScalarEvolutionIVs;
2093  }
2094 
2095  void print_factors_and_types(raw_ostream &OS) const;
2096  void print_fixups(raw_ostream &OS) const;
2097  void print_uses(raw_ostream &OS) const;
2098  void print(raw_ostream &OS) const;
2099  void dump() const;
2100 };
2101 
2102 } // end anonymous namespace
2103 
2104 /// If IV is used in a int-to-float cast inside the loop then try to eliminate
2105 /// the cast operation.
2106 void LSRInstance::OptimizeShadowIV() {
2107  const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
2108  if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2109  return;
2110 
2111  for (IVUsers::const_iterator UI = IU.begin(), E = IU.end();
2112  UI != E; /* empty */) {
2113  IVUsers::const_iterator CandidateUI = UI;
2114  ++UI;
2115  Instruction *ShadowUse = CandidateUI->getUser();
2116  Type *DestTy = nullptr;
2117  bool IsSigned = false;
2118 
2119  /* If shadow use is a int->float cast then insert a second IV
2120  to eliminate this cast.
2121 
2122  for (unsigned i = 0; i < n; ++i)
2123  foo((double)i);
2124 
2125  is transformed into
2126 
2127  double d = 0.0;
2128  for (unsigned i = 0; i < n; ++i, ++d)
2129  foo(d);
2130  */
2131  if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
2132  IsSigned = false;
2133  DestTy = UCast->getDestTy();
2134  }
2135  else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
2136  IsSigned = true;
2137  DestTy = SCast->getDestTy();
2138  }
2139  if (!DestTy) continue;
2140 
2141  // If target does not support DestTy natively then do not apply
2142  // this transformation.
2143  if (!TTI.isTypeLegal(DestTy)) continue;
2144 
2145  PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
2146  if (!PH) continue;
2147  if (PH->getNumIncomingValues() != 2) continue;
2148 
2149  // If the calculation in integers overflows, the result in FP type will
2150  // differ. So we only can do this transformation if we are guaranteed to not
2151  // deal with overflowing values
2152  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(PH));
2153  if (!AR) continue;
2154  if (IsSigned && !AR->hasNoSignedWrap()) continue;
2155  if (!IsSigned && !AR->hasNoUnsignedWrap()) continue;
2156 
2157  Type *SrcTy = PH->getType();
2158  int Mantissa = DestTy->getFPMantissaWidth();
2159  if (Mantissa == -1) continue;
2160  if ((int)SE.getTypeSizeInBits(SrcTy) > Mantissa)
2161  continue;
2162 
2163  unsigned Entry, Latch;
2164  if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
2165  Entry = 0;
2166  Latch = 1;
2167  } else {
2168  Entry = 1;
2169  Latch = 0;
2170  }
2171 
2172  ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
2173  if (!Init) continue;
2174  Constant *NewInit = ConstantFP::get(DestTy, IsSigned ?
2175  (double)Init->getSExtValue() :
2176  (double)Init->getZExtValue());
2177 
2178  BinaryOperator *Incr =
2179  dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
2180  if (!Incr) continue;
2181  if (Incr->getOpcode() != Instruction::Add
2182  && Incr->getOpcode() != Instruction::Sub)
2183  continue;
2184 
2185  /* Initialize new IV, double d = 0.0 in above example. */
2186  ConstantInt *C = nullptr;
2187  if (Incr->getOperand(0) == PH)
2188  C = dyn_cast<ConstantInt>(Incr->getOperand(1));
2189  else if (Incr->getOperand(1) == PH)
2190  C = dyn_cast<ConstantInt>(Incr->getOperand(0));
2191  else
2192  continue;
2193 
2194  if (!C) continue;
2195 
2196  // Ignore negative constants, as the code below doesn't handle them
2197  // correctly. TODO: Remove this restriction.
2198  if (!C->getValue().isStrictlyPositive()) continue;
2199 
2200  /* Add new PHINode. */
2201  PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH);
2202 
2203  /* create new increment. '++d' in above example. */
2204  Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
2205  BinaryOperator *NewIncr =
2207  Instruction::FAdd : Instruction::FSub,
2208  NewPH, CFP, "IV.S.next.", Incr);
2209 
2210  NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
2211  NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
2212 
2213  /* Remove cast operation */
2214  ShadowUse->replaceAllUsesWith(NewPH);
2215  ShadowUse->eraseFromParent();
2216  Changed = true;
2217  break;
2218  }
2219 }
2220 
2221 /// If Cond has an operand that is an expression of an IV, set the IV user and
2222 /// stride information and return true, otherwise return false.
2223 bool LSRInstance::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse) {
2224  for (IVStrideUse &U : IU)
2225  if (U.getUser() == Cond) {
2226  // NOTE: we could handle setcc instructions with multiple uses here, but
2227  // InstCombine does it as well for simple uses, it's not clear that it
2228  // occurs enough in real life to handle.
2229  CondUse = &U;
2230  return true;
2231  }
2232  return false;
2233 }
2234 
2235 /// Rewrite the loop's terminating condition if it uses a max computation.
2236 ///
2237 /// This is a narrow solution to a specific, but acute, problem. For loops
2238 /// like this:
2239 ///
2240 /// i = 0;
2241 /// do {
2242 /// p[i] = 0.0;
2243 /// } while (++i < n);
2244 ///
2245 /// the trip count isn't just 'n', because 'n' might not be positive. And
2246 /// unfortunately this can come up even for loops where the user didn't use
2247 /// a C do-while loop. For example, seemingly well-behaved top-test loops
2248 /// will commonly be lowered like this:
2249 ///
2250 /// if (n > 0) {
2251 /// i = 0;
2252 /// do {
2253 /// p[i] = 0.0;
2254 /// } while (++i < n);
2255 /// }
2256 ///
2257 /// and then it's possible for subsequent optimization to obscure the if
2258 /// test in such a way that indvars can't find it.
2259 ///
2260 /// When indvars can't find the if test in loops like this, it creates a
2261 /// max expression, which allows it to give the loop a canonical
2262 /// induction variable:
2263 ///
2264 /// i = 0;
2265 /// max = n < 1 ? 1 : n;
2266 /// do {
2267 /// p[i] = 0.0;
2268 /// } while (++i != max);
2269 ///
2270 /// Canonical induction variables are necessary because the loop passes
2271 /// are designed around them. The most obvious example of this is the
2272 /// LoopInfo analysis, which doesn't remember trip count values. It
2273 /// expects to be able to rediscover the trip count each time it is
2274 /// needed, and it does this using a simple analysis that only succeeds if
2275 /// the loop has a canonical induction variable.
2276 ///
2277 /// However, when it comes time to generate code, the maximum operation
2278 /// can be quite costly, especially if it's inside of an outer loop.
2279 ///
2280 /// This function solves this problem by detecting this type of loop and
2281 /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
2282 /// the instructions for the maximum computation.
2283 ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
2284  // Check that the loop matches the pattern we're looking for.
2285  if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
2286  Cond->getPredicate() != CmpInst::ICMP_NE)
2287  return Cond;
2288 
2289  SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
2290  if (!Sel || !Sel->hasOneUse()) return Cond;
2291 
2292  const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L);
2293  if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2294  return Cond;
2295  const SCEV *One = SE.getConstant(BackedgeTakenCount->getType(), 1);
2296 
2297  // Add one to the backedge-taken count to get the trip count.
2298  const SCEV *IterationCount = SE.getAddExpr(One, BackedgeTakenCount);
2299  if (IterationCount != SE.getSCEV(Sel)) return Cond;
2300 
2301  // Check for a max calculation that matches the pattern. There's no check
2302  // for ICMP_ULE here because the comparison would be with zero, which
2303  // isn't interesting.
2305  const SCEVNAryExpr *Max = nullptr;
2306  if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(BackedgeTakenCount)) {
2307  Pred = ICmpInst::ICMP_SLE;
2308  Max = S;
2309  } else if (const SCEVSMaxExpr *S = dyn_cast<SCEVSMaxExpr>(IterationCount)) {
2310  Pred = ICmpInst::ICMP_SLT;
2311  Max = S;
2312  } else if (const SCEVUMaxExpr *U = dyn_cast<SCEVUMaxExpr>(IterationCount)) {
2313  Pred = ICmpInst::ICMP_ULT;
2314  Max = U;
2315  } else {
2316  // No match; bail.
2317  return Cond;
2318  }
2319 
2320  // To handle a max with more than two operands, this optimization would
2321  // require additional checking and setup.
2322  if (Max->getNumOperands() != 2)
2323  return Cond;
2324 
2325  const SCEV *MaxLHS = Max->getOperand(0);
2326  const SCEV *MaxRHS = Max->getOperand(1);
2327 
2328  // ScalarEvolution canonicalizes constants to the left. For < and >, look
2329  // for a comparison with 1. For <= and >=, a comparison with zero.
2330  if (!MaxLHS ||
2331  (ICmpInst::isTrueWhenEqual(Pred) ? !MaxLHS->isZero() : (MaxLHS != One)))
2332  return Cond;
2333 
2334  // Check the relevant induction variable for conformance to
2335  // the pattern.
2336  const SCEV *IV = SE.getSCEV(Cond->getOperand(0));
2337  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
2338  if (!AR || !AR->isAffine() ||
2339  AR->getStart() != One ||
2340  AR->getStepRecurrence(SE) != One)
2341  return Cond;
2342 
2343  assert(AR->getLoop() == L &&
2344  "Loop condition operand is an addrec in a different loop!");
2345 
2346  // Check the right operand of the select, and remember it, as it will
2347  // be used in the new comparison instruction.
2348  Value *NewRHS = nullptr;
2349  if (ICmpInst::isTrueWhenEqual(Pred)) {
2350  // Look for n+1, and grab n.
2351  if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(1)))
2352  if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1)))
2353  if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS)
2354  NewRHS = BO->getOperand(0);
2355  if (AddOperator *BO = dyn_cast<AddOperator>(Sel->getOperand(2)))
2356  if (ConstantInt *BO1 = dyn_cast<ConstantInt>(BO->getOperand(1)))
2357  if (BO1->isOne() && SE.getSCEV(BO->getOperand(0)) == MaxRHS)
2358  NewRHS = BO->getOperand(0);
2359  if (!NewRHS)
2360  return Cond;
2361  } else if (SE.getSCEV(Sel->getOperand(1)) == MaxRHS)
2362  NewRHS = Sel->getOperand(1);
2363  else if (SE.getSCEV(Sel->getOperand(2)) == MaxRHS)
2364  NewRHS = Sel->getOperand(2);
2365  else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(MaxRHS))
2366  NewRHS = SU->getValue();
2367  else
2368  // Max doesn't match expected pattern.
2369  return Cond;
2370 
2371  // Determine the new comparison opcode. It may be signed or unsigned,
2372  // and the original comparison may be either equality or inequality.
2373  if (Cond->getPredicate() == CmpInst::ICMP_EQ)
2374  Pred = CmpInst::getInversePredicate(Pred);
2375 
2376  // Ok, everything looks ok to change the condition into an SLT or SGE and
2377  // delete the max calculation.
2378  ICmpInst *NewCond =
2379  new ICmpInst(Cond, Pred, Cond->getOperand(0), NewRHS, "scmp");
2380 
2381  // Delete the max calculation instructions.
2382  NewCond->setDebugLoc(Cond->getDebugLoc());
2383  Cond->replaceAllUsesWith(NewCond);
2384  CondUse->setUser(NewCond);
2385  Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
2386  Cond->eraseFromParent();
2387  Sel->eraseFromParent();
2388  if (Cmp->use_empty())
2389  Cmp->eraseFromParent();
2390  return NewCond;
2391 }
2392 
2393 /// Change loop terminating condition to use the postinc iv when possible.
2394 void
2395 LSRInstance::OptimizeLoopTermCond() {
2397 
2398  // We need a different set of heuristics for rotated and non-rotated loops.
2399  // If a loop is rotated then the latch is also the backedge, so inserting
2400  // post-inc expressions just before the latch is ideal. To reduce live ranges
2401  // it also makes sense to rewrite terminating conditions to use post-inc
2402  // expressions.
2403  //
2404  // If the loop is not rotated then the latch is not a backedge; the latch
2405  // check is done in the loop head. Adding post-inc expressions before the
2406  // latch will cause overlapping live-ranges of pre-inc and post-inc expressions
2407  // in the loop body. In this case we do *not* want to use post-inc expressions
2408  // in the latch check, and we want to insert post-inc expressions before
2409  // the backedge.
2410  BasicBlock *LatchBlock = L->getLoopLatch();
2411  SmallVector<BasicBlock*, 8> ExitingBlocks;
2412  L->getExitingBlocks(ExitingBlocks);
2413  if (llvm::all_of(ExitingBlocks, [&LatchBlock](const BasicBlock *BB) {
2414  return LatchBlock != BB;
2415  })) {
2416  // The backedge doesn't exit the loop; treat this as a head-tested loop.
2417  IVIncInsertPos = LatchBlock->getTerminator();
2418  return;
2419  }
2420 
2421  // Otherwise treat this as a rotated loop.
2422  for (BasicBlock *ExitingBlock : ExitingBlocks) {
2423  // Get the terminating condition for the loop if possible. If we
2424  // can, we want to change it to use a post-incremented version of its
2425  // induction variable, to allow coalescing the live ranges for the IV into
2426  // one register value.
2427 
2428  BranchInst *TermBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
2429  if (!TermBr)
2430  continue;
2431  // FIXME: Overly conservative, termination condition could be an 'or' etc..
2432  if (TermBr->isUnconditional() || !isa<ICmpInst>(TermBr->getCondition()))
2433  continue;
2434 
2435  // Search IVUsesByStride to find Cond's IVUse if there is one.
2436  IVStrideUse *CondUse = nullptr;
2437  ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
2438  if (!FindIVUserForCond(Cond, CondUse))
2439  continue;
2440 
2441  // If the trip count is computed in terms of a max (due to ScalarEvolution
2442  // being unable to find a sufficient guard, for example), change the loop
2443  // comparison to use SLT or ULT instead of NE.
2444  // One consequence of doing this now is that it disrupts the count-down
2445  // optimization. That's not always a bad thing though, because in such
2446  // cases it may still be worthwhile to avoid a max.
2447  Cond = OptimizeMax(Cond, CondUse);
2448 
2449  // If this exiting block dominates the latch block, it may also use
2450  // the post-inc value if it won't be shared with other uses.
2451  // Check for dominance.
2452  if (!DT.dominates(ExitingBlock, LatchBlock))
2453  continue;
2454 
2455  // Conservatively avoid trying to use the post-inc value in non-latch
2456  // exits if there may be pre-inc users in intervening blocks.
2457  if (LatchBlock != ExitingBlock)
2458  for (IVUsers::const_iterator UI = IU.begin(), E = IU.end(); UI != E; ++UI)
2459  // Test if the use is reachable from the exiting block. This dominator
2460  // query is a conservative approximation of reachability.
2461  if (&*UI != CondUse &&
2462  !DT.properlyDominates(UI->getUser()->getParent(), ExitingBlock)) {
2463  // Conservatively assume there may be reuse if the quotient of their
2464  // strides could be a legal scale.
2465  const SCEV *A = IU.getStride(*CondUse, L);
2466  const SCEV *B = IU.getStride(*UI, L);
2467  if (!A || !B) continue;
2468  if (SE.getTypeSizeInBits(A->getType()) !=
2469  SE.getTypeSizeInBits(B->getType())) {
2470  if (SE.getTypeSizeInBits(A->getType()) >
2471  SE.getTypeSizeInBits(B->getType()))
2472  B = SE.getSignExtendExpr(B, A->getType());
2473  else
2474  A = SE.getSignExtendExpr(A, B->getType());
2475  }
2476  if (const SCEVConstant *D =
2477  dyn_cast_or_null<SCEVConstant>(getExactSDiv(B, A, SE))) {
2478  const ConstantInt *C = D->getValue();
2479  // Stride of one or negative one can have reuse with non-addresses.
2480  if (C->isOne() || C->isMinusOne())
2481  goto decline_post_inc;
2482  // Avoid weird situations.
2483  if (C->getValue().getMinSignedBits() >= 64 ||
2484  C->getValue().isMinSignedValue())
2485  goto decline_post_inc;
2486  // Check for possible scaled-address reuse.
2487  if (isAddressUse(TTI, UI->getUser(), UI->getOperandValToReplace())) {
2488  MemAccessTy AccessTy = getAccessType(
2489  TTI, UI->getUser(), UI->getOperandValToReplace());
2490  int64_t Scale = C->getSExtValue();
2491  if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr,
2492  /*BaseOffset=*/0,
2493  /*HasBaseReg=*/false, Scale,
2494  AccessTy.AddrSpace))
2495  goto decline_post_inc;
2496  Scale = -Scale;
2497  if (TTI.isLegalAddressingMode(AccessTy.MemTy, /*BaseGV=*/nullptr,
2498  /*BaseOffset=*/0,
2499  /*HasBaseReg=*/false, Scale,
2500  AccessTy.AddrSpace))
2501  goto decline_post_inc;
2502  }
2503  }
2504  }
2505 
2506  LLVM_DEBUG(dbgs() << " Change loop exiting icmp to use postinc iv: "
2507  << *Cond << '\n');
2508 
2509  // It's possible for the setcc instruction to be anywhere in the loop, and
2510  // possible for it to have multiple users. If it is not immediately before
2511  // the exiting block branch, move it.
2512  if (Cond->getNextNonDebugInstruction() != TermBr) {
2513  if (Cond->hasOneUse()) {
2514  Cond->moveBefore(TermBr);
2515  } else {
2516  // Clone the terminating condition and insert into the loopend.
2517  ICmpInst *OldCond = Cond;
2518  Cond = cast<ICmpInst>(Cond->clone());
2519  Cond->setName(L->getHeader()->getName() + ".termcond");
2520  ExitingBlock->getInstList().insert(TermBr->getIterator(), Cond);
2521 
2522  // Clone the IVUse, as the old use still exists!
2523  CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
2524  TermBr->replaceUsesOfWith(OldCond, Cond);
2525  }
2526  }
2527 
2528  // If we get to here, we know that we can transform the setcc instruction to
2529  // use the post-incremented version of the IV, allowing us to coalesce the
2530  // live ranges for the IV correctly.
2531  CondUse->transformToPostInc(L);
2532  Changed = true;
2533 
2534  PostIncs.insert(Cond);
2535  decline_post_inc:;
2536  }
2537 
2538  // Determine an insertion point for the loop induction variable increment. It
2539  // must dominate all the post-inc comparisons we just set up, and it must
2540  // dominate the loop latch edge.
2541  IVIncInsertPos = L->getLoopLatch()->getTerminator();
2542  for (Instruction *Inst : PostIncs) {
2543  BasicBlock *BB =
2544  DT.findNearestCommonDominator(IVIncInsertPos->getParent(),
2545  Inst->getParent());
2546  if (BB == Inst->getParent())
2547  IVIncInsertPos = Inst;
2548  else if (BB != IVIncInsertPos->getParent())
2549  IVIncInsertPos = BB->getTerminator();
2550  }
2551 }
2552 
2553 /// Determine if the given use can accommodate a fixup at the given offset and
2554 /// other details. If so, update the use and return true.
2555 bool LSRInstance::reconcileNewOffset(LSRUse &LU, int64_t NewOffset,
2556  bool HasBaseReg, LSRUse::KindType Kind,
2557  MemAccessTy AccessTy) {
2558  int64_t NewMinOffset = LU.MinOffset;
2559  int64_t NewMaxOffset = LU.MaxOffset;
2560  MemAccessTy NewAccessTy = AccessTy;
2561 
2562  // Check for a mismatched kind. It's tempting to collapse mismatched kinds to
2563  // something conservative, however this can pessimize in the case that one of
2564  // the uses will have all its uses outside the loop, for example.
2565  if (LU.Kind != Kind)
2566  return false;
2567 
2568  // Check for a mismatched access type, and fall back conservatively as needed.
2569  // TODO: Be less conservative when the type is similar and can use the same
2570  // addressing modes.
2571  if (Kind == LSRUse::Address) {
2572  if (AccessTy.MemTy != LU.AccessTy.MemTy) {
2573  NewAccessTy = MemAccessTy::getUnknown(AccessTy.MemTy->getContext(),
2574  AccessTy.AddrSpace);
2575  }
2576  }
2577 
2578  // Conservatively assume HasBaseReg is true for now.
2579  if (NewOffset < LU.MinOffset) {
2580  if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr,
2581  LU.MaxOffset - NewOffset, HasBaseReg))
2582  return false;
2583  NewMinOffset = NewOffset;
2584  } else if (NewOffset > LU.MaxOffset) {
2585  if (!isAlwaysFoldable(TTI, Kind, NewAccessTy, /*BaseGV=*/nullptr,
2586  NewOffset - LU.MinOffset, HasBaseReg))
2587  return false;
2588  NewMaxOffset = NewOffset;
2589  }
2590 
2591  // Update the use.
2592  LU.MinOffset = NewMinOffset;
2593  LU.MaxOffset = NewMaxOffset;
2594  LU.AccessTy = NewAccessTy;
2595  return true;
2596 }
2597 
2598 /// Return an LSRUse index and an offset value for a fixup which needs the given
2599 /// expression, with the given kind and optional access type. Either reuse an
2600 /// existing use or create a new one, as needed.
2601 std::pair<size_t, int64_t> LSRInstance::getUse(const SCEV *&Expr,
2602  LSRUse::KindType Kind,
2603  MemAccessTy AccessTy) {
2604  const SCEV *Copy = Expr;
2605  int64_t Offset = ExtractImmediate(Expr, SE);
2606 
2607  // Basic uses can't accept any offset, for example.
2608  if (!isAlwaysFoldable(TTI, Kind, AccessTy, /*BaseGV=*/ nullptr,
2609  Offset, /*HasBaseReg=*/ true)) {
2610  Expr = Copy;
2611  Offset = 0;
2612  }
2613 
2614  std::pair<UseMapTy::iterator, bool> P =
2615  UseMap.insert(std::make_pair(LSRUse::SCEVUseKindPair(Expr, Kind), 0));
2616  if (!P.second) {
2617  // A use already existed with this base.
2618  size_t LUIdx = P.first->second;
2619  LSRUse &LU = Uses[LUIdx];
2620  if (reconcileNewOffset(LU, Offset, /*HasBaseReg=*/true, Kind, AccessTy))
2621  // Reuse this use.
2622  return std::make_pair(LUIdx, Offset);
2623  }
2624 
2625  // Create a new use.
2626  size_t LUIdx = Uses.size();
2627  P.first->second = LUIdx;
2628  Uses.push_back(LSRUse(Kind, AccessTy));
2629  LSRUse &LU = Uses[LUIdx];
2630 
2631  LU.MinOffset = Offset;
2632  LU.MaxOffset = Offset;
2633  return std::make_pair(LUIdx, Offset);
2634 }
2635 
2636 /// Delete the given use from the Uses list.
2637 void LSRInstance::DeleteUse(LSRUse &LU, size_t LUIdx) {
2638  if (&LU != &Uses.back())
2639  std::swap(LU, Uses.back());
2640  Uses.pop_back();
2641 
2642  // Update RegUses.
2643  RegUses.swapAndDropUse(LUIdx, Uses.size());
2644 }
2645 
2646 /// Look for a use distinct from OrigLU which is has a formula that has the same
2647 /// registers as the given formula.
2648 LSRUse *
2649 LSRInstance::FindUseWithSimilarFormula(const Formula &OrigF,
2650  const LSRUse &OrigLU) {
2651  // Search all uses for the formula. This could be more clever.
2652  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
2653  LSRUse &LU = Uses[LUIdx];
2654  // Check whether this use is close enough to OrigLU, to see whether it's
2655  // worthwhile looking through its formulae.
2656  // Ignore ICmpZero uses because they may contain formulae generated by
2657  // GenerateICmpZeroScales, in which case adding fixup offsets may
2658  // be invalid.
2659  if (&LU != &OrigLU &&
2660  LU.Kind != LSRUse::ICmpZero &&
2661  LU.Kind == OrigLU.Kind && OrigLU.AccessTy == LU.AccessTy &&
2662  LU.WidestFixupType == OrigLU.WidestFixupType &&
2663  LU.HasFormulaWithSameRegs(OrigF)) {
2664  // Scan through this use's formulae.
2665  for (const Formula &F : LU.Formulae) {
2666  // Check to see if this formula has the same registers and symbols
2667  // as OrigF.
2668  if (F.BaseRegs == OrigF.BaseRegs &&
2669  F.ScaledReg == OrigF.ScaledReg &&
2670  F.BaseGV == OrigF.BaseGV &&
2671  F.Scale == OrigF.Scale &&
2672  F.UnfoldedOffset == OrigF.UnfoldedOffset) {
2673  if (F.BaseOffset == 0)
2674  return &LU;
2675  // This is the formula where all the registers and symbols matched;
2676  // there aren't going to be any others. Since we declined it, we
2677  // can skip the rest of the formulae and proceed to the next LSRUse.
2678  break;
2679  }
2680  }
2681  }
2682  }
2683 
2684  // Nothing looked good.
2685  return nullptr;
2686 }
2687 
2688 void LSRInstance::CollectInterestingTypesAndFactors() {
2690 
2691  // Collect interesting types and strides.
2693  for (const IVStrideUse &U : IU) {
2694  const SCEV *Expr = IU.getExpr(U);
2695 
2696  // Collect interesting types.
2697  Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
2698 
2699  // Add strides for mentioned loops.
2700  Worklist.push_back(Expr);
2701  do {
2702  const SCEV *S = Worklist.pop_back_val();
2703  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
2704  if (AR->getLoop() == L)
2705  Strides.insert(AR->getStepRecurrence(SE));
2706  Worklist.push_back(AR->getStart());
2707  } else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2708  Worklist.append(Add->op_begin(), Add->op_end());
2709  }
2710  } while (!Worklist.empty());
2711  }
2712 
2713  // Compute interesting factors from the set of interesting strides.
2715  I = Strides.begin(), E = Strides.end(); I != E; ++I)
2717  std::next(I); NewStrideIter != E; ++NewStrideIter) {
2718  const SCEV *OldStride = *I;
2719  const SCEV *NewStride = *NewStrideIter;
2720 
2721  if (SE.getTypeSizeInBits(OldStride->getType()) !=
2722  SE.getTypeSizeInBits(NewStride->getType())) {
2723  if (SE.getTypeSizeInBits(OldStride->getType()) >
2724  SE.getTypeSizeInBits(NewStride->getType()))
2725  NewStride = SE.getSignExtendExpr(NewStride, OldStride->getType());
2726  else
2727  OldStride = SE.getSignExtendExpr(OldStride, NewStride->getType());
2728  }
2729  if (const SCEVConstant *Factor =
2730  dyn_cast_or_null<SCEVConstant>(getExactSDiv(NewStride, OldStride,
2731  SE, true))) {
2732  if (Factor->getAPInt().getMinSignedBits() <= 64 && !Factor->isZero())
2733  Factors.insert(Factor->getAPInt().getSExtValue());
2734  } else if (const SCEVConstant *Factor =
2735  dyn_cast_or_null<SCEVConstant>(getExactSDiv(OldStride,
2736  NewStride,
2737  SE, true))) {
2738  if (Factor->getAPInt().getMinSignedBits() <= 64 && !Factor->isZero())
2739  Factors.insert(Factor->getAPInt().getSExtValue());
2740  }
2741  }
2742 
2743  // If all uses use the same type, don't bother looking for truncation-based
2744  // reuse.
2745  if (Types.size() == 1)
2746  Types.clear();
2747 
2748  LLVM_DEBUG(print_factors_and_types(dbgs()));
2749 }
2750 
2751 /// Helper for CollectChains that finds an IV operand (computed by an AddRec in
2752 /// this loop) within [OI,OE) or returns OE. If IVUsers mapped Instructions to
2753 /// IVStrideUses, we could partially skip this.
2754 static User::op_iterator
2756  Loop *L, ScalarEvolution &SE) {
2757  for(; OI != OE; ++OI) {
2758  if (Instruction *Oper = dyn_cast<Instruction>(*OI)) {
2759  if (!SE.isSCEVable(Oper->getType()))
2760  continue;
2761 
2762  if (const SCEVAddRecExpr *AR =
2763  dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Oper))) {
2764  if (AR->getLoop() == L)
2765  break;
2766  }
2767  }
2768  }
2769  return OI;
2770 }
2771 
2772 /// IVChain logic must consistently peek base TruncInst operands, so wrap it in
2773 /// a convenient helper.
2774 static Value *getWideOperand(Value *Oper) {
2775  if (TruncInst *Trunc = dyn_cast<TruncInst>(Oper))
2776  return Trunc->getOperand(0);
2777  return Oper;
2778 }
2779 
2780 /// Return true if we allow an IV chain to include both types.
2781 static bool isCompatibleIVType(Value *LVal, Value *RVal) {
2782  Type *LType = LVal->getType();
2783  Type *RType = RVal->getType();
2784  return (LType == RType) || (LType->isPointerTy() && RType->isPointerTy() &&
2785  // Different address spaces means (possibly)
2786  // different types of the pointer implementation,
2787  // e.g. i16 vs i32 so disallow that.
2788  (LType->getPointerAddressSpace() ==
2789  RType->getPointerAddressSpace()));
2790 }
2791 
2792 /// Return an approximation of this SCEV expression's "base", or NULL for any
2793 /// constant. Returning the expression itself is conservative. Returning a
2794 /// deeper subexpression is more precise and valid as long as it isn't less
2795 /// complex than another subexpression. For expressions involving multiple
2796 /// unscaled values, we need to return the pointer-type SCEVUnknown. This avoids
2797 /// forming chains across objects, such as: PrevOper==a[i], IVOper==b[i],
2798 /// IVInc==b-a.
2799 ///
2800 /// Since SCEVUnknown is the rightmost type, and pointers are the rightmost
2801 /// SCEVUnknown, we simply return the rightmost SCEV operand.
2802 static const SCEV *getExprBase(const SCEV *S) {
2803  switch (S->getSCEVType()) {
2804  default: // uncluding scUnknown.
2805  return S;
2806  case scConstant:
2807  return nullptr;
2808  case scTruncate:
2809  return getExprBase(cast<SCEVTruncateExpr>(S)->getOperand());
2810  case scZeroExtend:
2811  return getExprBase(cast<SCEVZeroExtendExpr>(S)->getOperand());
2812  case scSignExtend:
2813  return getExprBase(cast<SCEVSignExtendExpr>(S)->getOperand());
2814  case scAddExpr: {
2815  // Skip over scaled operands (scMulExpr) to follow add operands as long as
2816  // there's nothing more complex.
2817  // FIXME: not sure if we want to recognize negation.
2818  const SCEVAddExpr *Add = cast<SCEVAddExpr>(S);
2819  for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(Add->op_end()),
2820  E(Add->op_begin()); I != E; ++I) {
2821  const SCEV *SubExpr = *I;
2822  if (SubExpr->getSCEVType() == scAddExpr)
2823  return getExprBase(SubExpr);
2824 
2825  if (SubExpr->getSCEVType() != scMulExpr)
2826  return SubExpr;
2827  }
2828  return S; // all operands are scaled, be conservative.
2829  }
2830  case scAddRecExpr:
2831  return getExprBase(cast<SCEVAddRecExpr>(S)->getStart());
2832  }
2833  llvm_unreachable("Unknown SCEV kind!");
2834 }
2835 
2836 /// Return true if the chain increment is profitable to expand into a loop
2837 /// invariant value, which may require its own register. A profitable chain
2838 /// increment will be an offset relative to the same base. We allow such offsets
2839 /// to potentially be used as chain increment as long as it's not obviously
2840 /// expensive to expand using real instructions.
2841 bool IVChain::isProfitableIncrement(const SCEV *OperExpr,
2842  const SCEV *IncExpr,
2843  ScalarEvolution &SE) {
2844  // Aggressively form chains when -stress-ivchain.
2845  if (StressIVChain)
2846  return true;
2847 
2848  // Do not replace a constant offset from IV head with a nonconstant IV
2849  // increment.
2850  if (!isa<SCEVConstant>(IncExpr)) {
2851  const SCEV *HeadExpr = SE.getSCEV(getWideOperand(Incs[0].IVOperand));
2852  if (isa<SCEVConstant>(SE.getMinusSCEV(OperExpr, HeadExpr)))
2853  return false;
2854  }
2855 
2856  SmallPtrSet<const SCEV*, 8> Processed;
2857  return !isHighCostExpansion(IncExpr, Processed, SE);
2858 }
2859 
2860 /// Return true if the number of registers needed for the chain is estimated to
2861 /// be less than the number required for the individual IV users. First prohibit
2862 /// any IV users that keep the IV live across increments (the Users set should
2863 /// be empty). Next count the number and type of increments in the chain.
2864 ///
2865 /// Chaining IVs can lead to considerable code bloat if ISEL doesn't
2866 /// effectively use postinc addressing modes. Only consider it profitable it the
2867 /// increments can be computed in fewer registers when chained.
2868 ///
2869 /// TODO: Consider IVInc free if it's already used in another chains.
2870 static bool isProfitableChain(IVChain &Chain,
2872  ScalarEvolution &SE,
2873  const TargetTransformInfo &TTI) {
2874  if (StressIVChain)
2875  return true;
2876 
2877  if (!Chain.hasIncs())
2878  return false;
2879 
2880  if (!Users.empty()) {
2881  LLVM_DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " users:\n";
2882  for (Instruction *Inst
2883  : Users) { dbgs() << " " << *Inst << "\n"; });
2884  return false;
2885  }
2886  assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
2887 
2888  // The chain itself may require a register, so intialize cost to 1.
2889  int cost = 1;
2890 
2891  // A complete chain likely eliminates the need for keeping the original IV in
2892  // a register. LSR does not currently know how to form a complete chain unless
2893  // the header phi already exists.
2894  if (isa<PHINode>(Chain.tailUserInst())
2895  && SE.getSCEV(Chain.tailUserInst()) == Chain.Incs[0].IncExpr) {
2896  --cost;
2897  }
2898  const SCEV *LastIncExpr = nullptr;
2899  unsigned NumConstIncrements = 0;
2900  unsigned NumVarIncrements = 0;
2901  unsigned NumReusedIncrements = 0;
2902 
2903  if (TTI.isProfitableLSRChainElement(Chain.Incs[0].UserInst))
2904  return true;
2905 
2906  for (const IVInc &Inc : Chain) {
2907  if (TTI.isProfitableLSRChainElement(Inc.UserInst))
2908  return true;
2909  if (Inc.IncExpr->isZero())
2910  continue;
2911 
2912  // Incrementing by zero or some constant is neutral. We assume constants can
2913  // be folded into an addressing mode or an add's immediate operand.
2914  if (isa<SCEVConstant>(Inc.IncExpr)) {
2915  ++NumConstIncrements;
2916  continue;
2917  }
2918 
2919  if (Inc.IncExpr == LastIncExpr)
2920  ++NumReusedIncrements;
2921  else
2922  ++NumVarIncrements;
2923 
2924  LastIncExpr = Inc.IncExpr;
2925  }
2926  // An IV chain with a single increment is handled by LSR's postinc
2927  // uses. However, a chain with multiple increments requires keeping the IV's
2928  // value live longer than it needs to be if chained.
2929  if (NumConstIncrements > 1)
2930  --cost;
2931 
2932  // Materializing increment expressions in the preheader that didn't exist in
2933  // the original code may cost a register. For example, sign-extended array
2934  // indices can produce ridiculous increments like this:
2935  // IV + ((sext i32 (2 * %s) to i64) + (-1 * (sext i32 %s to i64)))
2936  cost += NumVarIncrements;
2937 
2938  // Reusing variable increments likely saves a register to hold the multiple of
2939  // the stride.
2940  cost -= NumReusedIncrements;
2941 
2942  LLVM_DEBUG(dbgs() << "Chain: " << *Chain.Incs[0].UserInst << " Cost: " << cost
2943  << "\n");
2944 
2945  return cost < 0;
2946 }
2947 
2948 /// Add this IV user to an existing chain or make it the head of a new chain.
2949 void LSRInstance::ChainInstruction(Instruction *UserInst, Instruction *IVOper,
2950  SmallVectorImpl<ChainUsers> &ChainUsersVec) {
2951  // When IVs are used as types of varying widths, they are generally converted
2952  // to a wider type with some uses remaining narrow under a (free) trunc.
2953  Value *const NextIV = getWideOperand(IVOper);
2954  const SCEV *const OperExpr = SE.getSCEV(NextIV);
2955  const SCEV *const OperExprBase = getExprBase(OperExpr);
2956 
2957  // Visit all existing chains. Check if its IVOper can be computed as a
2958  // profitable loop invariant increment from the last link in the Chain.
2959  unsigned ChainIdx = 0, NChains = IVChainVec.size();
2960  const SCEV *LastIncExpr = nullptr;
2961  for (; ChainIdx < NChains; ++ChainIdx) {
2962  IVChain &Chain = IVChainVec[ChainIdx];
2963 
2964  // Prune the solution space aggressively by checking that both IV operands
2965  // are expressions that operate on the same unscaled SCEVUnknown. This
2966  // "base" will be canceled by the subsequent getMinusSCEV call. Checking
2967  // first avoids creating extra SCEV expressions.
2968  if (!StressIVChain && Chain.ExprBase != OperExprBase)
2969  continue;
2970 
2971  Value *PrevIV = getWideOperand(Chain.Incs.back().IVOperand);
2972  if (!isCompatibleIVType(PrevIV, NextIV))
2973  continue;
2974 
2975  // A phi node terminates a chain.
2976  if (isa<PHINode>(UserInst) && isa<PHINode>(Chain.tailUserInst()))
2977  continue;
2978 
2979  // The increment must be loop-invariant so it can be kept in a register.
2980  const SCEV *PrevExpr = SE.getSCEV(PrevIV);
2981  const SCEV *IncExpr = SE.getMinusSCEV(OperExpr, PrevExpr);
2982  if (isa<SCEVCouldNotCompute>(IncExpr) || !SE.isLoopInvariant(IncExpr, L))
2983  continue;
2984 
2985  if (Chain.isProfitableIncrement(OperExpr, IncExpr, SE)) {
2986  LastIncExpr = IncExpr;
2987  break;
2988  }
2989  }
2990  // If we haven't found a chain, create a new one, unless we hit the max. Don't
2991  // bother for phi nodes, because they must be last in the chain.
2992  if (ChainIdx == NChains) {
2993  if (isa<PHINode>(UserInst))
2994  return;
2995  if (NChains >= MaxChains && !StressIVChain) {
2996  LLVM_DEBUG(dbgs() << "IV Chain Limit\n");
2997  return;
2998  }
2999  LastIncExpr = OperExpr;
3000  // IVUsers may have skipped over sign/zero extensions. We don't currently
3001  // attempt to form chains involving extensions unless they can be hoisted
3002  // into this loop's AddRec.
3003  if (!isa<SCEVAddRecExpr>(LastIncExpr))
3004  return;
3005  ++NChains;
3006  IVChainVec.push_back(IVChain(IVInc(UserInst, IVOper, LastIncExpr),
3007  OperExprBase));
3008  ChainUsersVec.resize(NChains);
3009  LLVM_DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Head: (" << *UserInst
3010  << ") IV=" << *LastIncExpr << "\n");
3011  } else {
3012  LLVM_DEBUG(dbgs() << "IV Chain#" << ChainIdx << " Inc: (" << *UserInst
3013  << ") IV+" << *LastIncExpr << "\n");
3014  // Add this IV user to the end of the chain.
3015  IVChainVec[ChainIdx].add(IVInc(UserInst, IVOper, LastIncExpr));
3016  }
3017  IVChain &Chain = IVChainVec[ChainIdx];
3018 
3019  SmallPtrSet<Instruction*,4> &NearUsers = ChainUsersVec[ChainIdx].NearUsers;
3020  // This chain's NearUsers become FarUsers.
3021  if (!LastIncExpr->isZero()) {
3022  ChainUsersVec[ChainIdx].FarUsers.insert(NearUsers.begin(),
3023  NearUsers.end());
3024  NearUsers.clear();
3025  }
3026 
3027  // All other uses of IVOperand become near uses of the chain.
3028  // We currently ignore intermediate values within SCEV expressions, assuming
3029  // they will eventually be used be the current chain, or can be computed
3030  // from one of the chain increments. To be more precise we could
3031  // transitively follow its user and only add leaf IV users to the set.
3032  for (User *U : IVOper->users()) {
3033  Instruction *OtherUse = dyn_cast<Instruction>(U);
3034  if (!OtherUse)
3035  continue;
3036  // Uses in the chain will no longer be uses if the chain is formed.
3037  // Include the head of the chain in this iteration (not Chain.begin()).
3038  IVChain::const_iterator IncIter = Chain.Incs.begin();
3039  IVChain::const_iterator IncEnd = Chain.Incs.end();
3040  for( ; IncIter != IncEnd; ++IncIter) {
3041  if (IncIter->UserInst == OtherUse)
3042  break;
3043  }
3044  if (IncIter != IncEnd)
3045  continue;
3046 
3047  if (SE.isSCEVable(OtherUse->getType())
3048  && !isa<SCEVUnknown>(SE.getSCEV(OtherUse))
3049  && IU.isIVUserOrOperand(OtherUse)) {
3050  continue;
3051  }
3052  NearUsers.insert(OtherUse);
3053  }
3054 
3055  // Since this user is part of the chain, it's no longer considered a use
3056  // of the chain.
3057  ChainUsersVec[ChainIdx].FarUsers.erase(UserInst);
3058 }
3059 
3060 /// Populate the vector of Chains.
3061 ///
3062 /// This decreases ILP at the architecture level. Targets with ample registers,
3063 /// multiple memory ports, and no register renaming probably don't want
3064 /// this. However, such targets should probably disable LSR altogether.
3065 ///
3066 /// The job of LSR is to make a reasonable choice of induction variables across
3067 /// the loop. Subsequent passes can easily "unchain" computation exposing more
3068 /// ILP *within the loop* if the target wants it.
3069 ///
3070 /// Finding the best IV chain is potentially a scheduling problem. Since LSR
3071 /// will not reorder memory operations, it will recognize this as a chain, but
3072 /// will generate redundant IV increments. Ideally this would be corrected later
3073 /// by a smart scheduler:
3074 /// = A[i]
3075 /// = A[i+x]
3076 /// A[i] =
3077 /// A[i+x] =
3078 ///
3079 /// TODO: Walk the entire domtree within this loop, not just the path to the
3080 /// loop latch. This will discover chains on side paths, but requires
3081 /// maintaining multiple copies of the Chains state.
3082 void LSRInstance::CollectChains() {
3083  LLVM_DEBUG(dbgs() << "Collecting IV Chains.\n");
3084  SmallVector<ChainUsers, 8> ChainUsersVec;
3085 
3086  SmallVector<BasicBlock *,8> LatchPath;
3087  BasicBlock *LoopHeader = L->getHeader();
3088  for (DomTreeNode *Rung = DT.getNode(L->getLoopLatch());
3089  Rung->getBlock() != LoopHeader; Rung = Rung->getIDom()) {
3090  LatchPath.push_back(Rung->getBlock());
3091  }
3092  LatchPath.push_back(LoopHeader);
3093 
3094  // Walk the instruction stream from the loop header to the loop latch.
3095  for (BasicBlock *BB : reverse(LatchPath)) {
3096  for (Instruction &I : *BB) {
3097  // Skip instructions that weren't seen by IVUsers analysis.
3098  if (isa<PHINode>(I) || !IU.isIVUserOrOperand(&I))
3099  continue;
3100 
3101  // Ignore users that are part of a SCEV expression. This way we only
3102  // consider leaf IV Users. This effectively rediscovers a portion of
3103  // IVUsers analysis but in program order this time.
3104  if (SE.isSCEVable(I.getType()) && !isa<SCEVUnknown>(SE.getSCEV(&I)))
3105  continue;
3106 
3107  // Remove this instruction from any NearUsers set it may be in.
3108  for (unsigned ChainIdx = 0, NChains = IVChainVec.size();
3109  ChainIdx < NChains; ++ChainIdx) {
3110  ChainUsersVec[ChainIdx].NearUsers.erase(&I);
3111  }
3112  // Search for operands that can be chained.
3113  SmallPtrSet<Instruction*, 4> UniqueOperands;
3114  User::op_iterator IVOpEnd = I.op_end();
3115  User::op_iterator IVOpIter = findIVOperand(I.op_begin(), IVOpEnd, L, SE);
3116  while (IVOpIter != IVOpEnd) {
3117  Instruction *IVOpInst = cast<Instruction>(*IVOpIter);
3118  if (UniqueOperands.insert(IVOpInst).second)
3119  ChainInstruction(&I, IVOpInst, ChainUsersVec);
3120  IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3121  }
3122  } // Continue walking down the instructions.
3123  } // Continue walking down the domtree.
3124  // Visit phi backedges to determine if the chain can generate the IV postinc.
3125  for (PHINode &PN : L->getHeader()->phis()) {
3126  if (!SE.isSCEVable(PN.getType()))
3127  continue;
3128 
3129  Instruction *IncV =
3130  dyn_cast<Instruction>(PN.getIncomingValueForBlock(L->getLoopLatch()));
3131  if (IncV)
3132  ChainInstruction(&PN, IncV, ChainUsersVec);
3133  }
3134  // Remove any unprofitable chains.
3135  unsigned ChainIdx = 0;
3136  for (unsigned UsersIdx = 0, NChains = IVChainVec.size();
3137  UsersIdx < NChains; ++UsersIdx) {
3138  if (!isProfitableChain(IVChainVec[UsersIdx],
3139  ChainUsersVec[UsersIdx].FarUsers, SE, TTI))
3140  continue;
3141  // Preserve the chain at UsesIdx.
3142  if (ChainIdx != UsersIdx)
3143  IVChainVec[ChainIdx] = IVChainVec[UsersIdx];
3144  FinalizeChain(IVChainVec[ChainIdx]);
3145  ++ChainIdx;
3146  }
3147  IVChainVec.resize(ChainIdx);
3148 }
3149 
3150 void LSRInstance::FinalizeChain(IVChain &Chain) {
3151  assert(!Chain.Incs.empty() && "empty IV chains are not allowed");
3152  LLVM_DEBUG(dbgs() << "Final Chain: " << *Chain.Incs[0].UserInst << "\n");
3153 
3154  for (const IVInc &Inc : Chain) {
3155  LLVM_DEBUG(dbgs() << " Inc: " << *Inc.UserInst << "\n");
3156  auto UseI = find(Inc.UserInst->operands(), Inc.IVOperand);
3157  assert(UseI != Inc.UserInst->op_end() && "cannot find IV operand");
3158  IVIncSet.insert(UseI);
3159  }
3160 }
3161 
3162 /// Return true if the IVInc can be folded into an addressing mode.
3163 static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
3164  Value *Operand, const TargetTransformInfo &TTI) {
3165  const SCEVConstant *IncConst = dyn_cast<SCEVConstant>(IncExpr);
3166  if (!IncConst || !isAddressUse(TTI, UserInst, Operand))
3167  return false;
3168 
3169  if (IncConst->getAPInt().getMinSignedBits() > 64)
3170  return false;
3171 
3172  MemAccessTy AccessTy = getAccessType(TTI, UserInst, Operand);
3173  int64_t IncOffset = IncConst->getValue()->getSExtValue();
3174  if (!isAlwaysFoldable(TTI, LSRUse::Address, AccessTy, /*BaseGV=*/nullptr,
3175  IncOffset, /*HasBaseReg=*/false))
3176  return false;
3177 
3178  return true;
3179 }
3180 
3181 /// Generate an add or subtract for each IVInc in a chain to materialize the IV
3182 /// user's operand from the previous IV user's operand.
3183 void LSRInstance::GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
3184  SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
3185  // Find the new IVOperand for the head of the chain. It may have been replaced
3186  // by LSR.
3187  const IVInc &Head = Chain.Incs[0];
3188  User::op_iterator IVOpEnd = Head.UserInst->op_end();
3189  // findIVOperand returns IVOpEnd if it can no longer find a valid IV user.
3190  User::op_iterator IVOpIter = findIVOperand(Head.UserInst->op_begin(),
3191  IVOpEnd, L, SE);
3192  Value *IVSrc = nullptr;
3193  while (IVOpIter != IVOpEnd) {
3194  IVSrc = getWideOperand(*IVOpIter);
3195 
3196  // If this operand computes the expression that the chain needs, we may use
3197  // it. (Check this after setting IVSrc which is used below.)
3198  //
3199  // Note that if Head.IncExpr is wider than IVSrc, then this phi is too
3200  // narrow for the chain, so we can no longer use it. We do allow using a
3201  // wider phi, assuming the LSR checked for free truncation. In that case we
3202  // should already have a truncate on this operand such that
3203  // getSCEV(IVSrc) == IncExpr.
3204  if (SE.getSCEV(*IVOpIter) == Head.IncExpr
3205  || SE.getSCEV(IVSrc) == Head.IncExpr) {
3206  break;
3207  }
3208  IVOpIter = findIVOperand(std::next(IVOpIter), IVOpEnd, L, SE);
3209  }
3210  if (IVOpIter == IVOpEnd) {
3211  // Gracefully give up on this chain.
3212  LLVM_DEBUG(dbgs() << "Concealed chain head: " << *Head.UserInst << "\n");
3213  return;
3214  }
3215  assert(IVSrc && "Failed to find IV chain source");
3216 
3217  LLVM_DEBUG(dbgs() << "Generate chain at: " << *IVSrc << "\n");
3218  Type *IVTy = IVSrc->getType();
3219  Type *IntTy = SE.getEffectiveSCEVType(IVTy);
3220  const SCEV *LeftOverExpr = nullptr;
3221  for (const IVInc &Inc : Chain) {
3222  Instruction *InsertPt = Inc.UserInst;
3223  if (isa<PHINode>(InsertPt))
3224  InsertPt = L->getLoopLatch()->getTerminator();
3225 
3226  // IVOper will replace the current IV User's operand. IVSrc is the IV
3227  // value currently held in a register.
3228  Value *IVOper = IVSrc;
3229  if (!Inc.IncExpr->isZero()) {
3230  // IncExpr was the result of subtraction of two narrow values, so must
3231  // be signed.
3232  const SCEV *IncExpr = SE.getNoopOrSignExtend(Inc.IncExpr, IntTy);
3233  LeftOverExpr = LeftOverExpr ?
3234  SE.getAddExpr(LeftOverExpr, IncExpr) : IncExpr;
3235  }
3236  if (LeftOverExpr && !LeftOverExpr->isZero()) {
3237  // Expand the IV increment.
3238  Rewriter.clearPostInc();
3239  Value *IncV = Rewriter.expandCodeFor(LeftOverExpr, IntTy, InsertPt);
3240  const SCEV *IVOperExpr = SE.getAddExpr(SE.getUnknown(IVSrc),
3241  SE.getUnknown(IncV));
3242  IVOper = Rewriter.expandCodeFor(IVOperExpr, IVTy, InsertPt);
3243 
3244  // If an IV increment can't be folded, use it as the next IV value.
3245  if (!canFoldIVIncExpr(LeftOverExpr, Inc.UserInst, Inc.IVOperand, TTI)) {
3246  assert(IVTy == IVOper->getType() && "inconsistent IV increment type");
3247  IVSrc = IVOper;
3248  LeftOverExpr = nullptr;
3249  }
3250  }
3251  Type *OperTy = Inc.IVOperand->getType();
3252  if (IVTy != OperTy) {
3253  assert(SE.getTypeSizeInBits(IVTy) >= SE.getTypeSizeInBits(OperTy) &&
3254  "cannot extend a chained IV");
3255  IRBuilder<> Builder(InsertPt);
3256  IVOper = Builder.CreateTruncOrBitCast(IVOper, OperTy, "lsr.chain");
3257  }
3258  Inc.UserInst->replaceUsesOfWith(Inc.IVOperand, IVOper);
3259  if (auto *OperandIsInstr = dyn_cast<Instruction>(Inc.IVOperand))
3260  DeadInsts.emplace_back(OperandIsInstr);
3261  }
3262  // If LSR created a new, wider phi, we may also replace its postinc. We only
3263  // do this if we also found a wide value for the head of the chain.
3264  if (isa<PHINode>(Chain.tailUserInst())) {
3265  for (PHINode &Phi : L->getHeader()->phis()) {
3266  if (!isCompatibleIVType(&Phi, IVSrc))
3267  continue;
3268  Instruction *PostIncV = dyn_cast<Instruction>(
3269  Phi.getIncomingValueForBlock(L->getLoopLatch()));
3270  if (!PostIncV || (SE.getSCEV(PostIncV) != SE.getSCEV(IVSrc)))
3271  continue;
3272  Value *IVOper = IVSrc;
3273  Type *PostIncTy = PostIncV->getType();
3274  if (IVTy != PostIncTy) {
3275  assert(PostIncTy->isPointerTy() && "mixing int/ptr IV types");
3277  Builder.SetCurrentDebugLocation(PostIncV->getDebugLoc());
3278  IVOper = Builder.CreatePointerCast(IVSrc, PostIncTy, "lsr.chain");
3279  }
3280  Phi.replaceUsesOfWith(PostIncV, IVOper);
3281  DeadInsts.emplace_back(PostIncV);
3282  }
3283  }
3284 }
3285 
3286 void LSRInstance::CollectFixupsAndInitialFormulae() {
3287  BranchInst *ExitBranch = nullptr;
3288  bool SaveCmp = TTI.canSaveCmp(L, &ExitBranch, &SE, &LI, &DT, &AC, &TLI);
3289 
3290  for (const IVStrideUse &U : IU) {
3291  Instruction *UserInst = U.getUser();
3292  // Skip IV users that are part of profitable IV Chains.
3293  User::op_iterator UseI =
3294  find(UserInst->operands(), U.getOperandValToReplace());
3295  assert(UseI != UserInst->op_end() && "cannot find IV operand");
3296  if (IVIncSet.count(UseI)) {
3297  LLVM_DEBUG(dbgs() << "Use is in profitable chain: " << **UseI << '\n');
3298  continue;
3299  }
3300 
3301  LSRUse::KindType Kind = LSRUse::Basic;
3302  MemAccessTy AccessTy;
3303  if (isAddressUse(TTI, UserInst, U.getOperandValToReplace())) {
3304  Kind = LSRUse::Address;
3305  AccessTy = getAccessType(TTI, UserInst, U.getOperandValToReplace());
3306  }
3307 
3308  const SCEV *S = IU.getExpr(U);
3309  PostIncLoopSet TmpPostIncLoops = U.getPostIncLoops();
3310 
3311  // Equality (== and !=) ICmps are special. We can rewrite (i == N) as
3312  // (N - i == 0), and this allows (N - i) to be the expression that we work
3313  // with rather than just N or i, so we can consider the register
3314  // requirements for both N and i at the same time. Limiting this code to
3315  // equality icmps is not a problem because all interesting loops use
3316  // equality icmps, thanks to IndVarSimplify.
3317  if (ICmpInst *CI = dyn_cast<ICmpInst>(UserInst)) {
3318  // If CI can be saved in some target, like replaced inside hardware loop
3319  // in PowerPC, no need to generate initial formulae for it.
3320  if (SaveCmp && CI == dyn_cast<ICmpInst>(ExitBranch->getCondition()))
3321  continue;
3322  if (CI->isEquality()) {
3323  // Swap the operands if needed to put the OperandValToReplace on the
3324  // left, for consistency.
3325  Value *NV = CI->getOperand(1);
3326  if (NV == U.getOperandValToReplace()) {
3327  CI->setOperand(1, CI->getOperand(0));
3328  CI->setOperand(0, NV);
3329  NV = CI->getOperand(1);
3330  Changed = true;
3331  }
3332 
3333  // x == y --> x - y == 0
3334  const SCEV *N = SE.getSCEV(NV);
3335  if (SE.isLoopInvariant(N, L) && isSafeToExpand(N, SE) &&
3336  (!NV->getType()->isPointerTy() ||
3337  SE.getPointerBase(N) == SE.getPointerBase(S))) {
3338  // S is normalized, so normalize N before folding it into S
3339  // to keep the result normalized.
3340  N = normalizeForPostIncUse(N, TmpPostIncLoops, SE);
3341  Kind = LSRUse::ICmpZero;
3342  S = SE.getMinusSCEV(N, S);
3343  }
3344 
3345  // -1 and the negations of all interesting strides (except the negation
3346  // of -1) are now also interesting.
3347  for (size_t i = 0, e = Factors.size(); i != e; ++i)
3348  if (Factors[i] != -1)
3349  Factors.insert(-(uint64_t)Factors[i]);
3350  Factors.insert(-1);
3351  }
3352  }
3353 
3354  // Get or create an LSRUse.
3355  std::pair<size_t, int64_t> P = getUse(S, Kind, AccessTy);
3356  size_t LUIdx = P.first;
3357  int64_t Offset = P.second;
3358  LSRUse &LU = Uses[LUIdx];
3359 
3360  // Record the fixup.
3361  LSRFixup &LF = LU.getNewFixup();
3362  LF.UserInst = UserInst;
3363  LF.OperandValToReplace = U.getOperandValToReplace();
3364  LF.PostIncLoops = TmpPostIncLoops;
3365  LF.Offset = Offset;
3366  LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3367 
3368  if (!LU.WidestFixupType ||
3369  SE.getTypeSizeInBits(LU.WidestFixupType) <
3370  SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
3371  LU.WidestFixupType = LF.OperandValToReplace->getType();
3372 
3373  // If this is the first use of this LSRUse, give it a formula.
3374  if (LU.Formulae.empty()) {
3375  InsertInitialFormula(S, LU, LUIdx);
3376  CountRegisters(LU.Formulae.back(), LUIdx);
3377  }
3378  }
3379 
3380  LLVM_DEBUG(print_fixups(dbgs()));
3381 }
3382 
3383 /// Insert a formula for the given expression into the given use, separating out
3384 /// loop-variant portions from loop-invariant and loop-computable portions.
3385 void
3386 LSRInstance::InsertInitialFormula(const SCEV *S, LSRUse &LU, size_t LUIdx) {
3387  // Mark uses whose expressions cannot be expanded.
3388  if (!isSafeToExpand(S, SE))
3389  LU.RigidFormula = true;
3390 
3391  Formula F;
3392  F.initialMatch(S, L, SE);
3393  bool Inserted = InsertFormula(LU, LUIdx, F);
3394  assert(Inserted && "Initial formula already exists!"); (void)Inserted;
3395 }
3396 
3397 /// Insert a simple single-register formula for the given expression into the
3398 /// given use.
3399 void
3400 LSRInstance::InsertSupplementalFormula(const SCEV *S,
3401  LSRUse &LU, size_t LUIdx) {
3402  Formula F;
3403  F.BaseRegs.push_back(S);
3404  F.HasBaseReg = true;
3405  bool Inserted = InsertFormula(LU, LUIdx, F);
3406  assert(Inserted && "Supplemental formula already exists!"); (void)Inserted;
3407 }
3408 
3409 /// Note which registers are used by the given formula, updating RegUses.
3410 void LSRInstance::CountRegisters(const Formula &F, size_t LUIdx) {
3411  if (F.ScaledReg)
3412  RegUses.countRegister(F.ScaledReg, LUIdx);
3413  for (const SCEV *BaseReg : F.BaseRegs)
3414  RegUses.countRegister(BaseReg, LUIdx);
3415 }
3416 
3417 /// If the given formula has not yet been inserted, add it to the list, and
3418 /// return true. Return false otherwise.
3419 bool LSRInstance::InsertFormula(LSRUse &LU, unsigned LUIdx, const Formula &F) {
3420  // Do not insert formula that we will not be able to expand.
3421  assert(isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F) &&
3422  "Formula is illegal");
3423 
3424  if (!LU.InsertFormula(F, *L))
3425  return false;
3426 
3427  CountRegisters(F, LUIdx);
3428  return true;
3429 }
3430 
3431 /// Check for other uses of loop-invariant values which we're tracking. These
3432 /// other uses will pin these values in registers, making them less profitable
3433 /// for elimination.
3434 /// TODO: This currently misses non-constant addrec step registers.
3435 /// TODO: Should this give more weight to users inside the loop?
3436 void
3437 LSRInstance::CollectLoopInvariantFixupsAndFormulae() {
3438  SmallVector<const SCEV *, 8> Worklist(RegUses.begin(), RegUses.end());
3440 
3441  while (!Worklist.empty()) {
3442  const SCEV *S = Worklist.pop_back_val();
3443 
3444  // Don't process the same SCEV twice
3445  if (!Visited.insert(S).second)
3446  continue;
3447 
3448  if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S))
3449  Worklist.append(N->op_begin(), N->op_end());
3450  else if (const SCEVIntegralCastExpr *C = dyn_cast<SCEVIntegralCastExpr>(S))
3451  Worklist.push_back(C->getOperand());
3452  else if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
3453  Worklist.push_back(D->getLHS());
3454  Worklist.push_back(D->getRHS());
3455  } else if (const SCEVUnknown *US = dyn_cast<SCEVUnknown>(S)) {
3456  const Value *V = US->getValue();
3457  if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
3458  // Look for instructions defined outside the loop.
3459  if (L->contains(Inst)) continue;
3460  } else if (isa<UndefValue>(V))
3461  // Undef doesn't have a live range, so it doesn't matter.
3462  continue;
3463  for (const Use &U : V->uses()) {
3464  const Instruction *UserInst = dyn_cast<Instruction>(U.getUser());
3465  // Ignore non-instructions.
3466  if (!UserInst)
3467  continue;
3468  // Don't bother if the instruction is an EHPad.
3469  if (UserInst->isEHPad())
3470  continue;
3471  // Ignore instructions in other functions (as can happen with
3472  // Constants).
3473  if (UserInst->getParent()->getParent() != L->getHeader()->getParent())
3474  continue;
3475  // Ignore instructions not dominated by the loop.
3476  const BasicBlock *UseBB = !isa<PHINode>(UserInst) ?
3477  UserInst->getParent() :
3478  cast<PHINode>(UserInst)->getIncomingBlock(
3479  PHINode::getIncomingValueNumForOperand(U.getOperandNo()));
3480  if (!DT.dominates(L->getHeader(), UseBB))
3481  continue;
3482  // Don't bother if the instruction is in a BB which ends in an EHPad.
3483  if (UseBB->getTerminator()->isEHPad())
3484  continue;
3485  // Don't bother rewriting PHIs in catchswitch blocks.
3486  if (isa<CatchSwitchInst>(UserInst->getParent()->getTerminator()))
3487  continue;
3488  // Ignore uses which are part of other SCEV expressions, to avoid
3489  // analyzing them multiple times.
3490  if (SE.isSCEVable(UserInst->getType())) {
3491  const SCEV *UserS = SE.getSCEV(const_cast<Instruction *>(UserInst));
3492  // If the user is a no-op, look through to its uses.
3493  if (!isa<SCEVUnknown>(UserS))
3494  continue;
3495  if (UserS == US) {
3496  Worklist.push_back(
3497  SE.getUnknown(const_cast<Instruction *>(UserInst)));
3498  continue;
3499  }
3500  }
3501  // Ignore icmp instructions which are already being analyzed.
3502  if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UserInst)) {
3503  unsigned OtherIdx = !U.getOperandNo();
3504  Value *OtherOp = const_cast<Value *>(ICI->getOperand(OtherIdx));
3505  if (SE.hasComputableLoopEvolution(SE.getSCEV(OtherOp), L))
3506  continue;
3507  }
3508 
3509  std::pair<size_t, int64_t> P = getUse(
3510  S, LSRUse::Basic, MemAccessTy());
3511  size_t LUIdx = P.first;
3512  int64_t Offset = P.second;
3513  LSRUse &LU = Uses[LUIdx];
3514  LSRFixup &LF = LU.getNewFixup();
3515  LF.UserInst = const_cast<Instruction *>(UserInst);
3516  LF.OperandValToReplace = U;
3517  LF.Offset = Offset;
3518  LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L);
3519  if (!LU.WidestFixupType ||
3520  SE.getTypeSizeInBits(LU.WidestFixupType) <
3521  SE.getTypeSizeInBits(LF.OperandValToReplace->getType()))
3522  LU.WidestFixupType = LF.OperandValToReplace->getType();
3523  InsertSupplementalFormula(US, LU, LUIdx);
3524  CountRegisters(LU.Formulae.back(), Uses.size() - 1);
3525  break;
3526  }
3527  }
3528  }
3529 }
3530 
3531 /// Split S into subexpressions which can be pulled out into separate
3532 /// registers. If C is non-null, multiply each subexpression by C.
3533 ///
3534 /// Return remainder expression after factoring the subexpressions captured by
3535 /// Ops. If Ops is complete, return NULL.
3536 static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3538  const Loop *L,
3539  ScalarEvolution &SE,
3540  unsigned Depth = 0) {
3541  // Arbitrarily cap recursion to protect compile time.
3542  if (Depth >= 3)
3543  return S;
3544 
3545  if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3546  // Break out add operands.
3547  for (const SCEV *S : Add->operands()) {
3548  const SCEV *Remainder = CollectSubexprs(S, C, Ops, L, SE, Depth+1);
3549  if (Remainder)
3550  Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3551  }
3552  return nullptr;
3553  } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
3554  // Split a non-zero base out of an addrec.
3555  if (AR->getStart()->isZero() || !AR->isAffine())
3556  return S;
3557 
3558  const SCEV *Remainder = CollectSubexprs(AR->getStart(),
3559  C, Ops, L, SE, Depth+1);
3560  // Split the non-zero AddRec unless it is part of a nested recurrence that
3561  // does not pertain to this loop.
3562  if (Remainder && (AR->getLoop() == L || !isa<SCEVAddRecExpr>(Remainder))) {
3563  Ops.push_back(C ? SE.getMulExpr(C, Remainder) : Remainder);
3564  Remainder = nullptr;
3565  }
3566  if (Remainder != AR->getStart()) {
3567  if (!Remainder)
3568  Remainder = SE.getConstant(AR->getType(), 0);
3569  return SE.getAddRecExpr(Remainder,
3570  AR->getStepRecurrence(SE),
3571  AR->getLoop(),
3572  //FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
3574  }
3575  } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3576  // Break (C * (a + b + c)) into C*a + C*b + C*c.
3577  if (Mul->getNumOperands() != 2)
3578  return S;
3579  if (const SCEVConstant *Op0 =
3580  dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3581  C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3582  const SCEV *Remainder =
3583  CollectSubexprs(Mul->getOperand(1), C, Ops, L, SE, Depth+1);
3584  if (Remainder)
3585  Ops.push_back(SE.getMulExpr(C, Remainder));
3586  return nullptr;
3587  }
3588  }
3589  return S;
3590 }
3591 
3592 /// Return true if the SCEV represents a value that may end up as a
3593 /// post-increment operation.
3595  LSRUse &LU, const SCEV *S, const Loop *L,
3596  ScalarEvolution &SE) {
3597  if (LU.Kind != LSRUse::Address ||
3598  !LU.AccessTy.getType()->isIntOrIntVectorTy())
3599  return false;
3600  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
3601  if (!AR)
3602  return false;
3603  const SCEV *LoopStep = AR->getStepRecurrence(SE);
3604  if (!isa<SCEVConstant>(LoopStep))
3605  return false;
3606  // Check if a post-indexed load/store can be used.
3609  const SCEV *LoopStart = AR->getStart();
3610  if (!isa<SCEVConstant>(LoopStart) && SE.isLoopInvariant(LoopStart, L))
3611  return true;
3612  }
3613  return false;
3614 }
3615 
3616 /// Helper function for LSRInstance::GenerateReassociations.
3617 void LSRInstance::GenerateReassociationsImpl(LSRUse &LU, unsigned LUIdx,
3618  const Formula &Base,
3619  unsigned Depth, size_t Idx,
3620  bool IsScaledReg) {
3621  const SCEV *BaseReg = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
3622  // Don't generate reassociations for the base register of a value that
3623  // may generate a post-increment operator. The reason is that the
3624  // reassociations cause extra base+register formula to be created,
3625  // and possibly chosen, but the post-increment is more efficient.
3626  if (AMK == TTI::AMK_PostIndexed && mayUsePostIncMode(TTI, LU, BaseReg, L, SE))
3627  return;
3629  const SCEV *Remainder = CollectSubexprs(BaseReg, nullptr, AddOps, L, SE);
3630  if (Remainder)
3631  AddOps.push_back(Remainder);
3632 
3633  if (AddOps.size() == 1)
3634  return;
3635 
3636  for (SmallVectorImpl<const SCEV *>::const_iterator J = AddOps.begin(),
3637  JE = AddOps.end();
3638  J != JE; ++J) {
3639  // Loop-variant "unknown" values are uninteresting; we won't be able to
3640  // do anything meaningful with them.
3641  if (isa<SCEVUnknown>(*J) && !SE.isLoopInvariant(*J, L))
3642  continue;
3643 
3644  // Don't pull a constant into a register if the constant could be folded
3645  // into an immediate field.
3646  if (isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3647  LU.AccessTy, *J, Base.getNumRegs() > 1))
3648  continue;
3649 
3650  // Collect all operands except *J.
3651  SmallVector<const SCEV *, 8> InnerAddOps(
3652  ((const SmallVector<const SCEV *, 8> &)AddOps).begin(), J);
3653  InnerAddOps.append(std::next(J),
3654  ((const SmallVector<const SCEV *, 8> &)AddOps).end());
3655 
3656  // Don't leave just a constant behind in a register if the constant could
3657  // be folded into an immediate field.
3658  if (InnerAddOps.size() == 1 &&
3659  isAlwaysFoldable(TTI, SE, LU.MinOffset, LU.MaxOffset, LU.Kind,
3660  LU.AccessTy, InnerAddOps[0], Base.getNumRegs() > 1))
3661  continue;
3662 
3663  const SCEV *InnerSum = SE.getAddExpr(InnerAddOps);
3664  if (InnerSum->isZero())
3665  continue;
3666  Formula F = Base;
3667 
3668  // Add the remaining pieces of the add back into the new formula.
3669  const SCEVConstant *InnerSumSC = dyn_cast<SCEVConstant>(InnerSum);
3670  if (InnerSumSC && SE.getTypeSizeInBits(InnerSumSC->getType()) <= 64 &&
3671  TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3672  InnerSumSC->getValue()->getZExtValue())) {
3673  F.UnfoldedOffset =
3674  (uint64_t)F.UnfoldedOffset + InnerSumSC->getValue()->getZExtValue();
3675  if (IsScaledReg)
3676  F.ScaledReg = nullptr;
3677  else
3678  F.BaseRegs.erase(F.BaseRegs.begin() + Idx);
3679  } else if (IsScaledReg)
3680  F.ScaledReg = InnerSum;
3681  else
3682  F.BaseRegs[Idx] = InnerSum;
3683 
3684  // Add J as its own register, or an unfolded immediate.
3685  const SCEVConstant *SC = dyn_cast<SCEVConstant>(*J);
3686  if (SC && SE.getTypeSizeInBits(SC->getType()) <= 64 &&
3687  TTI.isLegalAddImmediate((uint64_t)F.UnfoldedOffset +
3688  SC->getValue()->getZExtValue()))
3689  F.UnfoldedOffset =
3690  (uint64_t)F.UnfoldedOffset + SC->getValue()->getZExtValue();
3691  else
3692  F.BaseRegs.push_back(*J);
3693  // We may have changed the number of register in base regs, adjust the
3694  // formula accordingly.
3695  F.canonicalize(*L);
3696 
3697  if (InsertFormula(LU, LUIdx, F))
3698  // If that formula hadn't been seen before, recurse to find more like
3699  // it.
3700  // Add check on Log16(AddOps.size()) - same as Log2_32(AddOps.size()) >> 2)
3701  // Because just Depth is not enough to bound compile time.
3702  // This means that every time AddOps.size() is greater 16^x we will add
3703  // x to Depth.
3704  GenerateReassociations(LU, LUIdx, LU.Formulae.back(),
3705  Depth + 1 + (Log2_32(AddOps.size()) >> 2));
3706  }
3707 }
3708 
3709 /// Split out subexpressions from adds and the bases of addrecs.
3710 void LSRInstance::GenerateReassociations(LSRUse &LU, unsigned LUIdx,
3711  Formula Base, unsigned Depth) {
3712  assert(Base.isCanonical(*L) && "Input must be in the canonical form");
3713  // Arbitrarily cap recursion to protect compile time.
3714  if (Depth >= 3)
3715  return;
3716 
3717  for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
3718  GenerateReassociationsImpl(LU, LUIdx, Base, Depth, i);
3719 
3720  if (Base.Scale == 1)
3721  GenerateReassociationsImpl(LU, LUIdx, Base, Depth,
3722  /* Idx */ -1, /* IsScaledReg */ true);
3723 }
3724 
3725 /// Generate a formula consisting of all of the loop-dominating registers added
3726 /// into a single register.
3727 void LSRInstance::GenerateCombinations(LSRUse &LU, unsigned LUIdx,
3728  Formula Base) {
3729  // This method is only interesting on a plurality of registers.
3730  if (Base.BaseRegs.size() + (Base.Scale == 1) +
3731  (Base.UnfoldedOffset != 0) <= 1)
3732  return;
3733 
3734  // Flatten the representation, i.e., reg1 + 1*reg2 => reg1 + reg2, before
3735  // processing the formula.
3736  Base.unscale();
3738  Formula NewBase = Base;
3739  NewBase.BaseRegs.clear();
3740  Type *CombinedIntegerType = nullptr;
3741  for (const SCEV *BaseReg : Base.BaseRegs) {
3742  if (SE.properlyDominates(BaseReg, L->getHeader()) &&
3743  !SE.hasComputableLoopEvolution(BaseReg, L)) {
3744  if (!CombinedIntegerType)
3745  CombinedIntegerType = SE.getEffectiveSCEVType(BaseReg->getType());
3746  Ops.push_back(BaseReg);
3747  }
3748  else
3749  NewBase.BaseRegs.push_back(BaseReg);
3750  }
3751 
3752  // If no register is relevant, we're done.
3753  if (Ops.size() == 0)
3754  return;
3755 
3756  // Utility function for generating the required variants of the combined
3757  // registers.
3758  auto GenerateFormula = [&](const SCEV *Sum) {
3759  Formula F = NewBase;
3760 
3761  // TODO: If Sum is zero, it probably means ScalarEvolution missed an
3762  // opportunity to fold something. For now, just ignore such cases
3763  // rather than proceed with zero in a register.
3764  if (Sum->isZero())
3765  return;
3766 
3767  F.BaseRegs.push_back(Sum);
3768  F.canonicalize(*L);
3769  (void)InsertFormula(LU, LUIdx, F);
3770  };
3771 
3772  // If we collected at least two registers, generate a formula combining them.
3773  if (Ops.size() > 1) {
3774  SmallVector<const SCEV *, 4> OpsCopy(Ops); // Don't let SE modify Ops.
3775  GenerateFormula(SE.getAddExpr(OpsCopy));
3776  }
3777 
3778  // If we have an unfolded offset, generate a formula combining it with the
3779  // registers collected.
3780  if (NewBase.UnfoldedOffset) {
3781  assert(CombinedIntegerType && "Missing a type for the unfolded offset");
3782  Ops.push_back(SE.getConstant(CombinedIntegerType, NewBase.UnfoldedOffset,
3783  true));
3784  NewBase.UnfoldedOffset = 0;
3785  GenerateFormula(SE.getAddExpr(Ops));
3786  }
3787 }
3788 
3789 /// Helper function for LSRInstance::GenerateSymbolicOffsets.
3790 void LSRInstance::GenerateSymbolicOffsetsImpl(LSRUse &LU, unsigned LUIdx,
3791  const Formula &Base, size_t Idx,
3792  bool IsScaledReg) {
3793  const SCEV *G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
3794  GlobalValue *GV = ExtractSymbol(G, SE);
3795  if (G->isZero() || !GV)
3796  return;
3797  Formula F = Base;
3798  F.BaseGV = GV;
3799  if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
3800  return;
3801  if (IsScaledReg)
3802  F.ScaledReg = G;
3803  else
3804  F.BaseRegs[Idx] = G;
3805  (void)InsertFormula(LU, LUIdx, F);
3806 }
3807 
3808 /// Generate reuse formulae using symbolic offsets.
3809 void LSRInstance::GenerateSymbolicOffsets(LSRUse &LU, unsigned LUIdx,
3810  Formula Base) {
3811  // We can't add a symbolic offset if the address already contains one.
3812  if (Base.BaseGV) return;
3813 
3814  for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
3815  GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, i);
3816  if (Base.Scale == 1)
3817  GenerateSymbolicOffsetsImpl(LU, LUIdx, Base, /* Idx */ -1,
3818  /* IsScaledReg */ true);
3819 }
3820 
3821 /// Helper function for LSRInstance::GenerateConstantOffsets.
3822 void LSRInstance::GenerateConstantOffsetsImpl(
3823  LSRUse &LU, unsigned LUIdx, const Formula &Base,
3824  const SmallVectorImpl<int64_t> &Worklist, size_t Idx, bool IsScaledReg) {
3825 
3826  auto GenerateOffset = [&](const SCEV *G, int64_t Offset) {
3827  Formula F = Base;
3828  F.BaseOffset = (uint64_t)Base.BaseOffset - Offset;
3829 
3830  if (isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F)) {
3831  // Add the offset to the base register.
3832  const SCEV *NewG = SE.getAddExpr(SE.getConstant(G->getType(), Offset), G);
3833  // If it cancelled out, drop the base register, otherwise update it.
3834  if (NewG->isZero()) {
3835  if (IsScaledReg) {
3836  F.Scale = 0;
3837  F.ScaledReg = nullptr;
3838  } else
3839  F.deleteBaseReg(F.BaseRegs[Idx]);
3840  F.canonicalize(*L);
3841  } else if (IsScaledReg)
3842  F.ScaledReg = NewG;
3843  else
3844  F.BaseRegs[Idx] = NewG;
3845 
3846  (void)InsertFormula(LU, LUIdx, F);
3847  }
3848  };
3849 
3850  const SCEV *G = IsScaledReg ? Base.ScaledReg : Base.BaseRegs[Idx];
3851 
3852  // With constant offsets and constant steps, we can generate pre-inc
3853  // accesses by having the offset equal the step. So, for access #0 with a
3854  // step of 8, we generate a G - 8 base which would require the first access
3855  // to be ((G - 8) + 8),+,8. The pre-indexed access then updates the pointer
3856  // for itself and hopefully becomes the base for other accesses. This means
3857  // means that a single pre-indexed access can be generated to become the new
3858  // base pointer for each iteration of the loop, resulting in no extra add/sub
3859  // instructions for pointer updating.
3860  if (AMK == TTI::AMK_PreIndexed && LU.Kind == LSRUse::Address) {
3861  if (auto *GAR = dyn_cast<SCEVAddRecExpr>(G)) {
3862  if (auto *StepRec =
3863  dyn_cast<SCEVConstant>(GAR->getStepRecurrence(SE))) {
3864  const APInt &StepInt = StepRec->getAPInt();
3865  int64_t Step = StepInt.isNegative() ?
3866  StepInt.getSExtValue() : StepInt.getZExtValue();
3867 
3868  for (int64_t Offset : Worklist) {
3869  Offset -= Step;
3870  GenerateOffset(G, Offset);
3871  }
3872  }
3873  }
3874  }
3875  for (int64_t Offset : Worklist)
3876  GenerateOffset(G, Offset);
3877 
3878  int64_t Imm = ExtractImmediate(G, SE);
3879  if (G->isZero() || Imm == 0)
3880  return;
3881  Formula F = Base;
3882  F.BaseOffset = (uint64_t)F.BaseOffset + Imm;
3883  if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, F))
3884  return;
3885  if (IsScaledReg) {
3886  F.ScaledReg = G;
3887  } else {
3888  F.BaseRegs[Idx] = G;
3889  // We may generate non canonical Formula if G is a recurrent expr reg
3890  // related with current loop while F.ScaledReg is not.
3891  F.canonicalize(*L);
3892  }
3893  (void)InsertFormula(LU, LUIdx, F);
3894 }
3895 
3896 /// GenerateConstantOffsets - Generate reuse formulae using symbolic offsets.
3897 void LSRInstance::GenerateConstantOffsets(LSRUse &LU, unsigned LUIdx,
3898  Formula Base) {
3899  // TODO: For now, just add the min and max offset, because it usually isn't
3900  // worthwhile looking at everything inbetween.
3901  SmallVector<int64_t, 2> Worklist;
3902  Worklist.push_back(LU.MinOffset);
3903  if (LU.MaxOffset != LU.MinOffset)
3904  Worklist.push_back(LU.MaxOffset);
3905 
3906  for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i)
3907  GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, i);
3908  if (Base.Scale == 1)
3909  GenerateConstantOffsetsImpl(LU, LUIdx, Base, Worklist, /* Idx */ -1,
3910  /* IsScaledReg */ true);
3911 }
3912 
3913 /// For ICmpZero, check to see if we can scale up the comparison. For example, x
3914 /// == y -> x*c == y*c.
3915 void LSRInstance::GenerateICmpZeroScales(LSRUse &LU, unsigned LUIdx,
3916  Formula Base) {
3917  if (LU.Kind != LSRUse::ICmpZero) return;
3918 
3919  // Determine the integer type for the base formula.
3920  Type *IntTy = Base.getType();
3921  if (!IntTy) return;
3922  if (SE.getTypeSizeInBits(IntTy) > 64) return;
3923 
3924  // Don't do this if there is more than one offset.
3925  if (LU.MinOffset != LU.MaxOffset) return;
3926 
3927  // Check if transformation is valid. It is illegal to multiply pointer.
3928  if (Base.ScaledReg && Base.ScaledReg->getType()->isPointerTy())
3929  return;
3930  for (const SCEV *BaseReg : Base.BaseRegs)
3931  if (BaseReg->getType()->isPointerTy())
3932  return;
3933  assert(!Base.BaseGV && "ICmpZero use is not legal!");
3934 
3935  // Check each interesting stride.
3936  for (int64_t Factor : Factors) {
3937  // Check that Factor can be represented by IntTy
3938  if (!ConstantInt::isValueValidForType(IntTy, Factor))
3939  continue;
3940  // Check that the multiplication doesn't overflow.
3941  if (Base.BaseOffset == std::numeric_limits<int64_t>::min() && Factor == -1)
3942  continue;
3943  int64_t NewBaseOffset = (uint64_t)Base.BaseOffset * Factor;
3944  assert(Factor != 0 && "Zero factor not expected!");
3945  if (NewBaseOffset / Factor != Base.BaseOffset)
3946  continue;
3947  // If the offset will be truncated at this use, check that it is in bounds.
3948  if (!IntTy->isPointerTy() &&
3949  !ConstantInt::isValueValidForType(IntTy, NewBaseOffset))
3950  continue;
3951 
3952  // Check that multiplying with the use offset doesn't overflow.
3953  int64_t Offset = LU.MinOffset;
3954  if (Offset == std::numeric_limits<int64_t>::min() && Factor == -1)
3955  continue;
3956  Offset = (uint64_t)Offset * Factor;
3957  if (Offset / Factor != LU.MinOffset)
3958  continue;
3959  // If the offset will be truncated at this use, check that it is in bounds.
3960  if (!IntTy->isPointerTy() &&
3962  continue;
3963 
3964  Formula F = Base;
3965  F.BaseOffset = NewBaseOffset;
3966 
3967  // Check that this scale is legal.
3968  if (!isLegalUse(TTI, Offset, Offset, LU.Kind, LU.AccessTy, F))
3969  continue;
3970 
3971  // Compensate for the use having MinOffset built into it.
3972  F.BaseOffset = (uint64_t)F.BaseOffset + Offset - LU.MinOffset;
3973 
3974  const SCEV *FactorS = SE.getConstant(IntTy, Factor);
3975 
3976  // Check that multiplying with each base register doesn't overflow.
3977  for (size_t i = 0, e = F.BaseRegs.size(); i != e; ++i) {
3978  F.BaseRegs[i] = SE.getMulExpr(F.BaseRegs[i], FactorS);
3979  if (getExactSDiv(F.BaseRegs[i], FactorS, SE) != Base.BaseRegs[i])
3980  goto next;
3981  }
3982 
3983  // Check that multiplying with the scaled register doesn't overflow.
3984  if (F.ScaledReg) {
3985  F.ScaledReg = SE.getMulExpr(F.ScaledReg, FactorS);
3986  if (getExactSDiv(F.ScaledReg, FactorS, SE) != Base.ScaledReg)
3987  continue;
3988  }
3989 
3990  // Check that multiplying with the unfolded offset doesn't overflow.
3991  if (F.UnfoldedOffset != 0) {
3992  if (F.UnfoldedOffset == std::numeric_limits<int64_t>::min() &&
3993  Factor == -1)
3994  continue;
3995  F.UnfoldedOffset = (uint64_t)F.UnfoldedOffset * Factor;
3996  if (F.UnfoldedOffset / Factor != Base.UnfoldedOffset)
3997  continue;
3998  // If the offset will be truncated, check that it is in bounds.
3999  if (!IntTy->isPointerTy() &&
4000  !ConstantInt::isValueValidForType(IntTy, F.UnfoldedOffset))
4001  continue;
4002  }
4003 
4004  // If we make it here and it's legal, add it.
4005  (void)InsertFormula(LU, LUIdx, F);
4006  next:;
4007  }
4008 }
4009 
4010 /// Generate stride factor reuse formulae by making use of scaled-offset address
4011 /// modes, for example.
4012 void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, Formula Base) {
4013  // Determine the integer type for the base formula.
4014  Type *IntTy = Base.getType();
4015  if (!IntTy) return;
4016 
4017  // If this Formula already has a scaled register, we can't add another one.
4018  // Try to unscale the formula to generate a better scale.
4019  if (Base.Scale != 0 && !Base.unscale())
4020  return;
4021 
4022  assert(Base.Scale == 0 && "unscale did not did its job!");
4023 
4024  // Check each interesting stride.
4025  for (int64_t Factor : Factors) {
4026  Base.Scale = Factor;
4027  Base.HasBaseReg = Base.BaseRegs.size() > 1;
4028  // Check whether this scale is going to be legal.
4029  if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4030  Base)) {
4031  // As a special-case, handle special out-of-loop Basic users specially.
4032  // TODO: Reconsider this special case.
4033  if (LU.Kind == LSRUse::Basic &&
4034  isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LSRUse::Special,
4035  LU.AccessTy, Base) &&
4036  LU.AllFixupsOutsideLoop)
4037  LU.Kind = LSRUse::Special;
4038  else
4039  continue;
4040  }
4041  // For an ICmpZero, negating a solitary base register won't lead to
4042  // new solutions.
4043  if (LU.Kind == LSRUse::ICmpZero &&
4044  !Base.HasBaseReg && Base.BaseOffset == 0 && !Base.BaseGV)
4045  continue;
4046  // For each addrec base reg, if its loop is current loop, apply the scale.
4047  for (size_t i = 0, e = Base.BaseRegs.size(); i != e; ++i) {
4048  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Base.BaseRegs[i]);
4049  if (AR && (AR->getLoop() == L || LU.AllFixupsOutsideLoop)) {
4050  const SCEV *FactorS = SE.getConstant(IntTy, Factor);
4051  if (FactorS->isZero())
4052  continue;
4053  // Divide out the factor, ignoring high bits, since we'll be
4054  // scaling the value back up in the end.
4055  if (const SCEV *Quotient = getExactSDiv(AR, FactorS, SE, true)) {
4056  // TODO: This could be optimized to avoid all the copying.
4057  Formula F = Base;
4058  F.ScaledReg = Quotient;
4059  F.deleteBaseReg(F.BaseRegs[i]);
4060  // The canonical representation of 1*reg is reg, which is already in
4061  // Base. In that case, do not try to insert the formula, it will be
4062  // rejected anyway.
4063  if (F.Scale == 1 && (F.BaseRegs.empty() ||
4064  (AR->getLoop() != L && LU.AllFixupsOutsideLoop)))
4065  continue;
4066  // If AllFixupsOutsideLoop is true and F.Scale is 1, we may generate
4067  // non canonical Formula with ScaledReg's loop not being L.
4068  if (F.Scale == 1 && LU.AllFixupsOutsideLoop)
4069  F.canonicalize(*L);
4070  (void)InsertFormula(LU, LUIdx, F);
4071  }
4072  }
4073  }
4074  }
4075 }
4076 
4077 /// Generate reuse formulae from different IV types.
4078 void LSRInstance::GenerateTruncates(LSRUse &LU, unsigned LUIdx, Formula Base) {
4079  // Don't bother truncating symbolic values.
4080  if (Base.BaseGV) return;
4081 
4082  // Determine the integer type for the base formula.
4083  Type *DstTy = Base.getType();
4084  if (!DstTy) return;
4085  if (DstTy->isPointerTy())
4086  return;
4087 
4088  // It is invalid to extend a pointer type so exit early if ScaledReg or
4089  // any of the BaseRegs are pointers.
4090  if (Base.ScaledReg && Base.ScaledReg->getType()->isPointerTy())
4091  return;
4092  if (any_of(Base.BaseRegs,
4093  [](const SCEV *S) { return S->getType()->isPointerTy(); }))
4094  return;
4095 
4096  for (Type *SrcTy : Types) {
4097  if (SrcTy != DstTy && TTI.isTruncateFree(SrcTy, DstTy)) {
4098  Formula F = Base;
4099 
4100  // Sometimes SCEV is able to prove zero during ext transform. It may
4101  // happen if SCEV did not do all possible transforms while creating the
4102  // initial node (maybe due to depth limitations), but it can do them while
4103  // taking ext.
4104  if (F.ScaledReg) {
4105  const SCEV *NewScaledReg = SE.getAnyExtendExpr(F.ScaledReg, SrcTy);
4106  if (NewScaledReg->isZero())
4107  continue;
4108  F.ScaledReg = NewScaledReg;
4109  }
4110  bool HasZeroBaseReg = false;
4111  for (const SCEV *&BaseReg : F.BaseRegs) {
4112  const SCEV *NewBaseReg = SE.getAnyExtendExpr(BaseReg, SrcTy);
4113  if (NewBaseReg->isZero()) {
4114  HasZeroBaseReg = true;
4115  break;
4116  }
4117  BaseReg = NewBaseReg;
4118  }
4119  if (HasZeroBaseReg)
4120  continue;
4121 
4122  // TODO: This assumes we've done basic processing on all uses and
4123  // have an idea what the register usage is.
4124  if (!F.hasRegsUsedByUsesOtherThan(LUIdx, RegUses))
4125  continue;
4126 
4127  F.canonicalize(*L);
4128  (void)InsertFormula(LU, LUIdx, F);
4129  }
4130  }
4131 }
4132 
4133 namespace {
4134 
4135 /// Helper class for GenerateCrossUseConstantOffsets. It's used to defer
4136 /// modifications so that the search phase doesn't have to worry about the data
4137 /// structures moving underneath it.
4138 struct WorkItem {
4139  size_t LUIdx;
4140  int64_t Imm;
4141  const SCEV *OrigReg;
4142 
4143  WorkItem(size_t LI, int64_t I, const SCEV *R)
4144  : LUIdx(LI), Imm(I), OrigReg(R) {}
4145 
4146  void print(raw_ostream &OS) const;
4147  void dump() const;
4148 };
4149 
4150 } // end anonymous namespace
4151 
4152 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4153 void WorkItem::print(raw_ostream &OS) const {
4154  OS << "in formulae referencing " << *OrigReg << " in use " << LUIdx
4155  << " , add offset " << Imm;
4156 }
4157 
4158 LLVM_DUMP_METHOD void WorkItem::dump() const {
4159  print(errs()); errs() << '\n';
4160 }
4161 #endif
4162 
4163 /// Look for registers which are a constant distance apart and try to form reuse
4164 /// opportunities between them.
4165 void LSRInstance::GenerateCrossUseConstantOffsets() {
4166  // Group the registers by their value without any added constant offset.
4167  using ImmMapTy = std::map<int64_t, const SCEV *>;
4168 
4170  DenseMap<const SCEV *, SmallBitVector> UsedByIndicesMap;
4172  for (const SCEV *Use : RegUses) {
4173  const SCEV *Reg = Use; // Make a copy for ExtractImmediate to modify.
4174  int64_t Imm = ExtractImmediate(Reg, SE);
4175  auto Pair = Map.insert(std::make_pair(Reg, ImmMapTy()));
4176  if (Pair.second)
4177  Sequence.push_back(Reg);
4178  Pair.first->second.insert(std::make_pair(Imm, Use));
4179  UsedByIndicesMap[Reg] |= RegUses.getUsedByIndices(Use);
4180  }
4181 
4182  // Now examine each set of registers with the same base value. Build up
4183  // a list of work to do and do the work in a separate step so that we're
4184  // not adding formulae and register counts while we're searching.
4185  SmallVector<WorkItem, 32> WorkItems;
4186  SmallSet<std::pair<size_t, int64_t>, 32> UniqueItems;
4187  for (const SCEV *Reg : Sequence) {
4188  const ImmMapTy &Imms = Map.find(Reg)->second;
4189 
4190  // It's not worthwhile looking for reuse if there's only one offset.
4191  if (Imms.size() == 1)
4192  continue;
4193 
4194  LLVM_DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';
4195  for (const auto &Entry
4196  : Imms) dbgs()
4197  << ' ' << Entry.first;
4198  dbgs() << '\n');
4199 
4200  // Examine each offset.
4201  for (ImmMapTy::const_iterator J = Imms.begin(), JE = Imms.end();
4202  J != JE; ++J) {
4203  const SCEV *OrigReg = J->second;
4204 
4205  int64_t JImm = J->first;
4206  const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(OrigReg);
4207 
4208  if (!isa<SCEVConstant>(OrigReg) &&
4209  UsedByIndicesMap[Reg].count() == 1) {
4210  LLVM_DEBUG(dbgs() << "Skipping cross-use reuse for " << *OrigReg
4211  << '\n');
4212  continue;
4213  }
4214 
4215  // Conservatively examine offsets between this orig reg a few selected
4216  // other orig regs.
4217  int64_t First = Imms.begin()->first;
4218  int64_t Last = std::prev(Imms.end())->first;
4219  // Compute (First + Last) / 2 without overflow using the fact that
4220  // First + Last = 2 * (First + Last) + (First ^ Last).
4221  int64_t Avg = (First & Last) + ((First ^ Last) >> 1);
4222  // If the result is negative and First is odd and Last even (or vice versa),
4223  // we rounded towards -inf. Add 1 in that case, to round towards 0.
4224  Avg = Avg + ((First ^ Last) & ((uint64_t)Avg >> 63));
4225  ImmMapTy::const_iterator OtherImms[] = {
4226  Imms.begin(), std::prev(Imms.end()),
4227  Imms.lower_bound(Avg)};
4228  for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
4229  ImmMapTy::const_iterator M = OtherImms[i];
4230  if (M == J || M == JE) continue;
4231 
4232  // Compute the difference between the two.
4233  int64_t Imm = (uint64_t)JImm - M->first;
4234  for (unsigned LUIdx : UsedByIndices.set_bits())
4235  // Make a memo of this use, offset, and register tuple.
4236  if (UniqueItems.insert(std::make_pair(LUIdx, Imm)).second)
4237  WorkItems.push_back(WorkItem(LUIdx, Imm, OrigReg));
4238  }
4239  }
4240  }
4241 
4242  Map.clear();
4243  Sequence.clear();
4244  UsedByIndicesMap.clear();
4245  UniqueItems.clear();
4246 
4247  // Now iterate through the worklist and add new formulae.
4248  for (const WorkItem &WI : WorkItems) {
4249  size_t LUIdx = WI.LUIdx;
4250  LSRUse &LU = Uses[LUIdx];
4251  int64_t Imm = WI.Imm;
4252  const SCEV *OrigReg = WI.OrigReg;
4253 
4254  Type *IntTy = SE.getEffectiveSCEVType(OrigReg->getType());
4255  const SCEV *NegImmS = SE.getSCEV(ConstantInt::get(IntTy, -(uint64_t)Imm));
4256  unsigned BitWidth = SE.getTypeSizeInBits(IntTy);
4257 
4258  // TODO: Use a more targeted data structure.
4259  for (size_t L = 0, LE = LU.Formulae.size(); L != LE; ++L) {
4260  Formula F = LU.Formulae[L];
4261  // FIXME: The code for the scaled and unscaled registers looks
4262  // very similar but slightly different. Investigate if they
4263  // could be merged. That way, we would not have to unscale the
4264  // Formula.
4265  F.unscale();
4266  // Use the immediate in the scaled register.
4267  if (F.ScaledReg == OrigReg) {
4268  int64_t Offset = (uint64_t)F.BaseOffset + Imm * (uint64_t)F.Scale;
4269  // Don't create 50 + reg(-50).
4270  if (F.referencesReg(SE.getSCEV(
4271  ConstantInt::get(IntTy, -(uint64_t)Offset))))
4272  continue;
4273  Formula NewF = F;
4274  NewF.BaseOffset = Offset;
4275  if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy,
4276  NewF))
4277  continue;
4278  NewF.ScaledReg = SE.getAddExpr(NegImmS, NewF.ScaledReg);
4279 
4280  // If the new scale is a constant in a register, and adding the constant
4281  // value to the immediate would produce a value closer to zero than the
4282  // immediate itself, then the formula isn't worthwhile.
4283  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewF.ScaledReg))
4284  if (C->getValue()->isNegative() != (NewF.BaseOffset < 0) &&
4285  (C->getAPInt().abs() * APInt(BitWidth, F.Scale))
4286  .ule(std::abs(NewF.BaseOffset)))
4287  continue;
4288 
4289  // OK, looks good.
4290  NewF.canonicalize(*this->L);
4291  (void)InsertFormula(LU, LUIdx, NewF);
4292  } else {
4293  // Use the immediate in a base register.
4294  for (size_t N = 0, NE = F.BaseRegs.size(); N != NE; ++N) {
4295  const SCEV *BaseReg = F.BaseRegs[N];
4296  if (BaseReg != OrigReg)
4297  continue;
4298  Formula NewF = F;
4299  NewF.BaseOffset = (uint64_t)NewF.BaseOffset + Imm;
4300  if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset,
4301  LU.Kind, LU.AccessTy, NewF)) {
4302  if (AMK == TTI::AMK_PostIndexed &&
4303  mayUsePostIncMode(TTI, LU, OrigReg, this->L, SE))
4304  continue;
4305  if (!TTI.isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm))
4306  continue;
4307  NewF = F;
4308  NewF.UnfoldedOffset = (uint64_t)NewF.UnfoldedOffset + Imm;
4309  }
4310  NewF.BaseRegs[N] = SE.getAddExpr(NegImmS, BaseReg);
4311 
4312  // If the new formula has a constant in a register, and adding the
4313  // constant value to the immediate would produce a value closer to
4314  // zero than the immediate itself, then the formula isn't worthwhile.
4315  for (const SCEV *NewReg : NewF.BaseRegs)
4316  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(NewReg))
4317  if ((C->getAPInt() + NewF.BaseOffset)
4318  .abs()
4319  .slt(std::abs(NewF.BaseOffset)) &&
4320  (C->getAPInt() + NewF.BaseOffset).countTrailingZeros() >=
4321  countTrailingZeros<uint64_t>(NewF.BaseOffset))
4322  goto skip_formula;
4323 
4324  // Ok, looks good.
4325  NewF.canonicalize(*this->L);
4326  (void)InsertFormula(LU, LUIdx, NewF);
4327  break;
4328  skip_formula:;
4329  }
4330  }
4331  }
4332  }
4333 }
4334 
4335 /// Generate formulae for each use.
4336 void
4337 LSRInstance::GenerateAllReuseFormulae() {
4338  // This is split into multiple loops so that hasRegsUsedByUsesOtherThan
4339  // queries are more precise.
4340  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4341  LSRUse &LU = Uses[LUIdx];
4342  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4343  GenerateReassociations(LU, LUIdx, LU.Formulae[i]);
4344  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4345  GenerateCombinations(LU, LUIdx, LU.Formulae[i]);
4346  }
4347  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4348  LSRUse &LU = Uses[LUIdx];
4349  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4350  GenerateSymbolicOffsets(LU, LUIdx, LU.Formulae[i]);
4351  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4352  GenerateConstantOffsets(LU, LUIdx, LU.Formulae[i]);
4353  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4354  GenerateICmpZeroScales(LU, LUIdx, LU.Formulae[i]);
4355  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4356  GenerateScales(LU, LUIdx, LU.Formulae[i]);
4357  }
4358  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4359  LSRUse &LU = Uses[LUIdx];
4360  for (size_t i = 0, f = LU.Formulae.size(); i != f; ++i)
4361  GenerateTruncates(LU, LUIdx, LU.Formulae[i]);
4362  }
4363 
4364  GenerateCrossUseConstantOffsets();
4365 
4366  LLVM_DEBUG(dbgs() << "\n"
4367  "After generating reuse formulae:\n";
4368  print_uses(dbgs()));
4369 }
4370 
4371 /// If there are multiple formulae with the same set of registers used
4372 /// by other uses, pick the best one and delete the others.
4373 void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
4374  DenseSet<const SCEV *> VisitedRegs;
4377 #ifndef NDEBUG
4378  bool ChangedFormulae = false;
4379 #endif
4380 
4381  // Collect the best formula for each unique set of shared registers. This
4382  // is reset for each use.
4383  using BestFormulaeTy =
4384  DenseMap<SmallVector<const SCEV *, 4>, size_t, UniquifierDenseMapInfo>;
4385 
4386  BestFormulaeTy BestFormulae;
4387 
4388  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4389  LSRUse &LU = Uses[LUIdx];
4390  LLVM_DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs());
4391  dbgs() << '\n');
4392 
4393  bool Any = false;
4394  for (size_t FIdx = 0, NumForms = LU.Formulae.size();
4395  FIdx != NumForms; ++FIdx) {
4396  Formula &F = LU.Formulae[FIdx];
4397 
4398  // Some formulas are instant losers. For example, they may depend on
4399  // nonexistent AddRecs from other loops. These need to be filtered
4400  // immediately, otherwise heuristics could choose them over others leading
4401  // to an unsatisfactory solution. Passing LoserRegs into RateFormula here
4402  // avoids the need to recompute this information across formulae using the
4403  // same bad AddRec. Passing LoserRegs is also essential unless we remove
4404  // the corresponding bad register from the Regs set.
4405  Cost CostF(L, SE, TTI, AMK);
4406  Regs.clear();
4407  CostF.RateFormula(F, Regs, VisitedRegs, LU, &LoserRegs);
4408  if (CostF.isLoser()) {
4409  // During initial formula generation, undesirable formulae are generated
4410  // by uses within other loops that have some non-trivial address mode or
4411  // use the postinc form of the IV. LSR needs to provide these formulae
4412  // as the basis of rediscovering the desired formula that uses an AddRec
4413  // corresponding to the existing phi. Once all formulae have been
4414  // generated, these initial losers may be pruned.
4415  LLVM_DEBUG(dbgs() << " Filtering loser "; F.print(dbgs());
4416  dbgs() << "\n");
4417  }
4418  else {
4420  for (const SCEV *Reg : F.BaseRegs) {
4421  if (RegUses.isRegUsedByUsesOtherThan(Reg, LUIdx))
4422  Key.push_back(Reg);
4423  }
4424  if (F.ScaledReg &&
4425  RegUses.isRegUsedByUsesOtherThan(F.ScaledReg, LUIdx))
4426  Key.push_back(F.ScaledReg);
4427  // Unstable sort by host order ok, because this is only used for
4428  // uniquifying.
4429  llvm::sort(Key);
4430 
4431  std::pair<BestFormulaeTy::const_iterator, bool> P =
4432  BestFormulae.insert(std::make_pair(Key, FIdx));
4433  if (P.second)
4434  continue;
4435 
4436  Formula &Best = LU.Formulae[P.first->second];
4437 
4438  Cost CostBest(L, SE, TTI, AMK);
4439  Regs.clear();
4440  CostBest.RateFormula(Best, Regs, VisitedRegs, LU);
4441  if (CostF.isLess(CostBest))
4442  std::swap(F, Best);
4443  LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
4444  dbgs() << "\n"
4445  " in favor of formula ";
4446  Best.print(dbgs()); dbgs() << '\n');
4447  }
4448 #ifndef NDEBUG
4449  ChangedFormulae = true;
4450 #endif
4451  LU.DeleteFormula(F);
4452  --FIdx;
4453  --NumForms;
4454  Any = true;
4455  }
4456 
4457  // Now that we've filtered out some formulae, recompute the Regs set.
4458  if (Any)
4459  LU.RecomputeRegs(LUIdx, RegUses);
4460 
4461  // Reset this to prepare for the next use.
4462  BestFormulae.clear();
4463  }
4464 
4465  LLVM_DEBUG(if (ChangedFormulae) {
4466  dbgs() << "\n"
4467  "After filtering out undesirable candidates:\n";
4468  print_uses(dbgs());
4469  });
4470 }
4471 
4472 /// Estimate the worst-case number of solutions the solver might have to
4473 /// consider. It almost never considers this many solutions because it prune the
4474 /// search space, but the pruning isn't always sufficient.
4475 size_t LSRInstance::EstimateSearchSpaceComplexity() const {
4476  size_t Power = 1;
4477  for (const LSRUse &LU : Uses) {
4478  size_t FSize = LU.Formulae.size();
4479  if (FSize >= ComplexityLimit) {
4480  Power = ComplexityLimit;
4481  break;
4482  }
4483  Power *= FSize;
4484  if (Power >= ComplexityLimit)
4485  break;
4486  }
4487  return Power;
4488 }
4489 
4490 /// When one formula uses a superset of the registers of another formula, it
4491 /// won't help reduce register pressure (though it may not necessarily hurt
4492 /// register pressure); remove it to simplify the system.
4493 void LSRInstance::NarrowSearchSpaceByDetectingSupersets() {
4494  if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
4495  LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
4496 
4497  LLVM_DEBUG(dbgs() << "Narrowing the search space by eliminating formulae "
4498  "which use a superset of registers used by other "
4499  "formulae.\n");
4500 
4501  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4502  LSRUse &LU = Uses[LUIdx];
4503  bool Any = false;
4504  for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
4505  Formula &F = LU.Formulae[i];
4506  // Look for a formula with a constant or GV in a register. If the use
4507  // also has a formula with that same value in an immediate field,
4508  // delete the one that uses a register.
4510  I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
4511  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
4512  Formula NewF = F;
4513  //FIXME: Formulas should store bitwidth to do wrapping properly.
4514  // See PR41034.
4515  NewF.BaseOffset += (uint64_t)C->getValue()->getSExtValue();
4516  NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
4517  (I - F.BaseRegs.begin()));
4518  if (LU.HasFormulaWithSameRegs(NewF)) {
4519  LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs());
4520  dbgs() << '\n');
4521  LU.DeleteFormula(F);
4522  --i;
4523  --e;
4524  Any = true;
4525  break;
4526  }
4527  } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(*I)) {
4528  if (GlobalValue *GV = dyn_cast<GlobalValue>(U->getValue()))
4529  if (!F.BaseGV) {
4530  Formula NewF = F;
4531  NewF.BaseGV = GV;
4532  NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
4533  (I - F.BaseRegs.begin()));
4534  if (LU.HasFormulaWithSameRegs(NewF)) {
4535  LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs());
4536  dbgs() << '\n');
4537  LU.DeleteFormula(F);
4538  --i;
4539  --e;
4540  Any = true;
4541  break;
4542  }
4543  }
4544  }
4545  }
4546  }
4547  if (Any)
4548  LU.RecomputeRegs(LUIdx, RegUses);
4549  }
4550 
4551  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4552  }
4553 }
4554 
4555 /// When there are many registers for expressions like A, A+1, A+2, etc.,
4556 /// allocate a single register for them.
4557 void LSRInstance::NarrowSearchSpaceByCollapsingUnrolledCode() {
4558  if (EstimateSearchSpaceComplexity() < ComplexityLimit)
4559  return;
4560 
4561  LLVM_DEBUG(
4562  dbgs() << "The search space is too complex.\n"
4563  "Narrowing the search space by assuming that uses separated "
4564  "by a constant offset will use the same registers.\n");
4565 
4566  // This is especially useful for unrolled loops.
4567 
4568  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4569  LSRUse &LU = Uses[LUIdx];
4570  for (const Formula &F : LU.Formulae) {
4571  if (F.BaseOffset == 0 || (F.Scale != 0 && F.Scale != 1))
4572  continue;
4573 
4574  LSRUse *LUThatHas = FindUseWithSimilarFormula(F, LU);
4575  if (!LUThatHas)
4576  continue;
4577 
4578  if (!reconcileNewOffset(*LUThatHas, F.BaseOffset, /*HasBaseReg=*/ false,
4579  LU.Kind, LU.AccessTy))
4580  continue;
4581 
4582  LLVM_DEBUG(dbgs() << " Deleting use "; LU.print(dbgs()); dbgs() << '\n');
4583 
4584  LUThatHas->AllFixupsOutsideLoop &= LU.AllFixupsOutsideLoop;
4585 
4586  // Transfer the fixups of LU to LUThatHas.
4587  for (LSRFixup &Fixup : LU.Fixups) {
4588  Fixup.Offset += F.BaseOffset;
4589  LUThatHas->pushFixup(Fixup);
4590  LLVM_DEBUG(dbgs() << "New fixup has offset " << Fixup.Offset << '\n');
4591  }
4592 
4593  // Delete formulae from the new use which are no longer legal.
4594  bool Any = false;
4595  for (size_t i = 0, e = LUThatHas->Formulae.size(); i != e; ++i) {
4596  Formula &F = LUThatHas->Formulae[i];
4597  if (!isLegalUse(TTI, LUThatHas->MinOffset, LUThatHas->MaxOffset,
4598  LUThatHas->Kind, LUThatHas->AccessTy, F)) {
4599  LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
4600  LUThatHas->DeleteFormula(F);
4601  --i;
4602  --e;
4603  Any = true;
4604  }
4605  }
4606 
4607  if (Any)
4608  LUThatHas->RecomputeRegs(LUThatHas - &Uses.front(), RegUses);
4609 
4610  // Delete the old use.
4611  DeleteUse(LU, LUIdx);
4612  --LUIdx;
4613  --NumUses;
4614  break;
4615  }
4616  }
4617 
4618  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4619 }
4620 
4621 /// Call FilterOutUndesirableDedicatedRegisters again, if necessary, now that
4622 /// we've done more filtering, as it may be able to find more formulae to
4623 /// eliminate.
4624 void LSRInstance::NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters(){
4625  if (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
4626  LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
4627 
4628  LLVM_DEBUG(dbgs() << "Narrowing the search space by re-filtering out "
4629  "undesirable dedicated registers.\n");
4630 
4631  FilterOutUndesirableDedicatedRegisters();
4632 
4633  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4634  }
4635 }
4636 
4637 /// If a LSRUse has multiple formulae with the same ScaledReg and Scale.
4638 /// Pick the best one and delete the others.
4639 /// This narrowing heuristic is to keep as many formulae with different
4640 /// Scale and ScaledReg pair as possible while narrowing the search space.
4641 /// The benefit is that it is more likely to find out a better solution
4642 /// from a formulae set with more Scale and ScaledReg variations than
4643 /// a formulae set with the same Scale and ScaledReg. The picking winner
4644 /// reg heuristic will often keep the formulae with the same Scale and
4645 /// ScaledReg and filter others, and we want to avoid that if possible.
4646 void LSRInstance::NarrowSearchSpaceByFilterFormulaWithSameScaledReg() {
4647  if (EstimateSearchSpaceComplexity() < ComplexityLimit)
4648  return;
4649 
4650  LLVM_DEBUG(
4651  dbgs() << "The search space is too complex.\n"
4652  "Narrowing the search space by choosing the best Formula "
4653  "from the Formulae with the same Scale and ScaledReg.\n");
4654 
4655  // Map the "Scale * ScaledReg" pair to the best formula of current LSRUse.
4656  using BestFormulaeTy = DenseMap<std::pair<const SCEV *, int64_t>, size_t>;
4657 
4658  BestFormulaeTy BestFormulae;
4659 #ifndef NDEBUG
4660  bool ChangedFormulae = false;
4661 #endif
4662  DenseSet<const SCEV *> VisitedRegs;
4664 
4665  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4666  LSRUse &LU = Uses[LUIdx];
4667  LLVM_DEBUG(dbgs() << "Filtering for use "; LU.print(dbgs());
4668  dbgs() << '\n');
4669 
4670  // Return true if Formula FA is better than Formula FB.
4671  auto IsBetterThan = [&](Formula &FA, Formula &FB) {
4672  // First we will try to choose the Formula with fewer new registers.
4673  // For a register used by current Formula, the more the register is
4674  // shared among LSRUses, the less we increase the register number
4675  // counter of the formula.
4676  size_t FARegNum = 0;
4677  for (const SCEV *Reg : FA.BaseRegs) {
4678  const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(Reg);
4679  FARegNum += (NumUses - UsedByIndices.count() + 1);
4680  }
4681  size_t FBRegNum = 0;
4682  for (const SCEV *Reg : FB.BaseRegs) {
4683  const SmallBitVector &UsedByIndices = RegUses.getUsedByIndices(Reg);
4684  FBRegNum += (NumUses - UsedByIndices.count() + 1);
4685  }
4686  if (FARegNum != FBRegNum)
4687  return FARegNum < FBRegNum;
4688 
4689  // If the new register numbers are the same, choose the Formula with
4690  // less Cost.
4691  Cost CostFA(L, SE, TTI, AMK);
4692  Cost CostFB(L, SE, TTI, AMK);
4693  Regs.clear();
4694  CostFA.RateFormula(FA, Regs, VisitedRegs, LU);
4695  Regs.clear();
4696  CostFB.RateFormula(FB, Regs, VisitedRegs, LU);
4697  return CostFA.isLess(CostFB);
4698  };
4699 
4700  bool Any = false;
4701  for (size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
4702  ++FIdx) {
4703  Formula &F = LU.Formulae[FIdx];
4704  if (!F.ScaledReg)
4705  continue;
4706  auto P = BestFormulae.insert({{F.ScaledReg, F.Scale}, FIdx});
4707  if (P.second)
4708  continue;
4709 
4710  Formula &Best = LU.Formulae[P.first->second];
4711  if (IsBetterThan(F, Best))
4712  std::swap(F, Best);
4713  LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
4714  dbgs() << "\n"
4715  " in favor of formula ";
4716  Best.print(dbgs()); dbgs() << '\n');
4717 #ifndef NDEBUG
4718  ChangedFormulae = true;
4719 #endif
4720  LU.DeleteFormula(F);
4721  --FIdx;
4722  --NumForms;
4723  Any = true;
4724  }
4725  if (Any)
4726  LU.RecomputeRegs(LUIdx, RegUses);
4727 
4728  // Reset this to prepare for the next use.
4729  BestFormulae.clear();
4730  }
4731 
4732  LLVM_DEBUG(if (ChangedFormulae) {
4733  dbgs() << "\n"
4734  "After filtering out undesirable candidates:\n";
4735  print_uses(dbgs());
4736  });
4737 }
4738 
4739 /// If we are over the complexity limit, filter out any post-inc prefering
4740 /// variables to only post-inc values.
4741 void LSRInstance::NarrowSearchSpaceByFilterPostInc() {
4742  if (AMK != TTI::AMK_PostIndexed)
4743  return;
4744  if (EstimateSearchSpaceComplexity() < ComplexityLimit)
4745  return;
4746 
4747  LLVM_DEBUG(dbgs() << "The search space is too complex.\n"
4748  "Narrowing the search space by choosing the lowest "
4749  "register Formula for PostInc Uses.\n");
4750 
4751  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4752  LSRUse &LU = Uses[LUIdx];
4753 
4754  if (LU.Kind != LSRUse::Address)
4755  continue;
4756  if (!TTI.isIndexedLoadLegal(TTI.MIM_PostInc, LU.AccessTy.getType()) &&
4757  !TTI.isIndexedStoreLegal(TTI.MIM_PostInc, LU.AccessTy.getType()))
4758  continue;
4759 
4760  size_t MinRegs = std::numeric_limits<size_t>::max();
4761  for (const Formula &F : LU.Formulae)
4762  MinRegs = std::min(F.getNumRegs(), MinRegs);
4763 
4764  bool Any = false;
4765  for (size_t FIdx = 0, NumForms = LU.Formulae.size(); FIdx != NumForms;
4766  ++FIdx) {
4767  Formula &F = LU.Formulae[FIdx];
4768  if (F.getNumRegs() > MinRegs) {
4769  LLVM_DEBUG(dbgs() << " Filtering out formula "; F.print(dbgs());
4770  dbgs() << "\n");
4771  LU.DeleteFormula(F);
4772  --FIdx;
4773  --NumForms;
4774  Any = true;
4775  }
4776  }
4777  if (Any)
4778  LU.RecomputeRegs(LUIdx, RegUses);
4779 
4780  if (EstimateSearchSpaceComplexity() < ComplexityLimit)
4781  break;
4782  }
4783 
4784  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4785 }
4786 
4787 /// The function delete formulas with high registers number expectation.
4788 /// Assuming we don't know the value of each formula (already delete
4789 /// all inefficient), generate probability of not selecting for each
4790 /// register.
4791 /// For example,
4792 /// Use1:
4793 /// reg(a) + reg({0,+,1})
4794 /// reg(a) + reg({-1,+,1}) + 1
4795 /// reg({a,+,1})
4796 /// Use2:
4797 /// reg(b) + reg({0,+,1})
4798 /// reg(b) + reg({-1,+,1}) + 1
4799 /// reg({b,+,1})
4800 /// Use3:
4801 /// reg(c) + reg(b) + reg({0,+,1})
4802 /// reg(c) + reg({b,+,1})
4803 ///
4804 /// Probability of not selecting
4805 /// Use1 Use2 Use3
4806 /// reg(a) (1/3) * 1 * 1
4807 /// reg(b) 1 * (1/3) * (1/2)
4808 /// reg({0,+,1}) (2/3) * (2/3) * (1/2)
4809 /// reg({-1,+,1}) (2/3) * (2/3) * 1
4810 /// reg({a,+,1}) (2/3) * 1 * 1
4811 /// reg({b,+,1}) 1 * (2/3) * (2/3)
4812 /// reg(c) 1 * 1 * 0
4813 ///
4814 /// Now count registers number mathematical expectation for each formula:
4815 /// Note that for each use we exclude probability if not selecting for the use.
4816 /// For example for Use1 probability for reg(a) would be just 1 * 1 (excluding
4817 /// probabilty 1/3 of not selecting for Use1).
4818 /// Use1:
4819 /// reg(a) + reg({0,+,1}) 1 + 1/3 -- to be deleted
4820 /// reg(a) + reg({-1,+,1}) + 1 1 + 4/9 -- to be deleted
4821 /// reg({a,+,1}) 1
4822 /// Use2:
4823 /// reg(b) + reg({0,+,1}) 1/2 + 1/3 -- to be deleted
4824 /// reg(b) + reg({-1,+,1}) + 1 1/2 + 2/3 -- to be deleted
4825 /// reg({b,+,1}) 2/3
4826 /// Use3:
4827 /// reg(c) + reg(b) + reg({0,+,1}) 1 + 1/3 + 4/9 -- to be deleted
4828 /// reg(c) + reg({b,+,1}) 1 + 2/3
4829 void LSRInstance::NarrowSearchSpaceByDeletingCostlyFormulas() {
4830  if (EstimateSearchSpaceComplexity() < ComplexityLimit)
4831  return;
4832  // Ok, we have too many of formulae on our hands to conveniently handle.
4833  // Use a rough heuristic to thin out the list.
4834 
4835  // Set of Regs wich will be 100% used in final solution.
4836  // Used in each formula of a solution (in example above this is reg(c)).
4837  // We can skip them in calculations.
4839  LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
4840 
4841  // Map each register to probability of not selecting
4843  for (const SCEV *Reg : RegUses) {
4844  if (UniqRegs.count(Reg))
4845  continue;
4846  float PNotSel = 1;
4847  for (const LSRUse &LU : Uses) {
4848  if (!LU.Regs.count(Reg))
4849  continue;
4850  float P = LU.getNotSelectedProbability(Reg);
4851  if (P != 0.0)
4852  PNotSel *= P;
4853  else
4854  UniqRegs.insert(Reg);
4855  }
4856  RegNumMap.insert(std::make_pair(Reg, PNotSel));
4857  }
4858 
4859  LLVM_DEBUG(
4860  dbgs() << "Narrowing the search space by deleting costly formulas\n");
4861 
4862  // Delete formulas where registers number expectation is high.
4863  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4864  LSRUse &LU = Uses[LUIdx];
4865  // If nothing to delete - continue.
4866  if (LU.Formulae.size() < 2)
4867  continue;
4868  // This is temporary solution to test performance. Float should be
4869  // replaced with round independent type (based on integers) to avoid
4870  // different results for different target builds.
4871  float FMinRegNum = LU.Formulae[0].getNumRegs();
4872  float FMinARegNum = LU.Formulae[0].getNumRegs();
4873  size_t MinIdx = 0;
4874  for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
4875  Formula &F = LU.Formulae[i];
4876  float FRegNum = 0;
4877  float FARegNum = 0;
4878  for (const SCEV *BaseReg : F.BaseRegs) {
4879  if (UniqRegs.count(BaseReg))
4880  continue;
4881  FRegNum += RegNumMap[BaseReg] / LU.getNotSelectedProbability(BaseReg);
4882  if (isa<SCEVAddRecExpr>(BaseReg))
4883  FARegNum +=
4884  RegNumMap[BaseReg] / LU.getNotSelectedProbability(BaseReg);
4885  }
4886  if (const SCEV *ScaledReg = F.ScaledReg) {
4887  if (!UniqRegs.count(ScaledReg)) {
4888  FRegNum +=
4889  RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
4890  if (isa<SCEVAddRecExpr>(ScaledReg))
4891  FARegNum +=
4892  RegNumMap[ScaledReg] / LU.getNotSelectedProbability(ScaledReg);
4893  }
4894  }
4895  if (FMinRegNum > FRegNum ||
4896  (FMinRegNum == FRegNum && FMinARegNum > FARegNum)) {
4897  FMinRegNum = FRegNum;
4898  FMinARegNum = FARegNum;
4899  MinIdx = i;
4900  }
4901  }
4902  LLVM_DEBUG(dbgs() << " The formula "; LU.Formulae[MinIdx].print(dbgs());
4903  dbgs() << " with min reg num " << FMinRegNum << '\n');
4904  if (MinIdx != 0)
4905  std::swap(LU.Formulae[MinIdx], LU.Formulae[0]);
4906  while (LU.Formulae.size() != 1) {
4907  LLVM_DEBUG(dbgs() << " Deleting "; LU.Formulae.back().print(dbgs());
4908  dbgs() << '\n');
4909  LU.Formulae.pop_back();
4910  }
4911  LU.RecomputeRegs(LUIdx, RegUses);
4912  assert(LU.Formulae.size() == 1 && "Should be exactly 1 min regs formula");
4913  Formula &F = LU.Formulae[0];
4914  LLVM_DEBUG(dbgs() << " Leaving only "; F.print(dbgs()); dbgs() << '\n');
4915  // When we choose the formula, the regs become unique.
4916  UniqRegs.insert(F.BaseRegs.begin(), F.BaseRegs.end());
4917  if (F.ScaledReg)
4918  UniqRegs.insert(F.ScaledReg);
4919  }
4920  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4921 }
4922 
4923 /// Pick a register which seems likely to be profitable, and then in any use
4924 /// which has any reference to that register, delete all formulae which do not
4925 /// reference that register.
4926 void LSRInstance::NarrowSearchSpaceByPickingWinnerRegs() {
4927  // With all other options exhausted, loop until the system is simple
4928  // enough to handle.
4930  while (EstimateSearchSpaceComplexity() >= ComplexityLimit) {
4931  // Ok, we have too many of formulae on our hands to conveniently handle.
4932  // Use a rough heuristic to thin out the list.
4933  LLVM_DEBUG(dbgs() << "The search space is too complex.\n");
4934 
4935  // Pick the register which is used by the most LSRUses, which is likely
4936  // to be a good reuse register candidate.
4937  const SCEV *Best = nullptr;
4938  unsigned BestNum = 0;
4939  for (const SCEV *Reg : RegUses) {
4940  if (Taken.count(Reg))
4941  continue;
4942  if (!Best) {
4943  Best = Reg;
4944  BestNum = RegUses.getUsedByIndices(Reg).count();
4945  } else {
4946  unsigned Count = RegUses.getUsedByIndices(Reg).count();
4947  if (Count > BestNum) {
4948  Best = Reg;
4949  BestNum = Count;
4950  }
4951  }
4952  }
4953  assert(Best && "Failed to find best LSRUse candidate");
4954 
4955  LLVM_DEBUG(dbgs() << "Narrowing the search space by assuming " << *Best
4956  << " will yield profitable reuse.\n");
4957  Taken.insert(Best);
4958 
4959  // In any use with formulae which references this register, delete formulae
4960  // which don't reference it.
4961  for (size_t LUIdx = 0, NumUses = Uses.size(); LUIdx != NumUses; ++LUIdx) {
4962  LSRUse &LU = Uses[LUIdx];
4963  if (!LU.Regs.count(Best)) continue;
4964 
4965  bool Any = false;
4966  for (size_t i = 0, e = LU.Formulae.size(); i != e; ++i) {
4967  Formula &F = LU.Formulae[i];
4968  if (!F.referencesReg(Best)) {
4969  LLVM_DEBUG(dbgs() << " Deleting "; F.print(dbgs()); dbgs() << '\n');
4970  LU.DeleteFormula(F);
4971  --e;
4972  --i;
4973  Any = true;
4974  assert(e != 0 && "Use has no formulae left! Is Regs inconsistent?");
4975  continue;
4976  }
4977  }
4978 
4979  if (Any)
4980  LU.RecomputeRegs(LUIdx, RegUses);
4981  }
4982 
4983  LLVM_DEBUG(dbgs() << "After pre-selection:\n"; print_uses(dbgs()));
4984  }
4985 }
4986 
4987 /// If there are an extraordinary number of formulae to choose from, use some
4988 /// rough heuristics to prune down the number of formulae. This keeps the main
4989 /// solver from taking an extraordinary amount of time in some worst-case
4990 /// scenarios.
4991 void LSRInstance::NarrowSearchSpaceUsingHeuristics() {
4992  NarrowSearchSpaceByDetectingSupersets();
4993  NarrowSearchSpaceByCollapsingUnrolledCode();
4994  NarrowSearchSpaceByRefilteringUndesirableDedicatedRegisters();
4995  if (FilterSameScaledReg)
4996  NarrowSearchSpaceByFilterFormulaWithSameScaledReg();
4997  NarrowSearchSpaceByFilterPostInc();
4998  if (LSRExpNarrow)
4999  NarrowSearchSpaceByDeletingCostlyFormulas();
5000  else
5001  NarrowSearchSpaceByPickingWinnerRegs();
5002 }
5003 
5004 /// This is the recursive solver.
5005 void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
5006  Cost &SolutionCost,
5008  const Cost &CurCost,
5009  const SmallPtrSet<const SCEV *, 16> &CurRegs,
5010  DenseSet<const SCEV *> &VisitedRegs) const {
5011  // Some ideas:
5012  // - prune more:
5013  // - use more aggressive filtering
5014  // - sort the formula so that the most profitable solutions are found first
5015  // - sort the uses too
5016  // - search faster:
5017  // - don't compute a cost, and then compare. compare while computing a cost
5018  // and bail early.
5019  // - track register sets with SmallBitVector
5020 
5021  const LSRUse &LU = Uses[Workspace.size()];
5022 
5023  // If this use references any register that's already a part of the
5024  // in-progress solution, consider it a requirement that a formula must
5025  // reference that register in order to be considered. This prunes out
5026  // unprofitable searching.
5028  for (const SCEV *S : CurRegs)
5029  if (LU.Regs.count(S))
5030  ReqRegs.insert(S);
5031 
5033  Cost NewCost(L, SE, TTI, AMK);
5034  for (const Formula &F : LU.Formulae) {
5035  // Ignore formulae which may not be ideal in terms of register reuse of
5036  // ReqRegs. The formula should use all required registers before
5037  // introducing new ones.
5038  // This can sometimes (notably when trying to favour postinc) lead to
5039  // sub-optimial decisions. There it is best left to the cost modelling to
5040  // get correct.
5041  if (AMK != TTI::AMK_PostIndexed || LU.Kind != LSRUse::Address) {
5042  int NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size());
5043  for (const SCEV *Reg : ReqRegs) {
5044  if ((F.ScaledReg && F.ScaledReg == Reg) ||
5045  is_contained(F.BaseRegs, Reg)) {
5046  --NumReqRegsToFind;
5047  if (NumReqRegsToFind == 0)
5048  break;
5049  }
5050  }
5051  if (NumReqRegsToFind != 0) {
5052  // If none of the formulae satisfied the required registers, then we could
5053  // clear ReqRegs and try again. Currently, we simply give up in this case.
5054  continue;
5055  }
5056  }
5057 
5058  // Evaluate the cost of the current formula. If it's already worse than
5059  // the current best, prune the search at that point.
5060  NewCost = CurCost;
5061  NewRegs = CurRegs;
5062  NewCost.RateFormula(F, NewRegs, VisitedRegs, LU);
5063  if (NewCost.isLess(SolutionCost)) {
5064  Workspace.push_back(&F);
5065  if (Workspace.size() != Uses.size()) {
5066  SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
5067  NewRegs, VisitedRegs);
5068  if (F.getNumRegs() == 1 && Workspace.size() == 1)
5069  VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
5070  } else {
5071  LLVM_DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
5072  dbgs() << ".\nRegs:\n";
5073  for (const SCEV *S : NewRegs) dbgs()
5074  << "- " << *S << "\n";
5075  dbgs() << '\n');
5076 
5077  SolutionCost = NewCost;
5078  Solution = Workspace;
5079  }
5080  Workspace.pop_back();
5081  }
5082  }
5083 }
5084 
5085 /// Choose one formula from each use. Return the results in the given Solution
5086 /// vector.
5087 void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
5089  Cost SolutionCost(L, SE, TTI, AMK);
5090  SolutionCost.Lose();
5091  Cost CurCost(L, SE, TTI, AMK);
5093  DenseSet<const SCEV *> VisitedRegs;
5094  Workspace.reserve(Uses.size());
5095 
5096  // SolveRecurse does all the work.
5097  SolveRecurse(Solution, SolutionCost, Workspace, CurCost,
5098  CurRegs, VisitedRegs);
5099  if (Solution.empty()) {
5100  LLVM_DEBUG(dbgs() << "\nNo Satisfactory Solution\n");
5101  return;
5102  }
5103 
5104  // Ok, we've now made all our decisions.
5105  LLVM_DEBUG(dbgs() << "\n"
5106  "The chosen solution requires ";
5107  SolutionCost.print(dbgs()); dbgs() << ":\n";
5108  for (size_t i = 0, e = Uses.size(); i != e; ++i) {
5109  dbgs() << " ";
5110  Uses[i].print(dbgs());
5111  dbgs() << "\n"
5112  " ";
5113  Solution[i]->print(dbgs());
5114  dbgs() << '\n';
5115  });
5116 
5117  assert(Solution.size() == Uses.size() && "Malformed solution!");
5118 }
5119 
5120 /// Helper for AdjustInsertPositionForExpand. Climb up the dominator tree far as
5121 /// we can go while still being dominated by the input positions. This helps
5122 /// canonicalize the insert position, which encourages sharing.
5124 LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
5125  const SmallVectorImpl<Instruction *> &Inputs)
5126  const {
5127  Instruction *Tentative = &*IP;
5128  while (true) {
5129  bool AllDominate = true;
5130  Instruction *BetterPos = nullptr;
5131  // Don't bother attempting to insert before a catchswitch, their basic block
5132  // cannot have other non-PHI instructions.
5133  if (isa<CatchSwitchInst>(Tentative))
5134  return IP;
5135 
5136  for (Instruction *Inst : Inputs) {
5137  if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
5138  AllDominate = false;
5139  break;
5140  }
5141  // Attempt to find an insert position in the middle of the block,
5142  // instead of at the end, so that it can be used for other expansions.
5143  if (Tentative->getParent() == Inst->getParent() &&
5144  (!BetterPos || !DT.dominates(Inst, BetterPos)))
5145  BetterPos = &*std::next(BasicBlock::iterator(Inst));
5146  }
5147  if (!AllDominate)
5148  break;
5149  if (BetterPos)
5150  IP = BetterPos->getIterator();
5151  else
5152  IP = Tentative->getIterator();
5153 
5154  const Loop *IPLoop = LI.getLoopFor(IP->getParent());
5155  unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
5156 
5157  BasicBlock *IDom;
5158  for (DomTreeNode *Rung = DT.getNode(IP->getParent()); ; ) {
5159  if (!Rung) return IP;
5160  Rung = Rung->getIDom();
5161  if (!Rung) return IP;
5162  IDom = Rung->getBlock();
5163 
5164  // Don't climb into a loop though.
5165  const Loop *IDomLoop = LI.getLoopFor(IDom);
5166  unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0;
5167  if (IDomDepth <= IPLoopDepth &&
5168  (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
5169  break;
5170  }
5171 
5172  Tentative = IDom->getTerminator();
5173  }
5174 
5175  return IP;
5176 }
5177 
5178 /// Determine an input position which will be dominated by the operands and
5179 /// which will dominate the result.
5181 LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator LowestIP,
5182  const LSRFixup &LF,
5183  const LSRUse &LU,
5184  SCEVExpander &Rewriter) const {
5185  // Collect some instructions which must be dominated by the
5186  // expanding replacement. These must be dominated by any operands that
5187  // will be required in the expansion.
5189  if (Instruction *I = dyn_cast<Instruction>(LF.OperandValToReplace))
5190  Inputs.push_back(I);
5191  if (LU.Kind == LSRUse::ICmpZero)
5192  if (Instruction *I =
5193  dyn_cast<Instruction>(cast<ICmpInst>(LF.UserInst)->getOperand(1)))
5194  Inputs.push_back(I);
5195  if (LF.PostIncLoops.count(L)) {
5196  if (LF.isUseFullyOutsideLoop(L))
5197  Inputs.push_back(L->getLoopLatch()->getTerminator());
5198  else
5199  Inputs.push_back(IVIncInsertPos);
5200  }
5201  // The expansion must also be dominated by the increment positions of any
5202  // loops it for which it is using post-inc mode.
5203  for (const Loop *PIL : LF.PostIncLoops) {
5204  if (PIL == L) continue;
5205 
5206  // Be dominated by the loop exit.
5207  SmallVector<BasicBlock *, 4> ExitingBlocks;
5208  PIL->getExitingBlocks(ExitingBlocks);
5209  if (!ExitingBlocks.empty()) {
5210  BasicBlock *BB = ExitingBlocks[0];
5211  for (unsigned i = 1, e = ExitingBlocks.size(); i != e; ++i)
5212  BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
5213  Inputs.push_back(BB->getTerminator());
5214  }
5215  }
5216 
5217  assert(!isa<PHINode>(LowestIP) && !LowestIP->isEHPad()
5218  && !isa<DbgInfoIntrinsic>(LowestIP) &&
5219  "Insertion point must be a normal instruction");
5220 
5221  // Then, climb up the immediate dominator tree as far as we can go while
5222  // still being dominated by the input positions.
5223  BasicBlock::iterator IP = HoistInsertPosition(LowestIP, Inputs);
5224 
5225  // Don't insert instructions before PHI nodes.
5226  while (isa<PHINode>(IP)) ++IP;
5227 
5228  // Ignore landingpad instructions.
5229  while (IP->isEHPad()) ++IP;
5230 
5231  // Ignore debug intrinsics.
5232  while (isa<DbgInfoIntrinsic>(IP)) ++IP;
5233 
5234  // Set IP below instructions recently inserted by SCEVExpander. This keeps the
5235  // IP consistent across expansions and allows the previously inserted
5236  // instructions to be reused by subsequen