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