1 //===- polly/ScopBuilder.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Create a polyhedral description for a static control flow region.
12 // The pass creates a polyhedral description of the Scops detected by the SCoP
13 // detection derived from their LLVM-IR code.
15 //===----------------------------------------------------------------------===//
17 #ifndef POLLY_SCOPBUILDER_H
18 #define POLLY_SCOPBUILDER_H
20 #include "polly/ScopInfo.h"
21 #include "polly/Support/ScopHelper.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallVector.h"
31 class AssumptionCache
;
40 class ScalarEvolution
;
45 void initializeScopInfoRegionPassPass(PassRegistry
&);
46 void initializeScopInfoWrapperPassPass(PassRegistry
&);
48 } // end namespace llvm
54 /// Command line switch whether to model read-only accesses.
55 extern bool ModelReadOnlyScalars
;
57 /// Build the Polly IR (Scop and ScopStmt) on a Region.
59 /// The AliasAnalysis to build AliasSetTracker.
62 /// Target data for element size computing.
65 /// DominatorTree to reason about guaranteed execution.
68 /// LoopInfo for information about loops.
71 /// Valid Regions for Scop
74 /// The ScalarEvolution to help building Scop.
77 /// Set of instructions that might read any memory location.
78 SmallVector
<std::pair
<ScopStmt
*, Instruction
*>, 16> GlobalReads
;
80 /// Set of all accessed array base pointers.
81 SmallSetVector
<Value
*, 16> ArrayBasePointers
;
84 std::unique_ptr
<Scop
> scop
;
86 // Methods for pattern matching against Fortran code generated by dragonegg.
89 /// Try to match for the descriptor of a Fortran array whose allocation
90 /// is not visible. That is, we can see the load/store into the memory, but
91 /// we don't actually know where the memory is allocated. If ALLOCATE had been
92 /// called on the Fortran array, then we will see the lowered malloc() call.
93 /// If not, this is dubbed as an "invisible allocation".
95 /// "<descriptor>" is the descriptor of the Fortran array.
97 /// Pattern match for "@descriptor":
98 /// 1. %mem = load double*, double** bitcast (%"struct.array1_real(kind=8)"*
99 /// <descriptor> to double**), align 32
101 /// 2. [%slot = getelementptr inbounds i8, i8* %mem, i64 <index>]
102 /// 2 is optional because if you are writing to the 0th index, you don't
105 /// 3.1 store/load <memtype> <val>, <memtype>* %slot
106 /// 3.2 store/load <memtype> <val>, <memtype>* %mem
108 /// @see polly::MemoryAccess, polly::ScopArrayInfo
110 /// @note assumes -polly-canonicalize has been run.
112 /// @param Inst The LoadInst/StoreInst that accesses the memory.
114 /// @returns Reference to <descriptor> on success, nullptr on failure.
115 Value
*findFADAllocationInvisible(MemAccInst Inst
);
117 /// Try to match for the descriptor of a Fortran array whose allocation
118 /// call is visible. When we have a Fortran array, we try to look for a
119 /// Fortran array where we can see the lowered ALLOCATE call. ALLOCATE
120 /// is materialized as a malloc(...) which we pattern match for.
122 /// Pattern match for "%untypedmem":
123 /// 1. %untypedmem = i8* @malloc(...)
125 /// 2. %typedmem = bitcast i8* %untypedmem to <memtype>
127 /// 3. [%slot = getelementptr inbounds i8, i8* %typedmem, i64 <index>]
128 /// 3 is optional because if you are writing to the 0th index, you don't
131 /// 4.1 store/load <memtype> <val>, <memtype>* %slot, align 8
132 /// 4.2 store/load <memtype> <val>, <memtype>* %mem, align 8
134 /// @see polly::MemoryAccess, polly::ScopArrayInfo
136 /// @note assumes -polly-canonicalize has been run.
138 /// @param Inst The LoadInst/StoreInst that accesses the memory.
140 /// @returns Reference to %untypedmem on success, nullptr on failure.
141 Value
*findFADAllocationVisible(MemAccInst Inst
);
145 // Build the SCoP for Region @p R.
146 void buildScop(Region
&R
, AssumptionCache
&AC
,
147 OptimizationRemarkEmitter
&ORE
);
149 /// Try to build a multi-dimensional fixed sized MemoryAccess from the
150 /// Load/Store instruction.
152 /// @param Inst The Load/Store instruction that access the memory
153 /// @param Stmt The parent statement of the instruction
155 /// @returns True if the access could be built, False otherwise.
156 bool buildAccessMultiDimFixed(MemAccInst Inst
, ScopStmt
*Stmt
);
158 /// Try to build a multi-dimensional parametric sized MemoryAccess.
159 /// from the Load/Store instruction.
161 /// @param Inst The Load/Store instruction that access the memory
162 /// @param Stmt The parent statement of the instruction
164 /// @returns True if the access could be built, False otherwise.
165 bool buildAccessMultiDimParam(MemAccInst Inst
, ScopStmt
*Stmt
);
167 /// Try to build a MemoryAccess for a memory intrinsic.
169 /// @param Inst The instruction that access the memory
170 /// @param Stmt The parent statement of the instruction
172 /// @returns True if the access could be built, False otherwise.
173 bool buildAccessMemIntrinsic(MemAccInst Inst
, ScopStmt
*Stmt
);
175 /// Try to build a MemoryAccess for a call instruction.
177 /// @param Inst The call instruction that access the memory
178 /// @param Stmt The parent statement of the instruction
180 /// @returns True if the access could be built, False otherwise.
181 bool buildAccessCallInst(MemAccInst Inst
, ScopStmt
*Stmt
);
183 /// Build a single-dimensional parametric sized MemoryAccess
184 /// from the Load/Store instruction.
186 /// @param Inst The Load/Store instruction that access the memory
187 /// @param Stmt The parent statement of the instruction
188 void buildAccessSingleDim(MemAccInst Inst
, ScopStmt
*Stmt
);
190 /// Build an instance of MemoryAccess from the Load/Store instruction.
192 /// @param Inst The Load/Store instruction that access the memory
193 /// @param Stmt The parent statement of the instruction
194 void buildMemoryAccess(MemAccInst Inst
, ScopStmt
*Stmt
);
196 /// Analyze and extract the cross-BB scalar dependences (or, dataflow
197 /// dependencies) of an instruction.
199 /// @param UserStmt The statement @p Inst resides in.
200 /// @param Inst The instruction to be analyzed.
201 void buildScalarDependences(ScopStmt
*UserStmt
, Instruction
*Inst
);
203 /// Build the escaping dependences for @p Inst.
205 /// Search for uses of the llvm::Value defined by @p Inst that are not
206 /// within the SCoP. If there is such use, add a SCALAR WRITE such that
207 /// it is available after the SCoP as escaping value.
209 /// @param Inst The instruction to be analyzed.
210 void buildEscapingDependences(Instruction
*Inst
);
212 /// Create MemoryAccesses for the given PHI node in the given region.
214 /// @param PHIStmt The statement @p PHI resides in.
215 /// @param PHI The PHI node to be handled
216 /// @param NonAffineSubRegion The non affine sub-region @p PHI is in.
217 /// @param IsExitBlock Flag to indicate that @p PHI is in the exit BB.
218 void buildPHIAccesses(ScopStmt
*PHIStmt
, PHINode
*PHI
,
219 Region
*NonAffineSubRegion
, bool IsExitBlock
= false);
221 /// Build the access functions for the subregion @p SR.
222 void buildAccessFunctions();
224 /// Create ScopStmt for all BBs and non-affine subregions of @p SR.
226 /// @param SR A subregion of @p R.
228 /// Some of the statements might be optimized away later when they do not
229 /// access any memory and thus have no effect.
230 void buildStmts(Region
&SR
);
232 /// Build the access functions for the statement @p Stmt in or represented by
235 /// @param Stmt Statement to add MemoryAccesses to.
236 /// @param BB A basic block in @p R.
237 /// @param NonAffineSubRegion The non affine sub-region @p BB is in.
238 /// @param IsExitBlock Flag to indicate that @p BB is in the exit BB.
239 void buildAccessFunctions(ScopStmt
*Stmt
, BasicBlock
&BB
,
240 Region
*NonAffineSubRegion
= nullptr,
241 bool IsExitBlock
= false);
243 /// Create a new MemoryAccess object and add it to #AccFuncMap.
245 /// @param Stmt The statement where the access takes place.
246 /// @param Inst The instruction doing the access. It is not necessarily
248 /// @param AccType The kind of access.
249 /// @param BaseAddress The accessed array's base address.
250 /// @param ElemType The type of the accessed array elements.
251 /// @param Affine Whether all subscripts are affine expressions.
252 /// @param AccessValue Value read or written.
253 /// @param Subscripts Access subscripts per dimension.
254 /// @param Sizes The array dimension's sizes.
255 /// @param Kind The kind of memory accessed.
257 /// @return The created MemoryAccess, or nullptr if the access is not within
259 MemoryAccess
*addMemoryAccess(ScopStmt
*Stmt
, Instruction
*Inst
,
260 MemoryAccess::AccessType AccType
,
261 Value
*BaseAddress
, Type
*ElemType
, bool Affine
,
263 ArrayRef
<const SCEV
*> Subscripts
,
264 ArrayRef
<const SCEV
*> Sizes
, MemoryKind Kind
);
266 /// Create a MemoryAccess that represents either a LoadInst or
269 /// @param Stmt The statement to add the MemoryAccess to.
270 /// @param MemAccInst The LoadInst or StoreInst.
271 /// @param AccType The kind of access.
272 /// @param BaseAddress The accessed array's base address.
273 /// @param ElemType The type of the accessed array elements.
274 /// @param IsAffine Whether all subscripts are affine expressions.
275 /// @param Subscripts Access subscripts per dimension.
276 /// @param Sizes The array dimension's sizes.
277 /// @param AccessValue Value read or written.
280 void addArrayAccess(ScopStmt
*Stmt
, MemAccInst MemAccInst
,
281 MemoryAccess::AccessType AccType
, Value
*BaseAddress
,
282 Type
*ElemType
, bool IsAffine
,
283 ArrayRef
<const SCEV
*> Subscripts
,
284 ArrayRef
<const SCEV
*> Sizes
, Value
*AccessValue
);
286 /// Create a MemoryAccess for writing an llvm::Instruction.
288 /// The access will be created at the position of @p Inst.
290 /// @param Inst The instruction to be written.
292 /// @see ensureValueRead()
294 void ensureValueWrite(Instruction
*Inst
);
296 /// Ensure an llvm::Value is available in the BB's statement, creating a
297 /// MemoryAccess for reloading it if necessary.
299 /// @param V The value expected to be loaded.
300 /// @param UserStmt Where to reload the value.
302 /// @see ensureValueStore()
304 void ensureValueRead(Value
*V
, ScopStmt
*UserStmt
);
306 /// Create a write MemoryAccess for the incoming block of a phi node.
308 /// Each of the incoming blocks write their incoming value to be picked in the
311 /// @param PHI PHINode under consideration.
312 /// @param IncomingStmt The statement to add the MemoryAccess to.
313 /// @param IncomingBlock Some predecessor block.
314 /// @param IncomingValue @p PHI's value when coming from @p IncomingBlock.
315 /// @param IsExitBlock When true, uses the .s2a alloca instead of the
316 /// .phiops one. Required for values escaping through a
317 /// PHINode in the SCoP region's exit block.
318 /// @see addPHIReadAccess()
320 void ensurePHIWrite(PHINode
*PHI
, ScopStmt
*IncomintStmt
,
321 BasicBlock
*IncomingBlock
, Value
*IncomingValue
,
324 /// Create a MemoryAccess for reading the value of a phi.
326 /// The modeling assumes that all incoming blocks write their incoming value
327 /// to the same location. Thus, this access will read the incoming block's
328 /// value as instructed by this @p PHI.
330 /// @param PHIStmt Statement @p PHI resides in.
331 /// @param PHI PHINode under consideration; the READ access will be added
334 /// @see ensurePHIWrite()
336 void addPHIReadAccess(ScopStmt
*PHIStmt
, PHINode
*PHI
);
338 /// Build the domain of @p Stmt.
339 void buildDomain(ScopStmt
&Stmt
);
341 /// Fill NestLoops with loops surrounding @p Stmt.
342 void collectSurroundingLoops(ScopStmt
&Stmt
);
344 /// Build the access relation of all memory accesses of @p Stmt.
345 void buildAccessRelations(ScopStmt
&Stmt
);
348 explicit ScopBuilder(Region
*R
, AssumptionCache
&AC
, AliasAnalysis
&AA
,
349 const DataLayout
&DL
, DominatorTree
&DT
, LoopInfo
&LI
,
350 ScopDetection
&SD
, ScalarEvolution
&SE
,
351 OptimizationRemarkEmitter
&ORE
);
352 ScopBuilder(const ScopBuilder
&) = delete;
353 ScopBuilder
&operator=(const ScopBuilder
&) = delete;
354 ~ScopBuilder() = default;
356 /// Try to build the Polly IR of static control part on the current
359 /// @return Give up the ownership of the scop object or static control part
361 std::unique_ptr
<Scop
> getScop() { return std::move(scop
); }
364 } // end namespace polly
366 #endif // POLLY_SCOPBUILDER_H