Coverage Report

Created: 2018-12-09 11:54

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/include/polly/Support/VirtualInstruction.h
Line
Count
Source (jump to first uncovered line)
1
//===------ VirtualInstruction.cpp ------------------------------*- 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
// Tools for determining which instructions are within a statement and the
11
// nature of their operands.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef POLLY_SUPPORT_VIRTUALINSTRUCTION_H
16
#define POLLY_SUPPORT_VIRTUALINSTRUCTION_H
17
18
#include "polly/ScopInfo.h"
19
20
namespace polly {
21
22
/// Determine the nature of a value's use within a statement.
23
///
24
/// These are not always representable by llvm::Use. For instance, scalar write
25
/// MemoryAccesses do use a value, but are not associated with an instruction's
26
/// argument.
27
///
28
/// Despite its name it is not tied to virtual instructions (although it works
29
/// fine with them), but to promote consistent handling of values used in
30
/// statements.
31
class VirtualUse {
32
public:
33
  /// The different types of uses. Handling usually differentiates a lot between
34
  /// these; one can use a switch to handle each case (and get warned by the
35
  /// compiler if one is not handled).
36
  enum UseKind {
37
    // An llvm::Constant.
38
    Constant,
39
40
    // An llvm::BasicBlock.
41
    Block,
42
43
    // A value that can be generated using ScopExpander.
44
    Synthesizable,
45
46
    // A load that always reads the same value throughout the SCoP (address and
47
    // the value located there a SCoP-invariant) and has been hoisted in front
48
    // of the SCoP.
49
    Hoisted,
50
51
    // Definition before the SCoP and not synthesizable. Can be an instruction
52
    // outside the SCoP, a function argument or a global value. Whether there is
53
    // a scalar MemoryAccess in this statement for reading it depends on the
54
    // -polly-analyze-read-only-scalars switch.
55
    ReadOnly,
56
57
    // A definition within the same statement. No MemoryAccess between
58
    // definition and use are necessary.
59
    Intra,
60
61
    // Definition in another statement. There is a scalar MemoryAccess that
62
    // makes it available in this statement.
63
    Inter
64
  };
65
66
private:
67
  /// The statement where a value is used.
68
  ScopStmt *User;
69
70
  /// The value that is used.
71
  Value *Val;
72
73
  /// The type of value use.
74
  UseKind Kind;
75
76
  /// The value represented as llvm::SCEV expression.
77
  const SCEV *ScevExpr;
78
79
  /// If this is an inter-statement (or read-only) use, contains the
80
  /// MemoryAccess that makes the value available in this statement. In case of
81
  /// intra-statement uses, can contain a MemoryKind::Array access. In all other
82
  /// cases, it is a nullptr.
83
  MemoryAccess *InputMA;
84
85
  VirtualUse(ScopStmt *User, Value *Val, UseKind Kind, const SCEV *ScevExpr,
86
             MemoryAccess *InputMA)
87
17.6k
      : User(User), Val(Val), Kind(Kind), ScevExpr(ScevExpr), InputMA(InputMA) {
88
17.6k
  }
89
90
public:
91
  /// Get a VirtualUse for an llvm::Use.
92
  ///
93
  /// @param S       The Scop object.
94
  /// @param U       The llvm::Use the get information for.
95
  /// @param LI      The LoopInfo analysis. Needed to determine whether the
96
  ///                value is synthesizable.
97
  /// @param Virtual Whether to ignore existing MemoryAcccess.
98
  ///
99
  /// @return The VirtualUse representing the same use as @p U.
100
  static VirtualUse create(Scop *S, const Use &U, LoopInfo *LI, bool Virtual);
101
102
  /// Get a VirtualUse for uses within statements.
103
  ///
104
  /// It is assumed that the user is not a PHINode. Such uses are always
105
  /// VirtualUse::Inter unless in a regions statement.
106
  ///
107
  /// @param S         The Scop object.
108
  /// @param UserStmt  The statement in which @p Val is used. Can be nullptr, in
109
  ///                  which case it assumed that the statement has been
110
  ///                  removed, which is only possible if no instruction in it
111
  ///                  had side-effects or computes a value used by another
112
  ///                  statement.
113
  /// @param UserScope Loop scope in which the value is used. Needed to
114
  ///                  determine whether the value is synthesizable.
115
  /// @param Val       The value being used.
116
  /// @param Virtual   Whether to use (and prioritize over instruction location)
117
  ///                  information about MemoryAccesses.
118
  ///
119
  /// @return A VirtualUse object that gives information about @p Val's use in
120
  ///         @p UserStmt.
121
  static VirtualUse create(Scop *S, ScopStmt *UserStmt, Loop *UserScope,
122
                           Value *Val, bool Virtual);
123
124
  static VirtualUse create(ScopStmt *UserStmt, Loop *UserScope, Value *Val,
125
2.77k
                           bool Virtual) {
126
2.77k
    return create(UserStmt->getParent(), UserStmt, UserScope, Val, Virtual);
127
2.77k
  }
128
129
0
  bool isConstant() const { return Kind == Constant; }
130
0
  bool isBlock() const { return Kind == Block; }
131
0
  bool isSynthesizable() const { return Kind == Synthesizable; }
132
0
  bool isHoisted() const { return Kind == Hoisted; }
133
0
  bool isReadOnly() const { return Kind == ReadOnly; }
134
0
  bool isIntra() const { return Kind == Intra; }
135
362
  bool isInter() const { return Kind == Inter; }
136
137
  /// Return user statement.
138
180
  ScopStmt *getUser() const { return User; }
139
140
  /// Return the used value.
141
180
  llvm::Value *getValue() const { return Val; }
142
143
  /// Return the type of use.
144
17.6k
  UseKind getKind() const { return Kind; }
145
146
  /// Return the ScalarEvolution representation of @p Val.
147
3
  const SCEV *getScevExpr() const { return ScevExpr; }
148
149
  /// Return the MemoryAccess that makes the value available in this statement,
150
  /// if any.
151
19
  MemoryAccess *getMemoryAccess() const { return InputMA; }
152
153
  /// Print a description of this object.
154
  ///
155
  /// @param OS           Stream to print to.
156
  /// @param Reproducible If true, ensures that the output is stable between
157
  ///                     runs and is suitable to check in regression tests.
158
  ///                     This excludes printing e.g. pointer values. If false,
159
  ///                     the output should not be used for regression tests,
160
  ///                     but may contain more information useful in debugger
161
  ///                     sessions.
162
  void print(raw_ostream &OS, bool Reproducible = true) const;
163
164
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
165
  void dump() const;
166
#endif
167
};
168
169
/// An iterator for virtual operands.
170
class VirtualOperandIterator
171
    : public std::iterator<std::forward_iterator_tag, VirtualUse> {
172
  friend class VirtualInstruction;
173
  friend class VirtualUse;
174
175
  using super = std::iterator<std::forward_iterator_tag, VirtualUse>;
176
  using Self = VirtualOperandIterator;
177
178
  ScopStmt *User;
179
  User::op_iterator U;
180
181
  VirtualOperandIterator(ScopStmt *User, User::op_iterator U)
182
402
      : User(User), U(U) {}
183
184
public:
185
  using pointer = typename super::pointer;
186
  using reference = typename super::reference;
187
188
0
  inline bool operator==(const Self &that) const {
189
0
    assert(this->User == that.User);
190
0
    return this->U == that.U;
191
0
  }
192
193
561
  inline bool operator!=(const Self &that) const {
194
561
    assert(this->User == that.User);
195
561
    return this->U != that.U;
196
561
  }
197
198
360
  VirtualUse operator*() const {
199
360
    return VirtualUse::create(User, User->getSurroundingLoop(), U->get(), true);
200
360
  }
201
202
0
  Use *operator->() const { return U; }
203
204
360
  Self &operator++() {
205
360
    U++;
206
360
    return *this;
207
360
  }
208
209
0
  Self operator++(int) {
210
0
    Self tmp = *this;
211
0
    ++*this;
212
0
    return tmp;
213
0
  }
214
};
215
216
/// This class represents a "virtual instruction", an instruction in a ScopStmt,
217
/// effectively a ScopStmt/Instruction-pair.
218
///
219
/// An instructions can be moved between statements (e.g. to avoid a scalar
220
/// dependency) and even can be contained in multiple statements (for instance,
221
/// to recompute a value instead of transferring it), hence 'virtual'. This
222
/// class is required to represent such instructions that are not in their
223
/// 'physical' location anymore.
224
///
225
/// A statement can currently not contain the same instructions multiple times
226
/// (that is, from different loop iterations). Therefore, a
227
/// ScopStmt/Instruction-pair uniquely identifies a virtual instructions.
228
/// ScopStmt::getInstruction() can contain the same instruction multiple times,
229
/// but they necessarily compute the same value.
230
class VirtualInstruction {
231
  friend class VirtualOperandIterator;
232
  friend struct llvm::DenseMapInfo<VirtualInstruction>;
233
234
private:
235
  /// The statement this virtual instruction is in.
236
  ScopStmt *Stmt = nullptr;
237
238
  /// The instruction of a statement.
239
  Instruction *Inst = nullptr;
240
241
public:
242
1.64k
  VirtualInstruction() {}
243
244
  /// Create a new virtual instruction of an instruction @p Inst in @p Stmt.
245
  VirtualInstruction(ScopStmt *Stmt, Instruction *Inst)
246
530
      : Stmt(Stmt), Inst(Inst) {
247
530
    assert(Stmt && Inst);
248
530
  }
249
250
201
  VirtualOperandIterator operand_begin() const {
251
201
    return VirtualOperandIterator(Stmt, Inst->op_begin());
252
201
  }
253
254
201
  VirtualOperandIterator operand_end() const {
255
201
    return VirtualOperandIterator(Stmt, Inst->op_end());
256
201
  }
257
258
  /// Returns a list of virtual operands.
259
  ///
260
  /// Virtual operands, like virtual instructions, need to encode the ScopStmt
261
  /// they are in.
262
201
  llvm::iterator_range<VirtualOperandIterator> operands() const {
263
201
    return {operand_begin(), operand_end()};
264
201
  }
265
266
  /// Return the SCoP everything is contained in.
267
0
  Scop *getScop() const { return Stmt->getParent(); }
268
269
  /// Return the ScopStmt this virtual instruction is in.
270
9.66k
  ScopStmt *getStmt() const { return Stmt; }
271
272
  /// Return the instruction in the statement.
273
7.73k
  Instruction *getInstruction() const { return Inst; }
274
275
  /// Print a description of this object.
276
  ///
277
  /// @param OS           Stream to print to.
278
  /// @param Reproducible If true, ensures that the output is stable between
279
  ///                     runs and is suitable for checks in regression tests.
280
  ///                     This excludes printing e.g., pointer values. If false,
281
  ///                     the output should not be used for regression tests,
282
  ///                     but may contain more information useful in debugger
283
  ///                     sessions.
284
  void print(raw_ostream &OS, bool Reproducible = true) const;
285
286
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287
  void dump() const;
288
#endif
289
};
290
291
0
static inline bool operator==(VirtualInstruction LHS, VirtualInstruction RHS) {
292
0
  return LHS.getStmt() == RHS.getStmt() &&
293
0
         LHS.getInstruction() == RHS.getInstruction();
294
0
}
Unexecuted instantiation: ScopBuilder.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
Unexecuted instantiation: BlockGenerators.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
Unexecuted instantiation: VirtualInstruction.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
Unexecuted instantiation: ForwardOpTree.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
Unexecuted instantiation: ZoneAlgo.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
Unexecuted instantiation: Simplify.cpp:polly::operator==(polly::VirtualInstruction, polly::VirtualInstruction)
295
296
/// Find all reachable instructions and accesses.
297
///
298
/// @param S              The SCoP to find everything reachable in.
299
/// @param LI             LoopInfo required for analysis.
300
/// @param UsedInsts[out] Receives all reachable instructions.
301
/// @param UsedAccs[out]  Receives all reachable accesses.
302
/// @param OnlyLocal      If non-nullptr, activates local mode: The SCoP is
303
///                       assumed to consist only of this statement and is
304
///                       conservatively correct. Does not require walking the
305
///                       whole SCoP.
306
void markReachable(Scop *S, LoopInfo *LI,
307
                   DenseSet<VirtualInstruction> &UsedInsts,
308
                   DenseSet<MemoryAccess *> &UsedAccs,
309
                   ScopStmt *OnlyLocal = nullptr);
310
} // namespace polly
311
312
namespace llvm {
313
/// Support VirtualInstructions in llvm::DenseMaps.
314
template <> struct DenseMapInfo<polly::VirtualInstruction> {
315
public:
316
  static bool isEqual(polly::VirtualInstruction LHS,
317
4.38k
                      polly::VirtualInstruction RHS) {
318
4.38k
    return DenseMapInfo<polly::ScopStmt *>::isEqual(LHS.getStmt(),
319
4.38k
                                                    RHS.getStmt()) &&
320
4.38k
           DenseMapInfo<Instruction *>::isEqual(LHS.getInstruction(),
321
3.41k
                                                RHS.getInstruction());
322
4.38k
  }
323
324
785
  static polly::VirtualInstruction getTombstoneKey() {
325
785
    polly::VirtualInstruction TombstoneKey;
326
785
    TombstoneKey.Stmt = DenseMapInfo<polly::ScopStmt *>::getTombstoneKey();
327
785
    TombstoneKey.Inst = DenseMapInfo<Instruction *>::getTombstoneKey();
328
785
    return TombstoneKey;
329
785
  }
330
331
861
  static polly::VirtualInstruction getEmptyKey() {
332
861
    polly::VirtualInstruction EmptyKey;
333
861
    EmptyKey.Stmt = DenseMapInfo<polly::ScopStmt *>::getEmptyKey();
334
861
    EmptyKey.Inst = DenseMapInfo<Instruction *>::getEmptyKey();
335
861
    return EmptyKey;
336
861
  }
337
338
571
  static unsigned getHashValue(polly::VirtualInstruction Val) {
339
571
    return DenseMapInfo<std::pair<polly::ScopStmt *, Instruction *>>::
340
571
        getHashValue(std::make_pair(Val.getStmt(), Val.getInstruction()));
341
571
  }
342
};
343
} // namespace llvm
344
345
#endif /* POLLY_SUPPORT_VIRTUALINSTRUCTION_H */