llvm.org GIT mirror llvm / 28a193e
Add an LLVM IR version of code sinking. This uses the same simple algorithm as MachineSink, but it isn't constrained by MachineInstr-level details. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@103257 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
5 changed file(s) with 299 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
137137 (void) llvm::createGEPSplitterPass();
138138 (void) llvm::createABCDPass();
139139 (void) llvm::createLintPass();
140 (void) llvm::createSinkingPass();
140141
141142 (void)new llvm::IntervalPartition();
142143 (void)new llvm::FindUsedTypes();
331331 //
332332 FunctionPass *createABCDPass();
333333
334 //===----------------------------------------------------------------------===//
335 //
336 // Sink - Code Sinking
337 //
338 FunctionPass *createSinkingPass();
339
334340 } // End llvm namespace
335341
336342 #endif
0 //===-- Sink.cpp - Code Sinking -------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass moves instructions into successor blocks, when possible, so that
10 // they aren't executed on paths where their results aren't needed.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "sink"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26
27 STATISTIC(NumSunk, "Number of instructions sunk");
28
29 namespace {
30 class Sinking : public FunctionPass {
31 DominatorTree *DT;
32 LoopInfo *LI;
33 AliasAnalysis *AA;
34
35 public:
36 static char ID; // Pass identification
37 Sinking() : FunctionPass(&ID) {}
38
39 virtual bool runOnFunction(Function &F);
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.setPreservesCFG();
43 FunctionPass::getAnalysisUsage(AU);
44 AU.addRequired();
45 AU.addRequired();
46 AU.addRequired();
47 AU.addPreserved();
48 AU.addPreserved();
49 }
50 private:
51 bool ProcessBlock(BasicBlock &BB);
52 bool SinkInstruction(Instruction *I, SmallPtrSet &Stores);
53 bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB) const;
54 };
55 } // end anonymous namespace
56
57 char Sinking::ID = 0;
58 static RegisterPass
59 X("sink", "Code sinking");
60
61 FunctionPass *llvm::createSinkingPass() { return new Sinking(); }
62
63 /// AllUsesDominatedByBlock - Return true if all uses of the specified value
64 /// occur in blocks dominated by the specified block.
65 bool Sinking::AllUsesDominatedByBlock(Instruction *Inst,
66 BasicBlock *BB) const {
67 // Ignoring debug uses is necessary so debug info doesn't affect the code.
68 // This may leave a referencing dbg_value in the original block, before
69 // the definition of the vreg. Dwarf generator handles this although the
70 // user might not get the right info at runtime.
71 for (Value::use_iterator I = Inst->use_begin(),
72 E = Inst->use_end(); I != E; ++I) {
73 // Determine the block of the use.
74 Instruction *UseInst = cast(*I);
75 BasicBlock *UseBlock = UseInst->getParent();
76 if (PHINode *PN = dyn_cast(UseInst)) {
77 // PHI nodes use the operand in the predecessor block, not the block with
78 // the PHI.
79 unsigned Num = PHINode::getIncomingValueNumForOperand(I.getOperandNo());
80 UseBlock = PN->getIncomingBlock(Num);
81 }
82 // Check that it dominates.
83 if (!DT->dominates(BB, UseBlock))
84 return false;
85 }
86 return true;
87 }
88
89 bool Sinking::runOnFunction(Function &F) {
90 DT = &getAnalysis();
91 LI = &getAnalysis();
92 AA = &getAnalysis();
93
94 bool EverMadeChange = false;
95
96 while (1) {
97 bool MadeChange = false;
98
99 // Process all basic blocks.
100 for (Function::iterator I = F.begin(), E = F.end();
101 I != E; ++I)
102 MadeChange |= ProcessBlock(*I);
103
104 // If this iteration over the code changed anything, keep iterating.
105 if (!MadeChange) break;
106 EverMadeChange = true;
107 }
108 return EverMadeChange;
109 }
110
111 bool Sinking::ProcessBlock(BasicBlock &BB) {
112 // Can't sink anything out of a block that has less than two successors.
113 if (BB.getTerminator()->getNumSuccessors() <= 1 || BB.empty()) return false;
114
115 // Don't bother sinking code out of unreachable blocks. In addition to being
116 // unprofitable, it can also lead to infinite looping, because in an unreachable
117 // loop there may be nowhere to stop.
118 if (!DT->isReachableFromEntry(&BB)) return false;
119
120 bool MadeChange = false;
121
122 // Walk the basic block bottom-up. Remember if we saw a store.
123 BasicBlock::iterator I = BB.end();
124 --I;
125 bool ProcessedBegin = false;
126 SmallPtrSet Stores;
127 do {
128 Instruction *Inst = I; // The instruction to sink.
129
130 // Predecrement I (if it's not begin) so that it isn't invalidated by
131 // sinking.
132 ProcessedBegin = I == BB.begin();
133 if (!ProcessedBegin)
134 --I;
135
136 if (isa(Inst))
137 continue;
138
139 if (SinkInstruction(Inst, Stores))
140 ++NumSunk, MadeChange = true;
141
142 // If we just processed the first instruction in the block, we're done.
143 } while (!ProcessedBegin);
144
145 return MadeChange;
146 }
147
148 static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA,
149 SmallPtrSet &Stores) {
150 if (LoadInst *L = dyn_cast(Inst)) {
151 if (L->isVolatile()) return false;
152
153 Value *Ptr = L->getPointerOperand();
154 unsigned Size = AA->getTypeStoreSize(L->getType());
155 for (SmallPtrSet::iterator I = Stores.begin(),
156 E = Stores.end(); I != E; ++I)
157 if (AA->getModRefInfo(*I, Ptr, Size) & AliasAnalysis::Mod)
158 return false;
159 }
160
161 if (Inst->mayWriteToMemory()) {
162 Stores.insert(Inst);
163 return false;
164 }
165
166 return Inst->isSafeToSpeculativelyExecute();
167 }
168
169 /// SinkInstruction - Determine whether it is safe to sink the specified machine
170 /// instruction out of its current block into a successor.
171 bool Sinking::SinkInstruction(Instruction *Inst,
172 SmallPtrSet &Stores) {
173 // Check if it's safe to move the instruction.
174 if (!isSafeToMove(Inst, AA, Stores))
175 return false;
176
177 // FIXME: This should include support for sinking instructions within the
178 // block they are currently in to shorten the live ranges. We often get
179 // instructions sunk into the top of a large block, but it would be better to
180 // also sink them down before their first use in the block. This xform has to
181 // be careful not to *increase* register pressure though, e.g. sinking
182 // "x = y + z" down if it kills y and z would increase the live ranges of y
183 // and z and only shrink the live range of x.
184
185 // Loop over all the operands of the specified instruction. If there is
186 // anything we can't handle, bail out.
187 BasicBlock *ParentBlock = Inst->getParent();
188
189 // SuccToSinkTo - This is the successor to sink this instruction to, once we
190 // decide.
191 BasicBlock *SuccToSinkTo = 0;
192
193 // FIXME: This picks a successor to sink into based on having one
194 // successor that dominates all the uses. However, there are cases where
195 // sinking can happen but where the sink point isn't a successor. For
196 // example:
197 // x = computation
198 // if () {} else {}
199 // use x
200 // the instruction could be sunk over the whole diamond for the
201 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
202 // after that.
203
204 // Instructions can only be sunk if all their uses are in blocks
205 // dominated by one of the successors.
206 // Look at all the successors and decide which one
207 // we should sink to.
208 for (succ_iterator SI = succ_begin(ParentBlock),
209 E = succ_end(ParentBlock); SI != E; ++SI) {
210 if (AllUsesDominatedByBlock(Inst, *SI)) {
211 SuccToSinkTo = *SI;
212 break;
213 }
214 }
215
216 // If we couldn't find a block to sink to, ignore this instruction.
217 if (SuccToSinkTo == 0)
218 return false;
219
220 // It is not possible to sink an instruction into its own block. This can
221 // happen with loops.
222 if (Inst->getParent() == SuccToSinkTo)
223 return false;
224
225 DEBUG(dbgs() << "Sink instr " << *Inst);
226 DEBUG(dbgs() << "to block ";
227 WriteAsOperand(dbgs(), SuccToSinkTo, false));
228
229 // If the block has multiple predecessors, this would introduce computation on
230 // a path that it doesn't already exist. We could split the critical edge,
231 // but for now we just punt.
232 // FIXME: Split critical edges if not backedges.
233 if (SuccToSinkTo->getUniquePredecessor() != ParentBlock) {
234 // We cannot sink a load across a critical edge - there may be stores in
235 // other code paths.
236 if (!Inst->isSafeToSpeculativelyExecute()) {
237 DEBUG(dbgs() << " *** PUNTING: Wont sink load along critical edge.\n");
238 return false;
239 }
240
241 // We don't want to sink across a critical edge if we don't dominate the
242 // successor. We could be introducing calculations to new code paths.
243 if (!DT->dominates(ParentBlock, SuccToSinkTo)) {
244 DEBUG(dbgs() << " *** PUNTING: Critical edge found\n");
245 return false;
246 }
247
248 // Don't sink instructions into a loop.
249 if (LI->isLoopHeader(SuccToSinkTo)) {
250 DEBUG(dbgs() << " *** PUNTING: Loop header found\n");
251 return false;
252 }
253
254 // Otherwise we are OK with sinking along a critical edge.
255 DEBUG(dbgs() << "Sinking along critical edge.\n");
256 }
257
258 // Determine where to insert into. Skip phi nodes.
259 BasicBlock::iterator InsertPos = SuccToSinkTo->begin();
260 while (InsertPos != SuccToSinkTo->end() && isa(InsertPos))
261 ++InsertPos;
262
263 // Move the instruction.
264 Inst->moveBefore(InsertPos);
265 return true;
266 }
0 ; RUN: opt < %s -sink -S | FileCheck %s
1
2 @A = external global i32
3 @B = external global i32
4
5 ; Sink should sink the load past the store (which doesn't overlap) into
6 ; the block that uses it.
7
8 ; CHECK: @foo
9 ; CHECK: true:
10 ; CHECK-NEXT: %l = load i32* @A
11 ; CHECK-NEXT: ret i32 %l
12
13 define i32 @foo(i1 %z) {
14 %l = load i32* @A
15 store i32 0, i32* @B
16 br i1 %z, label %true, label %false
17 true:
18 ret i32 %l
19 false:
20 ret i32 0
21 }
0 load_lib llvm.exp
1
2 RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,c,cpp}]]