LLVM 20.0.0git
LoopInstSimplify.cpp
Go to the documentation of this file.
1//===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
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 pass performs lightweight instruction simplification on loop bodies.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/Statistic.h"
26#include "llvm/IR/BasicBlock.h"
27#include "llvm/IR/Dominators.h"
28#include "llvm/IR/Instruction.h"
33#include <optional>
34#include <utility>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "loop-instsimplify"
39
40STATISTIC(NumSimplified, "Number of redundant instructions simplified");
41
43 AssumptionCache &AC, const TargetLibraryInfo &TLI,
44 MemorySSAUpdater *MSSAU) {
45 const DataLayout &DL = L.getHeader()->getDataLayout();
46 SimplifyQuery SQ(DL, &TLI, &DT, &AC);
47
48 // On the first pass over the loop body we try to simplify every instruction.
49 // On subsequent passes, we can restrict this to only simplifying instructions
50 // where the inputs have been updated. We end up needing two sets: one
51 // containing the instructions we are simplifying in *this* pass, and one for
52 // the instructions we will want to simplify in the *next* pass. We use
53 // pointers so we can swap between two stably allocated sets.
55
56 // Track the PHI nodes that have already been visited during each iteration so
57 // that we can identify when it is necessary to iterate.
58 SmallPtrSet<PHINode *, 4> VisitedPHIs;
59
60 // While simplifying we may discover dead code or cause code to become dead.
61 // Keep track of all such instructions and we will delete them at the end.
63
64 // First we want to create an RPO traversal of the loop body. By processing in
65 // RPO we can ensure that definitions are processed prior to uses (for non PHI
66 // uses) in all cases. This ensures we maximize the simplifications in each
67 // iteration over the loop and minimizes the possible causes for continuing to
68 // iterate.
69 LoopBlocksRPO RPOT(&L);
70 RPOT.perform(&LI);
71 MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
72
73 bool Changed = false;
74 for (;;) {
75 if (MSSAU && VerifyMemorySSA)
76 MSSA->verifyMemorySSA();
77 for (BasicBlock *BB : RPOT) {
78 for (Instruction &I : *BB) {
79 if (auto *PI = dyn_cast<PHINode>(&I))
80 VisitedPHIs.insert(PI);
81
82 if (I.use_empty()) {
83 if (isInstructionTriviallyDead(&I, &TLI))
84 DeadInsts.push_back(&I);
85 continue;
86 }
87
88 // We special case the first iteration which we can detect due to the
89 // empty `ToSimplify` set.
90 bool IsFirstIteration = ToSimplify->empty();
91
92 if (!IsFirstIteration && !ToSimplify->count(&I))
93 continue;
94
96 if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
97 continue;
98
99 for (Use &U : llvm::make_early_inc_range(I.uses())) {
100 auto *UserI = cast<Instruction>(U.getUser());
101 U.set(V);
102
103 // Do not bother dealing with unreachable code.
104 if (!DT.isReachableFromEntry(UserI->getParent()))
105 continue;
106
107 // If the instruction is used by a PHI node we have already processed
108 // we'll need to iterate on the loop body to converge, so add it to
109 // the next set.
110 if (auto *UserPI = dyn_cast<PHINode>(UserI))
111 if (VisitedPHIs.count(UserPI)) {
112 Next->insert(UserPI);
113 continue;
114 }
115
116 // If we are only simplifying targeted instructions and the user is an
117 // instruction in the loop body, add it to our set of targeted
118 // instructions. Because we process defs before uses (outside of PHIs)
119 // we won't have visited it yet.
120 //
121 // We also skip any uses outside of the loop being simplified. Those
122 // should always be PHI nodes due to LCSSA form, and we don't want to
123 // try to simplify those away.
124 assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
125 "Uses outside the loop should be PHI nodes due to LCSSA!");
126 if (!IsFirstIteration && L.contains(UserI))
127 ToSimplify->insert(UserI);
128 }
129
130 if (MSSAU)
131 if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V))
132 if (MemoryAccess *MA = MSSA->getMemoryAccess(&I))
133 if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI))
134 MA->replaceAllUsesWith(ReplacementMA);
135
136 assert(I.use_empty() && "Should always have replaced all uses!");
137 if (isInstructionTriviallyDead(&I, &TLI))
138 DeadInsts.push_back(&I);
139 ++NumSimplified;
140 Changed = true;
141 }
142 }
143
144 // Delete any dead instructions found thus far now that we've finished an
145 // iteration over all instructions in all the loop blocks.
146 if (!DeadInsts.empty()) {
147 Changed = true;
148 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU);
149 }
150
151 if (MSSAU && VerifyMemorySSA)
152 MSSA->verifyMemorySSA();
153
154 // If we never found a PHI that needs to be simplified in the next
155 // iteration, we're done.
156 if (Next->empty())
157 break;
158
159 // Otherwise, put the next set in place for the next iteration and reset it
160 // and the visited PHIs for that iteration.
161 std::swap(Next, ToSimplify);
162 Next->clear();
163 VisitedPHIs.clear();
164 DeadInsts.clear();
165 }
166
167 return Changed;
168}
169
172 LPMUpdater &) {
173 std::optional<MemorySSAUpdater> MSSAU;
174 if (AR.MSSA) {
175 MSSAU = MemorySSAUpdater(AR.MSSA);
176 if (VerifyMemorySSA)
177 AR.MSSA->verifyMemorySSA();
178 }
179 if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI,
180 MSSAU ? &*MSSAU : nullptr))
181 return PreservedAnalyses::all();
182
184 PA.preserveSet<CFGAnalyses>();
185 if (AR.MSSA)
186 PA.preserve<MemorySSAAnalysis>();
187 return PA;
188}
static const LLT S1
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, const TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU)
#define I(x, y, z)
Definition: MD5.cpp:58
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
SmallVector< Instruction *, 4 > ToSimplify
Definition: NVVMReflect.cpp:91
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
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:166
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
Definition: LoopIterator.h:172
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopIterator.h:180
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition: LoopInfo.h:439
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:928
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:701
void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1905
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:719
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Provides information about what library functions are available for the current target.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:546
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:406
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:84
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
SimplifyQuery getWithInstruction(const Instruction *I) const