LLVM: lib/Transforms/Scalar/Sink.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
23using namespace llvm;
24
25#define DEBUG_TYPE "sink"
26
27STATISTIC(NumSunk, "Number of instructions sunk");
28STATISTIC(NumSinkIter, "Number of sinking iterations");
29
32
35 return false;
36 }
37
38
39
41 if (AI->isStaticAlloca())
42 return false;
43
48 return false;
49 }
50
53 return false;
54
56
57
58 if (Call->isConvergent())
59 return false;
60
63 return false;
64 }
65
66 return true;
67}
68
69
70
73 assert(Inst && "Instruction to be sunk is null");
74 assert(SuccToSinkTo && "Candidate sink target is null");
75
76
77 if (SuccToSinkTo->isEHPad())
78 return false;
79
80
81
82
83
85
86
88 !Inst->hasMetadata(LLVMContext::MD_invariant_load))
89 return false;
90
91
94 if (succ != nullptr && succ != cur)
95 return false;
96 }
97
98 return true;
99}
100
101
102
106
107
109 return false;
110
111
112
113
114
115
116
117
118
119
120
122
123
125 for (Use &U : Inst->uses()) {
129
130
132 UseBlock = PN->getIncomingBlock(Num);
133 }
134
136 continue;
137
138 if (SuccToSinkTo)
140 else
141 SuccToSinkTo = UseBlock;
142 }
143
144 if (SuccToSinkTo) {
145
146
147 while (SuccToSinkTo != BB &&
150 if (SuccToSinkTo == BB)
151 SuccToSinkTo = nullptr;
152 }
153
154
155 if (!SuccToSinkTo)
156 return false;
157
159 Inst->getParent()->printAsOperand(dbgs(), false); dbgs() << " -> ";
161
162
163
164
166 "SuccToSinkTo must be dominated by current Inst location!");
167
168
170 return true;
171}
172
175
176
177
179
180 bool MadeChange = false;
181
182
184 --I;
185 bool ProcessedBegin = false;
187 do {
188 Instruction *Inst = &*I;
189
190
191
192 ProcessedBegin = I == BB.begin();
193 if (!ProcessedBegin)
194 --I;
195
197 continue;
198
200 ++NumSunk;
201 MadeChange = true;
202 }
203
204
205 } while (!ProcessedBegin);
206
207 return MadeChange;
208}
209
212 bool MadeChange, EverMadeChange = false;
213
214 do {
215 MadeChange = false;
216 LLVM_DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n");
217
220 EverMadeChange |= MadeChange;
221 NumSinkIter++;
222 } while (MadeChange);
223
224 return EverMadeChange;
225}
226
231
234
237 return PA;
238}
239
240namespace {
241 class SinkingLegacyPass : public FunctionPass {
242 public:
243 static char ID;
246 }
247
249 auto &DT = getAnalysis().getDomTree();
250 auto &LI = getAnalysis().getLoopInfo();
251 auto &AA = getAnalysis().getAAResults();
252
254 }
255
256 void getAnalysisUsage(AnalysisUsage &AU) const override {
258 FunctionPass::getAnalysisUsage(AU);
260 AU.addRequired();
264 }
265 };
266}
267
268char SinkingLegacyPass::ID = 0;
273INITIALIZE_PASS_END(SinkingLegacyPass, "sink", "Code sinking", false, false)
274
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool runOnFunction(Function &F, bool PostInlining)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
static bool iterativelySinkInstructions(Function &F, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
Definition Sink.cpp:210
static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo, DominatorTree &DT, LoopInfo &LI)
IsAcceptableTarget - Return true if it is possible to sink the instruction in the specified basic blo...
Definition Sink.cpp:71
static bool isSafeToMove(Instruction *Inst, AliasAnalysis &AA, SmallPtrSetImpl< Instruction * > &Stores)
Definition Sink.cpp:30
static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
Definition Sink.cpp:173
static bool SinkInstruction(Instruction *Inst, SmallPtrSetImpl< Instruction * > &Stores, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
SinkInstruction - Determine whether it is safe to sink the specified machine instruction out of its c...
Definition Sink.cpp:103
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
an instruction to allocate memory on the stack
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
InstListType::iterator iterator
Instruction iterators...
bool isEHPad() const
Return true if this basic block is an exception handling block.
Represents analyses that only rely on functions' control flow.
DomTreeNodeBase * getIDom() const
Analysis pass which computes a DominatorTree.
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.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
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.
LLVM_ABI bool mayThrow(bool IncludePhaseOneUnwind=false) const LLVM_READONLY
Return true if this instruction may throw an exception.
LLVM_ABI bool isDebugOrPseudoInst() const LLVM_READONLY
Return true if the instruction is a DbgInfoIntrinsic or PseudoProbeInst.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
bool isTerminator() const
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
LLVM_ABI bool willReturn() const LLVM_READONLY
Return true if the instruction will return (unwinding is considered as a form of returning control fl...
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static unsigned getIncomingValueNumForOperand(unsigned i)
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition Sink.cpp:227
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A Use represents the edge between a Value definition and its users.
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
iterator_range< use_iterator > uses()
const ParentTy * getParent() const
Abstract Attribute helper functions.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
LLVM_ABI void initializeSinkingLegacyPassPass(PassRegistry &)
LLVM_ABI FunctionPass * createSinkingPass()
Definition Sink.cpp:275
bool isModSet(const ModRefInfo MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.