blob: f4336394ed6420737214f90573b59205ef6f4bf0 [file] [log] [blame]
Michael Krusea6b2de32017-07-22 14:02:47 +00001//===------ ForwardOpTree.h -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Move instructions between statements.
11//
12//===----------------------------------------------------------------------===//
13
14#include "polly/ForwardOpTree.h"
15
16#include "polly/ScopInfo.h"
17#include "polly/ScopPass.h"
18#include "polly/Support/GICHelper.h"
19#include "polly/Support/VirtualInstruction.h"
20#include "llvm/Analysis/ValueTracking.h"
21
22#define DEBUG_TYPE "polly-delicm"
23
24using namespace polly;
25using namespace llvm;
26
27STATISTIC(TotalInstructionsCopied, "Number of copied instructions");
28STATISTIC(TotalForwardedTrees, "Number of forwarded operand trees");
29STATISTIC(TotalModifiedStmts,
30 "Number of statements with at least one forwarded tree");
31
32STATISTIC(ScopsModified, "Number of SCoPs with at least one forwarded tree");
33
34namespace {
35
36/// The state of whether an operand tree was/can be forwarded.
37enum ForwardingDecision {
38 FD_CannotForward,
39 FD_CanForward,
40 FD_DidForward,
41};
42
43/// Implementation of operand tree forwarding for a specific SCoP.
44///
45/// For a statement that requires a scalar value (through a value read
46/// MemoryAccess), see if its operand can be moved into the statement. If so,
47/// the MemoryAccess is removed and the all the operand tree instructions are
48/// moved into the statement. All original instructions are left in the source
49/// statements. The simplification pass can clean these up.
50class ForwardOpTreeImpl {
51private:
52 /// The SCoP we are currently processing.
53 Scop *S;
54
55 /// LoopInfo is required for VirtualUse.
56 LoopInfo *LI;
57
58 /// How many instructions have been copied to other statements.
59 int NumInstructionsCopied = 0;
60
61 /// How many operand trees have been forwarded.
62 int NumForwardedTrees = 0;
63
64 /// Number of statements with at least one forwarded operand tree.
65 int NumModifiedStmts = 0;
66
67 /// Whether we carried out at least one change to the SCoP.
68 bool Modified = false;
69
70 void printStatistics(raw_ostream &OS, int Indent = 0) {
71 OS.indent(Indent) << "Statistics {\n";
72 OS.indent(Indent + 4) << "Instructions copied: " << NumInstructionsCopied
73 << '\n';
74 OS.indent(Indent + 4) << "Operand trees forwarded: " << NumForwardedTrees
75 << '\n';
76 OS.indent(Indent + 4) << "Statements with forwarded operand trees: "
77 << NumModifiedStmts << '\n';
78 OS.indent(Indent) << "}\n";
79 }
80
81 void printStatements(llvm::raw_ostream &OS, int Indent = 0) const {
82 OS.indent(Indent) << "After statements {\n";
83 for (auto &Stmt : *S) {
84 OS.indent(Indent + 4) << Stmt.getBaseName() << "\n";
85 for (auto *MA : Stmt)
86 MA->print(OS);
87
88 OS.indent(Indent + 12);
89 Stmt.printInstructions(OS);
90 }
91 OS.indent(Indent) << "}\n";
92 }
93
94 /// Determines whether an operand tree can be forwarded or carries out a
95 /// forwarding, depending on the @p DoIt flag.
96 ///
97 /// @param TargetStmt The statement the operand tree will be copied to.
98 /// @param UseVal The value (usually an instruction) which is root of an
99 /// operand tree.
100 /// @param UseStmt The statement that uses @p UseVal.
101 /// @param UseLoop The loop @p UseVal is used in.
102 /// @param DoIt If false, only determine whether an operand tree can be
103 /// forwarded. If true, carry out the forwarding. Do not use
104 /// DoIt==true if an operand tree is not known to be
105 /// forwardable.
106 ///
107 /// @return When DoIt==true, return whether the operand tree can be forwarded.
108 /// When DoIt==false, return FD_DidForward.
109 ForwardingDecision canForwardTree(ScopStmt *TargetStmt, Value *UseVal,
110 ScopStmt *UseStmt, Loop *UseLoop,
111 bool DoIt) {
112
113 // PHis are not yet supported.
114 if (isa<PHINode>(UseVal)) {
115 DEBUG(dbgs() << " Cannot forward PHI: " << *UseVal << "\n");
116 return FD_CannotForward;
117 }
118
119 VirtualUse VUse = VirtualUse::create(UseStmt, UseLoop, UseVal, true);
120 switch (VUse.getKind()) {
121 case VirtualUse::Constant:
122 case VirtualUse::Block:
123 // These can be used anywhere without special considerations.
124 if (DoIt)
125 return FD_DidForward;
126 return FD_CanForward;
127
128 case VirtualUse::Synthesizable:
129 // Not supported yet.
130 DEBUG(dbgs() << " Cannot forward synthesizable: " << *UseVal << "\n");
131 return FD_CannotForward;
132
133 case VirtualUse::Hoisted:
134 // Not supported yet.
135 DEBUG(dbgs() << " Cannot forward hoisted load: " << *UseVal << "\n");
136 return FD_CannotForward;
137
138 case VirtualUse::ReadOnly:
139 // Not supported yet.
140 DEBUG(dbgs() << " Cannot forward read-only val: " << *UseVal << "\n");
141 return FD_CannotForward;
142
143 case VirtualUse::Intra:
144 case VirtualUse::Inter:
145 auto Inst = cast<Instruction>(UseVal);
146
147 // Compatible instructions must satisfy the following conditions:
148 // 1. Idempotent (instruction will be copied, not moved; although its
149 // original instance might be removed by simplification)
150 // 2. Not access memory (There might be memory writes between)
151 // 3. Not cause undefined behaviour (we might copy to a location when the
152 // original instruction was no executed; this is currently not possible
153 // because we do not forward PHINodes)
154 // 4. Not leak memory if executed multiple times (I am looking at you,
155 // malloc!)
156 //
157 // Instruction::mayHaveSideEffects is not sufficient because it considers
158 // malloc to not have side-effects. llvm::isSafeToSpeculativelyExecute is
159 // not sufficient because it allows memory accesses.
160 if (mayBeMemoryDependent(*Inst)) {
161 DEBUG(dbgs() << " Cannot forward side-effect instruction: " << *Inst
162 << "\n");
163 return FD_CannotForward;
164 }
165
166 Loop *DefLoop = LI->getLoopFor(Inst->getParent());
167 ScopStmt *DefStmt = S->getStmtFor(Inst);
168 assert(DefStmt && "Value must be defined somewhere");
169
170 if (DoIt) {
171 // To ensure the right order, prepend this instruction before its
172 // operands. This ensures that its operands are inserted before the
173 // instruction using them.
174 // TODO: The operand tree is not really a tree, but a DAG. We should be
175 // able to handle DAGs without duplication.
176 TargetStmt->prependInstrunction(Inst);
177 NumInstructionsCopied++;
178 TotalInstructionsCopied++;
179 }
180
181 for (Value *OpVal : Inst->operand_values()) {
182 ForwardingDecision OpDecision =
183 canForwardTree(TargetStmt, OpVal, DefStmt, DefLoop, DoIt);
184 switch (OpDecision) {
185 case FD_CannotForward:
186 assert(!DoIt);
187 return FD_CannotForward;
188
189 case FD_CanForward:
190 assert(!DoIt);
191 break;
192
193 case FD_DidForward:
194 assert(DoIt);
195 break;
196 }
197 }
198
199 if (DoIt)
200 return FD_DidForward;
201 return FD_CanForward;
202 }
203
204 llvm_unreachable("Case unhandled");
205 }
206
207 /// Try to forward an operand tree rooted in @p RA.
208 bool tryForwardTree(MemoryAccess *RA) {
209 assert(RA->isLatestScalarKind());
210 DEBUG(dbgs() << "Trying to forward operand tree " << RA << "...\n");
211
212 ScopStmt *Stmt = RA->getStatement();
213 Loop *InLoop = Stmt->getSurroundingLoop();
214
215 ForwardingDecision Assessment =
216 canForwardTree(Stmt, RA->getAccessValue(), Stmt, InLoop, false);
217 assert(Assessment != FD_DidForward);
218 if (Assessment == FD_CannotForward)
219 return false;
220
221 ForwardingDecision Execution =
222 canForwardTree(Stmt, RA->getAccessValue(), Stmt, InLoop, true);
223 assert(Execution == FD_DidForward);
224
225 Stmt->removeSingleMemoryAccess(RA);
226 return true;
227 }
228
229public:
230 ForwardOpTreeImpl(Scop *S, LoopInfo *LI) : S(S), LI(LI) {}
231
232 /// Return which SCoP this instance is processing.
233 Scop *getScop() const { return S; }
234
235 /// Run the algorithm: Use value read accesses as operand tree roots and try
236 /// to forward them into the statement.
237 bool forwardOperandTrees() {
238 for (ScopStmt &Stmt : *S) {
239 // Currently we cannot modify the instruction list of region statements.
240 if (!Stmt.isBlockStmt())
241 continue;
242
243 bool StmtModified = false;
244
245 // Because we are modifying the MemoryAccess list, collect them first to
246 // avoid iterator invalidation.
247 SmallVector<MemoryAccess *, 16> Accs;
248 for (MemoryAccess *RA : Stmt) {
249 if (!RA->isRead())
250 continue;
251 if (!RA->isLatestScalarKind())
252 continue;
253
254 Accs.push_back(RA);
255 }
256
257 for (MemoryAccess *RA : Accs) {
258 if (tryForwardTree(RA)) {
259 Modified = true;
260 StmtModified = true;
261 NumForwardedTrees++;
262 TotalForwardedTrees++;
263 }
264 }
265
266 if (StmtModified) {
267 NumModifiedStmts++;
268 TotalModifiedStmts++;
269 }
270 }
271
272 if (Modified)
273 ScopsModified++;
274 return Modified;
275 }
276
277 /// Print the pass result, performed transformations and the SCoP after the
278 /// transformation.
279 void print(llvm::raw_ostream &OS, int Indent = 0) {
280 printStatistics(OS, Indent);
281
282 if (!Modified) {
283 // This line can easily be checked in regression tests.
284 OS << "ForwardOpTree executed, but did not modify anything\n";
285 return;
286 }
287
288 printStatements(OS, Indent);
289 }
290};
291
292/// Pass that redirects scalar reads to array elements that are known to contain
293/// the same value.
294///
295/// This reduces the number of scalar accesses and therefore potentially
296/// increases the freedom of the scheduler. In the ideal case, all reads of a
297/// scalar definition are redirected (We currently do not care about removing
298/// the write in this case). This is also useful for the main DeLICM pass as
299/// there are less scalars to be mapped.
300class ForwardOpTree : public ScopPass {
301private:
302 ForwardOpTree(const ForwardOpTree &) = delete;
303 const ForwardOpTree &operator=(const ForwardOpTree &) = delete;
304
305 /// The pass implementation, also holding per-scop data.
306 std::unique_ptr<ForwardOpTreeImpl> Impl;
307
308public:
309 static char ID;
310
311 explicit ForwardOpTree() : ScopPass(ID) {}
312
313 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
314 AU.addRequiredTransitive<ScopInfoRegionPass>();
315 AU.addRequired<LoopInfoWrapperPass>();
316 AU.setPreservesAll();
317 }
318
319 virtual bool runOnScop(Scop &S) override {
320 // Free resources for previous SCoP's computation, if not yet done.
321 releaseMemory();
322
323 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
324 Impl = make_unique<ForwardOpTreeImpl>(&S, &LI);
325
326 DEBUG(dbgs() << "Forwarding operand trees...\n");
327 Impl->forwardOperandTrees();
328
329 DEBUG(dbgs() << "\nFinal Scop:\n");
330 DEBUG(dbgs() << S);
331
332 return false;
333 }
334
335 virtual void printScop(raw_ostream &OS, Scop &S) const override {
336 if (!Impl)
337 return;
338
339 assert(Impl->getScop() == &S);
340 Impl->print(OS);
341 }
342
343 virtual void releaseMemory() override { Impl.reset(); }
344
345}; // class ForwardOpTree
346
347char ForwardOpTree::ID;
348} // anonymous namespace
349
350ScopPass *polly::createForwardOpTreePass() { return new ForwardOpTree(); }
351
352INITIALIZE_PASS_BEGIN(ForwardOpTree, "polly-optree",
353 "Polly - Forward operand tree", false, false)
354INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
355INITIALIZE_PASS_END(ForwardOpTree, "polly-optree",
356 "Polly - Forward operand tree", false, false)