Coverage Report

Created: 2017-08-21 19:50

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/include/polly/CodeGen/IslExprBuilder.h
Line
Count
Source (jump to first uncovered line)
1
//===-IslExprBuilder.h - Helper to generate code for isl AST expressions --===//
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
//===----------------------------------------------------------------------===//
11
12
#ifndef POLLY_ISL_EXPR_BUILDER_H
13
#define POLLY_ISL_EXPR_BUILDER_H
14
15
#include "polly/CodeGen/IRBuilder.h"
16
#include "polly/Support/ScopHelper.h"
17
18
#include "llvm/ADT/MapVector.h"
19
#include "isl/ast.h"
20
21
namespace llvm {
22
class DataLayout;
23
class ScalarEvolution;
24
} // namespace llvm
25
26
struct isl_id;
27
28
namespace llvm {
29
// Provide PointerLikeTypeTraits for isl_id.
30
template <> struct PointerLikeTypeTraits<isl_id *> {
31
32
public:
33
0
  static inline const void *getAsVoidPointer(isl_id *P) { return (void *)P; }
34
0
  static inline const Region *getFromVoidPointer(void *P) {
35
0
    return (Region *)P;
36
0
  }
37
  enum { NumLowBitsAvailable = 0 };
38
};
39
} // namespace llvm
40
41
namespace polly {
42
class ScopArrayInfo;
43
44
/// LLVM-IR generator for isl_ast_expr[essions]
45
///
46
/// This generator generates LLVM-IR that performs the computation described by
47
/// an isl_ast_expr[ession].
48
///
49
/// Example:
50
///
51
///   An isl_ast_expr[ession] can look like this:
52
///
53
///     (N + M) + 10
54
///
55
///   The IslExprBuilder could create the following LLVM-IR:
56
///
57
///     %tmp1 = add nsw i64 %N
58
///     %tmp2 = add nsw i64 %tmp1, %M
59
///     %tmp3 = add nsw i64 %tmp2, 10
60
///
61
/// The implementation of this class is mostly a mapping from isl_ast_expr
62
/// constructs to the corresponding LLVM-IR constructs.
63
///
64
/// The following decisions may need some explanation:
65
///
66
/// 1) Which data-type to choose
67
///
68
/// isl_ast_expr[essions] are untyped expressions that assume arbitrary
69
/// precision integer computations. LLVM-IR instead has fixed size integers.
70
/// When lowering to LLVM-IR we need to chose both the size of the data type and
71
/// the sign of the operations we use.
72
///
73
/// At the moment, we hardcode i64 bit signed computations. Our experience has
74
/// shown that 64 bit are generally large enough for the loop bounds that appear
75
/// in the wild. Signed computations are needed, as loop bounds may become
76
/// negative.
77
///
78
/// It is possible to track overflows that occurred in the generated IR. See the
79
/// description of @see OverflowState for more information.
80
///
81
/// FIXME: Hardcoding sizes can cause issues:
82
///
83
///   -  On embedded systems and especially for high-level-synthesis 64 bit
84
///      computations are very costly.
85
///
86
///   The right approach is to compute the minimal necessary bitwidth and
87
///   signedness for each subexpression during in the isl AST generation and
88
///   to use this information in our IslAstGenerator. Preliminary patches are
89
///   available, but have not been committed yet.
90
///
91
class IslExprBuilder {
92
public:
93
  /// A map from isl_ids to llvm::Values.
94
  typedef llvm::MapVector<isl_id *, llvm::AssertingVH<llvm::Value>> IDToValueTy;
95
96
  typedef llvm::MapVector<isl_id *, const ScopArrayInfo *> IDToScopArrayInfoTy;
97
98
  /// A map from isl_ids to ScopArrayInfo objects.
99
  ///
100
  /// This map is used to obtain ScopArrayInfo objects for isl_ids which do not
101
  /// carry a ScopArrayInfo object in their user pointer. This is useful if the
102
  /// construction of ScopArrayInfo objects happens only after references (e.g.
103
  /// in an AST) to an isl_id are generated and the user pointer of the isl_id
104
  /// can not be changed any more.
105
  ///
106
  /// This is useful for external users who just use the IslExprBuilder for
107
  /// code generation.
108
  IDToScopArrayInfoTy *IDToSAI = nullptr;
109
110
  /// Set the isl_id to ScopArrayInfo map.
111
  ///
112
  /// @param NewIDToSAI The new isl_id to ScopArrayInfo map to use.
113
0
  void setIDToSAI(IDToScopArrayInfoTy *NewIDToSAI) { IDToSAI = NewIDToSAI; }
114
115
  /// Construct an IslExprBuilder.
116
  ///
117
  /// @param Builder     The IRBuilder used to construct the
118
  ///                    isl_ast_expr[ession]. The insert location of this
119
  ///                    IRBuilder defines WHERE the  corresponding LLVM-IR
120
  ///                    is generated.
121
  /// @param IDToValue   The isl_ast_expr[ession] may reference parameters or
122
  ///                    variables (identified by an isl_id). The IDTOValue map
123
  ///                    specifies the LLVM-IR Values that correspond to these
124
  ///                    parameters and variables.
125
  /// @param GlobalMap   A mapping from llvm::Values used in the original scop
126
  ///                    region to a new set of llvm::Values.
127
  /// @param DL          DataLayout for the current Module.
128
  /// @param SE          ScalarEvolution analysis for the current function.
129
  /// @param DT          DominatorTree analysis for the current function.
130
  /// @param LI          LoopInfo analysis for the current function.
131
  /// @param StartBlock The first basic block after the RTC.
132
  IslExprBuilder(Scop &S, PollyIRBuilder &Builder, IDToValueTy &IDToValue,
133
                 ValueMapT &GlobalMap, const llvm::DataLayout &DL,
134
                 llvm::ScalarEvolution &SE, llvm::DominatorTree &DT,
135
                 llvm::LoopInfo &LI, llvm::BasicBlock *StartBlock);
136
137
  /// Create LLVM-IR for an isl_ast_expr[ession].
138
  ///
139
  /// @param Expr The ast expression for which we generate LLVM-IR.
140
  ///
141
  /// @return The llvm::Value* containing the result of the computation.
142
  llvm::Value *create(__isl_take isl_ast_expr *Expr);
143
144
  /// Return the largest of two types.
145
  ///
146
  /// @param T1 The first type.
147
  /// @param T2 The second type.
148
  ///
149
  /// @return The largest of the two types.
150
  llvm::Type *getWidestType(llvm::Type *T1, llvm::Type *T2);
151
152
  /// Return the type with which this expression should be computed.
153
  ///
154
  /// The type needs to be large enough to hold all possible input and all
155
  /// possible output values.
156
  ///
157
  /// @param Expr The expression for which to find the type.
158
  /// @return The type with which the expression should be computed.
159
  llvm::IntegerType *getType(__isl_keep isl_ast_expr *Expr);
160
161
  /// Change if runtime overflows are tracked or not.
162
  ///
163
  /// @param Enable Flag to enable/disable the tracking.
164
  ///
165
  /// Note that this will reset the tracking state and that tracking is only
166
  /// allowed if the last tracked expression dominates the current insert point.
167
  void setTrackOverflow(bool Enable);
168
169
  /// Return the current overflow status or nullptr if it is not tracked.
170
  ///
171
  /// @return A nullptr if tracking is disabled or otherwise an i1 that has the
172
  ///         value of "0" if and only if no overflow happened since tracking
173
  ///         was enabled.
174
  llvm::Value *getOverflowState() const;
175
176
  /// Create LLVM-IR that computes the memory location of an access expression.
177
  ///
178
  /// For a given isl_ast_expr[ession] of type isl_ast_op_access this function
179
  /// creates IR that computes the address the access expression refers to.
180
  ///
181
  /// @param Expr The ast expression of type isl_ast_op_access
182
  ///             for which we generate LLVM-IR.
183
  ///
184
  /// @return The llvm::Value* containing the result of the computation.
185
  llvm::Value *createAccessAddress(__isl_take isl_ast_expr *Expr);
186
187
private:
188
  Scop &S;
189
190
  /// Flag that will be set if an overflow occurred at runtime.
191
  ///
192
  /// Note that this flag is by default a nullptr and if it is a nullptr
193
  /// we will not record overflows but simply perform the computations.
194
  /// The intended usage is as follows:
195
  ///   - If overflows in [an] expression[s] should be tracked, call
196
  ///     the setTrackOverflow(true) function.
197
  ///   - Use create(...) for all expressions that should be checked.
198
  ///   - Call getOverflowState() to get the value representing the current
199
  ///     state of the overflow flag.
200
  ///   - To stop tracking call setTrackOverflow(false).
201
  llvm::Value *OverflowState;
202
203
  PollyIRBuilder &Builder;
204
  IDToValueTy &IDToValue;
205
  ValueMapT &GlobalMap;
206
207
  const llvm::DataLayout &DL;
208
  llvm::ScalarEvolution &SE;
209
  llvm::DominatorTree &DT;
210
  llvm::LoopInfo &LI;
211
  llvm::BasicBlock *StartBlock;
212
213
  llvm::Value *createOp(__isl_take isl_ast_expr *Expr);
214
  llvm::Value *createOpUnary(__isl_take isl_ast_expr *Expr);
215
  llvm::Value *createOpAccess(__isl_take isl_ast_expr *Expr);
216
  llvm::Value *createOpBin(__isl_take isl_ast_expr *Expr);
217
  llvm::Value *createOpNAry(__isl_take isl_ast_expr *Expr);
218
  llvm::Value *createOpSelect(__isl_take isl_ast_expr *Expr);
219
  llvm::Value *createOpICmp(__isl_take isl_ast_expr *Expr);
220
  llvm::Value *createOpBoolean(__isl_take isl_ast_expr *Expr);
221
  llvm::Value *createOpBooleanConditional(__isl_take isl_ast_expr *Expr);
222
  llvm::Value *createId(__isl_take isl_ast_expr *Expr);
223
  llvm::Value *createInt(__isl_take isl_ast_expr *Expr);
224
  llvm::Value *createOpAddressOf(__isl_take isl_ast_expr *Expr);
225
226
  /// Create a binary operation @p Opc and track overflows if requested.
227
  ///
228
  /// @param OpC  The binary operation that should be performed [Add/Sub/Mul].
229
  /// @param LHS  The left operand.
230
  /// @param RHS  The right operand.
231
  /// @param Name The (base) name of the new IR operations.
232
  ///
233
  /// @return A value that represents the result of the binary operation.
234
  llvm::Value *createBinOp(llvm::BinaryOperator::BinaryOps Opc,
235
                           llvm::Value *LHS, llvm::Value *RHS,
236
                           const llvm::Twine &Name);
237
238
  /// Create an addition and track overflows if requested.
239
  ///
240
  /// @param LHS  The left operand.
241
  /// @param RHS  The right operand.
242
  /// @param Name The (base) name of the new IR operations.
243
  ///
244
  /// @return A value that represents the result of the addition.
245
  llvm::Value *createAdd(llvm::Value *LHS, llvm::Value *RHS,
246
                         const llvm::Twine &Name = "");
247
248
  /// Create a subtraction and track overflows if requested.
249
  ///
250
  /// @param LHS  The left operand.
251
  /// @param RHS  The right operand.
252
  /// @param Name The (base) name of the new IR operations.
253
  ///
254
  /// @return A value that represents the result of the subtraction.
255
  llvm::Value *createSub(llvm::Value *LHS, llvm::Value *RHS,
256
                         const llvm::Twine &Name = "");
257
258
  /// Create a multiplication and track overflows if requested.
259
  ///
260
  /// @param LHS  The left operand.
261
  /// @param RHS  The right operand.
262
  /// @param Name The (base) name of the new IR operations.
263
  ///
264
  /// @return A value that represents the result of the multiplication.
265
  llvm::Value *createMul(llvm::Value *LHS, llvm::Value *RHS,
266
                         const llvm::Twine &Name = "");
267
};
268
} // namespace polly
269
270
#endif