Coverage Report

Created: 2017-08-18 19:41

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/include/polly/ScopBuilder.h
Line
Count
Source
1
//===- polly/ScopBuilder.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
// Create a polyhedral description for a static control flow region.
11
//
12
// The pass creates a polyhedral description of the Scops detected by the SCoP
13
// detection derived from their LLVM-IR code.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#ifndef POLLY_SCOP_BUILDER_H
18
#define POLLY_SCOP_BUILDER_H
19
20
#include "polly/ScopInfo.h"
21
22
namespace polly {
23
24
/// Command line switch whether to model read-only accesses.
25
extern bool ModelReadOnlyScalars;
26
27
/// Build the Polly IR (Scop and ScopStmt) on a Region.
28
class ScopBuilder {
29
  //===-------------------------------------------------------------------===//
30
  ScopBuilder(const ScopBuilder &) = delete;
31
  const ScopBuilder &operator=(const ScopBuilder &) = delete;
32
33
  /// The AliasAnalysis to build AliasSetTracker.
34
  AliasAnalysis &AA;
35
36
  /// Target data for element size computing.
37
  const DataLayout &DL;
38
39
  /// DominatorTree to reason about guaranteed execution.
40
  DominatorTree &DT;
41
42
  /// LoopInfo for information about loops.
43
  LoopInfo &LI;
44
45
  /// Valid Regions for Scop
46
  ScopDetection &SD;
47
48
  /// The ScalarEvolution to help building Scop.
49
  ScalarEvolution &SE;
50
51
  /// Set of instructions that might read any memory location.
52
  SmallVector<std::pair<ScopStmt *, Instruction *>, 16> GlobalReads;
53
54
  /// Set of all accessed array base pointers.
55
  SmallSetVector<Value *, 16> ArrayBasePointers;
56
57
  // The Scop
58
  std::unique_ptr<Scop> scop;
59
60
  // Methods for pattern matching against Fortran code generated by dragonegg.
61
  // @{
62
63
  /// Try to match for the descriptor of a Fortran array whose allocation
64
  /// is not visible. That is, we can see the load/store into the memory, but
65
  /// we don't actually know where the memory is allocated. If ALLOCATE had been
66
  /// called on the Fortran array, then we will see the lowered malloc() call.
67
  /// If not, this is dubbed as an "invisible allocation".
68
  ///
69
  /// "<descriptor>" is the descriptor of the Fortran array.
70
  ///
71
  /// Pattern match for "@descriptor":
72
  ///  1. %mem = load double*, double** bitcast (%"struct.array1_real(kind=8)"*
73
  ///    <descriptor> to double**), align 32
74
  ///
75
  ///  2. [%slot = getelementptr inbounds i8, i8* %mem, i64 <index>]
76
  ///  2 is optional because if you are writing to the 0th index, you don't
77
  ///     need a GEP.
78
  ///
79
  ///  3.1 store/load <memtype> <val>, <memtype>* %slot
80
  ///  3.2 store/load <memtype> <val>, <memtype>* %mem
81
  ///
82
  /// @see polly::MemoryAccess, polly::ScopArrayInfo
83
  ///
84
  /// @note assumes -polly-canonicalize has been run.
85
  ///
86
  /// @param Inst The LoadInst/StoreInst that accesses the memory.
87
  ///
88
  /// @returns Reference to <descriptor> on success, nullptr on failure.
89
  Value *findFADAllocationInvisible(MemAccInst Inst);
90
91
  /// Try to match for the descriptor of a Fortran array whose allocation
92
  /// call is visible. When we have a Fortran array, we try to look for a
93
  /// Fortran array where we can see the lowered ALLOCATE call. ALLOCATE
94
  /// is materialized as a malloc(...) which we pattern match for.
95
  ///
96
  /// Pattern match for "%untypedmem":
97
  ///  1. %untypedmem = i8* @malloc(...)
98
  ///
99
  ///  2. %typedmem = bitcast i8* %untypedmem to <memtype>
100
  ///
101
  ///  3. [%slot = getelementptr inbounds i8, i8* %typedmem, i64 <index>]
102
  ///  3 is optional because if you are writing to the 0th index, you don't
103
  ///     need a GEP.
104
  ///
105
  ///  4.1 store/load <memtype> <val>, <memtype>* %slot, align 8
106
  ///  4.2 store/load <memtype> <val>, <memtype>* %mem, align 8
107
  ///
108
  /// @see polly::MemoryAccess, polly::ScopArrayInfo
109
  ///
110
  /// @note assumes -polly-canonicalize has been run.
111
  ///
112
  /// @param Inst The LoadInst/StoreInst that accesses the memory.
113
  ///
114
  /// @returns Reference to %untypedmem on success, nullptr on failure.
115
  Value *findFADAllocationVisible(MemAccInst Inst);
116
117
  // @}
118
119
  // Build the SCoP for Region @p R.
120
  void buildScop(Region &R, AssumptionCache &AC);
121
122
  /// Try to build a multi-dimensional fixed sized MemoryAccess from the
123
  /// Load/Store instruction.
124
  ///
125
  /// @param Inst       The Load/Store instruction that access the memory
126
  /// @param Stmt       The parent statement of the instruction
127
  ///
128
  /// @returns True if the access could be built, False otherwise.
129
  bool buildAccessMultiDimFixed(MemAccInst Inst, ScopStmt *Stmt);
130
131
  /// Try to build a multi-dimensional parametric sized MemoryAccess.
132
  ///        from the Load/Store instruction.
133
  ///
134
  /// @param Inst       The Load/Store instruction that access the memory
135
  /// @param Stmt       The parent statement of the instruction
136
  ///
137
  /// @returns True if the access could be built, False otherwise.
138
  bool buildAccessMultiDimParam(MemAccInst Inst, ScopStmt *Stmt);
139
140
  /// Try to build a MemoryAccess for a memory intrinsic.
141
  ///
142
  /// @param Inst       The instruction that access the memory
143
  /// @param Stmt       The parent statement of the instruction
144
  ///
145
  /// @returns True if the access could be built, False otherwise.
146
  bool buildAccessMemIntrinsic(MemAccInst Inst, ScopStmt *Stmt);
147
148
  /// Try to build a MemoryAccess for a call instruction.
149
  ///
150
  /// @param Inst       The call instruction that access the memory
151
  /// @param Stmt       The parent statement of the instruction
152
  ///
153
  /// @returns True if the access could be built, False otherwise.
154
  bool buildAccessCallInst(MemAccInst Inst, ScopStmt *Stmt);
155
156
  /// Build a single-dimensional parametric sized MemoryAccess
157
  ///        from the Load/Store instruction.
158
  ///
159
  /// @param Inst       The Load/Store instruction that access the memory
160
  /// @param Stmt       The parent statement of the instruction
161
  void buildAccessSingleDim(MemAccInst Inst, ScopStmt *Stmt);
162
163
  /// Build an instance of MemoryAccess from the Load/Store instruction.
164
  ///
165
  /// @param Inst       The Load/Store instruction that access the memory
166
  /// @param Stmt       The parent statement of the instruction
167
  void buildMemoryAccess(MemAccInst Inst, ScopStmt *Stmt);
168
169
  /// Analyze and extract the cross-BB scalar dependences (or, dataflow
170
  /// dependencies) of an instruction.
171
  ///
172
  /// @param UserStmt The statement @p Inst resides in.
173
  /// @param Inst     The instruction to be analyzed.
174
  void buildScalarDependences(ScopStmt *UserStmt, Instruction *Inst);
175
176
  /// Build the escaping dependences for @p Inst.
177
  ///
178
  /// Search for uses of the llvm::Value defined by @p Inst that are not
179
  /// within the SCoP. If there is such use, add a SCALAR WRITE such that
180
  /// it is available after the SCoP as escaping value.
181
  ///
182
  /// @param Inst The instruction to be analyzed.
183
  void buildEscapingDependences(Instruction *Inst);
184
185
  /// Create MemoryAccesses for the given PHI node in the given region.
186
  ///
187
  /// @param PHIStmt            The statement @p PHI resides in.
188
  /// @param PHI                The PHI node to be handled
189
  /// @param NonAffineSubRegion The non affine sub-region @p PHI is in.
190
  /// @param IsExitBlock        Flag to indicate that @p PHI is in the exit BB.
191
  void buildPHIAccesses(ScopStmt *PHIStmt, PHINode *PHI,
192
                        Region *NonAffineSubRegion, bool IsExitBlock = false);
193
194
  /// Build the access functions for the subregion @p SR.
195
  void buildAccessFunctions();
196
197
  /// Create ScopStmt for all BBs and non-affine subregions of @p SR.
198
  ///
199
  /// @param SR A subregion of @p R.
200
  ///
201
  /// Some of the statements might be optimized away later when they do not
202
  /// access any memory and thus have no effect.
203
  void buildStmts(Region &SR);
204
205
  /// Build the access functions for the basic block @p BB in or represented by
206
  /// @p Stmt.
207
  ///
208
  /// @param Stmt               Statement to add MemoryAccesses to.
209
  /// @param BB                 A basic block in @p R.
210
  /// @param NonAffineSubRegion The non affine sub-region @p BB is in.
211
  /// @param IsExitBlock        Flag to indicate that @p BB is in the exit BB.
212
  void buildAccessFunctions(ScopStmt *Stmt, BasicBlock &BB,
213
                            Region *NonAffineSubRegion = nullptr,
214
                            bool IsExitBlock = false);
215
216
  /// Create a new MemoryAccess object and add it to #AccFuncMap.
217
  ///
218
  /// @param Stmt        The statement where the access takes place.
219
  /// @param Inst        The instruction doing the access. It is not necessarily
220
  ///                    inside @p BB.
221
  /// @param AccType     The kind of access.
222
  /// @param BaseAddress The accessed array's base address.
223
  /// @param ElemType    The type of the accessed array elements.
224
  /// @param Affine      Whether all subscripts are affine expressions.
225
  /// @param AccessValue Value read or written.
226
  /// @param Subscripts  Access subscripts per dimension.
227
  /// @param Sizes       The array dimension's sizes.
228
  /// @param Kind        The kind of memory accessed.
229
  ///
230
  /// @return The created MemoryAccess, or nullptr if the access is not within
231
  ///         the SCoP.
232
  MemoryAccess *addMemoryAccess(ScopStmt *Stmt, Instruction *Inst,
233
                                MemoryAccess::AccessType AccType,
234
                                Value *BaseAddress, Type *ElemType, bool Affine,
235
                                Value *AccessValue,
236
                                ArrayRef<const SCEV *> Subscripts,
237
                                ArrayRef<const SCEV *> Sizes, MemoryKind Kind);
238
239
  /// Create a MemoryAccess that represents either a LoadInst or
240
  /// StoreInst.
241
  ///
242
  /// @param Stmt        The statement to add the MemoryAccess to.
243
  /// @param MemAccInst  The LoadInst or StoreInst.
244
  /// @param AccType     The kind of access.
245
  /// @param BaseAddress The accessed array's base address.
246
  /// @param ElemType    The type of the accessed array elements.
247
  /// @param IsAffine    Whether all subscripts are affine expressions.
248
  /// @param Subscripts  Access subscripts per dimension.
249
  /// @param Sizes       The array dimension's sizes.
250
  /// @param AccessValue Value read or written.
251
  ///
252
  /// @see MemoryKind
253
  void addArrayAccess(ScopStmt *Stmt, MemAccInst MemAccInst,
254
                      MemoryAccess::AccessType AccType, Value *BaseAddress,
255
                      Type *ElemType, bool IsAffine,
256
                      ArrayRef<const SCEV *> Subscripts,
257
                      ArrayRef<const SCEV *> Sizes, Value *AccessValue);
258
259
  /// Create a MemoryAccess for writing an llvm::Instruction.
260
  ///
261
  /// The access will be created at the position of @p Inst.
262
  ///
263
  /// @param Inst The instruction to be written.
264
  ///
265
  /// @see ensureValueRead()
266
  /// @see MemoryKind
267
  void ensureValueWrite(Instruction *Inst);
268
269
  /// Ensure an llvm::Value is available in the BB's statement, creating a
270
  /// MemoryAccess for reloading it if necessary.
271
  ///
272
  /// @param V        The value expected to be loaded.
273
  /// @param UserStmt Where to reload the value.
274
  ///
275
  /// @see ensureValueStore()
276
  /// @see MemoryKind
277
  void ensureValueRead(Value *V, ScopStmt *UserStmt);
278
279
  /// Create a write MemoryAccess for the incoming block of a phi node.
280
  ///
281
  /// Each of the incoming blocks write their incoming value to be picked in the
282
  /// phi's block.
283
  ///
284
  /// @param PHI           PHINode under consideration.
285
  /// @param IncomingStmt  The statement to add the MemoryAccess to.
286
  /// @param IncomingBlock Some predecessor block.
287
  /// @param IncomingValue @p PHI's value when coming from @p IncomingBlock.
288
  /// @param IsExitBlock   When true, uses the .s2a alloca instead of the
289
  ///                      .phiops one. Required for values escaping through a
290
  ///                      PHINode in the SCoP region's exit block.
291
  /// @see addPHIReadAccess()
292
  /// @see MemoryKind
293
  void ensurePHIWrite(PHINode *PHI, ScopStmt *IncomintStmt,
294
                      BasicBlock *IncomingBlock, Value *IncomingValue,
295
                      bool IsExitBlock);
296
297
  /// Create a MemoryAccess for reading the value of a phi.
298
  ///
299
  /// The modeling assumes that all incoming blocks write their incoming value
300
  /// to the same location. Thus, this access will read the incoming block's
301
  /// value as instructed by this @p PHI.
302
  ///
303
  /// @param PHIStmt Statement @p PHI resides in.
304
  /// @param PHI     PHINode under consideration; the READ access will be added
305
  ///                here.
306
  ///
307
  /// @see ensurePHIWrite()
308
  /// @see MemoryKind
309
  void addPHIReadAccess(ScopStmt *PHIStmt, PHINode *PHI);
310
311
public:
312
  explicit ScopBuilder(Region *R, AssumptionCache &AC, AliasAnalysis &AA,
313
                       const DataLayout &DL, DominatorTree &DT, LoopInfo &LI,
314
                       ScopDetection &SD, ScalarEvolution &SE);
315
1.10k
  ~ScopBuilder() {}
316
317
  /// Try to build the Polly IR of static control part on the current
318
  /// SESE-Region.
319
  ///
320
  /// @return Give up the ownership of the scop object or static control part
321
  ///         for the region
322
1.10k
  std::unique_ptr<Scop> getScop() { return std::move(scop); }
323
};
324
325
} // end namespace polly
326
327
namespace llvm {
328
class PassRegistry;
329
void initializeScopInfoRegionPassPass(llvm::PassRegistry &);
330
void initializeScopInfoWrapperPassPass(llvm::PassRegistry &);
331
} // namespace llvm
332
333
#endif