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