LLVM 23.0.0git
CanonicalizeFreezeInLoops.cpp
Go to the documentation of this file.
1//==- CanonicalizeFreezeInLoops - Canonicalize freezes in a loop-*- 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// This pass canonicalizes freeze instructions in a loop by pushing them out to
10// the preheader.
11//
12// loop:
13// i = phi init, i.next
14// i.next = add nsw i, 1
15// i.next.fr = freeze i.next // push this out of this loop
16// use(i.next.fr)
17// br i1 (i.next <= N), loop, exit
18// =>
19// init.fr = freeze init
20// loop:
21// i = phi init.fr, i.next
22// i.next = add i, 1 // nsw is dropped here
23// use(i.next)
24// br i1 (i.next <= N), loop, exit
25//
26// Removing freezes from these chains help scalar evolution successfully analyze
27// expressions.
28//
29//===----------------------------------------------------------------------===//
30
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/ADT/SetVector.h"
41#include "llvm/IR/Dominators.h"
43#include "llvm/Pass.h"
44#include "llvm/Support/Debug.h"
46
47using namespace llvm;
48
49#define DEBUG_TYPE "canon-freeze"
50
51namespace {
52
53class CanonicalizeFreezeInLoops : public LoopPass {
54public:
55 static char ID;
56
57 CanonicalizeFreezeInLoops();
58
59private:
60 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
61 void getAnalysisUsage(AnalysisUsage &AU) const override;
62};
63
64class CanonicalizeFreezeInLoopsImpl {
65 Loop *L;
67 DominatorTree &DT;
68
69 // Can freeze instruction be pushed into operands of I?
70 // In order to do this, I should not create a poison after I's flags are
71 // stripped.
72 bool canHandleInst(const Instruction *I) {
73 auto Opc = I->getOpcode();
74 // If add/sub/mul, drop nsw/nuw flags.
75 return Opc == Instruction::Add || Opc == Instruction::Sub ||
76 Opc == Instruction::Mul;
77 }
78
79 void InsertFreezeAndForgetFromSCEV(Use &U);
80
81public:
82 CanonicalizeFreezeInLoopsImpl(Loop *L, ScalarEvolution &SE, DominatorTree &DT)
83 : L(L), SE(SE), DT(DT) {}
84 bool run();
85};
86
87struct FrozenIndPHIInfo {
88 // A freeze instruction that uses an induction phi
89 FreezeInst *FI = nullptr;
90 // The induction phi, step instruction, the operand idx of StepInst which is
91 // a step value
92 PHINode *PHI;
93 BinaryOperator *StepInst;
94 unsigned StepValIdx = 0;
95
96 FrozenIndPHIInfo(PHINode *PHI, BinaryOperator *StepInst)
97 : PHI(PHI), StepInst(StepInst) {}
98
99 bool operator==(const FrozenIndPHIInfo &Other) { return FI == Other.FI; }
100};
101
102} // namespace
103
104template <> struct llvm::DenseMapInfo<FrozenIndPHIInfo> {
105 static inline FrozenIndPHIInfo getEmptyKey() {
106 return FrozenIndPHIInfo(DenseMapInfo<PHINode *>::getEmptyKey(),
108 }
109
110 static unsigned getHashValue(const FrozenIndPHIInfo &Val) {
112 };
113
114 static bool isEqual(const FrozenIndPHIInfo &LHS,
115 const FrozenIndPHIInfo &RHS) {
116 return LHS.FI == RHS.FI;
117 };
118};
119
120// Given U = (value, user), replace value with freeze(value), and let
121// SCEV forget user. The inserted freeze is placed in the preheader.
122void CanonicalizeFreezeInLoopsImpl::InsertFreezeAndForgetFromSCEV(Use &U) {
123 auto *PH = L->getLoopPreheader();
124
125 auto *UserI = cast<Instruction>(U.getUser());
126 auto *ValueToFr = U.get();
127 assert(L->contains(UserI->getParent()) &&
128 "Should not process an instruction that isn't inside the loop");
129 if (isGuaranteedNotToBeUndefOrPoison(ValueToFr, nullptr, UserI, &DT))
130 return;
131
132 LLVM_DEBUG(dbgs() << "canonfr: inserting freeze:\n");
133 LLVM_DEBUG(dbgs() << "\tUser: " << *U.getUser() << "\n");
134 LLVM_DEBUG(dbgs() << "\tOperand: " << *U.get() << "\n");
135
136 U.set(new FreezeInst(ValueToFr, ValueToFr->getName() + ".frozen",
137 PH->getTerminator()->getIterator()));
138
139 SE.forgetValue(UserI);
140}
141
142bool CanonicalizeFreezeInLoopsImpl::run() {
143 // The loop should be in LoopSimplify form.
144 if (!L->isLoopSimplifyForm())
145 return false;
146
147 SmallSetVector<FrozenIndPHIInfo, 4> Candidates;
148
149 for (auto &PHI : L->getHeader()->phis()) {
150 InductionDescriptor ID;
152 continue;
153
154 LLVM_DEBUG(dbgs() << "canonfr: PHI: " << PHI << "\n");
155 FrozenIndPHIInfo Info(&PHI, ID.getInductionBinOp());
156 if (!Info.StepInst || !canHandleInst(Info.StepInst)) {
157 // The stepping instruction has unknown form.
158 // Ignore this PHI.
159 continue;
160 }
161
162 Info.StepValIdx = Info.StepInst->getOperand(0) == &PHI;
163 Value *StepV = Info.StepInst->getOperand(Info.StepValIdx);
164 if (auto *StepI = dyn_cast<Instruction>(StepV)) {
165 if (L->contains(StepI->getParent())) {
166 // The step value is inside the loop. Freezing step value will introduce
167 // another freeze into the loop, so skip this PHI.
168 continue;
169 }
170 }
171
172 auto Visit = [&](User *U) {
173 if (auto *FI = dyn_cast<FreezeInst>(U)) {
174 LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n");
175 Info.FI = FI;
176 Candidates.insert(Info);
177 }
178 };
179 for_each(PHI.users(), Visit);
180 for_each(Info.StepInst->users(), Visit);
181 }
182
183 if (Candidates.empty())
184 return false;
185
186 SmallPtrSet<PHINode *, 8> ProcessedPHIs;
187 for (const auto &Info : Candidates) {
188 PHINode *PHI = Info.PHI;
189 if (!ProcessedPHIs.insert(Info.PHI).second)
190 continue;
191
192 BinaryOperator *StepI = Info.StepInst;
193 assert(StepI && "Step instruction should have been found");
194
195 // Drop flags from the step instruction.
196 if (!isGuaranteedNotToBeUndefOrPoison(StepI, nullptr, StepI, &DT)) {
197 LLVM_DEBUG(dbgs() << "canonfr: drop flags: " << *StepI << "\n");
199 SE.forgetValue(StepI);
200 }
201
202 InsertFreezeAndForgetFromSCEV(StepI->getOperandUse(Info.StepValIdx));
203
204 unsigned OperandIdx =
205 PHI->getOperandNumForIncomingValue(PHI->getIncomingValue(0) == StepI);
206 InsertFreezeAndForgetFromSCEV(PHI->getOperandUse(OperandIdx));
207 }
208
209 // Finally, remove the old freeze instructions.
210 for (const auto &Item : Candidates) {
211 auto *FI = Item.FI;
212 LLVM_DEBUG(dbgs() << "canonfr: removing " << *FI << "\n");
213 SE.forgetValue(FI);
214 FI->replaceAllUsesWith(FI->getOperand(0));
215 FI->eraseFromParent();
216 }
217
218 return true;
219}
220
221CanonicalizeFreezeInLoops::CanonicalizeFreezeInLoops() : LoopPass(ID) {
223}
224
225void CanonicalizeFreezeInLoops::getAnalysisUsage(AnalysisUsage &AU) const {
234}
235
236bool CanonicalizeFreezeInLoops::runOnLoop(Loop *L, LPPassManager &) {
237 if (skipLoop(L))
238 return false;
239
240 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
241 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
242 return CanonicalizeFreezeInLoopsImpl(L, SE, DT).run();
243}
244
248 LPMUpdater &U) {
249 if (!CanonicalizeFreezeInLoopsImpl(&L, AR.SE, AR.DT).run())
250 return PreservedAnalyses::all();
251
253}
254
255INITIALIZE_PASS_BEGIN(CanonicalizeFreezeInLoops, "canon-freeze",
256 "Canonicalize Freeze Instructions in Loops", false, false)
259INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
260INITIALIZE_PASS_END(CanonicalizeFreezeInLoops, "canon-freeze",
261 "Canonicalize Freeze Instructions in Loops", false, false)
262
264 return new CanonicalizeFreezeInLoops();
265}
266
267char CanonicalizeFreezeInLoops::ID = 0;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
This file defines DenseMapInfo traits for DenseMap.
This header provides classes for managing per-loop analyses.
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
Definition Pass.cpp:284
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:530
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:310
DominatorTree & getDomTree()
Definition Dominators.h:318
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
This class represents a freeze function that returns random concrete value if an operand is either a ...
static LLVM_ABI bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, ArrayRef< const SCEVPredicate * > NoWrapPreds={}, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction * > *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
LLVM_ABI void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:612
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
Definition LoopInfo.cpp:501
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
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
The main scalar evolution driver.
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
const Use & getOperandUse(unsigned i) const
Definition User.h:220
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ User
could "use" a pointer
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1731
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 initializeCanonicalizeFreezeInLoopsPass(PassRegistry &)
LLVM_ABI char & LoopSimplifyID
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
LLVM_ABI Pass * createCanonicalizeFreezeInLoopsPass()
static unsigned getHashValue(const FrozenIndPHIInfo &Val)
static bool isEqual(const FrozenIndPHIInfo &LHS, const FrozenIndPHIInfo &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...