Intel SPMD Program Compiler  1.12.0
llvmutil.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2019, 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 #ifndef ISPC_LLVMUTIL_H
39 #define ISPC_LLVMUTIL_H 1
40 
41 #include "ispc_version.h"
42 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
43 #include <llvm/Constants.h>
44 #include <llvm/DerivedTypes.h>
45 #include <llvm/LLVMContext.h>
46 #include <llvm/Type.h>
47 #else // 3.3+
48 #include <llvm/IR/Constants.h>
49 #include <llvm/IR/DerivedTypes.h>
50 #include <llvm/IR/LLVMContext.h>
51 #include <llvm/IR/Type.h>
52 #endif
53 
54 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9
55 #define PTYPE(p) (llvm::cast<llvm::SequentialType>((p)->getType()->getScalarType())->getElementType())
56 #else // LLVM 4.0+
57 #define PTYPE(p) (llvm::cast<llvm::PointerType>((p)->getType()->getScalarType())->getElementType())
58 #endif
59 
60 namespace llvm {
61 class PHINode;
62 class InsertElementInst;
63 } // namespace llvm
64 
65 /** This structure holds pointers to a variety of LLVM types; code
66  elsewhere can use them from here, ratherthan needing to make more
67  verbose LLVM API calls.
68  */
69 struct LLVMTypes {
70  static llvm::Type *VoidType;
71  static llvm::PointerType *VoidPointerType;
72  static llvm::Type *PointerIntType;
73  static llvm::Type *BoolType;
74 
75  static llvm::Type *Int8Type;
76  static llvm::Type *Int16Type;
77  static llvm::Type *Int32Type;
78  static llvm::Type *Int64Type;
79  static llvm::Type *FloatType;
80  static llvm::Type *DoubleType;
81 
82  static llvm::Type *Int8PointerType;
83  static llvm::Type *Int16PointerType;
84  static llvm::Type *Int32PointerType;
85  static llvm::Type *Int64PointerType;
86  static llvm::Type *FloatPointerType;
87  static llvm::Type *DoublePointerType;
88 
89  static llvm::VectorType *MaskType;
90 
91  static llvm::VectorType *BoolVectorType;
92  static llvm::VectorType *Int1VectorType;
93  static llvm::VectorType *Int8VectorType;
94  static llvm::VectorType *Int16VectorType;
95  static llvm::VectorType *Int32VectorType;
96  static llvm::VectorType *Int64VectorType;
97  static llvm::VectorType *FloatVectorType;
98  static llvm::VectorType *DoubleVectorType;
99 
100  static llvm::Type *Int8VectorPointerType;
101  static llvm::Type *Int16VectorPointerType;
102  static llvm::Type *Int32VectorPointerType;
103  static llvm::Type *Int64VectorPointerType;
104  static llvm::Type *FloatVectorPointerType;
105  static llvm::Type *DoubleVectorPointerType;
106 
107  static llvm::VectorType *VoidPointerVectorType;
108 };
109 
110 /** These variables hold the corresponding LLVM constant values as a
111  convenience to code elsewhere in the system.
112  */
113 extern llvm::Constant *LLVMTrue, *LLVMFalse;
114 
115 /** This should be called early in initialization to initialize the members
116  of LLVMTypes and the LLVMTrue/LLVMFalse constants. However, it can't
117  be called until the compilation target is known.
118  */
119 class Target;
120 extern void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target);
121 
122 /** Returns an LLVM i8 constant of the given value */
123 extern llvm::ConstantInt *LLVMInt8(int8_t i);
124 /** Returns an LLVM i8 constant of the given value */
125 extern llvm::ConstantInt *LLVMUInt8(uint8_t i);
126 /** Returns an LLVM i16 constant of the given value */
127 extern llvm::ConstantInt *LLVMInt16(int16_t i);
128 /** Returns an LLVM i16 constant of the given value */
129 extern llvm::ConstantInt *LLVMUInt16(uint16_t i);
130 /** Returns an LLVM i32 constant of the given value */
131 extern llvm::ConstantInt *LLVMInt32(int32_t i);
132 /** Returns an LLVM i32 constant of the given value */
133 extern llvm::ConstantInt *LLVMUInt32(uint32_t i);
134 /** Returns an LLVM i64 constant of the given value */
135 extern llvm::ConstantInt *LLVMInt64(int64_t i);
136 /** Returns an LLVM i64 constant of the given value */
137 extern llvm::ConstantInt *LLVMUInt64(uint64_t i);
138 /** Returns an LLVM float constant of the given value */
139 extern llvm::Constant *LLVMFloat(float f);
140 /** Returns an LLVM double constant of the given value */
141 extern llvm::Constant *LLVMDouble(double f);
142 
143 /** Returns an LLVM boolean vector constant of the given value smeared
144  across all elements */
145 extern llvm::Constant *LLVMBoolVector(bool v);
146 
147 /** Returns an LLVM i8 vector constant of the given value smeared
148  across all elements */
149 extern llvm::Constant *LLVMInt8Vector(int8_t i);
150 /** Returns an LLVM i8 vector constant of the given value smeared
151  across all elements */
152 extern llvm::Constant *LLVMUInt8Vector(uint8_t i);
153 
154 /** Returns an LLVM i16 vector constant of the given value smeared
155  across all elements */
156 extern llvm::Constant *LLVMInt16Vector(int16_t i);
157 /** Returns an LLVM i16 vector constant of the given value smeared
158  across all elements */
159 extern llvm::Constant *LLVMUInt16Vector(uint16_t i);
160 
161 /** Returns an LLVM i32 vector constant of the given value smeared
162  across all elements */
163 extern llvm::Constant *LLVMInt32Vector(int32_t i);
164 /** Returns an LLVM i32 vector constant of the given value smeared
165  across all elements */
166 extern llvm::Constant *LLVMUInt32Vector(uint32_t i);
167 
168 /** Returns an LLVM i64 vector constant of the given value smeared
169  across all elements */
170 extern llvm::Constant *LLVMInt64Vector(int64_t i);
171 /** Returns an LLVM i64 vector constant of the given value smeared
172  across all elements */
173 extern llvm::Constant *LLVMUInt64Vector(uint64_t i);
174 
175 /** Returns an LLVM float vector constant of the given value smeared
176  across all elements */
177 extern llvm::Constant *LLVMFloatVector(float f);
178 /** Returns an LLVM double vector constant of the given value smeared
179  across all elements */
180 extern llvm::Constant *LLVMDoubleVector(double f);
181 
182 /** Returns a constant integer or vector (according to the given type) of
183  the given signed integer value. */
184 extern llvm::Constant *LLVMIntAsType(int64_t, llvm::Type *t);
185 
186 /** Returns a constant integer or vector (according to the given type) of
187  the given unsigned integer value. */
188 extern llvm::Constant *LLVMUIntAsType(uint64_t, llvm::Type *t);
189 
190 /** Returns an LLVM boolean vector based on the given array of values.
191  The array should have g->target.vectorWidth elements. */
192 extern llvm::Constant *LLVMBoolVector(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 /** Given vector of integer-typed values, this function returns true if it
240  can determine that the elements of the vector have a step of 'stride'
241  between their values and false otherwise. This function tries to
242  handle as many possibilities as possible, including things like all
243  elements equal to some non-constant value plus an integer offset, etc.
244  Needless to say (the halting problem and all that), it may return false
245  for some vectors that are in fact linear.
246  */
247 extern bool LLVMVectorIsLinear(llvm::Value *v, int stride);
248 
249 /** Given a vector-typed value v, if the vector is a vector with constant
250  element values, this function extracts those element values into the
251  ret[] array and returns the number of elements (i.e. the vector type's
252  width) in *nElts. It returns true if successful and false if the given
253  vector is not in fact a vector of constants. */
254 extern bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts);
255 
256 /** This function takes chains of InsertElement instructions along the
257  lines of:
258 
259  %v0 = insertelement undef, value_0, i32 index_0
260  %v1 = insertelement %v1, value_1, i32 index_1
261  ...
262  %vn = insertelement %vn-1, value_n-1, i32 index_n-1
263 
264  and initializes the provided elements array such that the i'th
265  llvm::Value * in the array is the element that was inserted into the
266  i'th element of the vector.
267 
268  When the chain of insertelement instruction comes to an end, the only
269  base case that this function handles is the initial value being a
270  constant vector. For anything more complex (e.g. some other arbitrary
271  value, it doesn't try to extract element values into the returned
272  array.
273 
274  This also handles one of two common broadcast patterns:
275  1. %broadcast_init.0 = insertelement <4 x i32> undef, i32 %val, i32 0
276  %broadcast.1 = shufflevector <4 x i32> %smear.0, <4 x i32> undef,
277  <4 x i32> zeroinitializer
278  2. %gep_ptr2int_broadcast_init = insertelement <8 x i64> undef, i64 %gep_ptr2int, i32 0
279  %0 = add <8 x i64> %gep_ptr2int_broadcast_init,
280  <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
281  %gep_offset = shufflevector <8 x i64> %0, <8 x i64> undef, <8 x i32> zeroinitializer
282  Function returns:
283  Compare all elements and return one of them if all are equal, otherwise NULL.
284  If searchFirstUndef argument is true, look for the vector with the first not-undef element, like:
285  <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
286  If compare argument is false, don't do compare and return first element instead.
287  If undef argument is true, ignore undef elements (but all undef yields NULL anyway).
288 
289  */
290 extern llvm::Value *LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare = true, bool undef = true,
291  bool searchFirstUndef = false);
292 
293 /** This is a utility routine for debugging that dumps out the given LLVM
294  value as well as (recursively) all of the other values that it depends
295  on. */
296 #ifndef ISPC_NO_DUMPS
297 extern void LLVMDumpValue(llvm::Value *v);
298 #endif
299 
300 /** Given a vector-typed value, this function returns the value of its
301  first element. Rather than just doing the straightforward thing of
302  using a single extractelement instruction to do this, this function
303  tries to rewrite the computation for the first element in scalar form;
304  this is generally more efficient than computing the entire vector's
305  worth of values just to extract the first element, in cases where only
306  the first element's value is needed.
307  */
308 extern llvm::Value *LLVMExtractFirstVectorElement(llvm::Value *v);
309 
310 /** This function takes two vectors, expected to be the same length, and
311  returns a new vector of twice the length that represents concatenating
312  the two of them. */
313 extern llvm::Value *LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore);
314 
315 /** This is a utility function for vector shuffling; it takes two vectors
316  v1 and v2, and a compile-time constant set of integer permutations in
317  shuf[] and returns a new vector of length shufSize that represents the
318  corresponding shufflevector operation. */
319 extern llvm::Value *LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize,
320  llvm::Instruction *insertBefore);
321 
322 /** Utility routines to concat strings with the names of existing values to
323  create meaningful new names for instruction values.
324 */
325 extern const char *LLVMGetName(llvm::Value *v, const char *);
326 extern const char *LLVMGetName(const char *op, llvm::Value *v1, llvm::Value *v2);
327 
328 #endif // ISPC_LLVMUTIL_H
static llvm::Type * FloatType
Definition: llvmutil.h:79
llvm::Constant * LLVMMaskAllOn
Definition: llvmutil.cpp:92
static llvm::Type * Int32VectorPointerType
Definition: llvmutil.h:102
llvm::Value * LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1502
Definition: ispc.h:72
llvm::Constant * LLVMUInt16Vector(uint16_t i)
Definition: llvmutil.cpp:293
llvm::Constant * LLVMInt64Vector(int64_t i)
Definition: llvmutil.cpp:368
llvm::Constant * LLVMUInt32Vector(uint32_t i)
Definition: llvmutil.cpp:323
static llvm::Type * DoubleType
Definition: llvmutil.h:80
Structure that defines a compilation target.
Definition: ispc.h:171
llvm::Constant * LLVMFloat(float f)
Definition: llvmutil.cpp:244
static llvm::VectorType * VoidPointerVectorType
Definition: llvmutil.h:107
static llvm::VectorType * BoolVectorType
Definition: llvmutil.h:91
llvm::ConstantInt * LLVMUInt64(uint64_t i)
Definition: llvmutil.cpp:240
llvm::Constant * LLVMFloatVector(float f)
Definition: llvmutil.cpp:338
static llvm::Type * BoolType
Definition: llvmutil.h:73
bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts)
Definition: llvmutil.cpp:690
llvm::Constant * LLVMInt32Vector(int32_t i)
Definition: llvmutil.cpp:308
static llvm::VectorType * Int32VectorType
Definition: llvmutil.h:95
static llvm::Type * FloatVectorPointerType
Definition: llvmutil.h:104
llvm::ConstantInt * LLVMInt8(int8_t i)
Definition: llvmutil.cpp:212
static llvm::Type * Int8PointerType
Definition: llvmutil.h:82
static llvm::Type * Int32PointerType
Definition: llvmutil.h:84
static llvm::Type * Int16VectorPointerType
Definition: llvmutil.h:101
void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target)
Definition: llvmutil.cpp:95
llvm::Constant * LLVMUInt64Vector(uint64_t i)
Definition: llvmutil.cpp:383
static llvm::Type * Int16Type
Definition: llvmutil.h:76
static llvm::Type * DoubleVectorPointerType
Definition: llvmutil.h:105
llvm::Constant * LLVMFalse
Definition: llvmutil.cpp:91
llvm::Constant * LLVMMaskAllOff
Definition: llvmutil.cpp:93
static llvm::VectorType * Int1VectorType
Definition: llvmutil.h:92
llvm::Constant * LLVMInt8Vector(int8_t i)
Definition: llvmutil.cpp:248
static llvm::Type * VoidType
Definition: llvmutil.h:70
llvm::ConstantInt * LLVMInt32(int32_t i)
Definition: llvmutil.cpp:228
static llvm::Type * Int8VectorPointerType
Definition: llvmutil.h:100
static llvm::VectorType * Int8VectorType
Definition: llvmutil.h:93
llvm::Constant * LLVMUInt8Vector(uint8_t i)
Definition: llvmutil.cpp:263
llvm::ConstantInt * LLVMUInt8(uint8_t i)
Definition: llvmutil.cpp:216
bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat=NULL)
Definition: llvmutil.cpp:1061
llvm::ConstantInt * LLVMInt16(int16_t i)
Definition: llvmutil.cpp:220
llvm::Constant * LLVMTrue
Definition: llvmutil.cpp:90
llvm::ConstantInt * LLVMUInt16(uint16_t i)
Definition: llvmutil.cpp:224
llvm::Constant * LLVMUIntAsType(uint64_t, llvm::Type *t)
Definition: llvmutil.cpp:454
static llvm::VectorType * FloatVectorType
Definition: llvmutil.h:97
static llvm::Type * Int64Type
Definition: llvmutil.h:78
static llvm::Type * Int8Type
Definition: llvmutil.h:75
llvm::Constant * LLVMBoolVector(bool v)
Definition: llvmutil.cpp:398
static llvm::VectorType * Int64VectorType
Definition: llvmutil.h:96
llvm::Constant * LLVMDouble(double f)
Definition: llvmutil.cpp:246
static llvm::Type * Int64PointerType
Definition: llvmutil.h:85
llvm::ConstantInt * LLVMUInt32(uint32_t i)
Definition: llvmutil.cpp:232
llvm::Value * LLVMExtractFirstVectorElement(llvm::Value *v)
Definition: llvmutil.cpp:1472
static llvm::Type * FloatPointerType
Definition: llvmutil.h:86
static llvm::Type * Int16PointerType
Definition: llvmutil.h:83
const char * LLVMGetName(llvm::Value *v, const char *)
Definition: llvmutil.cpp:1518
void LLVMDumpValue(llvm::Value *v)
Definition: llvmutil.cpp:1362
static llvm::Type * PointerIntType
Definition: llvmutil.h:72
static llvm::PointerType * VoidPointerType
Definition: llvmutil.h:71
static llvm::Type * Int64VectorPointerType
Definition: llvmutil.h:103
static llvm::Type * Int32Type
Definition: llvmutil.h:77
static llvm::Type * DoublePointerType
Definition: llvmutil.h:87
llvm::Constant * LLVMInt16Vector(int16_t i)
Definition: llvmutil.cpp:278
llvm::Constant * LLVMDoubleVector(double f)
Definition: llvmutil.cpp:353
llvm::Value * LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare=true, bool undef=true, bool searchFirstUndef=false)
Definition: llvmutil.cpp:565
static llvm::VectorType * MaskType
Definition: llvmutil.h:89
static llvm::VectorType * DoubleVectorType
Definition: llvmutil.h:98
llvm::Constant * LLVMIntAsType(int64_t, llvm::Type *t)
Definition: llvmutil.cpp:441
static llvm::VectorType * Int16VectorType
Definition: llvmutil.h:94
llvm::Value * LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1483
bool LLVMVectorIsLinear(llvm::Value *v, int stride)
Definition: llvmutil.cpp:1326
llvm::ConstantInt * LLVMInt64(int64_t i)
Definition: llvmutil.cpp:236