LLVM 24.0.0git
LoopSplitUtils.h
Go to the documentation of this file.
1//===- LoopSplitUtils.h - Split a loop's iteration space --------*- C++ -*-===//
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// Splits a counted loop's iteration space into a chain of per-partition
10// sub-loops. See LoopSplitUtils.cpp for the structure produced.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_LOOPSPLITUTILS_H
15#define LLVM_TRANSFORMS_UTILS_LOOPSPLITUTILS_H
16
21#include <memory>
22
23namespace llvm {
24
25class DominatorTree;
26class SCEV;
27class SCEVExpander;
28class ScalarEvolution;
29
30/// Splits a counted loop into a chain of per-partition sub-loops.
31///
32/// Usage:
33/// \code
34/// LoopSplitUtils LSU(L, LI, SE, DT);
35/// if (!LSU.isLegal())
36/// return false;
37/// LSU.addPartition(S0, E0); // one call per partition, in order
38/// LSU.addPartition(S1, E1);
39/// LSU.split();
40/// \endcode
42public:
44 DominatorTree *DT)
45 : L(L), LI(LI), SE(SE), DT(DT) {}
46
47 /// Analyze \p L and return true if it is a counted loop this utility can
48 /// split: a bottom-tested single-exit loop in LCSSA form with a unique
49 /// unit-step integer induction and a computable trip count. Must succeed
50 /// before split().
51 LLVM_ABI bool isLegal();
52
53 /// Return the loop's induction variable. Valid only after isLegal() succeeds.
55 return L->getInductionVariable(*SE);
56 }
57
58 /// Append an inclusive partition range [Start, End] in iteration order.
59 /// Partitions must tile the whole space: first Start = induction start, each
60 /// later Start = previous End +/- step, last End = induction end (desc: S >=
61 /// E).
62 ///
63 /// Bounds must be loop-invariant and representable in the induction type
64 /// without wrapping: a Start +/- offset that wraps past TYPE_MAX/MIN/0 looks
65 /// in-range and silently miscompiles. See LoopSplitUtils.cpp for the
66 /// rationale.
67 ///
68 /// Every partition is guarded by default; use avoidPartitionGuard() to opt
69 /// out.
70 LLVM_ABI void addPartition(const SCEV *Start, const SCEV *End);
71
72 /// Suppress the entry guard for partition \p PartitionIndex (already added).
73 /// Use only for a partition the caller can prove runs at least once; for a
74 /// runtime- empty partition this is incorrect and yields one spurious
75 /// iteration.
76 LLVM_ABI void avoidPartitionGuard(unsigned PartitionIndex);
77
78 LLVM_ABI unsigned getNumPartitions() const { return Partitions.size(); }
79
80 /// Perform the split. Requires a successful isLegal() and at least two
81 /// partitions. Returns true if the loop was rewritten.
82 LLVM_ABI bool split();
83
84 /// Return the counterpart of original-loop value \p V in partition
85 /// \p PartitionIndex (0-based). Partition 0 maps values to themselves; a
86 /// later partition returns the clone, or null if not cloned. Valid only after
87 /// split().
88 LLVM_ABI Value *getPartitionValue(Value *V, unsigned PartitionIndex) const;
89
90 /// Return the original-to-clone value map for the partition at
91 /// \p PartitionIndex, for callers that want to remap many values. Null for
92 /// partition 0 (identity) and for any partition that was not cloned.
94 getPartitionValueMap(unsigned PartitionIndex) const;
95
96private:
97 /// Everything known about one partition: the caller-supplied range plus the
98 /// state split() derives. Indexed by partition number in \c Partitions.
99 struct PartitionInfo {
100 PartitionInfo() = default;
101 PartitionInfo(const SCEV *StartExpr, const SCEV *EndExpr)
102 : StartExpr(StartExpr), EndExpr(EndExpr) {}
103
104 // Set by addPartition() / avoidPartitionGuard() before split():
105 const SCEV *StartExpr = nullptr; // inclusive iteration range [Start, End].
106 const SCEV *EndExpr = nullptr;
107 bool Guarded = true; // emit an entry guard?
108
109 // Filled in by split():
110 std::unique_ptr<ValueToValueMapTy> VMap; // null for partition 0 (identity).
111 Value *StartVal = nullptr; // expanded start.
112 Value *SelEnd = nullptr; // clamped end min(End, indEnd).
113 bool Empty = false; // provably zero-iteration.
114 BasicBlock *GuardBlock = nullptr;
115 BasicBlock *Preheader = nullptr;
116 BasicBlock *Exit = nullptr;
117 Loop *SubLoop = nullptr;
118 Value *LatchIndOp = nullptr; // induction operand of the latch compare.
119 };
120
121 /// Per-split() scratch threaded through the phase helpers (the escaping
122 /// values, new blocks, etc.). A pure transform internal, so it is defined in
123 /// the implementation file.
124 struct SplitState;
125
126 Loop *L;
127 LoopInfo *LI;
128 ScalarEvolution *SE;
129 DominatorTree *DT;
130
131 // Induction analysis, populated by isLegal().
132 Value *LatchIndOperand = nullptr; // induction operand of the latch compare.
133 bool InductionIsSigned = false; // iteration ordering signedness.
134 const SCEV *InductionEnd = nullptr;
135
136 /// One record per partition, in add order.
137 SmallVector<PartitionInfo, 4> Partitions;
138
139 // split() phase helpers, run in order; each is documented at its definition.
140 /// Collect loop-carried and live-out values and split off the final exit.
141 void collectEscapingValues(SplitState &S);
142 /// Expand each partition's start and clamped end into the entry guard.
143 void expandPartitionBounds(SplitState &S, SCEVExpander &Expander);
144 /// Pass 1: clone each later partition's sub-loop and create its guard/exit.
145 void clonePartitions(SplitState &S);
146 /// Pass 2: emit each guard, clamp each latch, and chain the partitions.
147 void chainPartitions(SplitState &S);
148 /// Rebuild SSA for every escaping value with a per-value SSAUpdater.
149 void reconstructSSA(SplitState &S);
150};
151
152} // namespace llvm
153
154#endif // LLVM_TRANSFORMS_UTILS_LOOPSPLITUTILS_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the SmallVector class.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool split()
Perform the split.
LLVM_ABI unsigned getNumPartitions() const
LLVM_ABI LoopSplitUtils(Loop *L, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT)
LLVM_ABI PHINode * getInductionVariable() const
Return the loop's induction variable. Valid only after isLegal() succeeds.
LLVM_ABI bool isLegal()
Analyze L and return true if it is a counted loop this utility can split: a bottom-tested single-exit...
LLVM_ABI void addPartition(const SCEV *Start, const SCEV *End)
Append an inclusive partition range [Start, End] in iteration order.
LLVM_ABI Value * getPartitionValue(Value *V, unsigned PartitionIndex) const
Return the counterpart of original-loop value V in partition PartitionIndex (0-based).
LLVM_ABI const ValueToValueMapTy * getPartitionValueMap(unsigned PartitionIndex) const
Return the original-to-clone value map for the partition at PartitionIndex, for callers that want to ...
LLVM_ABI void avoidPartitionGuard(unsigned PartitionIndex)
Suppress the entry guard for partition PartitionIndex (already added).
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This class uses information about analyze scalars to rewrite expressions in canonical form.
This class represents an analyzed expression in the program.
The main scalar evolution driver.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
Per-split() scratch shared by the phase helpers; lives for one split() call.