Coverage Report

Created: 2017-08-21 19:50

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "isl-noexceptions.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include <memory>
20
21
namespace llvm {
22
class Value;
23
class LoopInfo;
24
class Loop;
25
} // namespace llvm
26
27
namespace polly {
28
class Scop;
29
class ScopStmt;
30
class MemoryAccess;
31
32
/// Return only the mappings that map to known values.
33
///
34
/// @param UMap { [] -> ValInst[] }
35
///
36
/// @return { [] -> ValInst[] }
37
isl::union_map filterKnownValInst(const isl::union_map &UMap);
38
39
/// Base class for algorithms based on zones, like DeLICM.
40
class ZoneAlgorithm {
41
protected:
42
  /// The name of the pass this is used from. Used for optimization remarks.
43
  const char *PassName;
44
45
  /// Hold a reference to the isl_ctx to avoid it being freed before we released
46
  /// all of the isl objects.
47
  ///
48
  /// This must be declared before any other member that holds an isl object.
49
  /// This guarantees that the shared_ptr and its isl_ctx is destructed last,
50
  /// after all other members free'd the isl objects they were holding.
51
  std::shared_ptr<isl_ctx> IslCtx;
52
53
  /// Cached reaching definitions for each ScopStmt.
54
  ///
55
  /// Use getScalarReachingDefinition() to get its contents.
56
  llvm::DenseMap<ScopStmt *, isl::map> ScalarReachDefZone;
57
58
  /// The analyzed Scop.
59
  Scop *S;
60
61
  /// LoopInfo analysis used to determine whether values are synthesizable.
62
  llvm::LoopInfo *LI;
63
64
  /// Parameter space that does not need realignment.
65
  isl::space ParamSpace;
66
67
  /// Space the schedule maps to.
68
  isl::space ScatterSpace;
69
70
  /// Cached version of the schedule and domains.
71
  isl::union_map Schedule;
72
73
  /// Combined access relations of all MemoryKind::Array READ accesses.
74
  /// { DomainRead[] -> Element[] }
75
  isl::union_map AllReads;
76
77
  /// The loaded values (llvm::LoadInst) of all reads.
78
  /// { [Element[] -> DomainRead[]] -> ValInst[] }
79
  isl::union_map AllReadValInst;
80
81
  /// Combined access relations of all MemoryKind::Array, MAY_WRITE accesses.
82
  /// { DomainMayWrite[] -> Element[] }
83
  isl::union_map AllMayWrites;
84
85
  /// Combined access relations of all MemoryKind::Array, MUST_WRITE accesses.
86
  /// { DomainMustWrite[] -> Element[] }
87
  isl::union_map AllMustWrites;
88
89
  /// Combined access relations of all MK_Array write accesses (union of
90
  /// AllMayWrites and AllMustWrites).
91
  /// { DomainWrite[] -> Element[] }
92
  isl::union_map AllWrites;
93
94
  /// The value instances written to array elements of all write accesses.
95
  /// { [Element[] -> DomainWrite[]] -> ValInst[] }
96
  isl::union_map AllWriteValInst;
97
98
  /// All reaching definitions for  MemoryKind::Array writes.
99
  /// { [Element[] -> Zone[]] -> DomainWrite[] }
100
  isl::union_map WriteReachDefZone;
101
102
  /// Map llvm::Values to an isl identifier.
103
  /// Used with -polly-use-llvm-names=false as an alternative method to get
104
  /// unique ids that do not depend on pointer values.
105
  llvm::DenseMap<llvm::Value *, isl::id> ValueIds;
106
107
  /// Prepare the object before computing the zones of @p S.
108
  ///
109
  /// @param PassName Name of the pass using this analysis.
110
  /// @param S        The SCoP to process.
111
  /// @param LI       LoopInfo analysis used to determine synthesizable values.
112
  ZoneAlgorithm(const char *PassName, Scop *S, llvm::LoopInfo *LI);
113
114
private:
115
  /// Check whether @p Stmt can be accurately analyzed by zones.
116
  ///
117
  /// What violates our assumptions:
118
  /// - A load after a write of the same location; we assume that all reads
119
  ///   occur before the writes.
120
  /// - Two writes to the same location; we cannot model the order in which
121
  ///   these occur.
122
  ///
123
  /// Scalar reads implicitly always occur before other accesses therefore never
124
  /// violate the first condition. There is also at most one write to a scalar,
125
  /// satisfying the second condition.
126
  bool isCompatibleStmt(ScopStmt *Stmt);
127
128
  void addArrayReadAccess(MemoryAccess *MA);
129
130
  void addArrayWriteAccess(MemoryAccess *MA);
131
132
protected:
133
  isl::union_set makeEmptyUnionSet() const;
134
135
  isl::union_map makeEmptyUnionMap() const;
136
137
  /// Check whether @p S can be accurately analyzed by zones.
138
  bool isCompatibleScop();
139
140
  /// Get the schedule for @p Stmt.
141
  ///
142
  /// The domain of the result is as narrow as possible.
143
  isl::map getScatterFor(ScopStmt *Stmt) const;
144
145
  /// Get the schedule of @p MA's parent statement.
146
  isl::map getScatterFor(MemoryAccess *MA) const;
147
148
  /// Get the schedule for the statement instances of @p Domain.
149
  isl::union_map getScatterFor(isl::union_set Domain) const;
150
151
  /// Get the schedule for the statement instances of @p Domain.
152
  isl::map getScatterFor(isl::set Domain) const;
153
154
  /// Get the domain of @p Stmt.
155
  isl::set getDomainFor(ScopStmt *Stmt) const;
156
157
  /// Get the domain @p MA's parent statement.
158
  isl::set getDomainFor(MemoryAccess *MA) const;
159
160
  /// Get the access relation of @p MA.
161
  ///
162
  /// The domain of the result is as narrow as possible.
163
  isl::map getAccessRelationFor(MemoryAccess *MA) const;
164
165
  /// Get the reaching definition of a scalar defined in @p Stmt.
166
  ///
167
  /// Note that this does not depend on the llvm::Instruction, only on the
168
  /// statement it is defined in. Therefore the same computation can be reused.
169
  ///
170
  /// @param Stmt The statement in which a scalar is defined.
171
  ///
172
  /// @return { Scatter[] -> DomainDef[] }
173
  isl::map getScalarReachingDefinition(ScopStmt *Stmt);
174
175
  /// Get the reaching definition of a scalar defined in @p DefDomain.
176
  ///
177
  /// @param DomainDef { DomainDef[] }
178
  ///              The write statements to get the reaching definition for.
179
  ///
180
  /// @return { Scatter[] -> DomainDef[] }
181
  isl::map getScalarReachingDefinition(isl::set DomainDef);
182
183
  /// Create a statement-to-unknown value mapping.
184
  ///
185
  /// @param Stmt The statement whose instances are mapped to unknown.
186
  ///
187
  /// @return { Domain[] -> ValInst[] }
188
  isl::map makeUnknownForDomain(ScopStmt *Stmt) const;
189
190
  /// Create an isl_id that represents @p V.
191
  isl::id makeValueId(llvm::Value *V);
192
193
  /// Create the space for an llvm::Value that is available everywhere.
194
  isl::space makeValueSpace(llvm::Value *V);
195
196
  /// Create a set with the llvm::Value @p V which is available everywhere.
197
  isl::set makeValueSet(llvm::Value *V);
198
199
  /// Create a mapping from a statement instance to the instance of an
200
  /// llvm::Value that can be used in there.
201
  ///
202
  /// Although LLVM IR uses single static assignment, llvm::Values can have
203
  /// different contents in loops, when they get redefined in the last
204
  /// iteration. This function tries to get the statement instance of the
205
  /// previous definition, relative to a user.
206
  ///
207
  /// Example:
208
  /// for (int i = 0; i < N; i += 1) {
209
  /// DEF:
210
  ///    int v = A[i];
211
  /// USE:
212
  ///    use(v);
213
  ///  }
214
  ///
215
  /// The value instance used by statement instance USE[i] is DEF[i]. Hence,
216
  /// makeValInst returns:
217
  ///
218
  /// { USE[i] -> [DEF[i] -> v[]] : 0 <= i < N }
219
  ///
220
  /// @param Val       The value to get the instance of.
221
  /// @param UserStmt  The statement that uses @p Val. Can be nullptr.
222
  /// @param Scope     Loop the using instruction resides in.
223
  /// @param IsCertain Pass true if the definition of @p Val is a
224
  ///                  MUST_WRITE or false if the write is conditional.
225
  ///
226
  /// @return { DomainUse[] -> ValInst[] }
227
  isl::map makeValInst(llvm::Value *Val, ScopStmt *UserStmt, llvm::Loop *Scope,
228
                       bool IsCertain = true);
229
230
  /// Compute the different zones.
231
  void computeCommon();
232
233
  /// Print the current state of all MemoryAccesses to @p.
234
  void printAccesses(llvm::raw_ostream &OS, int Indent = 0) const;
235
236
public:
237
  /// Return the SCoP this object is analyzing.
238
0
  Scop *getScop() const { return S; }
239
240
  /// A reaching definition zone is known to have the definition's written value
241
  /// if the definition is a MUST_WRITE.
242
  ///
243
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
244
  isl::union_map computeKnownFromMustWrites() const;
245
246
  /// A reaching definition zone is known to be the same value as any load that
247
  /// reads from that array element in that period.
248
  ///
249
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
250
  isl::union_map computeKnownFromLoad() const;
251
252
  /// Compute which value an array element stores at every instant.
253
  ///
254
  /// @param FromWrite Use stores as source of information.
255
  /// @param FromRead  Use loads as source of information.
256
  ///
257
  /// @return { [Element[] -> Zone[]] -> ValInst[] }
258
  isl::union_map computeKnown(bool FromWrite, bool FromRead) const;
259
};
260
261
/// Create a domain-to-unknown value mapping.
262
///
263
/// Value instances that do not represent a specific value are represented by an
264
/// unnamed tuple of 0 dimensions. Its meaning depends on the context. It can
265
/// either mean a specific but unknown value which cannot be represented by
266
/// other means. It conflicts with itself because those two unknown ValInsts may
267
/// have different concrete values at runtime.
268
///
269
/// The other meaning is an arbitrary or wildcard value that can be chosen
270
/// freely, like LLVM's undef. If matched with an unknown ValInst, there is no
271
/// conflict.
272
///
273
/// @param Domain { Domain[] }
274
///
275
/// @return { Domain[] -> ValInst[] }
276
isl::union_map makeUnknownForDomain(isl::union_set Domain);
277
} // namespace polly
278
279
#endif /* POLLY_ZONEALGO_H */