Coverage Report

Created: 2019-04-21 11:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/include/polly/ScopBuilder.h
Line
Count
Source
1
//===- polly/ScopBuilder.h --------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Create a polyhedral description for a static control flow region.
10
//
11
// The pass creates a polyhedral description of the Scops detected by the SCoP
12
// detection derived from their LLVM-IR code.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef POLLY_SCOPBUILDER_H
17
#define POLLY_SCOPBUILDER_H
18
19
#include "polly/ScopInfo.h"
20
#include "polly/Support/ScopHelper.h"
21
#include "llvm/ADT/ArrayRef.h"
22
#include "llvm/ADT/SetVector.h"
23
24
namespace polly {
25
26
class ScopDetection;
27
28
/// Command line switch whether to model read-only accesses.
29
extern bool ModelReadOnlyScalars;
30
31
/// Build the Polly IR (Scop and ScopStmt) on a Region.
32
class ScopBuilder {
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
                 OptimizationRemarkEmitter &ORE);
122
123
  /// Try to build a multi-dimensional fixed sized MemoryAccess from the
124
  /// Load/Store instruction.
125
  ///
126
  /// @param Inst       The Load/Store instruction that access the memory
127
  /// @param Stmt       The parent statement of the instruction
128
  ///
129
  /// @returns True if the access could be built, False otherwise.
130
  bool buildAccessMultiDimFixed(MemAccInst Inst, ScopStmt *Stmt);
131
132
  /// Try to build a multi-dimensional parametric sized MemoryAccess.
133
  ///        from the Load/Store instruction.
134
  ///
135
  /// @param Inst       The Load/Store instruction that access the memory
136
  /// @param Stmt       The parent statement of the instruction
137
  ///
138
  /// @returns True if the access could be built, False otherwise.
139
  bool buildAccessMultiDimParam(MemAccInst Inst, ScopStmt *Stmt);
140
141
  /// Try to build a MemoryAccess for a memory intrinsic.
142
  ///
143
  /// @param Inst       The instruction that access the memory
144
  /// @param Stmt       The parent statement of the instruction
145
  ///
146
  /// @returns True if the access could be built, False otherwise.
147
  bool buildAccessMemIntrinsic(MemAccInst Inst, ScopStmt *Stmt);
148
149
  /// Try to build a MemoryAccess for a call instruction.
150
  ///
151
  /// @param Inst       The call instruction that access the memory
152
  /// @param Stmt       The parent statement of the instruction
153
  ///
154
  /// @returns True if the access could be built, False otherwise.
155
  bool buildAccessCallInst(MemAccInst Inst, ScopStmt *Stmt);
156
157
  /// Build a single-dimensional parametric sized MemoryAccess
158
  ///        from the Load/Store instruction.
159
  ///
160
  /// @param Inst       The Load/Store instruction that access the memory
161
  /// @param Stmt       The parent statement of the instruction
162
  void buildAccessSingleDim(MemAccInst Inst, ScopStmt *Stmt);
163
164
  /// Build an instance of MemoryAccess from the Load/Store instruction.
165
  ///
166
  /// @param Inst       The Load/Store instruction that access the memory
167
  /// @param Stmt       The parent statement of the instruction
168
  void buildMemoryAccess(MemAccInst Inst, ScopStmt *Stmt);
169
170
  /// Analyze and extract the cross-BB scalar dependences (or, dataflow
171
  /// dependencies) of an instruction.
172
  ///
173
  /// @param UserStmt The statement @p Inst resides in.
174
  /// @param Inst     The instruction to be analyzed.
175
  void buildScalarDependences(ScopStmt *UserStmt, Instruction *Inst);
176
177
  /// Build the escaping dependences for @p Inst.
178
  ///
179
  /// Search for uses of the llvm::Value defined by @p Inst that are not
180
  /// within the SCoP. If there is such use, add a SCALAR WRITE such that
181
  /// it is available after the SCoP as escaping value.
182
  ///
183
  /// @param Inst The instruction to be analyzed.
184
  void buildEscapingDependences(Instruction *Inst);
185
186
  /// Create MemoryAccesses for the given PHI node in the given region.
187
  ///
188
  /// @param PHIStmt            The statement @p PHI resides in.
189
  /// @param PHI                The PHI node to be handled
190
  /// @param NonAffineSubRegion The non affine sub-region @p PHI is in.
191
  /// @param IsExitBlock        Flag to indicate that @p PHI is in the exit BB.
192
  void buildPHIAccesses(ScopStmt *PHIStmt, PHINode *PHI,
193
                        Region *NonAffineSubRegion, bool IsExitBlock = false);
194
195
  /// Build the access functions for the subregion @p SR.
196
  void buildAccessFunctions();
197
198
  /// Should an instruction be modeled in a ScopStmt.
199
  ///
200
  /// @param Inst The instruction to check.
201
  /// @param L    The loop in which context the instruction is looked at.
202
  ///
203
  /// @returns True if the instruction should be modeled.
204
  bool shouldModelInst(Instruction *Inst, Loop *L);
205
206
  /// Create one or more ScopStmts for @p BB.
207
  ///
208
  /// Consecutive instructions are associated to the same statement until a
209
  /// separator is found.
210
  void buildSequentialBlockStmts(BasicBlock *BB, bool SplitOnStore = false);
211
212
  /// Create one or more ScopStmts for @p BB using equivalence classes.
213
  ///
214
  /// Instructions of a basic block that belong to the same equivalence class
215
  /// are added to the same statement.
216
  void buildEqivClassBlockStmts(BasicBlock *BB);
217
218
  /// Create ScopStmt for all BBs and non-affine subregions of @p SR.
219
  ///
220
  /// @param SR A subregion of @p R.
221
  ///
222
  /// Some of the statements might be optimized away later when they do not
223
  /// access any memory and thus have no effect.
224
  void buildStmts(Region &SR);
225
226
  /// Build the access functions for the statement @p Stmt in or represented by
227
  /// @p BB.
228
  ///
229
  /// @param Stmt               Statement to add MemoryAccesses to.
230
  /// @param BB                 A basic block in @p R.
231
  /// @param NonAffineSubRegion The non affine sub-region @p BB is in.
232
  void buildAccessFunctions(ScopStmt *Stmt, BasicBlock &BB,
233
                            Region *NonAffineSubRegion = nullptr);
234
235
  /// Create a new MemoryAccess object and add it to #AccFuncMap.
236
  ///
237
  /// @param Stmt        The statement where the access takes place.
238
  /// @param Inst        The instruction doing the access. It is not necessarily
239
  ///                    inside @p BB.
240
  /// @param AccType     The kind of access.
241
  /// @param BaseAddress The accessed array's base address.
242
  /// @param ElemType    The type of the accessed array elements.
243
  /// @param Affine      Whether all subscripts are affine expressions.
244
  /// @param AccessValue Value read or written.
245
  /// @param Subscripts  Access subscripts per dimension.
246
  /// @param Sizes       The array dimension's sizes.
247
  /// @param Kind        The kind of memory accessed.
248
  ///
249
  /// @return The created MemoryAccess, or nullptr if the access is not within
250
  ///         the SCoP.
251
  MemoryAccess *addMemoryAccess(ScopStmt *Stmt, Instruction *Inst,
252
                                MemoryAccess::AccessType AccType,
253
                                Value *BaseAddress, Type *ElemType, bool Affine,
254
                                Value *AccessValue,
255
                                ArrayRef<const SCEV *> Subscripts,
256
                                ArrayRef<const SCEV *> Sizes, MemoryKind Kind);
257
258
  /// Create a MemoryAccess that represents either a LoadInst or
259
  /// StoreInst.
260
  ///
261
  /// @param Stmt        The statement to add the MemoryAccess to.
262
  /// @param MemAccInst  The LoadInst or StoreInst.
263
  /// @param AccType     The kind of access.
264
  /// @param BaseAddress The accessed array's base address.
265
  /// @param ElemType    The type of the accessed array elements.
266
  /// @param IsAffine    Whether all subscripts are affine expressions.
267
  /// @param Subscripts  Access subscripts per dimension.
268
  /// @param Sizes       The array dimension's sizes.
269
  /// @param AccessValue Value read or written.
270
  ///
271
  /// @see MemoryKind
272
  void addArrayAccess(ScopStmt *Stmt, MemAccInst MemAccInst,
273
                      MemoryAccess::AccessType AccType, Value *BaseAddress,
274
                      Type *ElemType, bool IsAffine,
275
                      ArrayRef<const SCEV *> Subscripts,
276
                      ArrayRef<const SCEV *> Sizes, Value *AccessValue);
277
278
  /// Create a MemoryAccess for writing an llvm::Instruction.
279
  ///
280
  /// The access will be created at the position of @p Inst.
281
  ///
282
  /// @param Inst The instruction to be written.
283
  ///
284
  /// @see ensureValueRead()
285
  /// @see MemoryKind
286
  void ensureValueWrite(Instruction *Inst);
287
288
  /// Ensure an llvm::Value is available in the BB's statement, creating a
289
  /// MemoryAccess for reloading it if necessary.
290
  ///
291
  /// @param V        The value expected to be loaded.
292
  /// @param UserStmt Where to reload the value.
293
  ///
294
  /// @see ensureValueStore()
295
  /// @see MemoryKind
296
  void ensureValueRead(Value *V, ScopStmt *UserStmt);
297
298
  /// Create a write MemoryAccess for the incoming block of a phi node.
299
  ///
300
  /// Each of the incoming blocks write their incoming value to be picked in the
301
  /// phi's block.
302
  ///
303
  /// @param PHI           PHINode under consideration.
304
  /// @param IncomingStmt  The statement to add the MemoryAccess to.
305
  /// @param IncomingBlock Some predecessor block.
306
  /// @param IncomingValue @p PHI's value when coming from @p IncomingBlock.
307
  /// @param IsExitBlock   When true, uses the .s2a alloca instead of the
308
  ///                      .phiops one. Required for values escaping through a
309
  ///                      PHINode in the SCoP region's exit block.
310
  /// @see addPHIReadAccess()
311
  /// @see MemoryKind
312
  void ensurePHIWrite(PHINode *PHI, ScopStmt *IncomintStmt,
313
                      BasicBlock *IncomingBlock, Value *IncomingValue,
314
                      bool IsExitBlock);
315
316
  /// Create a MemoryAccess for reading the value of a phi.
317
  ///
318
  /// The modeling assumes that all incoming blocks write their incoming value
319
  /// to the same location. Thus, this access will read the incoming block's
320
  /// value as instructed by this @p PHI.
321
  ///
322
  /// @param PHIStmt Statement @p PHI resides in.
323
  /// @param PHI     PHINode under consideration; the READ access will be added
324
  ///                here.
325
  ///
326
  /// @see ensurePHIWrite()
327
  /// @see MemoryKind
328
  void addPHIReadAccess(ScopStmt *PHIStmt, PHINode *PHI);
329
330
  /// Build the domain of @p Stmt.
331
  void buildDomain(ScopStmt &Stmt);
332
333
  /// Fill NestLoops with loops surrounding @p Stmt.
334
  void collectSurroundingLoops(ScopStmt &Stmt);
335
336
  /// Check for reductions in @p Stmt.
337
  ///
338
  /// Iterate over all store memory accesses and check for valid binary
339
  /// reduction like chains. For all candidates we check if they have the same
340
  /// base address and there are no other accesses which overlap with them. The
341
  /// base address check rules out impossible reductions candidates early. The
342
  /// overlap check, together with the "only one user" check in
343
  /// collectCandidateReductionLoads, guarantees that none of the intermediate
344
  /// results will escape during execution of the loop nest. We basically check
345
  /// here that no other memory access can access the same memory as the
346
  /// potential reduction.
347
  void checkForReductions(ScopStmt &Stmt);
348
349
  /// Collect loads which might form a reduction chain with @p StoreMA.
350
  ///
351
  /// Check if the stored value for @p StoreMA is a binary operator with one or
352
  /// two loads as operands. If the binary operand is commutative & associative,
353
  /// used only once (by @p StoreMA) and its load operands are also used only
354
  /// once, we have found a possible reduction chain. It starts at an operand
355
  /// load and includes the binary operator and @p StoreMA.
356
  ///
357
  /// Note: We allow only one use to ensure the load and binary operator cannot
358
  ///       escape this block or into any other store except @p StoreMA.
359
  void collectCandidateReductionLoads(MemoryAccess *StoreMA,
360
                                      SmallVectorImpl<MemoryAccess *> &Loads);
361
362
  /// Build the access relation of all memory accesses of @p Stmt.
363
  void buildAccessRelations(ScopStmt &Stmt);
364
365
public:
366
  explicit ScopBuilder(Region *R, AssumptionCache &AC, AliasAnalysis &AA,
367
                       const DataLayout &DL, DominatorTree &DT, LoopInfo &LI,
368
                       ScopDetection &SD, ScalarEvolution &SE,
369
                       OptimizationRemarkEmitter &ORE);
370
  ScopBuilder(const ScopBuilder &) = delete;
371
  ScopBuilder &operator=(const ScopBuilder &) = delete;
372
383
  ~ScopBuilder() = default;
373
374
  /// Try to build the Polly IR of static control part on the current
375
  /// SESE-Region.
376
  ///
377
  /// @return Give up the ownership of the scop object or static control part
378
  ///         for the region
379
383
  std::unique_ptr<Scop> getScop() { return std::move(scop); }
380
};
381
} // end namespace polly
382
383
#endif // POLLY_SCOPBUILDER_H