LLVM 24.0.0git
StraightLineStrengthReduce.cpp
Go to the documentation of this file.
1//===- StraightLineStrengthReduce.cpp - -----------------------------------===//
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 file implements straight-line strength reduction (SLSR). Unlike loop
10// strength reduction, this algorithm is designed to reduce arithmetic
11// redundancy in straight-line code instead of loops. It has proven to be
12// effective in simplifying arithmetic statements derived from an unrolled loop.
13// It can also simplify the logic of SeparateConstOffsetFromGEP.
14//
15// There are many optimizations we can perform in the domain of SLSR.
16// We look for strength reduction candidates in the following forms:
17//
18// Form Add: B + i * S
19// Form Mul: (B + i) * S
20// Form GEP: &B[i * S]
21//
22// where S is an integer variable, and i is a constant integer. If we found two
23// candidates S1 and S2 in the same form and S1 dominates S2, we may rewrite S2
24// in a simpler way with respect to S1 (index delta). For example,
25//
26// S1: X = B + i * S
27// S2: Y = B + i' * S => X + (i' - i) * S
28//
29// S1: X = (B + i) * S
30// S2: Y = (B + i') * S => X + (i' - i) * S
31//
32// S1: X = &B[i * S]
33// S2: Y = &B[i' * S] => &X[(i' - i) * S]
34//
35// Note: (i' - i) * S is folded to the extent possible.
36//
37// For Add and GEP forms, we can also rewrite a candidate in a simpler way
38// with respect to other dominating candidates if their B or S are different
39// but other parts are the same. For example,
40//
41// Base Delta:
42// S1: X = B + i * S
43// S2: Y = B' + i * S => X + (B' - B)
44//
45// S1: X = &B [i * S]
46// S2: Y = &B'[i * S] => X + (B' - B)
47//
48// Stride Delta:
49// S1: X = B + i * S
50// S2: Y = B + i * S' => X + i * (S' - S)
51//
52// S1: X = &B[i * S]
53// S2: Y = &B[i * S'] => X + i * (S' - S)
54//
55// PS: Stride delta rewrite on Mul form is usually non-profitable, and Base
56// delta rewrite sometimes is profitable, so we do not support them on Mul.
57//
58// This rewriting is in general a good idea. The code patterns we focus on
59// usually come from loop unrolling, so the delta is likely the same
60// across iterations and can be reused. When that happens, the optimized form
61// takes only one add starting from the second iteration.
62//
63// When such rewriting is possible, we call S1 a "basis" of S2. When S2 has
64// multiple bases, we choose to rewrite S2 with respect to its "immediate"
65// basis, the basis that is the closest ancestor in the dominator tree.
66//
67// TODO:
68//
69// - Floating point arithmetics when fast math is enabled.
70
72#include "ScalarOptions.h"
73#include "llvm/ADT/APInt.h"
75#include "llvm/ADT/SetVector.h"
78#include "llvm/ADT/Statistic.h"
83#include "llvm/IR/Constants.h"
84#include "llvm/IR/DataLayout.h"
86#include "llvm/IR/Dominators.h"
88#include "llvm/IR/IRBuilder.h"
89#include "llvm/IR/Instruction.h"
91#include "llvm/IR/Module.h"
92#include "llvm/IR/Operator.h"
94#include "llvm/IR/Type.h"
95#include "llvm/IR/Value.h"
97#include "llvm/Pass.h"
103#include <cassert>
104#include <cstdint>
105#include <limits>
106#include <list>
107#include <queue>
108#include <vector>
109
110using namespace llvm;
111using namespace PatternMatch;
112
113#define DEBUG_TYPE "slsr"
114
115static const unsigned UnknownAddressSpace =
116 std::numeric_limits<unsigned>::max();
117
118DEBUG_COUNTER(StraightLineStrengthReduceCounter, "slsr-counter",
119 "Controls whether rewriteCandidate is executed.");
120
121STATISTIC(NumSCEVCandidateBasisDifferences,
122 "Number of candidate-basis SCEV differences computed by SLSR");
123
124namespace {
125
126class StraightLineStrengthReduceLegacyPass : public FunctionPass {
127 const DataLayout *DL = nullptr;
128
129public:
130 static char ID;
131
132 StraightLineStrengthReduceLegacyPass() : FunctionPass(ID) {
135 }
136
137 void getAnalysisUsage(AnalysisUsage &AU) const override {
138 AU.addRequired<DominatorTreeWrapperPass>();
139 AU.addRequired<ScalarEvolutionWrapperPass>();
140 AU.addRequired<TargetTransformInfoWrapperPass>();
141 // We do not modify the shape of the CFG.
142 AU.setPreservesCFG();
143 }
144
145 bool doInitialization(Module &M) override {
146 DL = &M.getDataLayout();
147 return false;
148 }
149
150 bool runOnFunction(Function &F) override;
151};
152
153class StraightLineStrengthReduce {
154public:
155 StraightLineStrengthReduce(const DataLayout *DL, DominatorTree *DT,
156 ScalarEvolution *SE, TargetTransformInfo *TTI)
157 : DL(DL), DT(DT), SE(SE), TTI(TTI) {}
158
159 // SLSR candidate. Such a candidate must be in one of the forms described in
160 // the header comments.
161 struct Candidate {
162 enum Kind {
163 Invalid, // reserved for the default constructor
164 Add, // B + i * S
165 Mul, // (B + i) * S
166 GEP, // &B[..][i * S][..]
167 };
168
169 enum DKind {
170 InvalidDelta, // reserved for the default constructor
171 IndexDelta, // Delta is a constant from Index
172 BaseDelta, // Delta is a constant or variable from Base
173 StrideDelta, // Delta is a constant or variable from Stride
174 };
175
176 Candidate() = default;
177 Candidate(Kind CT, const SCEV *B, ConstantInt *Idx, Value *S,
178 Instruction *I, const SCEV *StrideSCEV)
179 : CandidateKind(CT), Base(B), Index(Idx), Stride(S), Ins(I),
180 StrideSCEV(StrideSCEV) {}
181
182 Kind CandidateKind = Invalid;
183
184 const SCEV *Base = nullptr;
185 // TODO: Swap Index and Stride's name.
186 // Note that Index and Stride of a GEP candidate do not necessarily have the
187 // same integer type. In that case, during rewriting, Stride will be
188 // sign-extended or truncated to Index's type.
189 ConstantInt *Index = nullptr;
190
191 Value *Stride = nullptr;
192
193 // The instruction this candidate corresponds to. It helps us to rewrite a
194 // candidate with respect to its immediate basis. Note that one instruction
195 // can correspond to multiple candidates depending on how you associate the
196 // expression. For instance,
197 //
198 // (a + 1) * (b + 2)
199 //
200 // can be treated as
201 //
202 // <Base: a, Index: 1, Stride: b + 2>
203 //
204 // or
205 //
206 // <Base: b, Index: 2, Stride: a + 1>
207 Instruction *Ins = nullptr;
208
209 // Points to the immediate basis of this candidate, or nullptr if we cannot
210 // find any basis for this candidate.
211 Candidate *Basis = nullptr;
212
213 DKind DeltaKind = InvalidDelta;
214
215 // Store SCEV of Stride to compute delta from different strides
216 const SCEV *StrideSCEV = nullptr;
217
218 // Points to (Y - X) that will be used to rewrite this candidate.
219 Value *Delta = nullptr;
220
221 // List of instructions whose poison-generating annotations must be dropped
222 // if this candidate is used as the basis of an executed rewrite.
223 SmallVector<Instruction *> DropList;
224
225 /// Cost model: Evaluate the computational efficiency of the candidate.
226 ///
227 /// Efficiency levels (higher is better):
228 /// ZeroInst (5) - [Variable] or [Const]
229 /// OneInstOneVar (4) - [Variable + Const] or [Variable * Const]
230 /// OneInstTwoVar (3) - [Variable + Variable] or [Variable * Variable]
231 /// TwoInstOneVar (2) - [Const + Const * Variable]
232 /// TwoInstTwoVar (1) - [Variable + Const * Variable]
233 enum EfficiencyLevel : unsigned {
234 Unknown = 0,
235 TwoInstTwoVar = 1,
236 TwoInstOneVar = 2,
237 OneInstTwoVar = 3,
238 OneInstOneVar = 4,
239 ZeroInst = 5
240 };
241
242 static EfficiencyLevel
243 getComputationEfficiency(Kind CandidateKind, const ConstantInt *Index,
244 const Value *Stride, const SCEV *Base = nullptr) {
245 bool IsConstantBase = false;
246 bool IsZeroBase = false;
247 // When evaluating the efficiency of a rewrite, if the Base's SCEV is
248 // not available, conservatively assume the base is not constant.
249 if (auto *ConstBase = dyn_cast_or_null<SCEVConstant>(Base)) {
250 IsConstantBase = true;
251 IsZeroBase = ConstBase->getValue()->isZero();
252 }
253
254 bool IsConstantStride = isa<ConstantInt>(Stride);
255 bool IsZeroStride =
256 IsConstantStride && cast<ConstantInt>(Stride)->isZero();
257 // All constants
258 if (IsConstantBase && IsConstantStride)
259 return ZeroInst;
260
261 // (Base + Index) * Stride
262 if (CandidateKind == Mul) {
263 if (IsZeroStride)
264 return ZeroInst;
265 if (Index->isZero())
266 return (IsConstantStride || IsConstantBase) ? OneInstOneVar
267 : OneInstTwoVar;
268
269 if (IsConstantBase)
270 return IsZeroBase && (Index->isOne() || Index->isMinusOne())
271 ? ZeroInst
272 : OneInstOneVar;
273
274 if (IsConstantStride) {
275 auto *CI = cast<ConstantInt>(Stride);
276 return (CI->isOne() || CI->isMinusOne()) ? OneInstOneVar
277 : TwoInstOneVar;
278 }
279 return TwoInstTwoVar;
280 }
281
282 // Base + Index * Stride
283 assert(CandidateKind == Add || CandidateKind == GEP);
284 if (Index->isZero() || IsZeroStride)
285 return ZeroInst;
286
287 bool IsSimpleIndex = Index->isOne() || Index->isMinusOne();
288
289 if (IsConstantBase)
290 return IsZeroBase ? (IsSimpleIndex ? ZeroInst : OneInstOneVar)
291 : (IsSimpleIndex ? OneInstOneVar : TwoInstOneVar);
292
293 if (IsConstantStride)
294 return IsZeroStride ? ZeroInst : OneInstOneVar;
295
296 if (IsSimpleIndex)
297 return OneInstTwoVar;
298
299 return TwoInstTwoVar;
300 }
301
302 // Evaluate if the given delta is profitable to rewrite this candidate.
303 bool isProfitableRewrite(const Value &Delta, const DKind DeltaKind) const {
304 // This function cannot accurately evaluate the profit of whole expression
305 // with context. A candidate (B + I * S) cannot express whether this
306 // instruction needs to compute on its own (I * S), which may be shared
307 // with other candidates or may need instructions to compute.
308 // If the rewritten form has the same strength, still rewrite to
309 // (X + Delta) since it may expose more CSE opportunities on Delta, as
310 // unrolled loops usually have identical Delta for each unrolled body.
311 //
312 // Note, this function should only be used on Index Delta rewrite.
313 // Base and Stride delta need context info to evaluate the register
314 // pressure impact from variable delta.
315 return getComputationEfficiency(CandidateKind, Index, Stride, Base) <=
316 getRewriteEfficiency(Delta, DeltaKind);
317 }
318
319 // Evaluate the rewrite efficiency of this candidate with its Basis
320 EfficiencyLevel getRewriteEfficiency() const {
321 return Basis ? getRewriteEfficiency(*Delta, DeltaKind) : Unknown;
322 }
323
324 // Evaluate the rewrite efficiency of this candidate with a given delta
325 EfficiencyLevel getRewriteEfficiency(const Value &Delta,
326 const DKind DeltaKind) const {
327 switch (DeltaKind) {
328 case BaseDelta: // [X + Delta]
329 return getComputationEfficiency(
330 CandidateKind,
331 ConstantInt::get(cast<IntegerType>(Delta.getType()), 1), &Delta);
332 case StrideDelta: // [X + Index * Delta]
333 return getComputationEfficiency(CandidateKind, Index, &Delta);
334 case IndexDelta: // [X + Delta * Stride]
335 return getComputationEfficiency(CandidateKind,
336 cast<ConstantInt>(&Delta), Stride);
337 default:
338 return Unknown;
339 }
340 }
341
342 bool isHighEfficiency() const {
343 return getComputationEfficiency(CandidateKind, Index, Stride, Base) >=
344 OneInstOneVar;
345 }
346
347 // Verify that this candidate has valid delta components relative to the
348 // basis
349 bool hasValidDelta(const Candidate &Basis) const {
350 switch (DeltaKind) {
351 case IndexDelta:
352 // Index differs, Base and Stride must match
353 return Base == Basis.Base && StrideSCEV == Basis.StrideSCEV;
354 case StrideDelta:
355 // Stride differs, Base and Index must match
356 return Base == Basis.Base && Index == Basis.Index;
357 case BaseDelta:
358 // Base differs, Stride and Index must match
359 return StrideSCEV == Basis.StrideSCEV && Index == Basis.Index;
360 default:
361 return false;
362 }
363 }
364 };
365
366 bool runOnFunction(Function &F);
367
368private:
369 // Fetch straight-line basis for rewriting C, update C.Basis to point to it,
370 // and store the delta between C and its Basis in C.Delta.
371 void setBasisAndDeltaFor(Candidate &C);
372 // Returns whether the candidate can be folded into an addressing mode.
373 bool isFoldable(const Candidate &C, TargetTransformInfo *TTI);
374
375 // Checks whether I is in a candidate form. If so, adds all the matching forms
376 // to Candidates, and tries to find the immediate basis for each of them.
377 void allocateCandidatesAndFindBasis(Instruction *I);
378
379 // Allocate candidates and find bases for Add instructions.
380 void allocateCandidatesAndFindBasisForAdd(Instruction *I);
381
382 // Given I = LHS + RHS, factors RHS into i * S and makes (LHS + i * S) a
383 // candidate.
384 void allocateCandidatesAndFindBasisForAdd(Value *LHS, Value *RHS,
385 Instruction *I);
386 // Allocate candidates and find bases for Mul instructions.
387 void allocateCandidatesAndFindBasisForMul(Instruction *I);
388
389 // Splits LHS into Base + Index and, if succeeds, calls
390 // allocateCandidatesAndFindBasis.
391 void allocateCandidatesAndFindBasisForMul(Value *LHS, Value *RHS,
392 Instruction *I);
393
394 // Allocate candidates and find bases for GetElementPtr instructions.
395 void allocateCandidatesAndFindBasisForGEP(GetElementPtrInst *GEP);
396
397 // Adds the given form <CT, B, Idx, S> to Candidates, and finds its immediate
398 // basis.
399 void allocateCandidatesAndFindBasis(Candidate::Kind CT, const SCEV *B,
400 ConstantInt *Idx, Value *S,
401 Instruction *I);
402
403 // Rewrites candidate C with respect to Basis.
404 void rewriteCandidate(const Candidate &C);
405
406 // Emit code that computes the "bump" from Basis to C.
407 static Value *emitBump(const Candidate &Basis, const Candidate &C,
408 IRBuilder<> &Builder, const DataLayout *DL);
409
410 const DataLayout *DL = nullptr;
411 DominatorTree *DT = nullptr;
412 ScalarEvolution *SE;
413 TargetTransformInfo *TTI = nullptr;
414 std::list<Candidate> Candidates;
415
416 // Map from SCEV to instructions that represent the value,
417 // instructions are sorted in depth-first order.
418 DenseMap<const SCEV *, SmallSetVector<Instruction *, 2>> SCEVToInsts;
419
420 using SCEVUnknownSet = SmallPtrSet<const SCEVUnknown *, 4>;
421 DenseMap<const SCEV *, SCEVUnknownSet> SCEVUnknownsCache;
422
423 // Record the dependency between instructions. If C.Basis == B, we would have
424 // {B.Ins -> {C.Ins, ...}}.
425 MapVector<Instruction *, std::vector<Instruction *>> DependencyGraph;
426
427 // Map between each instruction and its possible candidates.
428 DenseMap<Instruction *, SmallVector<Candidate *, 3>> RewriteCandidates;
429
430 // All instructions that have candidates sort in topological order based on
431 // dependency graph, from roots to leaves.
432 std::vector<Instruction *> SortedCandidateInsts;
433
434 // Record all instructions that are already rewritten and will be removed
435 // later.
436 std::vector<Instruction *> DeadInstructions;
437
438 // Classify candidates against Delta kind
439 class CandidateDictTy {
440 public:
441 using CandsTy = SmallVector<Candidate *, 8>;
442 using BBToCandsTy = DenseMap<const BasicBlock *, CandsTy>;
443
444 private:
445 // Index delta Basis must have the same (Base, StrideSCEV, Inst.Type)
446 using IndexDeltaKeyTy = std::tuple<const SCEV *, const SCEV *, Type *>;
447 DenseMap<IndexDeltaKeyTy, BBToCandsTy> IndexDeltaCandidates;
448
449 // Base delta Basis must have the same (StrideSCEV, Index, Inst.Type)
450 using BaseDeltaKeyTy = std::tuple<const SCEV *, ConstantInt *, Type *>;
451 DenseMap<BaseDeltaKeyTy, BBToCandsTy> BaseDeltaCandidates;
452
453 // Stride delta Basis must have the same (Base, Index, Inst.Type)
454 using StrideDeltaKeyTy = std::tuple<const SCEV *, ConstantInt *, Type *>;
455 DenseMap<StrideDeltaKeyTy, BBToCandsTy> StrideDeltaCandidates;
456
457 public:
458 // TODO: Disable index delta on GEP after we completely move
459 // from typed GEP to PtrAdd.
460 const BBToCandsTy *getCandidatesWithDeltaKind(const Candidate &C,
461 Candidate::DKind K) const {
462 assert(K != Candidate::InvalidDelta);
463 if (K == Candidate::IndexDelta) {
464 IndexDeltaKeyTy IndexDeltaKey(C.Base, C.StrideSCEV, C.Ins->getType());
465 auto It = IndexDeltaCandidates.find(IndexDeltaKey);
466 if (It != IndexDeltaCandidates.end())
467 return &It->second;
468 } else if (K == Candidate::BaseDelta) {
469 BaseDeltaKeyTy BaseDeltaKey(C.StrideSCEV, C.Index, C.Ins->getType());
470 auto It = BaseDeltaCandidates.find(BaseDeltaKey);
471 if (It != BaseDeltaCandidates.end())
472 return &It->second;
473 } else {
474 assert(K == Candidate::StrideDelta);
475 StrideDeltaKeyTy StrideDeltaKey(C.Base, C.Index, C.Ins->getType());
476 auto It = StrideDeltaCandidates.find(StrideDeltaKey);
477 if (It != StrideDeltaCandidates.end())
478 return &It->second;
479 }
480 return nullptr;
481 }
482
483 // Pointers to C must remain valid until CandidateDict is cleared.
484 void add(Candidate &C) {
485 Type *ValueType = C.Ins->getType();
486 BasicBlock *BB = C.Ins->getParent();
487 IndexDeltaKeyTy IndexDeltaKey(C.Base, C.StrideSCEV, ValueType);
488 BaseDeltaKeyTy BaseDeltaKey(C.StrideSCEV, C.Index, ValueType);
489 StrideDeltaKeyTy StrideDeltaKey(C.Base, C.Index, ValueType);
490 IndexDeltaCandidates[IndexDeltaKey][BB].push_back(&C);
491 BaseDeltaCandidates[BaseDeltaKey][BB].push_back(&C);
492 StrideDeltaCandidates[StrideDeltaKey][BB].push_back(&C);
493 }
494 // Remove all mappings from set
495 void clear() {
496 IndexDeltaCandidates.clear();
497 BaseDeltaCandidates.clear();
498 StrideDeltaCandidates.clear();
499 }
500 } CandidateDict;
501
502 const SCEV *getAndRecordSCEV(Value *V) {
503 auto *S = SE->getSCEV(V);
506 SCEVToInsts[S].insert(cast<Instruction>(V));
507
508 return S;
509 }
510
511 bool candidatePredicate(Candidate *Basis, Candidate &C, Candidate::DKind K);
512
513 bool hasSameSCEVUnknowns(const SCEV *A, const SCEV *B);
514
515 bool searchFrom(const CandidateDictTy::BBToCandsTy &BBToCands, Candidate &C,
516 Candidate::DKind K);
517
518 // Get the nearest instruction before CI that represents the value of S,
519 // return nullptr if no instruction is associated with S or S is not a
520 // reusable expression.
521 Value *getNearestValueOfSCEV(const SCEV *S, const Instruction *CI) const {
523 return nullptr;
524
525 if (auto *SU = dyn_cast<SCEVUnknown>(S))
526 return SU->getValue();
527 if (auto *SC = dyn_cast<SCEVConstant>(S))
528 return SC->getValue();
529
530 auto It = SCEVToInsts.find(S);
531 if (It == SCEVToInsts.end())
532 return nullptr;
533
534 // Instructions are sorted in depth-first order, so search for the nearest
535 // instruction by walking the list in reverse order.
536 for (Instruction *I : reverse(It->second))
537 if (DT->dominates(I, CI))
538 return I;
539
540 return nullptr;
541 }
542
543 struct DeltaInfo {
544 Candidate *Cand;
545 Candidate::DKind DeltaKind;
546 Value *Delta;
547
548 DeltaInfo()
549 : Cand(nullptr), DeltaKind(Candidate::InvalidDelta), Delta(nullptr) {}
550 DeltaInfo(Candidate *Cand, Candidate::DKind DeltaKind, Value *Delta)
551 : Cand(Cand), DeltaKind(DeltaKind), Delta(Delta) {}
552 operator bool() const { return Cand != nullptr; }
553 };
554
555 friend raw_ostream &operator<<(raw_ostream &OS, const DeltaInfo &DI);
556
557 DeltaInfo compressPath(Candidate &C, Candidate *Basis) const;
558
559 Candidate *pickRewriteCandidate(Instruction *I) const;
560 void sortCandidateInstructions();
561 Value *getDelta(const Candidate &C, const Candidate &Basis,
562 Candidate::DKind K) const;
563 static bool isSimilar(Candidate &C, Candidate &Basis, Candidate::DKind K);
564
565 // Add Basis -> C in DependencyGraph and propagate
566 // C.Stride and C.Delta's dependency to C
567 void addDependency(Candidate &C, Candidate *Basis) {
568 if (Basis)
569 DependencyGraph[Basis->Ins].emplace_back(C.Ins);
570
571 // If any candidate of Inst has a basis, then Inst will be rewritten,
572 // C must be rewritten after rewriting Inst, so we need to propagate
573 // the dependency to C
574 auto PropagateDependency = [&](Instruction *Inst) {
575 if (auto CandsIt = RewriteCandidates.find(Inst);
576 CandsIt != RewriteCandidates.end() &&
577 llvm::any_of(CandsIt->second,
578 [](Candidate *Cand) { return Cand->Basis; }))
579 DependencyGraph[Inst].emplace_back(C.Ins);
580 };
581
582 // If C has a variable delta and the delta is a candidate,
583 // propagate its dependency to C
584 if (auto *DeltaInst = dyn_cast_or_null<Instruction>(C.Delta))
585 PropagateDependency(DeltaInst);
586
587 // If the stride is a candidate, propagate its dependency to C
588 if (auto *StrideInst = dyn_cast<Instruction>(C.Stride))
589 PropagateDependency(StrideInst);
590 };
591};
592
594 const StraightLineStrengthReduce::Candidate &C) {
595 OS << "Ins: " << *C.Ins << "\n Base: " << *C.Base
596 << "\n Index: " << *C.Index << "\n Stride: " << *C.Stride
597 << "\n StrideSCEV: " << *C.StrideSCEV;
598 if (C.Basis)
599 OS << "\n Delta: " << *C.Delta << "\n Basis: \n [ " << *C.Basis << " ]";
600 return OS;
601}
602
603[[maybe_unused]] LLVM_DUMP_METHOD inline raw_ostream &
604operator<<(raw_ostream &OS, const StraightLineStrengthReduce::DeltaInfo &DI) {
605 OS << "Cand: " << *DI.Cand << "\n";
606 OS << "Delta Kind: ";
607 switch (DI.DeltaKind) {
608 case StraightLineStrengthReduce::Candidate::IndexDelta:
609 OS << "Index";
610 break;
611 case StraightLineStrengthReduce::Candidate::BaseDelta:
612 OS << "Base";
613 break;
614 case StraightLineStrengthReduce::Candidate::StrideDelta:
615 OS << "Stride";
616 break;
617 default:
618 break;
619 }
620 OS << "\nDelta: " << *DI.Delta;
621 return OS;
622}
623
624} // end anonymous namespace
625
626char StraightLineStrengthReduceLegacyPass::ID = 0;
627
628INITIALIZE_PASS_BEGIN(StraightLineStrengthReduceLegacyPass, "slsr",
629 "Straight line strength reduction", false, false)
633INITIALIZE_PASS_END(StraightLineStrengthReduceLegacyPass, "slsr",
634 "Straight line strength reduction", false, false)
635
637 return new StraightLineStrengthReduceLegacyPass();
638}
639
640// A helper function that unifies the bitwidth of A and B.
641static void unifyBitWidth(APInt &A, APInt &B) {
642 if (A.getBitWidth() < B.getBitWidth())
643 A = A.sext(B.getBitWidth());
644 else if (A.getBitWidth() > B.getBitWidth())
645 B = B.sext(A.getBitWidth());
646}
647
648// Whether sign-extending V to a wider type may not distribute over arithmetic,
649// i.e. the narrow value does not sign-extend linearly. Only an add/sub/mul/shl
650// carrying the `nsw` flag is known to sign-extend linearly; anything else is
651// treated conservatively as possibly wrapping. This notably covers
652// `xor X, signmask`, which merely flips the sign bit but ScalarEvolution models
653// as a non-nsw `add X, signmask` (so sext does not distribute over it).
654static bool mayHaveSignedWrap(const Value *V) {
655 // OverflowingBinaryOperator covers exactly add/sub/mul/shl.
656 const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V);
657 return !OBO || !OBO->hasNoSignedWrap();
658}
659
660// True when the GEP index is narrower than the index width, i.e. it is
661// implicitly sign-extended to the index width (not the pointer width) of the
662// address space before the address computation. A value already at or wider
663// than the index width is not sign-extended (it is used as-is or truncated), so
664// it cannot trigger the non-distributing-sext problem.
666 const DataLayout *DL) {
667 return Idx->getType()->getIntegerBitWidth() <
668 DL->getIndexSizeInBits(GEP->getAddressSpace());
669}
670
671// A narrow GEP index is sign-extended to the index width before the address
672// computation. SLSR's Stride-delta rewrite turns two such GEPs into
673// Basis + Index * (Sc - Sb), so the stride difference Sc - Sb is reconstructed
674// in the sign-extended domain. This requires sext(Sc) == sext(Sb) +
675// sext(Delta).
676//
677// This screens the rewritten candidate's stride Sc = Sb + Delta: if Sc is
678// computed by a possibly-wrapping op, sext(Sc) does not equal sext(Sb) +
679// sext(Delta) and the rewrite would produce a wrong pointer.
681 const DataLayout *DL) {
682 return !isSignExtendedGepIndex(Idx, GEP, DL) || !mayHaveSignedWrap(Idx);
683}
684
685Value *StraightLineStrengthReduce::getDelta(const Candidate &C,
686 const Candidate &Basis,
687 Candidate::DKind K) const {
688 if (K == Candidate::IndexDelta) {
689 APInt Idx = C.Index->getValue();
690 APInt BasisIdx = Basis.Index->getValue();
691 unifyBitWidth(Idx, BasisIdx);
692 APInt IndexDelta = Idx - BasisIdx;
693 IntegerType *DeltaType =
694 IntegerType::get(C.Ins->getContext(), IndexDelta.getBitWidth());
695 return ConstantInt::get(DeltaType, IndexDelta);
696 } else if (K == Candidate::BaseDelta || K == Candidate::StrideDelta) {
697 const SCEV *BasisPart =
698 (K == Candidate::BaseDelta) ? Basis.Base : Basis.StrideSCEV;
699 const SCEV *CandPart = (K == Candidate::BaseDelta) ? C.Base : C.StrideSCEV;
700 ++NumSCEVCandidateBasisDifferences;
701 const SCEV *Diff = SE->getMinusSCEV(CandPart, BasisPart);
702 return getNearestValueOfSCEV(Diff, C.Ins);
703 }
704 return nullptr;
705}
706
707bool StraightLineStrengthReduce::isSimilar(Candidate &C, Candidate &Basis,
708 Candidate::DKind K) {
709 bool SameType = false;
710 switch (K) {
711 case Candidate::StrideDelta:
712 SameType = C.StrideSCEV->getType() == Basis.StrideSCEV->getType();
713 break;
714 case Candidate::BaseDelta:
715 SameType = C.Base->getType() == Basis.Base->getType();
716 break;
717 case Candidate::IndexDelta:
718 SameType = true;
719 break;
720 default:;
721 }
722 return SameType && Basis.Ins != C.Ins &&
723 Basis.CandidateKind == C.CandidateKind;
724}
725
726bool StraightLineStrengthReduce::hasSameSCEVUnknowns(const SCEV *A,
727 const SCEV *B) {
728 auto CacheUnknowns = [&](const SCEV *Root) {
729 auto [It, Inserted] = SCEVUnknownsCache.try_emplace(Root);
730 if (!Inserted)
731 return;
732
733 struct Collector {
734 SCEVUnknownSet &Unknowns;
735
736 bool follow(const SCEV *S) {
737 if (auto *Unknown = dyn_cast<SCEVUnknown>(S))
738 Unknowns.insert(Unknown);
739 return true;
740 }
741 bool isDone() const { return false; }
742 } C{It->second};
743 visitAll(Root, C);
744 };
745 CacheUnknowns(A);
746 CacheUnknowns(B);
747
748 return SCEVUnknownsCache.find(A)->second == SCEVUnknownsCache.find(B)->second;
749}
750
751// Try to find a Delta that C can reuse Basis to rewrite.
752// Set C.Delta, C.Basis, and C.DeltaKind if found.
753// Return true if found a constant delta.
754// Return false if not found or the delta is not a constant.
755bool StraightLineStrengthReduce::candidatePredicate(Candidate *Basis,
756 Candidate &C,
757 Candidate::DKind K) {
758 if (!isSimilar(C, *Basis, K))
759 return false;
760
761 // Once a reusable delta is found, only a constant delta can improve it.
762 // Different symbolic leaves cannot cancel to a constant, so such a basis
763 // cannot improve C. Skip it and continue searching older candidates.
764 if (C.Delta && K != Candidate::IndexDelta) {
765 const SCEV *CandidateSCEV =
766 K == Candidate::BaseDelta ? C.Base : C.StrideSCEV;
767 const SCEV *BasisSCEV =
768 K == Candidate::BaseDelta ? Basis->Base : Basis->StrideSCEV;
769 if (!hasSameSCEVUnknowns(CandidateSCEV, BasisSCEV))
770 return false;
771 }
772
773 assert(DT->dominates(Basis->Ins, C.Ins));
774 Value *Delta = getDelta(C, *Basis, K);
775 if (!Delta)
776 return false;
777
778 // For a GEP Stride-delta rewrite g2 = g1 + Index * Delta, the addresses are
779 // computed from the sign-extended strides, so this requires
780 // sext(Sc) == sext(Sb) + sext(Delta).
781 //
782 // The rewritten candidate's stride Sc = Sb + Delta is already screened
783 // broadly at allocation time (allocateCandidatesAndFindBasis): a wrapping Sc
784 // breaks the identity for any Delta. The basis's stride Sb = Sc - Delta only
785 // needs screening when Delta folds to a *constant*: then sext(Sb) + C can
786 // differ from sext(Sc) if Sb wraps. For a *variable* Delta the basis may wrap
787 // and still be sound, because the candidate stride carries the no-wrap
788 // guarantee (e.g. Sc is an `add nsw`, as in stride_var); rejecting it would
789 // pessimize those.
790 if (K == Candidate::StrideDelta && C.CandidateKind == Candidate::GEP &&
791 isa<ConstantInt>(Delta)) {
792 auto *BasisGEP = cast<GetElementPtrInst>(Basis->Ins);
793 if (!isSafeToFactorGepIndex(Basis->Stride, BasisGEP, DL))
794 return false;
795 }
796
797 // IndexDelta rewrite is not always profitable, e.g.,
798 // X = B + 8 * S
799 // Y = B + S,
800 // rewriting Y to X - 7 * S is probably a bad idea.
801 // So, we need to check if the rewrite form's computation efficiency
802 // is better than the original form.
803 if (K == Candidate::IndexDelta &&
804 !C.isProfitableRewrite(*Delta, Candidate::IndexDelta))
805 return false;
806
807 // Record delta if none has been found yet, or the new delta is
808 // a constant that is better than the existing delta.
809 if (!C.Delta || isa<ConstantInt>(Delta)) {
810 C.Delta = Delta;
811 C.Basis = Basis;
812 C.DeltaKind = K;
813 }
814 return isa<ConstantInt>(C.Delta);
815}
816
817// return true if find a Basis with constant delta and stop searching,
818// return false if did not find a Basis or the delta is not a constant
819// and continue searching for a Basis with constant delta
820bool StraightLineStrengthReduce::searchFrom(
821 const CandidateDictTy::BBToCandsTy &BBToCands, Candidate &C,
822 Candidate::DKind K) {
823
824 // Stride delta rewrite on Mul form is usually non-profitable, and Base
825 // delta rewrite sometimes is profitable, so we do not support them on Mul.
826 if (C.CandidateKind == Candidate::Mul && K != Candidate::IndexDelta)
827 return false;
828
829 // Search dominating candidates by walking the immediate-dominator chain
830 // from the candidate's defining block upward. Visiting blocks in this
831 // order ensures we prefer the closest dominating basis.
832 const BasicBlock *BB = C.Ins->getParent();
833 while (BB) {
834 auto It = BBToCands.find(BB);
835 if (It != BBToCands.end())
836 for (Candidate *Basis : reverse(It->second))
837 if (candidatePredicate(Basis, C, K))
838 return true;
839
840 const DomTreeNode *Node = DT->getNode(BB);
841 if (!Node)
842 break;
843 Node = Node->getIDom();
844 BB = Node ? Node->getBlock() : nullptr;
845 }
846 return false;
847}
848
849void StraightLineStrengthReduce::setBasisAndDeltaFor(Candidate &C) {
850 if (const auto *BaseDeltaCandidates =
851 CandidateDict.getCandidatesWithDeltaKind(C, Candidate::BaseDelta))
852 if (searchFrom(*BaseDeltaCandidates, C, Candidate::BaseDelta)) {
853 LLVM_DEBUG(dbgs() << "Found delta from Base: " << *C.Delta << "\n");
854 return;
855 }
856
857 if (const auto *StrideDeltaCandidates =
858 CandidateDict.getCandidatesWithDeltaKind(C, Candidate::StrideDelta))
859 if (searchFrom(*StrideDeltaCandidates, C, Candidate::StrideDelta)) {
860 LLVM_DEBUG(dbgs() << "Found delta from Stride: " << *C.Delta << "\n");
861 return;
862 }
863
864 if (const auto *IndexDeltaCandidates =
865 CandidateDict.getCandidatesWithDeltaKind(C, Candidate::IndexDelta))
866 if (searchFrom(*IndexDeltaCandidates, C, Candidate::IndexDelta)) {
867 LLVM_DEBUG(dbgs() << "Found delta from Index: " << *C.Delta << "\n");
868 return;
869 }
870
871 // If we did not find a constant delta, we might have found a variable delta
872 if (C.Delta) {
873 LLVM_DEBUG({
874 dbgs() << "Found delta from ";
875 if (C.DeltaKind == Candidate::BaseDelta)
876 dbgs() << "Base: ";
877 else
878 dbgs() << "Stride: ";
879 dbgs() << *C.Delta << "\n";
880 });
881 assert(C.DeltaKind != Candidate::InvalidDelta && C.Basis);
882 }
883}
884
885// Compress the path from `Basis` to the deepest Basis in the Basis chain
886// to avoid non-profitable data dependency and improve ILP.
887// X = A + 1
888// Y = X + 1
889// Z = Y + 1
890// ->
891// X = A + 1
892// Y = A + 2
893// Z = A + 3
894// Return the delta info for C aginst the new Basis
895auto StraightLineStrengthReduce::compressPath(Candidate &C,
896 Candidate *Basis) const
897 -> DeltaInfo {
898 if (!Basis || !Basis->Basis || C.CandidateKind == Candidate::Mul)
899 return {};
900 Candidate *Root = Basis;
901 Value *NewDelta = nullptr;
902 auto NewKind = Candidate::InvalidDelta;
903
904 while (Root->Basis) {
905 Candidate *NextRoot = Root->Basis;
906 if (C.Base == NextRoot->Base && C.StrideSCEV == NextRoot->StrideSCEV &&
907 isSimilar(C, *NextRoot, Candidate::IndexDelta)) {
908 ConstantInt *CI =
909 cast<ConstantInt>(getDelta(C, *NextRoot, Candidate::IndexDelta));
910 if (CI->isZero() || CI->isOne() || isa<SCEVConstant>(C.StrideSCEV)) {
911 Root = NextRoot;
912 NewKind = Candidate::IndexDelta;
913 NewDelta = CI;
914 continue;
915 }
916 }
917
918 const SCEV *CandPart = nullptr;
919 const SCEV *BasisPart = nullptr;
920 auto CurrKind = Candidate::InvalidDelta;
921 if (C.Base == NextRoot->Base && C.Index == NextRoot->Index) {
922 CandPart = C.StrideSCEV;
923 BasisPart = NextRoot->StrideSCEV;
924 CurrKind = Candidate::StrideDelta;
925 } else if (C.StrideSCEV == NextRoot->StrideSCEV &&
926 C.Index == NextRoot->Index) {
927 CandPart = C.Base;
928 BasisPart = NextRoot->Base;
929 CurrKind = Candidate::BaseDelta;
930 } else
931 break;
932
933 assert(CandPart && BasisPart);
934 if (!isSimilar(C, *NextRoot, CurrKind))
935 break;
936
937 // Path compression folds a constant Stride-delta directly against the
938 // deeper basis NextRoot, bypassing candidatePredicate's wrap guard. With a
939 // constant delta sext(Sb) + C can differ from sext(Sc) if the deeper
940 // basis's stride wraps, so do not compress past such a basis (mirrors the
941 // check in candidatePredicate).
942 if (CurrKind == Candidate::StrideDelta &&
943 C.CandidateKind == Candidate::GEP &&
944 !isSafeToFactorGepIndex(NextRoot->Stride,
945 cast<GetElementPtrInst>(NextRoot->Ins), DL))
946 break;
947
948 ++NumSCEVCandidateBasisDifferences;
949 if (auto DeltaVal =
950 dyn_cast<SCEVConstant>(SE->getMinusSCEV(CandPart, BasisPart))) {
951 Root = NextRoot;
952 NewDelta = DeltaVal->getValue();
953 NewKind = CurrKind;
954 } else
955 break;
956 }
957
958 if (Root != Basis) {
959 assert(NewKind != Candidate::InvalidDelta && NewDelta);
960 LLVM_DEBUG(dbgs() << "Found new Basis with " << *NewDelta
961 << " from path compression.\n");
962 return {Root, NewKind, NewDelta};
963 }
964
965 return {};
966}
967
968// Topologically sort candidate instructions based on their relationship in
969// dependency graph.
970void StraightLineStrengthReduce::sortCandidateInstructions() {
971 SortedCandidateInsts.clear();
972 // An instruction may have multiple candidates that get different Basis
973 // instructions, and each candidate can get dependencies from Basis and
974 // Stride when Stride will also be rewritten by SLSR. Hence, an instruction
975 // may have multiple dependencies. Use InDegree to ensure all dependencies
976 // processed before processing itself.
977 DenseMap<Instruction *, int> InDegree;
978 for (auto &KV : DependencyGraph) {
979 InDegree.try_emplace(KV.first, 0);
980
981 for (auto *Child : KV.second) {
982 InDegree[Child]++;
983 }
984 }
985 std::queue<Instruction *> WorkList;
986 DenseSet<Instruction *> Visited;
987
988 for (auto &KV : DependencyGraph)
989 if (InDegree[KV.first] == 0)
990 WorkList.push(KV.first);
991
992 while (!WorkList.empty()) {
993 Instruction *I = WorkList.front();
994 WorkList.pop();
995 if (!Visited.insert(I).second)
996 continue;
997
998 SortedCandidateInsts.push_back(I);
999
1000 for (auto *Next : DependencyGraph[I]) {
1001 auto &Degree = InDegree[Next];
1002 if (--Degree == 0)
1003 WorkList.push(Next);
1004 }
1005 }
1006
1007 assert(SortedCandidateInsts.size() == DependencyGraph.size() &&
1008 "Dependency graph should not have cycles");
1009}
1010
1011auto StraightLineStrengthReduce::pickRewriteCandidate(Instruction *I) const
1012 -> Candidate * {
1013 // Return the candidate of instruction I that has the highest profit.
1014 auto It = RewriteCandidates.find(I);
1015 if (It == RewriteCandidates.end())
1016 return nullptr;
1017
1018 Candidate *BestC = nullptr;
1019 auto BestEfficiency = Candidate::Unknown;
1020 for (Candidate *C : reverse(It->second))
1021 if (C->Basis) {
1022 auto Efficiency = C->getRewriteEfficiency();
1023 if (Efficiency > BestEfficiency) {
1024 BestEfficiency = Efficiency;
1025 BestC = C;
1026 }
1027 }
1028
1029 return BestC;
1030}
1031
1033 const TargetTransformInfo *TTI) {
1034 SmallVector<const Value *, 4> Indices(GEP->indices());
1035 return TTI->getGEPCost(
1036 GEP->getSourceElementType(), GEP->getPointerOperand(), Indices,
1039}
1040
1041// Returns whether (Base + Index * Stride) can be folded to an addressing mode.
1042static bool isAddFoldable(const SCEV *Base, ConstantInt *Index, Value *Stride,
1044 // Index->getSExtValue() may crash if Index is wider than 64-bit.
1045 return Index->getBitWidth() <= 64 &&
1046 TTI->isLegalAddressingMode(Base->getType(), nullptr, 0, true,
1047 Index->getSExtValue(), UnknownAddressSpace);
1048}
1049
1050bool StraightLineStrengthReduce::isFoldable(const Candidate &C,
1051 TargetTransformInfo *TTI) {
1052 if (C.CandidateKind == Candidate::Add)
1053 return isAddFoldable(C.Base, C.Index, C.Stride, TTI);
1054 if (C.CandidateKind == Candidate::GEP)
1056 return false;
1057}
1058
1059void StraightLineStrengthReduce::allocateCandidatesAndFindBasis(
1060 Candidate::Kind CT, const SCEV *B, ConstantInt *Idx, Value *S,
1061 Instruction *I) {
1062 bool IsSafe = CT != Candidate::GEP ||
1064 // Record the SCEV of S that we may use it as a variable delta.
1065 // Ensure that we rewrite C with a existing IR that reproduces delta value.
1066
1067 Candidate C(CT, B, Idx, S, I, getAndRecordSCEV(S));
1068 // If we can fold I into an addressing mode, computing I is likely free or
1069 // takes only one instruction. So, we don't need to analyze or rewrite it.
1070 //
1071 // Currently, this algorithm can at best optimize complex computations into
1072 // a `variable +/* constant` form. However, some targets have stricter
1073 // constraints on the their addressing mode.
1074 // For example, a `variable + constant` can only be folded to an addressing
1075 // mode if the constant falls within a certain range.
1076 // So, we also check if the instruction is already high efficient enough
1077 // for the strength reduction algorithm.
1078 if (IsSafe && !isFoldable(C, TTI) && !C.isHighEfficiency()) {
1079 setBasisAndDeltaFor(C);
1080
1081 // Compress unnecessary rewrite to improve ILP
1082 if (auto Res = compressPath(C, C.Basis)) {
1083 C.Basis = Res.Cand;
1084 C.DeltaKind = Res.DeltaKind;
1085 C.Delta = Res.Delta;
1086 }
1087 }
1088 // Regardless of whether we find a basis for C, we need to push C to the
1089 // candidate list so that it can be the basis of other candidates.
1090 LLVM_DEBUG(dbgs() << "Allocated Candidate: " << C << "\n");
1091 Candidates.push_back(C);
1092 RewriteCandidates[C.Ins].push_back(&Candidates.back());
1093 // Only add to the dict if this instruction is safe to reuse as a basis. By
1094 // doing this early we avoid calling canReuseInstruction repeatedly for the
1095 // same instruction. The DropList is stored on the Candidate so the flags can
1096 // be dropped only if this candidate is used by an executed rewrite.
1097 if (!ScalarOptions::Global.enable_poison_reuse_guard ||
1098 SE->canReuseInstruction(SE->getSCEV(I), I, Candidates.back().DropList)) {
1099 CandidateDict.add(Candidates.back());
1100 }
1101}
1102
1103void StraightLineStrengthReduce::allocateCandidatesAndFindBasis(
1104 Instruction *I) {
1105 switch (I->getOpcode()) {
1106 case Instruction::Add:
1107 allocateCandidatesAndFindBasisForAdd(I);
1108 break;
1109 case Instruction::Mul:
1110 allocateCandidatesAndFindBasisForMul(I);
1111 break;
1112 case Instruction::GetElementPtr:
1113 allocateCandidatesAndFindBasisForGEP(cast<GetElementPtrInst>(I));
1114 break;
1115 }
1116}
1117
1118void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForAdd(
1119 Instruction *I) {
1120 // Try matching B + i * S.
1121 if (!isa<IntegerType>(I->getType()))
1122 return;
1123
1124 assert(I->getNumOperands() == 2 && "isn't I an add?");
1125 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
1126 allocateCandidatesAndFindBasisForAdd(LHS, RHS, I);
1127 if (LHS != RHS)
1128 allocateCandidatesAndFindBasisForAdd(RHS, LHS, I);
1129}
1130
1131void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForAdd(
1132 Value *LHS, Value *RHS, Instruction *I) {
1133 Value *S = nullptr;
1134 ConstantInt *Idx = nullptr;
1135 if (match(RHS, m_Mul(m_Value(S), m_ConstantInt(Idx)))) {
1136 // I = LHS + RHS = LHS + Idx * S
1137 allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I);
1138 } else if (match(RHS, m_Shl(m_Value(S), m_ConstantInt(Idx)))) {
1139 // I = LHS + RHS = LHS + (S << Idx) = LHS + S * (1 << Idx)
1140 APInt One(Idx->getBitWidth(), 1);
1141 Idx = ConstantInt::get(Idx->getContext(), One << Idx->getValue());
1142 allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I);
1143 } else {
1144 // At least, I = LHS + 1 * RHS
1145 ConstantInt *One = ConstantInt::get(cast<IntegerType>(I->getType()), 1);
1146 allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), One, RHS,
1147 I);
1148 }
1149}
1150
1151// Returns true if A matches B + C where C is constant.
1152static bool matchesAdd(Value *A, Value *&B, ConstantInt *&C) {
1153 return match(A, m_c_Add(m_Value(B), m_ConstantInt(C)));
1154}
1155
1156// Returns true if A matches B | C where C is constant.
1157static bool matchesOr(Value *A, Value *&B, ConstantInt *&C) {
1158 return match(A, m_c_Or(m_Value(B), m_ConstantInt(C)));
1159}
1160
1161void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForMul(
1162 Value *LHS, Value *RHS, Instruction *I) {
1163 Value *B = nullptr;
1164 ConstantInt *Idx = nullptr;
1165 if (matchesAdd(LHS, B, Idx)) {
1166 // If LHS is in the form of "Base + Index", then I is in the form of
1167 // "(Base + Index) * RHS".
1168 allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(B), Idx, RHS, I);
1169 } else if (matchesOr(LHS, B, Idx) && haveNoCommonBitsSet(B, Idx, *DL)) {
1170 // If LHS is in the form of "Base | Index" and Base and Index have no common
1171 // bits set, then
1172 // Base | Index = Base + Index
1173 // and I is thus in the form of "(Base + Index) * RHS".
1174 allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(B), Idx, RHS, I);
1175 } else {
1176 // Otherwise, at least try the form (LHS + 0) * RHS.
1177 ConstantInt *Zero = ConstantInt::get(cast<IntegerType>(I->getType()), 0);
1178 allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(LHS), Zero, RHS,
1179 I);
1180 }
1181}
1182
1183void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForMul(
1184 Instruction *I) {
1185 // Try matching (B + i) * S.
1186 // TODO: we could extend SLSR to float and vector types.
1187 if (!isa<IntegerType>(I->getType()))
1188 return;
1189
1190 assert(I->getNumOperands() == 2 && "isn't I a mul?");
1191 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
1192 allocateCandidatesAndFindBasisForMul(LHS, RHS, I);
1193 if (LHS != RHS) {
1194 // Symmetrically, try to split RHS to Base + Index.
1195 allocateCandidatesAndFindBasisForMul(RHS, LHS, I);
1196 }
1197}
1198
1199void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForGEP(
1200 GetElementPtrInst *GEP) {
1201 // TODO: handle vector GEPs
1202 if (GEP->getType()->isVectorTy())
1203 return;
1204
1205 SmallVector<SCEVUse, 4> IndexExprs;
1206 for (Use &Idx : GEP->indices())
1207 IndexExprs.push_back(SE->getSCEV(Idx));
1208
1210 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
1211 if (GTI.isStruct())
1212 continue;
1213
1214 SCEVUse OrigIndexExpr = IndexExprs[I - 1];
1215 IndexExprs[I - 1] = SE->getZero(OrigIndexExpr.getPointer()->getType());
1216
1217 // The base of this candidate is GEP's base plus the offsets of all
1218 // indices except this current one.
1219 SCEVUse BaseExpr = SE->getGEPExpr(cast<GEPOperator>(GEP), IndexExprs);
1220 Value *ArrayIdx = GEP->getOperand(I);
1221 uint64_t ElementSize = GTI.getSequentialElementStride(*DL);
1222 IntegerType *PtrIdxTy = cast<IntegerType>(DL->getIndexType(GEP->getType()));
1223 // If the element size overflows the type, truncate.
1224 ConstantInt *ElementSizeIdx =
1225 ConstantInt::getSigned(PtrIdxTy, ElementSize, /*ImplicitTrunc=*/true);
1226 if (ArrayIdx->getType()->getIntegerBitWidth() <=
1227 DL->getIndexSizeInBits(GEP->getAddressSpace())) {
1228 // Skip factoring if ArrayIdx is wider than the index size, because
1229 // ArrayIdx is implicitly truncated to the index size.
1230 allocateCandidatesAndFindBasis(Candidate::GEP, BaseExpr, ElementSizeIdx,
1231 ArrayIdx, GEP);
1232 }
1233 // When ArrayIdx is the sext of a value, we try to factor that value as
1234 // well. Handling this case is important because array indices are
1235 // typically sign-extended to the pointer index size.
1236 Value *TruncatedArrayIdx = nullptr;
1237 if (match(ArrayIdx, m_SExt(m_Value(TruncatedArrayIdx))) &&
1238 TruncatedArrayIdx->getType()->getIntegerBitWidth() <=
1239 DL->getIndexSizeInBits(GEP->getAddressSpace())) {
1240 // Skip factoring if TruncatedArrayIdx is wider than the pointer size,
1241 // because TruncatedArrayIdx is implicitly truncated to the pointer size.
1242 allocateCandidatesAndFindBasis(Candidate::GEP, BaseExpr, ElementSizeIdx,
1243 TruncatedArrayIdx, GEP);
1244 }
1245
1246 IndexExprs[I - 1] = OrigIndexExpr;
1247 }
1248}
1249
1250Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis,
1251 const Candidate &C,
1252 IRBuilder<> &Builder,
1253 const DataLayout *DL) {
1254 auto CreateMul = [&](Value *LHS, Value *RHS) {
1255 if (ConstantInt *CR = dyn_cast<ConstantInt>(RHS)) {
1256 const APInt &ConstRHS = CR->getValue();
1257 IntegerType *DeltaType =
1258 IntegerType::get(C.Ins->getContext(), ConstRHS.getBitWidth());
1259 if (ConstRHS.isPowerOf2()) {
1260 ConstantInt *Exponent =
1261 ConstantInt::get(DeltaType, ConstRHS.logBase2());
1262 return Builder.CreateShl(LHS, Exponent);
1263 }
1264 if (ConstRHS.isNegatedPowerOf2()) {
1265 ConstantInt *Exponent =
1266 ConstantInt::get(DeltaType, (-ConstRHS).logBase2());
1267 return Builder.CreateNeg(Builder.CreateShl(LHS, Exponent));
1268 }
1269 }
1270
1271 return Builder.CreateMul(LHS, RHS);
1272 };
1273
1274 Value *Delta = C.Delta;
1275 // If Delta is 0, C is a fully redundant of C.Basis,
1276 // just replace C.Ins with Basis.Ins
1277 if (ConstantInt *CI = dyn_cast<ConstantInt>(Delta);
1278 CI && CI->getValue().isZero())
1279 return nullptr;
1280
1281 if (C.DeltaKind == Candidate::IndexDelta) {
1282 APInt IndexDelta = cast<ConstantInt>(C.Delta)->getValue();
1283 // IndexDelta
1284 // X = B + i * S
1285 // Y = B + i` * S
1286 // = B + (i + IndexDelta) * S
1287 // = B + i * S + IndexDelta * S
1288 // = X + IndexDelta * S
1289 // Bump = (i' - i) * S
1290
1291 // Common case 1: if (i' - i) is 1, Bump = S.
1292 if (IndexDelta == 1)
1293 return C.Stride;
1294 // Common case 2: if (i' - i) is -1, Bump = -S.
1295 if (IndexDelta.isAllOnes())
1296 return Builder.CreateNeg(C.Stride);
1297
1298 IntegerType *DeltaType =
1299 IntegerType::get(Basis.Ins->getContext(), IndexDelta.getBitWidth());
1300 Value *ExtendedStride = Builder.CreateSExtOrTrunc(C.Stride, DeltaType);
1301
1302 return CreateMul(ExtendedStride, C.Delta);
1303 }
1304
1305 assert(C.DeltaKind == Candidate::StrideDelta ||
1306 C.DeltaKind == Candidate::BaseDelta);
1307 assert(C.CandidateKind != Candidate::Mul);
1308 // StrideDelta
1309 // X = B + i * S
1310 // Y = B + i * S'
1311 // = B + i * (S + StrideDelta)
1312 // = B + i * S + i * StrideDelta
1313 // = X + i * StrideDelta
1314 // Bump = i * (S' - S)
1315 //
1316 // BaseDelta
1317 // X = B + i * S
1318 // Y = B' + i * S
1319 // = (B + BaseDelta) + i * S
1320 // = X + BaseDelta
1321 // Bump = (B' - B).
1322 Value *Bump = C.Delta;
1323 if (C.DeltaKind == Candidate::StrideDelta) {
1324 // If this value is consumed by a GEP, promote StrideDelta before doing
1325 // StrideDelta * Index to ensure the same semantics as the original GEP.
1326 if (C.CandidateKind == Candidate::GEP) {
1327 auto *GEP = cast<GetElementPtrInst>(C.Ins);
1328 Type *NewScalarIndexTy =
1329 DL->getIndexType(GEP->getPointerOperandType()->getScalarType());
1330 Bump = Builder.CreateSExtOrTrunc(Bump, NewScalarIndexTy);
1331 }
1332 if (!C.Index->isOne()) {
1333 Value *ExtendedIndex =
1334 Builder.CreateSExtOrTrunc(C.Index, Bump->getType());
1335 Bump = CreateMul(Bump, ExtendedIndex);
1336 }
1337 }
1338 return Bump;
1339}
1340
1341void StraightLineStrengthReduce::rewriteCandidate(const Candidate &C) {
1342 if (!DebugCounter::shouldExecute(StraightLineStrengthReduceCounter))
1343 return;
1344
1345 const Candidate &Basis = *C.Basis;
1346 assert(C.Delta && C.CandidateKind == Basis.CandidateKind &&
1347 C.hasValidDelta(Basis));
1348
1349 for (Instruction *I : Basis.DropList)
1350 I->dropPoisonGeneratingAnnotations();
1351
1352 IRBuilder<> Builder(C.Ins);
1353 Value *Bump = emitBump(Basis, C, Builder, DL);
1354 Value *Reduced = nullptr; // equivalent to but weaker than C.Ins
1355 // If delta is 0, C is a fully redundant of Basis, and Bump is nullptr,
1356 // just replace C.Ins with Basis.Ins
1357 if (!Bump)
1358 Reduced = Basis.Ins;
1359 else {
1360 switch (C.CandidateKind) {
1361 case Candidate::Add:
1362 case Candidate::Mul: {
1363 // C = Basis + Bump
1364 Value *NegBump;
1365 if (match(Bump, m_Neg(m_Value(NegBump)))) {
1366 // If Bump is a neg instruction, emit C = Basis - (-Bump).
1367 Reduced = Builder.CreateSub(Basis.Ins, NegBump);
1368 // We only use the negative argument of Bump, and Bump itself may be
1369 // trivially dead.
1371 } else {
1372 // It's tempting to preserve nsw on Bump and/or Reduced. However, it's
1373 // usually unsound, e.g.,
1374 //
1375 // X = (-2 +nsw 1) *nsw INT_MAX
1376 // Y = (-2 +nsw 3) *nsw INT_MAX
1377 // =>
1378 // Y = X + 2 * INT_MAX
1379 //
1380 // Neither + and * in the resultant expression are nsw.
1381 Reduced = Builder.CreateAdd(Basis.Ins, Bump);
1382 }
1383 break;
1384 }
1385 case Candidate::GEP: {
1386 bool InBounds = cast<GetElementPtrInst>(C.Ins)->isInBounds();
1387 // C = (char *)Basis + Bump
1388 Reduced = Builder.CreatePtrAdd(Basis.Ins, Bump, "", InBounds);
1389 break;
1390 }
1391 default:
1392 llvm_unreachable("C.CandidateKind is invalid");
1393 };
1394 Reduced->takeName(C.Ins);
1395 }
1396 C.Ins->replaceAllUsesWith(Reduced);
1397 DeadInstructions.push_back(C.Ins);
1398}
1399
1400bool StraightLineStrengthReduceLegacyPass::runOnFunction(Function &F) {
1401 if (skipFunction(F))
1402 return false;
1403
1404 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1405 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1406 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1407 return StraightLineStrengthReduce(DL, DT, SE, TTI).runOnFunction(F);
1408}
1409
1410bool StraightLineStrengthReduce::runOnFunction(Function &F) {
1411 LLVM_DEBUG(dbgs() << "SLSR on Function: " << F.getName() << "\n");
1412 // Traverse the dominator tree in the depth-first order. This order makes sure
1413 // all bases of a candidate are in Candidates when we process it.
1414 for (const auto Node : depth_first(DT))
1415 for (auto &I : *(Node->getBlock()))
1416 allocateCandidatesAndFindBasis(&I);
1417
1418 // Build the dependency graph and sort candidate instructions from dependency
1419 // roots to leaves
1420 for (auto &C : Candidates) {
1421 DependencyGraph.try_emplace(C.Ins);
1422 addDependency(C, C.Basis);
1423 }
1424 sortCandidateInstructions();
1425
1426 // Rewrite candidates in the topological order that rewrites a Candidate
1427 // always before rewriting its Basis
1428 for (Instruction *I : reverse(SortedCandidateInsts))
1429 if (Candidate *C = pickRewriteCandidate(I))
1430 rewriteCandidate(*C);
1431
1432 for (auto *DeadIns : DeadInstructions)
1433 // A dead instruction may be another dead instruction's op,
1434 // don't delete an instruction twice
1435 if (DeadIns->getParent())
1437
1438 bool Ret = !DeadInstructions.empty();
1439 DeadInstructions.clear();
1440 DependencyGraph.clear();
1441 RewriteCandidates.clear();
1442 SortedCandidateInsts.clear();
1443 // First clear all references to candidates in the list
1444 CandidateDict.clear();
1445 // Then destroy the list
1446 Candidates.clear();
1447 return Ret;
1448}
1449
1450PreservedAnalyses
1452 const DataLayout *DL = &F.getDataLayout();
1453 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1454 auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
1455 auto *TTI = &AM.getResult<TargetIRAnalysis>(F);
1456
1457 if (!StraightLineStrengthReduce(DL, DT, SE, TTI).runOnFunction(F))
1458 return PreservedAnalyses::all();
1459
1464 return PA;
1465}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:686
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:540
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static bool isGEPFoldable(GetElementPtrInst *GEP, const TargetTransformInfo *TTI)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore, Value *FlagsOp)
Register Usage Information Collector
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
static bool matchesOr(Value *A, Value *&B, ConstantInt *&C)
static bool isAddFoldable(const SCEV *Base, ConstantInt *Index, Value *Stride, TargetTransformInfo *TTI)
static void unifyBitWidth(APInt &A, APInt &B)
static bool matchesAdd(Value *A, Value *&B, ConstantInt *&C)
static const unsigned UnknownAddressSpace
static bool mayHaveSignedWrap(const Value *V)
static bool isSignExtendedGepIndex(const Value *Idx, GetElementPtrInst *GEP, const DataLayout *DL)
static bool isSafeToFactorGepIndex(const Value *Idx, GetElementPtrInst *GEP, const DataLayout *DL)
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition APInt.h:445
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:367
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
unsigned logBase2() const
Definition APInt.h:1781
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:436
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:278
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition Constants.h:225
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:135
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static bool shouldExecute(CounterInfo &Counter)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:767
iterator end()
Definition DenseMap.h:687
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:857
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:2101
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition IRBuilder.h:1835
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1444
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1516
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1427
Value * CreateSExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a SExt or Trunc from the integer value V to DestTy.
Definition IRBuilder.h:2163
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1461
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:338
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
LLVM_ABI const SCEV * getGEPExpr(GEPOperator *GEP, ArrayRef< SCEVUse > IndexExprs)
Returns an expression for a GEP.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCC_Free
Expected to fold away in lowering.
LLVM_ABI unsigned getIntegerBitWidth() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
TypeSize getSequentialElementStride(const DataLayout &DL) const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
bool match(Val *V, const Pattern &P)
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:526
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void initializeStraightLineStrengthReduceLegacyPassPass(PassRegistry &)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
generic_gep_type_iterator<> gep_type_iterator
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
TargetTransformInfo TTI
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
gep_type_iterator gep_type_begin(const User *GEP)
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI FunctionPass * createStraightLineStrengthReducePass()
SCEVUseT< const SCEV * > SCEVUse
SCEVPtrT getPointer() const