Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
llvmutil.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2020, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file llvmutil.h
35  @brief Header file with declarations for various LLVM utility stuff
36 */
37 
38 #pragma once
39 
40 #include "ispc_version.h"
41 
42 #include <llvm/IR/Constants.h>
43 #include <llvm/IR/DerivedTypes.h>
44 #include <llvm/IR/LLVMContext.h>
45 #include <llvm/IR/Type.h>
46 
47 #define PTYPE(p) (llvm::cast<llvm::PointerType>((p)->getType()->getScalarType())->getElementType())
48 
49 namespace llvm {
50 class PHINode;
51 class InsertElementInst;
52 } // namespace llvm
53 
54 /** This structure holds pointers to a variety of LLVM types; code
55  elsewhere can use them from here, ratherthan needing to make more
56  verbose LLVM API calls.
57  */
58 struct LLVMTypes {
59  static llvm::Type *VoidType;
60  static llvm::PointerType *VoidPointerType;
61  static llvm::Type *PointerIntType;
62  static llvm::Type *BoolType;
63  static llvm::Type *BoolStorageType;
64 
65  static llvm::Type *Int8Type;
66  static llvm::Type *Int16Type;
67  static llvm::Type *Int32Type;
68  static llvm::Type *Int64Type;
69  static llvm::Type *FloatType;
70  static llvm::Type *DoubleType;
71 
72  static llvm::Type *Int8PointerType;
73  static llvm::Type *Int16PointerType;
74  static llvm::Type *Int32PointerType;
75  static llvm::Type *Int64PointerType;
76  static llvm::Type *FloatPointerType;
77  static llvm::Type *DoublePointerType;
78 
79  static llvm::VectorType *MaskType;
80 
81  static llvm::VectorType *BoolVectorType;
82  static llvm::VectorType *BoolVectorStorageType;
83  static llvm::VectorType *Int1VectorType;
84  static llvm::VectorType *Int8VectorType;
85  static llvm::VectorType *Int16VectorType;
86  static llvm::VectorType *Int32VectorType;
87  static llvm::VectorType *Int64VectorType;
88  static llvm::VectorType *FloatVectorType;
89  static llvm::VectorType *DoubleVectorType;
90 
91  static llvm::Type *Int8VectorPointerType;
92  static llvm::Type *Int16VectorPointerType;
93  static llvm::Type *Int32VectorPointerType;
94  static llvm::Type *Int64VectorPointerType;
95  static llvm::Type *FloatVectorPointerType;
96  static llvm::Type *DoubleVectorPointerType;
97 
98  static llvm::VectorType *VoidPointerVectorType;
99 };
100 
101 /** These variables hold the corresponding LLVM constant values as a
102  convenience to code elsewhere in the system.
103  */
104 extern llvm::Constant *LLVMTrue, *LLVMFalse, *LLVMTrueInStorage, *LLVMFalseInStorage;
105 
106 /** This should be called early in initialization to initialize the members
107  of LLVMTypes and the LLVMTrue/LLVMFalse constants. However, it can't
108  be called until the compilation target is known.
109  */
110 class Target;
111 extern void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target);
112 
113 /** Returns an LLVM i8 constant of the given value */
114 extern llvm::ConstantInt *LLVMInt8(int8_t i);
115 /** Returns an LLVM i8 constant of the given value */
116 extern llvm::ConstantInt *LLVMUInt8(uint8_t i);
117 /** Returns an LLVM i16 constant of the given value */
118 extern llvm::ConstantInt *LLVMInt16(int16_t i);
119 /** Returns an LLVM i16 constant of the given value */
120 extern llvm::ConstantInt *LLVMUInt16(uint16_t i);
121 /** Returns an LLVM i32 constant of the given value */
122 extern llvm::ConstantInt *LLVMInt32(int32_t i);
123 /** Returns an LLVM i32 constant of the given value */
124 extern llvm::ConstantInt *LLVMUInt32(uint32_t i);
125 /** Returns an LLVM i64 constant of the given value */
126 extern llvm::ConstantInt *LLVMInt64(int64_t i);
127 /** Returns an LLVM i64 constant of the given value */
128 extern llvm::ConstantInt *LLVMUInt64(uint64_t i);
129 /** Returns an LLVM float constant of the given value */
130 extern llvm::Constant *LLVMFloat(float f);
131 /** Returns an LLVM double constant of the given value */
132 extern llvm::Constant *LLVMDouble(double f);
133 
134 /** Returns an LLVM boolean vector constant of the given value smeared
135  across all elements */
136 extern llvm::Constant *LLVMBoolVector(bool v);
137 
138 /** Returns an LLVM boolean vector constant of the given value smeared
139  across all elements with bool represented as storage type(i8)*/
140 extern llvm::Constant *LLVMBoolVectorInStorage(bool v);
141 
142 /** Returns an LLVM i8 vector constant of the given value smeared
143  across all elements */
144 extern llvm::Constant *LLVMInt8Vector(int8_t i);
145 /** Returns an LLVM i8 vector constant of the given value smeared
146  across all elements */
147 extern llvm::Constant *LLVMUInt8Vector(uint8_t i);
148 
149 /** Returns an LLVM i16 vector constant of the given value smeared
150  across all elements */
151 extern llvm::Constant *LLVMInt16Vector(int16_t i);
152 /** Returns an LLVM i16 vector constant of the given value smeared
153  across all elements */
154 extern llvm::Constant *LLVMUInt16Vector(uint16_t i);
155 
156 /** Returns an LLVM i32 vector constant of the given value smeared
157  across all elements */
158 extern llvm::Constant *LLVMInt32Vector(int32_t i);
159 /** Returns an LLVM i32 vector constant of the given value smeared
160  across all elements */
161 extern llvm::Constant *LLVMUInt32Vector(uint32_t i);
162 
163 /** Returns an LLVM i64 vector constant of the given value smeared
164  across all elements */
165 extern llvm::Constant *LLVMInt64Vector(int64_t i);
166 /** Returns an LLVM i64 vector constant of the given value smeared
167  across all elements */
168 extern llvm::Constant *LLVMUInt64Vector(uint64_t i);
169 
170 /** Returns an LLVM float vector constant of the given value smeared
171  across all elements */
172 extern llvm::Constant *LLVMFloatVector(float f);
173 /** Returns an LLVM double vector constant of the given value smeared
174  across all elements */
175 extern llvm::Constant *LLVMDoubleVector(double f);
176 
177 /** Returns a constant integer or vector (according to the given type) of
178  the given signed integer value. */
179 extern llvm::Constant *LLVMIntAsType(int64_t, llvm::Type *t);
180 
181 /** Returns a constant integer or vector (according to the given type) of
182  the given unsigned integer value. */
183 extern llvm::Constant *LLVMUIntAsType(uint64_t, llvm::Type *t);
184 
185 /** Returns an LLVM boolean vector based on the given array of values.
186  The array should have g->target.vectorWidth elements. */
187 extern llvm::Constant *LLVMBoolVector(const bool *v);
188 
189 /** Returns an LLVM boolean vector based on the given array of values
190  with bool represented as storage type(i8).
191  The array should have g->target.vectorWidth elements. */
192 extern llvm::Constant *LLVMBoolVectorInStorage(const bool *v);
193 
194 /** Returns an LLVM i8 vector based on the given array of values.
195  The array should have g->target.vectorWidth elements. */
196 extern llvm::Constant *LLVMInt8Vector(const int8_t *i);
197 /** Returns an LLVM i8 vector based on the given array of values.
198  The array should have g->target.vectorWidth elements. */
199 extern llvm::Constant *LLVMUInt8Vector(const uint8_t *i);
200 
201 /** Returns an LLVM i16 vector based on the given array of values.
202  The array should have g->target.vectorWidth elements. */
203 extern llvm::Constant *LLVMInt16Vector(const int16_t *i);
204 /** Returns an LLVM i16 vector based on the given array of values.
205  The array should have g->target.vectorWidth elements. */
206 extern llvm::Constant *LLVMUInt16Vector(const uint16_t *i);
207 
208 /** Returns an LLVM i32 vector based on the given array of values.
209  The array should have g->target.vectorWidth elements. */
210 extern llvm::Constant *LLVMInt32Vector(const int32_t *i);
211 /** Returns an LLVM i32 vector based on the given array of values.
212  The array should have g->target.vectorWidth elements. */
213 extern llvm::Constant *LLVMUInt32Vector(const uint32_t *i);
214 
215 /** Returns an LLVM i64 vector based on the given array of values.
216  The array should have g->target.vectorWidth elements. */
217 extern llvm::Constant *LLVMInt64Vector(const int64_t *i);
218 /** Returns an LLVM i64 vector based on the given array of values.
219  The array should have g->target.vectorWidth elements. */
220 extern llvm::Constant *LLVMUInt64Vector(const uint64_t *i);
221 
222 /** Returns an LLVM float vector based on the given array of values.
223  The array should have g->target.vectorWidth elements. */
224 extern llvm::Constant *LLVMFloatVector(const float *f);
225 /** Returns an LLVM double vector based on the given array of values.
226  The array should have g->target.vectorWidth elements. */
227 extern llvm::Constant *LLVMDoubleVector(const double *f);
228 
229 /** LLVM constant value representing an 'all on' SIMD lane mask */
230 extern llvm::Constant *LLVMMaskAllOn;
231 /** LLVM constant value representing an 'all off' SIMD lane mask */
232 extern llvm::Constant *LLVMMaskAllOff;
233 
234 /** Tests to see if all of the elements of the vector in the 'v' parameter
235  are equal. Like lValuesAreEqual(), this is a conservative test and may
236  return false for arrays where the values are actually all equal. */
237 extern bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat = NULL);
238 
239 /** Tests to see if OR is actually an ADD. */
240 extern bool IsOrEquivalentToAdd(llvm::Value *op);
241 
242 /** Given vector of integer-typed values, this function returns true if it
243  can determine that the elements of the vector have a step of 'stride'
244  between their values and false otherwise. This function tries to
245  handle as many possibilities as possible, including things like all
246  elements equal to some non-constant value plus an integer offset, etc.
247  Needless to say (the halting problem and all that), it may return false
248  for some vectors that are in fact linear.
249  */
250 extern bool LLVMVectorIsLinear(llvm::Value *v, int stride);
251 
252 /** Given a vector-typed value v, if the vector is a vector with constant
253  element values, this function extracts those element values into the
254  ret[] array and returns the number of elements (i.e. the vector type's
255  width) in *nElts. It returns true if successful and false if the given
256  vector is not in fact a vector of constants. */
257 extern bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts);
258 
259 /** This function takes chains of InsertElement instructions along the
260  lines of:
261 
262  %v0 = insertelement undef, value_0, i32 index_0
263  %v1 = insertelement %v1, value_1, i32 index_1
264  ...
265  %vn = insertelement %vn-1, value_n-1, i32 index_n-1
266 
267  and initializes the provided elements array such that the i'th
268  llvm::Value * in the array is the element that was inserted into the
269  i'th element of the vector.
270 
271  When the chain of insertelement instruction comes to an end, the only
272  base case that this function handles is the initial value being a
273  constant vector. For anything more complex (e.g. some other arbitrary
274  value, it doesn't try to extract element values into the returned
275  array.
276 
277  This also handles one of two common broadcast patterns:
278  1. %broadcast_init.0 = insertelement <4 x i32> undef, i32 %val, i32 0
279  %broadcast.1 = shufflevector <4 x i32> %smear.0, <4 x i32> undef,
280  <4 x i32> zeroinitializer
281  2. %gep_ptr2int_broadcast_init = insertelement <8 x i64> undef, i64 %gep_ptr2int, i32 0
282  %0 = add <8 x i64> %gep_ptr2int_broadcast_init,
283  <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
284  %gep_offset = shufflevector <8 x i64> %0, <8 x i64> undef, <8 x i32> zeroinitializer
285  Function returns:
286  Compare all elements and return one of them if all are equal, otherwise NULL.
287  If searchFirstUndef argument is true, look for the vector with the first not-undef element, like:
288  <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
289  If compare argument is false, don't do compare and return first element instead.
290  If undef argument is true, ignore undef elements (but all undef yields NULL anyway).
291 
292  */
293 extern llvm::Value *LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare = true, bool undef = true,
294  bool searchFirstUndef = false);
295 
296 /** This is a utility routine for debugging that dumps out the given LLVM
297  value as well as (recursively) all of the other values that it depends
298  on. */
299 #ifndef ISPC_NO_DUMPS
300 extern void LLVMDumpValue(llvm::Value *v);
301 #endif
302 
303 /** Given a vector-typed value, this function returns the value of its
304  first element. Rather than just doing the straightforward thing of
305  using a single extractelement instruction to do this, this function
306  tries to rewrite the computation for the first element in scalar form;
307  this is generally more efficient than computing the entire vector's
308  worth of values just to extract the first element, in cases where only
309  the first element's value is needed.
310  */
311 extern llvm::Value *LLVMExtractFirstVectorElement(llvm::Value *v);
312 
313 /** This function takes two vectors, expected to be the same length, and
314  returns a new vector of twice the length that represents concatenating
315  the two of them. */
316 extern llvm::Value *LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore);
317 
318 /** This is a utility function for vector shuffling; it takes two vectors
319  v1 and v2, and a compile-time constant set of integer permutations in
320  shuf[] and returns a new vector of length shufSize that represents the
321  corresponding shufflevector operation. */
322 extern llvm::Value *LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize,
323  llvm::Instruction *insertBefore);
324 
325 /** Utility routines to concat strings with the names of existing values to
326  create meaningful new names for instruction values.
327 */
328 extern const char *LLVMGetName(llvm::Value *v, const char *);
329 extern const char *LLVMGetName(const char *op, llvm::Value *v1, llvm::Value *v2);
static llvm::Type * FloatType
Definition: llvmutil.h:69
llvm::Constant * LLVMMaskAllOn
Definition: llvmutil.cpp:94
static llvm::Type * Int32VectorPointerType
Definition: llvmutil.h:93
llvm::Value * LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1533
Definition: ispc.h:75
llvm::Constant * LLVMUInt16Vector(uint16_t i)
Definition: llvmutil.cpp:298
llvm::Constant * LLVMInt64Vector(int64_t i)
Definition: llvmutil.cpp:373
llvm::Constant * LLVMUInt32Vector(uint32_t i)
Definition: llvmutil.cpp:328
static llvm::Type * DoubleType
Definition: llvmutil.h:70
llvm::Constant * LLVMFalseInStorage
Definition: llvmutil.cpp:93
Structure that defines a compilation target.
Definition: ispc.h:149
llvm::Constant * LLVMFloat(float f)
Definition: llvmutil.cpp:249
static llvm::VectorType * VoidPointerVectorType
Definition: llvmutil.h:98
static llvm::VectorType * BoolVectorType
Definition: llvmutil.h:81
llvm::ConstantInt * LLVMUInt64(uint64_t i)
Definition: llvmutil.cpp:245
defines the ISPC version
llvm::Constant * LLVMFloatVector(float f)
Definition: llvmutil.cpp:343
static llvm::Type * BoolType
Definition: llvmutil.h:62
bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts)
Definition: llvmutil.cpp:709
llvm::Constant * LLVMInt32Vector(int32_t i)
Definition: llvmutil.cpp:313
static llvm::VectorType * Int32VectorType
Definition: llvmutil.h:86
static llvm::VectorType * BoolVectorStorageType
Definition: llvmutil.h:82
static llvm::Type * FloatVectorPointerType
Definition: llvmutil.h:95
llvm::ConstantInt * LLVMInt8(int8_t i)
Definition: llvmutil.cpp:217
static llvm::Type * Int8PointerType
Definition: llvmutil.h:72
static llvm::Type * Int32PointerType
Definition: llvmutil.h:74
static llvm::Type * Int16VectorPointerType
Definition: llvmutil.h:92
void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target)
Definition: llvmutil.cpp:97
llvm::Constant * LLVMUInt64Vector(uint64_t i)
Definition: llvmutil.cpp:388
static llvm::Type * Int16Type
Definition: llvmutil.h:66
static llvm::Type * DoubleVectorPointerType
Definition: llvmutil.h:96
llvm::Constant * LLVMFalse
Definition: llvmutil.cpp:91
llvm::Constant * LLVMMaskAllOff
Definition: llvmutil.cpp:95
static llvm::VectorType * Int1VectorType
Definition: llvmutil.h:83
llvm::Constant * LLVMInt8Vector(int8_t i)
Definition: llvmutil.cpp:253
static llvm::Type * VoidType
Definition: llvmutil.h:59
llvm::ConstantInt * LLVMInt32(int32_t i)
Definition: llvmutil.cpp:233
static llvm::Type * Int8VectorPointerType
Definition: llvmutil.h:91
static llvm::VectorType * Int8VectorType
Definition: llvmutil.h:84
llvm::Constant * LLVMUInt8Vector(uint8_t i)
Definition: llvmutil.cpp:268
llvm::ConstantInt * LLVMUInt8(uint8_t i)
Definition: llvmutil.cpp:221
bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat=NULL)
Definition: llvmutil.cpp:1080
llvm::ConstantInt * LLVMInt16(int16_t i)
Definition: llvmutil.cpp:225
llvm::Constant * LLVMTrue
Definition: llvmutil.cpp:90
llvm::ConstantInt * LLVMUInt16(uint16_t i)
Definition: llvmutil.cpp:229
llvm::Constant * LLVMUIntAsType(uint64_t, llvm::Type *t)
Definition: llvmutil.cpp:476
static llvm::VectorType * FloatVectorType
Definition: llvmutil.h:88
static llvm::Type * Int64Type
Definition: llvmutil.h:68
static llvm::Type * Int8Type
Definition: llvmutil.h:65
llvm::Constant * LLVMBoolVector(bool v)
Definition: llvmutil.cpp:403
static llvm::VectorType * Int64VectorType
Definition: llvmutil.h:87
llvm::Constant * LLVMDouble(double f)
Definition: llvmutil.cpp:251
static llvm::Type * Int64PointerType
Definition: llvmutil.h:75
llvm::ConstantInt * LLVMUInt32(uint32_t i)
Definition: llvmutil.cpp:237
llvm::Value * LLVMExtractFirstVectorElement(llvm::Value *v)
Definition: llvmutil.cpp:1503
static llvm::Type * FloatPointerType
Definition: llvmutil.h:76
static llvm::Type * Int16PointerType
Definition: llvmutil.h:73
llvm::Constant * LLVMBoolVectorInStorage(bool v)
Definition: llvmutil.cpp:446
const char * LLVMGetName(llvm::Value *v, const char *)
Definition: llvmutil.cpp:1549
void LLVMDumpValue(llvm::Value *v)
Definition: llvmutil.cpp:1397
static llvm::Type * PointerIntType
Definition: llvmutil.h:61
static llvm::PointerType * VoidPointerType
Definition: llvmutil.h:60
static llvm::Type * Int64VectorPointerType
Definition: llvmutil.h:94
llvm::Constant * LLVMTrueInStorage
Definition: llvmutil.cpp:92
static llvm::Type * Int32Type
Definition: llvmutil.h:67
static llvm::Type * DoublePointerType
Definition: llvmutil.h:77
static llvm::Type * BoolStorageType
Definition: llvmutil.h:63
llvm::Constant * LLVMInt16Vector(int16_t i)
Definition: llvmutil.cpp:283
llvm::Constant * LLVMDoubleVector(double f)
Definition: llvmutil.cpp:358
llvm::Value * LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare=true, bool undef=true, bool searchFirstUndef=false)
Definition: llvmutil.cpp:587
static llvm::VectorType * MaskType
Definition: llvmutil.h:79
static llvm::VectorType * DoubleVectorType
Definition: llvmutil.h:89
llvm::Constant * LLVMIntAsType(int64_t, llvm::Type *t)
Definition: llvmutil.cpp:463
bool IsOrEquivalentToAdd(llvm::Value *op)
Definition: llvmutil.cpp:1098
static llvm::VectorType * Int16VectorType
Definition: llvmutil.h:85
llvm::Value * LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1514
bool LLVMVectorIsLinear(llvm::Value *v, int stride)
Definition: llvmutil.cpp:1361
llvm::ConstantInt * LLVMInt64(int64_t i)
Definition: llvmutil.cpp:241