Coverage Report

Created: 2018-12-09 11:54

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/include/polly/ZoneAlgo.h
Line
Count
Source (jump to first uncovered line)
1
//===------ ZoneAlgo.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
// Derive information about array elements between statements ("Zones").
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef POLLY_ZONEALGO_H
15
#define POLLY_ZONEALGO_H
16
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/DenseSet.h"
19
#include "llvm/ADT/SmallPtrSet.h"
20
#include "isl/isl-noexceptions.h"
21
#include <memory>
22
23
namespace llvm {
24
class Value;
25
class LoopInfo;
26
class Loop;
27
class PHINode;
28
class raw_ostream;
29
} // namespace llvm
30
31
namespace polly {
32
class Scop;
33
class ScopStmt;
34
class MemoryAccess;
35
class ScopArrayInfo;
36
37
/// Return only the mappings that map to known values.
38
///
39
/// @param UMap { [] -> ValInst[] }
40
///
41
/// @return { [] -> ValInst[] }
42
isl::union_map filterKnownValInst(const isl::union_map &UMap);
43
44
/// Base class for algorithms based on zones, like DeLICM.
45
class ZoneAlgorithm {
46
protected:
47
  /// The name of the pass this is used from. Used for optimization remarks.
48
  const char *PassName;
49
50
  /// Hold a reference to the isl_ctx to avoid it being freed before we released
51
  /// all of the isl objects.
52
  ///
53
  /// This must be declared before any other member that holds an isl object.
54
  /// This guarantees that the shared_ptr and its isl_ctx is destructed last,
55
  /// after all other members free'd the isl objects they were holding.
56
  std::shared_ptr<isl_ctx> IslCtx;
57
58
  /// Cached reaching definitions for each ScopStmt.
59
  ///
60
  /// Use getScalarReachingDefinition() to get its contents.
61
  llvm::DenseMap<ScopStmt *, isl::map> ScalarReachDefZone;
62
63
  /// The analyzed Scop.
64
  Scop *S;
65
66
  /// LoopInfo analysis used to determine whether values are synthesizable.
67
  llvm::LoopInfo *LI;
68
69
  /// Parameter space that does not need realignment.
70
  isl::space ParamSpace;
71
72
  /// Space the schedule maps to.
73
  isl::space ScatterSpace;
74
75
  /// Cached version of the schedule and domains.
76
  isl::union_map Schedule;
77
78
  /// Combined access relations of all MemoryKind::Array READ accesses.
79
  /// { DomainRead[] -> Element[] }
80
  isl::union_map AllReads;
81
82
  /// The loaded values (llvm::LoadInst) of all reads.
83
  /// { [Element[] -> DomainRead[]] -> ValInst[] }
84
  isl::union_map AllReadValInst;
85
86
  /// Combined access relations of all MemoryKind::Array, MAY_WRITE accesses.
87
  /// { DomainMayWrite[] -> Element[] }
88
  isl::union_map AllMayWrites;
89
90
  /// Combined access relations of all MemoryKind::Array, MUST_WRITE accesses.
91
  /// { DomainMustWrite[] -> Element[] }
92
  isl::union_map AllMustWrites;
93
94
  /// Combined access relations of all MK_Array write accesses (union of
95
  /// AllMayWrites and AllMustWrites).
96
  /// { DomainWrite[] -> Element[] }
97
  isl::union_map AllWrites;
98
99
  /// The value instances written to array elements of all write accesses.
100
  /// { [Element[] -> DomainWrite[]] -> ValInst[] }
101
  isl::union_map AllWriteValInst;
102
103
  /// All reaching definitions for  MemoryKind::Array writes.
104
  /// { [Element[] -> Zone[]] -> DomainWrite[] }
105
  isl::union_map WriteReachDefZone;
106
107
  /// Map llvm::Values to an isl identifier.
108
  /// Used with -polly-use-llvm-names=false as an alternative method to get
109
  /// unique ids that do not depend on pointer values.
110
  llvm::DenseMap<llvm::Value *, isl::id> ValueIds;
111
112
  /// Set of array elements that can be reliably used for zone analysis.
113
  /// { Element[] }
114
  isl::union_set CompatibleElts;
115
116
  /// List of PHIs that may transitively refer to themselves.
117
  ///
118
  /// Computing them would require a polyhedral transitive closure operation,
119
  /// for which isl may only return an approximation. For correctness, we always
120
  /// require an exact result. Hence, we exclude such PHIs.
121
  llvm::SmallPtrSet<llvm::PHINode *, 4> RecursivePHIs;
122
123
  /// PHIs that have been computed.
124
  ///
125
  /// Computed PHIs are replaced by their incoming values using #NormalizeMap.
126
  llvm::DenseSet<llvm::PHINode *> ComputedPHIs;
127
128
  /// For computed PHIs, contains the ValInst they stand for.
129
  ///
130
  /// To show an example, assume the following PHINode:
131
  ///
132
  ///   Stmt:
133
  ///     %phi = phi double [%val1, %bb1], [%val2, %bb2]
134
  ///
135
  /// It's ValInst is:
136
  ///
137
  ///   { [Stmt[i] -> phi[]] }
138
  ///
139
  /// The value %phi will be either %val1 or %val2, depending on whether in
140
  /// iteration i %bb1 or %bb2 has been executed before. In SCoPs, this can be
141
  /// determined at compile-time, and the result stored in #NormalizeMap. For
142
  /// the previous example, it could be:
143
  ///
144
  ///   { [Stmt[i] -> phi[]] -> [Stmt[0] -> val1[]];
145
  ///     [Stmt[i] -> phi[]] -> [Stmt[i] -> val2[]] : i > 0 }
146
  ///
147
  /// Only ValInsts in #ComputedPHIs are present in this map. Other values are
148
  /// assumed to represent themselves. This is to avoid adding lots of identity
149
  /// entries to this map.
150
  ///
151
  /// { PHIValInst[] -> IncomingValInst[] }
152
  isl::union_map NormalizeMap;
153
154
  /// Cache for computePerPHI(const ScopArrayInfo *)
155
  llvm::SmallDenseMap<llvm::PHINode *, isl::union_map> PerPHIMaps;
156
157
  /// A cache for getDefToTarget().
158
  llvm::DenseMap<std::pair<ScopStmt *, ScopStmt *>, isl::map> DefToTargetCache;
159
160
  /// Prepare the object before computing the zones of @p S.
161
  ///
162
  /// @param PassName Name of the pass using this analysis.
163
  /// @param S        The SCoP to process.
164
  /// @param LI       LoopInfo analysis used to determine synthesizable values.
165
  ZoneAlgorithm(const char *PassName, Scop *S, llvm::LoopInfo *LI);
166
167
private:
168
  /// Find the array elements that violate the zone analysis assumptions.
169
  ///
170
  /// What violates our assumptions:
171
  /// - A load after a write of the same location; we assume that all reads
172
  ///   occur before the writes.
173
  /// - Two writes to the same location; we cannot model the order in which
174
  ///   these occur.
175
  ///
176
  /// Scalar reads implicitly always occur before other accesses therefore never
177
  /// violate the first condition. There is also at most one write to a scalar,
178
  /// satisfying the second condition.
179
  ///
180
  /// @param Stmt                  The statement to be analyzed.
181
  /// @param[out] IncompatibleElts Receives the elements that are not
182
  ///                              zone-analysis compatible.
183
  /// @param[out]                  AllElts receives all encountered elements.
184
  void collectIncompatibleElts(ScopStmt *Stmt, isl::union_set &IncompatibleElts,
185
                               isl::union_set &AllElts);
186
187
  void addArrayReadAccess(MemoryAccess *MA);
188
189
  /// Return the ValInst write by a (must-)write access. Returns the 'unknown'
190
  /// ValInst if there is no single ValInst[] the array element written to will
191
  /// have.
192
  ///
193
  /// @return { ValInst[] }
194
  isl::union_map getWrittenValue(MemoryAccess *MA, isl::map AccRel);
195
196
  void addArrayWriteAccess(MemoryAccess *MA);
197
198
  /// For an llvm::Value defined in @p DefStmt, compute the RAW dependency for a
199
  /// use in every instance of @p UseStmt.
200
  ///
201
  /// @param UseStmt Statement a scalar is used in.
202
  /// @param DefStmt Statement a scalar is defined in.
203
  ///
204
  /// @return { DomainUse[] -> DomainDef[] }
205
  isl::map computeUseToDefFlowDependency(ScopStmt *UseStmt, ScopStmt *DefStmt);
206
207
protected:
208
  isl::union_set makeEmptyUnionSet() const;
209
210
  isl::union_map makeEmptyUnionMap() const;
211
212
  /// For each 'execution' of a PHINode, get the incoming block that was
213
  /// executed before.
214
  ///
215
  /// For each PHI instance we can directly determine which was the incoming
216
  /// block, and hence derive which value the PHI has.
217
  ///
218
  /// @param SAI The ScopArrayInfo representing the PHI's storage.
219
  ///
220
  /// @return { DomainPHIRead[] -> DomainPHIWrite[] }
221
  isl::union_map computePerPHI(const polly::ScopArrayInfo *SAI);
222
223
  /// Find the array elements that can be used for zone analysis.
224
  void collectCompatibleElts();
225
226
  /// Get the schedule for @p Stmt.
227
  ///
228
  /// The domain of the result is as narrow as possible.
229
  isl::map getScatterFor(ScopStmt *Stmt) const;
230
231
  /// Get the schedule of @p MA's parent statement.
232
  isl::map getScatterFor(MemoryAccess *MA) const;
233
234
  /// Get the schedule for the statement instances of @p Domain.
235
  isl::union_map getScatterFor(isl::union_set Domain) const;
236
237
  /// Get the schedule for the statement instances of @p Domain.
238
  isl::map getScatterFor(isl::set Domain) const;
239
240
  /// Get the domain of @p Stmt.
241
  isl::set getDomainFor(ScopStmt *Stmt) const;
242
243
  /// Get the domain @p MA's parent statement.
244
  isl::set getDomainFor(MemoryAccess *MA) const;
245
246
  /// Get the access relation of @p MA.
247
  ///
248
  /// The domain of the result is as narrow as possible.
249
  isl::map getAccessRelationFor(MemoryAccess *MA) const;
250
251
  /// Get a domain translation map from a (scalar) definition to the statement
252
  /// where the definition is being moved to.
253
  ///
254
  /// @p TargetStmt can also be seen at an llvm::Use of an llvm::Value in
255
  /// @p DefStmt. In addition, we allow transitive uses:
256
  ///
257
  /// DefStmt -> MiddleStmt -> TargetStmt
258
  ///
259
  /// where an operand tree of instructions in DefStmt and MiddleStmt are to be
260
  /// moved to TargetStmt. To be generally correct, we also need to know all the
261
  /// intermediate statements. However, we make use of the fact that
262
  /// ForwardOpTree currently does not support a move from a loop body across
263
  /// its header such that only the first definition and the target statement
264
  /// are relevant.
265
  ///
266
  /// @param DefStmt    Statement from where a definition might be moved from.
267
  /// @param TargetStmt Statement where the definition is potentially being
268
  ///                   moved to (should contain a use of that definition).
269
  ///
270
  /// @return { DomainDef[] -> DomainTarget[] }
271
  isl::map getDefToTarget(ScopStmt *DefStmt, ScopStmt *TargetStmt);
272
273
  /// Get the reaching definition of a scalar defined in @p Stmt.
274
  ///
275
  /// Note that this does not depend on the llvm::Instruction, only on the
276
  /// statement it is defined in. Therefore the same computation can be reused.
277
  ///
278
  /// @param Stmt The statement in which a scalar is defined.
279
  ///
280
  /// @return { Scatter[] -> DomainDef[] }
281
  isl::map getScalarReachingDefinition(ScopStmt *Stmt);
282
283
  /// Get the reaching definition of a scalar defined in @p DefDomain.
284
  ///
285
  /// @param DomainDef { DomainDef[] }
286
  ///              The write statements to get the reaching definition for.
287
  ///
288
  /// @return { Scatter[] -> DomainDef[] }
289
  isl::map getScalarReachingDefinition(isl::set DomainDef);
290
291
  /// Create a statement-to-unknown value mapping.
292
  ///
293
  /// @param Stmt The statement whose instances are mapped to unknown.
294
  ///
295
  /// @return { Domain[] -> ValInst[] }
296
  isl::map makeUnknownForDomain(ScopStmt *Stmt) const;
297
298
  /// Create an isl_id that represents @p V.
299
  isl::id makeValueId(llvm::Value *V);
300
301
  /// Create the space for an llvm::Value that is available everywhere.
302
  isl::space makeValueSpace(llvm::Value *V);
303
304
  /// Create a set with the llvm::Value @p V which is available everywhere.
305
  isl::set makeValueSet(llvm::Value *V);
306
307
  /// Create a mapping from a statement instance to the instance of an
308
  /// llvm::Value that can be used in there.
309
  ///
310
  /// Although LLVM IR uses single static assignment, llvm::Values can have
311
  /// different contents in loops, when they get redefined in the last
312
  /// iteration. This function tries to get the statement instance of the
313
  /// previous definition, relative to a user.
314
  ///
315
  /// Example:
316
  /// for (int i = 0; i < N; i += 1) {
317
  /// DEF:
318
  ///    int v = A[i];
319
  /// USE:
320
  ///    use(v);
321
  ///  }
322
  ///
323
  /// The value instance used by statement instance USE[i] is DEF[i]. Hence,
324
  /// makeValInst returns:
325
  ///
326
  /// { USE[i] -> [DEF[i] -> v[]] : 0 <= i < N }
327
  ///
328
  /// @param Val       The value to get the instance of.
329
  /// @param UserStmt  The statement that uses @p Val. Can be nullptr.
330
  /// @param Scope     Loop the using instruction resides in.
331
  /// @param IsCertain Pass true if the definition of @p Val is a
332
  ///                  MUST_WRITE or false if the write is conditional.
333
  ///
334
  /// @return { DomainUse[] -> ValInst[] }
335
  isl::map makeValInst(llvm::Value *Val, ScopStmt *UserStmt, llvm::Loop *Scope,
336
                       bool IsCertain = true);
337
338
  /// Create and normalize a ValInst.
339
  ///
340
  /// @see makeValInst
341
  /// @see normalizeValInst
342
  /// @see #NormalizedPHI
343
  isl::union_map makeNormalizedValInst(llvm::Value *Val, ScopStmt *UserStmt,
344
                                       llvm::Loop *Scope,
345
                                       bool IsCertain = true);
346
347
  /// Return whether @p MA can be used for transformations (e.g. OpTree load
348
  /// forwarding, DeLICM mapping).
349
  bool isCompatibleAccess(MemoryAccess *MA);
350
351
  /// Compute the different zones.
352
  void computeCommon();
353
354
  ///  Compute the normalization map that replaces PHIs by their incoming
355
  ///  values.
356
  ///
357
  /// @see #NormalizeMap
358
  void computeNormalizedPHIs();
359
360
  /// Print the current state of all MemoryAccesses to @p.
361
  void printAccesses(llvm::raw_ostream &OS, int Indent = 0) const;
362
363
  /// Is @p MA a PHI READ access that can be normalized?
364
  ///
365
  /// @see #NormalizeMap
366
  bool isNormalizable(MemoryAccess *MA);
367
368
  /// @{
369
  /// Determine whether the argument does not map to any computed PHI. Those
370
  /// should have been replaced by their incoming values.
371
  ///
372
  /// @see #NormalizedPHI
373
  isl::boolean isNormalized(isl::map Map);
374
  isl::boolean isNormalized(isl::union_map Map);
375
  /// @}
376
377
public:
378
  /// Return the SCoP this object is analyzing.
379
0
  Scop *getScop() const { return S; }
380
381
  /// A reaching definition zone is known to have the definition's written value
382
  /// if the definition is a MUST_WRITE.
383
  ///
384
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
385
  isl::union_map computeKnownFromMustWrites() const;
386
387
  /// A reaching definition zone is known to be the same value as any load that
388
  /// reads from that array element in that period.
389
  ///
390
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
391
  isl::union_map computeKnownFromLoad() const;
392
393
  /// Compute which value an array element stores at every instant.
394
  ///
395
  /// @param FromWrite Use stores as source of information.
396
  /// @param FromRead  Use loads as source of information.
397
  ///
398
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
399
  isl::union_map computeKnown(bool FromWrite, bool FromRead) const;
400
};
401
402
/// Create a domain-to-unknown value mapping.
403
///
404
/// Value instances that do not represent a specific value are represented by an
405
/// unnamed tuple of 0 dimensions. Its meaning depends on the context. It can
406
/// either mean a specific but unknown value which cannot be represented by
407
/// other means. It conflicts with itself because those two unknown ValInsts may
408
/// have different concrete values at runtime.
409
///
410
/// The other meaning is an arbitrary or wildcard value that can be chosen
411
/// freely, like LLVM's undef. If matched with an unknown ValInst, there is no
412
/// conflict.
413
///
414
/// @param Domain { Domain[] }
415
///
416
/// @return { Domain[] -> ValInst[] }
417
isl::union_map makeUnknownForDomain(isl::union_set Domain);
418
} // namespace polly
419
420
#endif /* POLLY_ZONEALGO_H */