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