Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
llvmutil.cpp
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.cpp
35  @brief Implementations of various LLVM utility types and classes.
36 */
37 
38 #include "llvmutil.h"
39 #include "ispc.h"
40 #include "type.h"
41 #include <llvm/Analysis/ValueTracking.h>
42 #include <llvm/IR/BasicBlock.h>
43 #include <llvm/IR/Instructions.h>
44 #include <llvm/IR/Module.h>
45 #include <map>
46 #include <set>
47 #include <vector>
48 
49 llvm::Type *LLVMTypes::VoidType = NULL;
50 llvm::PointerType *LLVMTypes::VoidPointerType = NULL;
51 llvm::Type *LLVMTypes::PointerIntType = NULL;
52 llvm::Type *LLVMTypes::BoolType = NULL;
53 llvm::Type *LLVMTypes::BoolStorageType = NULL;
54 
55 llvm::Type *LLVMTypes::Int8Type = NULL;
56 llvm::Type *LLVMTypes::Int16Type = NULL;
57 llvm::Type *LLVMTypes::Int32Type = NULL;
58 llvm::Type *LLVMTypes::Int64Type = NULL;
59 llvm::Type *LLVMTypes::FloatType = NULL;
60 llvm::Type *LLVMTypes::DoubleType = NULL;
61 
62 llvm::Type *LLVMTypes::Int8PointerType = NULL;
63 llvm::Type *LLVMTypes::Int16PointerType = NULL;
64 llvm::Type *LLVMTypes::Int32PointerType = NULL;
65 llvm::Type *LLVMTypes::Int64PointerType = NULL;
66 llvm::Type *LLVMTypes::FloatPointerType = NULL;
67 llvm::Type *LLVMTypes::DoublePointerType = NULL;
68 
69 llvm::VectorType *LLVMTypes::MaskType = NULL;
70 llvm::VectorType *LLVMTypes::BoolVectorType = NULL;
71 llvm::VectorType *LLVMTypes::BoolVectorStorageType = NULL;
72 
73 llvm::VectorType *LLVMTypes::Int1VectorType = NULL;
74 llvm::VectorType *LLVMTypes::Int8VectorType = NULL;
75 llvm::VectorType *LLVMTypes::Int16VectorType = NULL;
76 llvm::VectorType *LLVMTypes::Int32VectorType = NULL;
77 llvm::VectorType *LLVMTypes::Int64VectorType = NULL;
78 llvm::VectorType *LLVMTypes::FloatVectorType = NULL;
79 llvm::VectorType *LLVMTypes::DoubleVectorType = NULL;
80 
81 llvm::Type *LLVMTypes::Int8VectorPointerType = NULL;
82 llvm::Type *LLVMTypes::Int16VectorPointerType = NULL;
83 llvm::Type *LLVMTypes::Int32VectorPointerType = NULL;
84 llvm::Type *LLVMTypes::Int64VectorPointerType = NULL;
85 llvm::Type *LLVMTypes::FloatVectorPointerType = NULL;
86 llvm::Type *LLVMTypes::DoubleVectorPointerType = NULL;
87 
88 llvm::VectorType *LLVMTypes::VoidPointerVectorType = NULL;
89 
90 llvm::Constant *LLVMTrue = NULL;
91 llvm::Constant *LLVMFalse = NULL;
92 llvm::Constant *LLVMTrueInStorage = NULL;
93 llvm::Constant *LLVMFalseInStorage = NULL;
94 llvm::Constant *LLVMMaskAllOn = NULL;
95 llvm::Constant *LLVMMaskAllOff = NULL;
96 
97 void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target) {
98  LLVMTypes::VoidType = llvm::Type::getVoidTy(*ctx);
99  LLVMTypes::VoidPointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*ctx), 0);
100  LLVMTypes::PointerIntType = target.is32Bit() ? llvm::Type::getInt32Ty(*ctx) : llvm::Type::getInt64Ty(*ctx);
101 
102  LLVMTypes::BoolType = llvm::Type::getInt1Ty(*ctx);
103  LLVMTypes::Int8Type = LLVMTypes::BoolStorageType = llvm::Type::getInt8Ty(*ctx);
104  LLVMTypes::Int16Type = llvm::Type::getInt16Ty(*ctx);
105  LLVMTypes::Int32Type = llvm::Type::getInt32Ty(*ctx);
106  LLVMTypes::Int64Type = llvm::Type::getInt64Ty(*ctx);
107  LLVMTypes::FloatType = llvm::Type::getFloatTy(*ctx);
108  LLVMTypes::DoubleType = llvm::Type::getDoubleTy(*ctx);
109 
110  LLVMTypes::Int8PointerType = llvm::PointerType::get(LLVMTypes::Int8Type, 0);
111  LLVMTypes::Int16PointerType = llvm::PointerType::get(LLVMTypes::Int16Type, 0);
112  LLVMTypes::Int32PointerType = llvm::PointerType::get(LLVMTypes::Int32Type, 0);
113  LLVMTypes::Int64PointerType = llvm::PointerType::get(LLVMTypes::Int64Type, 0);
114  LLVMTypes::FloatPointerType = llvm::PointerType::get(LLVMTypes::FloatType, 0);
115  LLVMTypes::DoublePointerType = llvm::PointerType::get(LLVMTypes::DoubleType, 0);
116 
117  switch (target.getMaskBitCount()) {
118  case 1:
120  llvm::VectorType::get(llvm::Type::getInt1Ty(*ctx), target.getVectorWidth());
121  break;
122  case 8:
124  llvm::VectorType::get(llvm::Type::getInt8Ty(*ctx), target.getVectorWidth());
125  break;
126  case 16:
128  llvm::VectorType::get(llvm::Type::getInt16Ty(*ctx), target.getVectorWidth());
129  break;
130  case 32:
132  llvm::VectorType::get(llvm::Type::getInt32Ty(*ctx), target.getVectorWidth());
133  break;
134  case 64:
136  llvm::VectorType::get(llvm::Type::getInt64Ty(*ctx), target.getVectorWidth());
137  break;
138  default:
139  FATAL("Unhandled mask width for initializing MaskType");
140  }
141 
142  LLVMTypes::Int1VectorType = llvm::VectorType::get(llvm::Type::getInt1Ty(*ctx), target.getVectorWidth());
144  llvm::VectorType::get(LLVMTypes::Int8Type, target.getVectorWidth());
145  LLVMTypes::Int16VectorType = llvm::VectorType::get(LLVMTypes::Int16Type, target.getVectorWidth());
146  LLVMTypes::Int32VectorType = llvm::VectorType::get(LLVMTypes::Int32Type, target.getVectorWidth());
147  LLVMTypes::Int64VectorType = llvm::VectorType::get(LLVMTypes::Int64Type, target.getVectorWidth());
148  LLVMTypes::FloatVectorType = llvm::VectorType::get(LLVMTypes::FloatType, target.getVectorWidth());
149  LLVMTypes::DoubleVectorType = llvm::VectorType::get(LLVMTypes::DoubleType, target.getVectorWidth());
150 
151  LLVMTypes::Int8VectorPointerType = llvm::PointerType::get(LLVMTypes::Int8VectorType, 0);
152  LLVMTypes::Int16VectorPointerType = llvm::PointerType::get(LLVMTypes::Int16VectorType, 0);
153  LLVMTypes::Int32VectorPointerType = llvm::PointerType::get(LLVMTypes::Int32VectorType, 0);
154  LLVMTypes::Int64VectorPointerType = llvm::PointerType::get(LLVMTypes::Int64VectorType, 0);
155  LLVMTypes::FloatVectorPointerType = llvm::PointerType::get(LLVMTypes::FloatVectorType, 0);
156  LLVMTypes::DoubleVectorPointerType = llvm::PointerType::get(LLVMTypes::DoubleVectorType, 0);
157 
159 
160  LLVMTrue = llvm::ConstantInt::getTrue(*ctx);
161  LLVMFalse = llvm::ConstantInt::getFalse(*ctx);
162  LLVMTrueInStorage = llvm::ConstantInt::get(LLVMTypes::Int8Type, 0xff, false /*unsigned*/);
163  LLVMFalseInStorage = llvm::ConstantInt::get(LLVMTypes::Int8Type, 0x00, false /*unsigned*/);
164 
165  std::vector<llvm::Constant *> maskOnes;
166  llvm::Constant *onMask = NULL;
167  switch (target.getMaskBitCount()) {
168  case 1:
169  onMask = llvm::ConstantInt::get(llvm::Type::getInt1Ty(*ctx), 1, false /*unsigned*/); // 0x1
170  break;
171  case 8:
172  onMask = llvm::ConstantInt::get(llvm::Type::getInt8Ty(*ctx), -1, true /*signed*/); // 0xff
173  break;
174  case 16:
175  onMask = llvm::ConstantInt::get(llvm::Type::getInt16Ty(*ctx), -1, true /*signed*/); // 0xffff
176  break;
177  case 32:
178  onMask = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), -1, true /*signed*/); // 0xffffffff
179  break;
180  case 64:
181  onMask = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*ctx), -1, true /*signed*/); // 0xffffffffffffffffull
182  break;
183  default:
184  FATAL("Unhandled mask width for onMask");
185  }
186 
187  for (int i = 0; i < target.getVectorWidth(); ++i)
188  maskOnes.push_back(onMask);
189  LLVMMaskAllOn = llvm::ConstantVector::get(maskOnes);
190 
191  std::vector<llvm::Constant *> maskZeros;
192  llvm::Constant *offMask = NULL;
193  switch (target.getMaskBitCount()) {
194  case 1:
195  offMask = llvm::ConstantInt::get(llvm::Type::getInt1Ty(*ctx), 0, true /*signed*/);
196  break;
197  case 8:
198  offMask = llvm::ConstantInt::get(llvm::Type::getInt8Ty(*ctx), 0, true /*signed*/);
199  break;
200  case 16:
201  offMask = llvm::ConstantInt::get(llvm::Type::getInt16Ty(*ctx), 0, true /*signed*/);
202  break;
203  case 32:
204  offMask = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0, true /*signed*/);
205  break;
206  case 64:
207  offMask = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*ctx), 0, true /*signed*/);
208  break;
209  default:
210  FATAL("Unhandled mask width for offMask");
211  }
212  for (int i = 0; i < target.getVectorWidth(); ++i)
213  maskZeros.push_back(offMask);
214  LLVMMaskAllOff = llvm::ConstantVector::get(maskZeros);
215 }
216 
217 llvm::ConstantInt *LLVMInt8(int8_t ival) {
218  return llvm::ConstantInt::get(llvm::Type::getInt8Ty(*g->ctx), ival, true /*signed*/);
219 }
220 
221 llvm::ConstantInt *LLVMUInt8(uint8_t ival) {
222  return llvm::ConstantInt::get(llvm::Type::getInt8Ty(*g->ctx), ival, false /*unsigned*/);
223 }
224 
225 llvm::ConstantInt *LLVMInt16(int16_t ival) {
226  return llvm::ConstantInt::get(llvm::Type::getInt16Ty(*g->ctx), ival, true /*signed*/);
227 }
228 
229 llvm::ConstantInt *LLVMUInt16(uint16_t ival) {
230  return llvm::ConstantInt::get(llvm::Type::getInt16Ty(*g->ctx), ival, false /*unsigned*/);
231 }
232 
233 llvm::ConstantInt *LLVMInt32(int32_t ival) {
234  return llvm::ConstantInt::get(llvm::Type::getInt32Ty(*g->ctx), ival, true /*signed*/);
235 }
236 
237 llvm::ConstantInt *LLVMUInt32(uint32_t ival) {
238  return llvm::ConstantInt::get(llvm::Type::getInt32Ty(*g->ctx), ival, false /*unsigned*/);
239 }
240 
241 llvm::ConstantInt *LLVMInt64(int64_t ival) {
242  return llvm::ConstantInt::get(llvm::Type::getInt64Ty(*g->ctx), ival, true /*signed*/);
243 }
244 
245 llvm::ConstantInt *LLVMUInt64(uint64_t ival) {
246  return llvm::ConstantInt::get(llvm::Type::getInt64Ty(*g->ctx), ival, false /*unsigned*/);
247 }
248 
249 llvm::Constant *LLVMFloat(float fval) { return llvm::ConstantFP::get(llvm::Type::getFloatTy(*g->ctx), fval); }
250 
251 llvm::Constant *LLVMDouble(double dval) { return llvm::ConstantFP::get(llvm::Type::getDoubleTy(*g->ctx), dval); }
252 
253 llvm::Constant *LLVMInt8Vector(int8_t ival) {
254  llvm::Constant *v = LLVMInt8(ival);
255  std::vector<llvm::Constant *> vals;
256  for (int i = 0; i < g->target->getVectorWidth(); ++i)
257  vals.push_back(v);
258  return llvm::ConstantVector::get(vals);
259 }
260 
261 llvm::Constant *LLVMInt8Vector(const int8_t *ivec) {
262  std::vector<llvm::Constant *> vals;
263  for (int i = 0; i < g->target->getVectorWidth(); ++i)
264  vals.push_back(LLVMInt8(ivec[i]));
265  return llvm::ConstantVector::get(vals);
266 }
267 
268 llvm::Constant *LLVMUInt8Vector(uint8_t ival) {
269  llvm::Constant *v = LLVMUInt8(ival);
270  std::vector<llvm::Constant *> vals;
271  for (int i = 0; i < g->target->getVectorWidth(); ++i)
272  vals.push_back(v);
273  return llvm::ConstantVector::get(vals);
274 }
275 
276 llvm::Constant *LLVMUInt8Vector(const uint8_t *ivec) {
277  std::vector<llvm::Constant *> vals;
278  for (int i = 0; i < g->target->getVectorWidth(); ++i)
279  vals.push_back(LLVMUInt8(ivec[i]));
280  return llvm::ConstantVector::get(vals);
281 }
282 
283 llvm::Constant *LLVMInt16Vector(int16_t ival) {
284  llvm::Constant *v = LLVMInt16(ival);
285  std::vector<llvm::Constant *> vals;
286  for (int i = 0; i < g->target->getVectorWidth(); ++i)
287  vals.push_back(v);
288  return llvm::ConstantVector::get(vals);
289 }
290 
291 llvm::Constant *LLVMInt16Vector(const int16_t *ivec) {
292  std::vector<llvm::Constant *> vals;
293  for (int i = 0; i < g->target->getVectorWidth(); ++i)
294  vals.push_back(LLVMInt16(ivec[i]));
295  return llvm::ConstantVector::get(vals);
296 }
297 
298 llvm::Constant *LLVMUInt16Vector(uint16_t ival) {
299  llvm::Constant *v = LLVMUInt16(ival);
300  std::vector<llvm::Constant *> vals;
301  for (int i = 0; i < g->target->getVectorWidth(); ++i)
302  vals.push_back(v);
303  return llvm::ConstantVector::get(vals);
304 }
305 
306 llvm::Constant *LLVMUInt16Vector(const uint16_t *ivec) {
307  std::vector<llvm::Constant *> vals;
308  for (int i = 0; i < g->target->getVectorWidth(); ++i)
309  vals.push_back(LLVMUInt16(ivec[i]));
310  return llvm::ConstantVector::get(vals);
311 }
312 
313 llvm::Constant *LLVMInt32Vector(int32_t ival) {
314  llvm::Constant *v = LLVMInt32(ival);
315  std::vector<llvm::Constant *> vals;
316  for (int i = 0; i < g->target->getVectorWidth(); ++i)
317  vals.push_back(v);
318  return llvm::ConstantVector::get(vals);
319 }
320 
321 llvm::Constant *LLVMInt32Vector(const int32_t *ivec) {
322  std::vector<llvm::Constant *> vals;
323  for (int i = 0; i < g->target->getVectorWidth(); ++i)
324  vals.push_back(LLVMInt32(ivec[i]));
325  return llvm::ConstantVector::get(vals);
326 }
327 
328 llvm::Constant *LLVMUInt32Vector(uint32_t ival) {
329  llvm::Constant *v = LLVMUInt32(ival);
330  std::vector<llvm::Constant *> vals;
331  for (int i = 0; i < g->target->getVectorWidth(); ++i)
332  vals.push_back(v);
333  return llvm::ConstantVector::get(vals);
334 }
335 
336 llvm::Constant *LLVMUInt32Vector(const uint32_t *ivec) {
337  std::vector<llvm::Constant *> vals;
338  for (int i = 0; i < g->target->getVectorWidth(); ++i)
339  vals.push_back(LLVMUInt32(ivec[i]));
340  return llvm::ConstantVector::get(vals);
341 }
342 
343 llvm::Constant *LLVMFloatVector(float fval) {
344  llvm::Constant *v = LLVMFloat(fval);
345  std::vector<llvm::Constant *> vals;
346  for (int i = 0; i < g->target->getVectorWidth(); ++i)
347  vals.push_back(v);
348  return llvm::ConstantVector::get(vals);
349 }
350 
351 llvm::Constant *LLVMFloatVector(const float *fvec) {
352  std::vector<llvm::Constant *> vals;
353  for (int i = 0; i < g->target->getVectorWidth(); ++i)
354  vals.push_back(LLVMFloat(fvec[i]));
355  return llvm::ConstantVector::get(vals);
356 }
357 
358 llvm::Constant *LLVMDoubleVector(double dval) {
359  llvm::Constant *v = LLVMDouble(dval);
360  std::vector<llvm::Constant *> vals;
361  for (int i = 0; i < g->target->getVectorWidth(); ++i)
362  vals.push_back(v);
363  return llvm::ConstantVector::get(vals);
364 }
365 
366 llvm::Constant *LLVMDoubleVector(const double *dvec) {
367  std::vector<llvm::Constant *> vals;
368  for (int i = 0; i < g->target->getVectorWidth(); ++i)
369  vals.push_back(LLVMDouble(dvec[i]));
370  return llvm::ConstantVector::get(vals);
371 }
372 
373 llvm::Constant *LLVMInt64Vector(int64_t ival) {
374  llvm::Constant *v = LLVMInt64(ival);
375  std::vector<llvm::Constant *> vals;
376  for (int i = 0; i < g->target->getVectorWidth(); ++i)
377  vals.push_back(v);
378  return llvm::ConstantVector::get(vals);
379 }
380 
381 llvm::Constant *LLVMInt64Vector(const int64_t *ivec) {
382  std::vector<llvm::Constant *> vals;
383  for (int i = 0; i < g->target->getVectorWidth(); ++i)
384  vals.push_back(LLVMInt64(ivec[i]));
385  return llvm::ConstantVector::get(vals);
386 }
387 
388 llvm::Constant *LLVMUInt64Vector(uint64_t ival) {
389  llvm::Constant *v = LLVMUInt64(ival);
390  std::vector<llvm::Constant *> vals;
391  for (int i = 0; i < g->target->getVectorWidth(); ++i)
392  vals.push_back(v);
393  return llvm::ConstantVector::get(vals);
394 }
395 
396 llvm::Constant *LLVMUInt64Vector(const uint64_t *ivec) {
397  std::vector<llvm::Constant *> vals;
398  for (int i = 0; i < g->target->getVectorWidth(); ++i)
399  vals.push_back(LLVMUInt64(ivec[i]));
400  return llvm::ConstantVector::get(vals);
401 }
402 
403 llvm::Constant *LLVMBoolVector(bool b) {
404  llvm::Constant *v;
406  v = llvm::ConstantInt::get(LLVMTypes::Int64Type, b ? 0xffffffffffffffffull : 0, false /*unsigned*/);
408  v = llvm::ConstantInt::get(LLVMTypes::Int32Type, b ? 0xffffffff : 0, false /*unsigned*/);
410  v = llvm::ConstantInt::get(LLVMTypes::Int16Type, b ? 0xffff : 0, false /*unsigned*/);
412  v = llvm::ConstantInt::get(LLVMTypes::Int8Type, b ? 0xff : 0, false /*unsigned*/);
413  else {
415  v = b ? LLVMTrue : LLVMFalse;
416  }
417 
418  std::vector<llvm::Constant *> vals;
419  for (int i = 0; i < g->target->getVectorWidth(); ++i)
420  vals.push_back(v);
421  return llvm::ConstantVector::get(vals);
422 }
423 
424 llvm::Constant *LLVMBoolVector(const bool *bvec) {
425  std::vector<llvm::Constant *> vals;
426  for (int i = 0; i < g->target->getVectorWidth(); ++i) {
427  llvm::Constant *v;
429  v = llvm::ConstantInt::get(LLVMTypes::Int64Type, bvec[i] ? 0xffffffffffffffffull : 0, false /*unsigned*/);
431  v = llvm::ConstantInt::get(LLVMTypes::Int32Type, bvec[i] ? 0xffffffff : 0, false /*unsigned*/);
433  v = llvm::ConstantInt::get(LLVMTypes::Int16Type, bvec[i] ? 0xffff : 0, false /*unsigned*/);
435  v = llvm::ConstantInt::get(LLVMTypes::Int8Type, bvec[i] ? 0xff : 0, false /*unsigned*/);
436  else {
438  v = bvec[i] ? LLVMTrue : LLVMFalse;
439  }
440 
441  vals.push_back(v);
442  }
443  return llvm::ConstantVector::get(vals);
444 }
445 
446 llvm::Constant *LLVMBoolVectorInStorage(bool b) {
447  llvm::Constant *v = b ? LLVMTrueInStorage : LLVMFalseInStorage;
448  std::vector<llvm::Constant *> vals;
449  for (int i = 0; i < g->target->getVectorWidth(); ++i)
450  vals.push_back(v);
451  return llvm::ConstantVector::get(vals);
452 }
453 
454 llvm::Constant *LLVMBoolVectorInStorage(const bool *bvec) {
455  std::vector<llvm::Constant *> vals;
456  for (int i = 0; i < g->target->getVectorWidth(); ++i) {
457  llvm::Constant *v = llvm::ConstantInt::get(LLVMTypes::Int8Type, bvec[i] ? 0xff : 0, false /*unsigned*/);
458  vals.push_back(v);
459  }
460  return llvm::ConstantVector::get(vals);
461 }
462 
463 llvm::Constant *LLVMIntAsType(int64_t val, llvm::Type *type) {
464  llvm::VectorType *vecType = llvm::dyn_cast<llvm::VectorType>(type);
465 
466  if (vecType != NULL) {
467  llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(), val, true /* signed */);
468  std::vector<llvm::Constant *> vals;
469  for (int i = 0; i < (int)vecType->getNumElements(); ++i)
470  vals.push_back(v);
471  return llvm::ConstantVector::get(vals);
472  } else
473  return llvm::ConstantInt::get(type, val, true /* signed */);
474 }
475 
476 llvm::Constant *LLVMUIntAsType(uint64_t val, llvm::Type *type) {
477  llvm::VectorType *vecType = llvm::dyn_cast<llvm::VectorType>(type);
478 
479  if (vecType != NULL) {
480  llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(), val, false /* unsigned */);
481  std::vector<llvm::Constant *> vals;
482  for (int i = 0; i < (int)vecType->getNumElements(); ++i)
483  vals.push_back(v);
484  return llvm::ConstantVector::get(vals);
485  } else
486  return llvm::ConstantInt::get(type, val, false /* unsigned */);
487 }
488 
489 /** Conservative test to see if two llvm::Values are equal. There are
490  (potentially many) cases where the two values actually are equal but
491  this will return false. However, if it does return true, the two
492  vectors definitely are equal.
493 */
494 static bool lValuesAreEqual(llvm::Value *v0, llvm::Value *v1, std::vector<llvm::PHINode *> &seenPhi0,
495  std::vector<llvm::PHINode *> &seenPhi1) {
496  // Thanks to the fact that LLVM hashes and returns the same pointer for
497  // constants (of all sorts, even constant expressions), this first test
498  // actually catches a lot of cases. LLVM's SSA form also helps a lot
499  // with this..
500  if (v0 == v1)
501  return true;
502 
503  Assert(seenPhi0.size() == seenPhi1.size());
504  for (unsigned int i = 0; i < seenPhi0.size(); ++i)
505  if (v0 == seenPhi0[i] && v1 == seenPhi1[i])
506  return true;
507 
508  llvm::BinaryOperator *bo0 = llvm::dyn_cast<llvm::BinaryOperator>(v0);
509  llvm::BinaryOperator *bo1 = llvm::dyn_cast<llvm::BinaryOperator>(v1);
510  if (bo0 != NULL && bo1 != NULL) {
511  if (bo0->getOpcode() != bo1->getOpcode())
512  return false;
513  return (lValuesAreEqual(bo0->getOperand(0), bo1->getOperand(0), seenPhi0, seenPhi1) &&
514  lValuesAreEqual(bo0->getOperand(1), bo1->getOperand(1), seenPhi0, seenPhi1));
515  }
516 
517  llvm::CastInst *cast0 = llvm::dyn_cast<llvm::CastInst>(v0);
518  llvm::CastInst *cast1 = llvm::dyn_cast<llvm::CastInst>(v1);
519  if (cast0 != NULL && cast1 != NULL) {
520  if (cast0->getOpcode() != cast1->getOpcode())
521  return false;
522  return lValuesAreEqual(cast0->getOperand(0), cast1->getOperand(0), seenPhi0, seenPhi1);
523  }
524 
525  llvm::PHINode *phi0 = llvm::dyn_cast<llvm::PHINode>(v0);
526  llvm::PHINode *phi1 = llvm::dyn_cast<llvm::PHINode>(v1);
527  if (phi0 != NULL && phi1 != NULL) {
528  if (phi0->getNumIncomingValues() != phi1->getNumIncomingValues())
529  return false;
530 
531  seenPhi0.push_back(phi0);
532  seenPhi1.push_back(phi1);
533 
534  unsigned int numIncoming = phi0->getNumIncomingValues();
535  // Check all of the incoming values: if all of them are all equal,
536  // then we're good.
537  bool anyFailure = false;
538  for (unsigned int i = 0; i < numIncoming; ++i) {
539  // FIXME: should it be ok if the incoming blocks are different,
540  // where we just return faliure in this case?
541  Assert(phi0->getIncomingBlock(i) == phi1->getIncomingBlock(i));
542  if (!lValuesAreEqual(phi0->getIncomingValue(i), phi1->getIncomingValue(i), seenPhi0, seenPhi1)) {
543  anyFailure = true;
544  break;
545  }
546  }
547 
548  seenPhi0.pop_back();
549  seenPhi1.pop_back();
550 
551  return !anyFailure;
552  }
553 
554  return false;
555 }
556 
557 /** Given an llvm::Value known to be an integer, return its value as
558  an int64_t.
559 */
560 static int64_t lGetIntValue(llvm::Value *offset) {
561  llvm::ConstantInt *intOffset = llvm::dyn_cast<llvm::ConstantInt>(offset);
562  Assert(intOffset && (intOffset->getBitWidth() == 32 || intOffset->getBitWidth() == 64));
563  return intOffset->getSExtValue();
564 }
565 
566 /** Recognizes constant vector with undef operands except the first one:
567  * <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
568  */
569 static bool lIsFirstElementConstVector(llvm::Value *v) {
570  llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v);
571  if (cv != NULL) {
572  llvm::Constant *c = llvm::dyn_cast<llvm::Constant>(cv->getOperand(0));
573  if (c == NULL) {
574  return false;
575  }
576 
577  for (int i = 1; i < (int)cv->getNumOperands(); ++i) {
578  if (!llvm::isa<llvm::UndefValue>(cv->getOperand(i))) {
579  return false;
580  }
581  }
582  return true;
583  }
584  return false;
585 }
586 
587 llvm::Value *LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare, bool undef,
588  bool searchFirstUndef) {
589  std::vector<llvm::Value *> elements(vectorWidth, nullptr);
590 
591  // Catch a pattern of InsertElement chain.
592  if (llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(inst)) {
593  // Gather elements of vector
594  while (ie != NULL) {
595  int64_t iOffset = lGetIntValue(ie->getOperand(2));
596  Assert(iOffset >= 0 && iOffset < vectorWidth);
597 
598  // Get the scalar value from this insert
599  if (elements[iOffset] == NULL) {
600  elements[iOffset] = ie->getOperand(1);
601  }
602 
603  // Do we have another insert?
604  llvm::Value *insertBase = ie->getOperand(0);
605  ie = llvm::dyn_cast<llvm::InsertElementInst>(insertBase);
606  if (ie != NULL) {
607  continue;
608  }
609 
610  if (llvm::isa<llvm::UndefValue>(insertBase)) {
611  break;
612  }
613 
614  if (llvm::isa<llvm::ConstantVector>(insertBase) || llvm::isa<llvm::ConstantAggregateZero>(insertBase)) {
615  llvm::Constant *cv = llvm::dyn_cast<llvm::Constant>(insertBase);
616  Assert(vectorWidth == (int)(cv->getNumOperands()));
617  for (int i = 0; i < vectorWidth; i++) {
618  if (elements[i] == NULL) {
619  elements[i] = cv->getOperand(i);
620  }
621  }
622  break;
623  } else {
624  // Here chain ends in llvm::LoadInst or some other.
625  // They are not equal to each other so we should return NULL if compare
626  // and first element if we have it.
627  Assert(compare == true || elements[0] != NULL);
628  if (compare) {
629  return NULL;
630  } else {
631  return elements[0];
632  }
633  }
634  // TODO: Also, should we handle some other values like
635  // ConstantDataVectors.
636  }
637  if (compare == false) {
638  // We simply want first element
639  return elements[0];
640  }
641 
642  int null_number = 0;
643  int NonNull = 0;
644  for (int i = 0; i < vectorWidth; i++) {
645  if (elements[i] == NULL) {
646  null_number++;
647  } else {
648  NonNull = i;
649  }
650  }
651  if (null_number == vectorWidth) {
652  // All of elements are NULLs
653  return NULL;
654  }
655  if ((undef == false) && (null_number != 0)) {
656  // We don't want NULLs in chain, but we have them
657  return NULL;
658  }
659 
660  // Compare elements of vector
661  for (int i = 0; i < vectorWidth; i++) {
662  if (elements[i] == NULL) {
663  continue;
664  }
665 
666  std::vector<llvm::PHINode *> seenPhi0;
667  std::vector<llvm::PHINode *> seenPhi1;
668  if (lValuesAreEqual(elements[NonNull], elements[i], seenPhi0, seenPhi1) == false) {
669  return NULL;
670  }
671  }
672  return elements[NonNull];
673  }
674 
675  // Catch a pattern of broadcast implemented as InsertElement + Shuffle:
676  // %broadcast_init.0 = insertelement <4 x i32> undef, i32 %val, i32 0
677  // %broadcast.1 = shufflevector <4 x i32> %smear.0, <4 x i32> undef,
678  // <4 x i32> zeroinitializer
679  // Or:
680  // %gep_ptr2int_broadcast_init = insertelement <8 x i64> undef, i64 %gep_ptr2int, i32 0
681  // %0 = add <8 x i64> %gep_ptr2int_broadcast_init,
682  // <i64 4, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef, i64 undef>
683  // %gep_offset = shufflevector <8 x i64> %0, <8 x i64> undef, <8 x i32> zeroinitializer
684  else if (llvm::ShuffleVectorInst *shuf = llvm::dyn_cast<llvm::ShuffleVectorInst>(inst)) {
685  llvm::Value *indices = shuf->getOperand(2);
686  if (llvm::isa<llvm::ConstantAggregateZero>(indices)) {
687  llvm::Value *op = shuf->getOperand(0);
688  llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(op);
689  if (ie == NULL && searchFirstUndef) {
690  // Trying to recognize 2nd pattern
691  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(op);
692  if (bop != NULL && ((bop->getOpcode() == llvm::Instruction::Add) || IsOrEquivalentToAdd(bop))) {
693  if (lIsFirstElementConstVector(bop->getOperand(1))) {
694  ie = llvm::dyn_cast<llvm::InsertElementInst>(bop->getOperand(0));
695  }
696  }
697  }
698  if (ie != NULL && llvm::isa<llvm::UndefValue>(ie->getOperand(0))) {
699  llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(ie->getOperand(2));
700  if (ci->isZero()) {
701  return ie->getOperand(1);
702  }
703  }
704  }
705  }
706  return NULL;
707 }
708 
709 bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts) {
710  // Make sure we do in fact have a vector of integer values here
711  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(v->getType());
712  Assert(vt != NULL);
713  Assert(llvm::isa<llvm::IntegerType>(vt->getElementType()));
714 
715  *nElts = (int)vt->getNumElements();
716 
717  if (llvm::isa<llvm::ConstantAggregateZero>(v)) {
718  for (int i = 0; i < (int)vt->getNumElements(); ++i)
719  ret[i] = 0;
720  return true;
721  }
722 
723  llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
724  if (cv == NULL)
725  return false;
726 
727  for (int i = 0; i < (int)cv->getNumElements(); ++i)
728  ret[i] = cv->getElementAsInteger(i);
729  return true;
730 }
731 
732 static bool lVectorValuesAllEqual(llvm::Value *v, int vectorLength, std::vector<llvm::PHINode *> &seenPhis,
733  llvm::Value **splatValue = NULL);
734 
735 /** This function checks to see if the given (scalar or vector) value is an
736  exact multiple of baseValue. It returns true if so, and false if not
737  (or if it's not able to determine if it is). Any vector value passed
738  in is required to have the same value in all elements (so that we can
739  just check the first element to be a multiple of the given value.)
740  */
741 static bool lIsExactMultiple(llvm::Value *val, int baseValue, int vectorLength,
742  std::vector<llvm::PHINode *> &seenPhis) {
743  if (llvm::isa<llvm::VectorType>(val->getType()) == false) {
744  // If we've worked down to a constant int, then the moment of truth
745  // has arrived...
746  llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(val);
747  if (ci != NULL)
748  return (ci->getZExtValue() % baseValue) == 0;
749  } else
751 
752  if (llvm::isa<llvm::InsertElementInst>(val) || llvm::isa<llvm::ShuffleVectorInst>(val)) {
753  llvm::Value *element = LLVMFlattenInsertChain(val, g->target->getVectorWidth());
754  // We just need to check the scalar first value, since we know that
755  // all elements are equal
756  return element ? lIsExactMultiple(element, baseValue, vectorLength, seenPhis) : false;
757  }
758 
759  llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(val);
760  if (phi != NULL) {
761  for (unsigned int i = 0; i < seenPhis.size(); ++i)
762  if (phi == seenPhis[i])
763  return true;
764 
765  seenPhis.push_back(phi);
766  unsigned int numIncoming = phi->getNumIncomingValues();
767 
768  // Check all of the incoming values: if all of them pass, then
769  // we're good.
770  for (unsigned int i = 0; i < numIncoming; ++i) {
771  llvm::Value *incoming = phi->getIncomingValue(i);
772  bool mult = lIsExactMultiple(incoming, baseValue, vectorLength, seenPhis);
773  if (mult == false) {
774  seenPhis.pop_back();
775  return false;
776  }
777  }
778  seenPhis.pop_back();
779  return true;
780  }
781 
782  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(val);
783  if (bop != NULL && ((bop->getOpcode() == llvm::Instruction::Add) || IsOrEquivalentToAdd(bop))) {
784  llvm::Value *op0 = bop->getOperand(0);
785  llvm::Value *op1 = bop->getOperand(1);
786 
787  bool be0 = lIsExactMultiple(op0, baseValue, vectorLength, seenPhis);
788  bool be1 = lIsExactMultiple(op1, baseValue, vectorLength, seenPhis);
789  return (be0 && be1);
790  }
791  // FIXME: mul? casts? ... ?
792 
793  return false;
794 }
795 
796 /** Returns the next power of two greater than or equal to the given
797  value. */
798 static int lRoundUpPow2(int v) {
799  v--;
800  v |= v >> 1;
801  v |= v >> 2;
802  v |= v >> 4;
803  v |= v >> 8;
804  v |= v >> 16;
805  return v + 1;
806 }
807 
808 /** Try to determine if all of the elements of the given vector value have
809  the same value when divided by the given baseValue. The function
810  returns true if this can be determined to be the case, and false
811  otherwise. (This function may fail to identify some cases where it
812  does in fact have this property, but should never report a given value
813  as being a multiple if it isn't!)
814  */
815 static bool lAllDivBaseEqual(llvm::Value *val, int64_t baseValue, int vectorLength,
816  std::vector<llvm::PHINode *> &seenPhis, bool &canAdd) {
817  Assert(llvm::isa<llvm::VectorType>(val->getType()));
818  // Make sure the base value is a positive power of 2
819  Assert(baseValue > 0 && (baseValue & (baseValue - 1)) == 0);
820 
821  // The easy case
822  if (lVectorValuesAllEqual(val, vectorLength, seenPhis))
823  return true;
824 
825  int64_t vecVals[ISPC_MAX_NVEC];
826  int nElts;
827  if (llvm::isa<llvm::VectorType>(val->getType()) && LLVMExtractVectorInts(val, vecVals, &nElts)) {
828  // If we have a vector of compile-time constant integer values,
829  // then go ahead and check them directly..
830  int64_t firstDiv = vecVals[0] / baseValue;
831  for (int i = 1; i < nElts; ++i)
832  if ((vecVals[i] / baseValue) != firstDiv)
833  return false;
834 
835  return true;
836  }
837 
838  llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(val);
839  if (phi != NULL) {
840  for (unsigned int i = 0; i < seenPhis.size(); ++i)
841  if (phi == seenPhis[i])
842  return true;
843 
844  seenPhis.push_back(phi);
845  unsigned int numIncoming = phi->getNumIncomingValues();
846 
847  // Check all of the incoming values: if all of them pass, then
848  // we're good.
849  for (unsigned int i = 0; i < numIncoming; ++i) {
850  llvm::Value *incoming = phi->getIncomingValue(i);
851  bool ca = canAdd;
852  bool mult = lAllDivBaseEqual(incoming, baseValue, vectorLength, seenPhis, ca);
853  if (mult == false) {
854  seenPhis.pop_back();
855  return false;
856  }
857  }
858  seenPhis.pop_back();
859  return true;
860  }
861 
862  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(val);
863  if (bop != NULL && ((bop->getOpcode() == llvm::Instruction::Add) || IsOrEquivalentToAdd(bop)) && canAdd == true) {
864  llvm::Value *op0 = bop->getOperand(0);
865  llvm::Value *op1 = bop->getOperand(1);
866 
867  // Otherwise we're only going to worry about the following case,
868  // which comes up often when looping over SOA data:
869  // ashr %val, <constant shift>
870  // where %val = add %smear, <0,1,2,3...>
871  // and where the maximum of the <0,...> vector in the add is less than
872  // 1<<(constant shift),
873  // and where %smear is a smear of a value that is a multiple of
874  // baseValue.
875 
876  int64_t addConstants[ISPC_MAX_NVEC];
877  if (LLVMExtractVectorInts(op1, addConstants, &nElts) == false)
878  return false;
879  Assert(nElts == vectorLength);
880 
881  // Do all of them give the same value when divided by baseValue?
882  int64_t firstConstDiv = addConstants[0] / baseValue;
883  for (int i = 1; i < vectorLength; ++i)
884  if ((addConstants[i] / baseValue) != firstConstDiv)
885  return false;
886 
887  if (lVectorValuesAllEqual(op0, vectorLength, seenPhis) == false)
888  return false;
889 
890  // Note that canAdd is a reference parameter; setting this ensures
891  // that we don't allow multiple adds in other parts of the chain of
892  // dependent values from here.
893  canAdd = false;
894 
895  // Now we need to figure out the required alignment (in numbers of
896  // elements of the underlying type being indexed) of the value to
897  // which these integer addConstant[] values are being added to. We
898  // know that we have addConstant[] values that all give the same
899  // value when divided by baseValue, but we may need a less-strict
900  // alignment than baseValue depending on the actual values.
901  //
902  // As an example, consider a case where the baseValue alignment is
903  // 16, but the addConstants here are <0,1,2,3>. In that case, the
904  // value to which addConstants is added to only needs to be a
905  // multiple of 4. Conversely, if addConstants are <4,5,6,7>, then
906  // we need a multiple of 8 to ensure that the final added result
907  // will still have the same value for all vector elements when
908  // divided by baseValue.
909  //
910  // All that said, here we need to find the maximum value of any of
911  // the addConstants[], mod baseValue. If we round that up to the
912  // next power of 2, we'll have a value that will be no greater than
913  // baseValue and sometimes less.
914  int maxMod = int(addConstants[0] % baseValue);
915  for (int i = 1; i < vectorLength; ++i)
916  maxMod = std::max(maxMod, int(addConstants[i] % baseValue));
917  int requiredAlignment = lRoundUpPow2(maxMod);
918 
919  std::vector<llvm::PHINode *> seenPhisEEM;
920  return lIsExactMultiple(op0, requiredAlignment, vectorLength, seenPhisEEM);
921  }
922  // TODO: could handle mul by a vector of equal constant integer values
923  // and the like here and adjust the 'baseValue' value when it evenly
924  // divides, but unclear if it's worthwhile...
925 
926  return false;
927 }
928 
929 /** Given a vector shift right of some value by some amount, try to
930  determine if all of the elements of the final result have the same
931  value (i.e. whether the high bits are all equal, disregarding the low
932  bits that are shifted out.) Returns true if so, and false otherwise.
933  */
934 static bool lVectorShiftRightAllEqual(llvm::Value *val, llvm::Value *shift, int vectorLength) {
935  // Are we shifting all elements by a compile-time constant amount? If
936  // not, give up.
937  int64_t shiftAmount[ISPC_MAX_NVEC];
938  int nElts;
939  if (LLVMExtractVectorInts(shift, shiftAmount, &nElts) == false)
940  return false;
941  Assert(nElts == vectorLength);
942 
943  // Is it the same amount for all elements?
944  for (int i = 0; i < vectorLength; ++i)
945  if (shiftAmount[i] != shiftAmount[0])
946  return false;
947 
948  // Now see if the value divided by (1 << shift) can be determined to
949  // have the same value for all vector elements.
950  int pow2 = 1 << shiftAmount[0];
951  bool canAdd = true;
952  std::vector<llvm::PHINode *> seenPhis;
953  bool eq = lAllDivBaseEqual(val, pow2, vectorLength, seenPhis, canAdd);
954 #if 0
955  fprintf(stderr, "check all div base equal:\n");
956  LLVMDumpValue(shift);
957  LLVMDumpValue(val);
958  fprintf(stderr, "----> %s\n\n", eq ? "true" : "false");
959 #endif
960  return eq;
961 }
962 
963 static bool lVectorValuesAllEqual(llvm::Value *v, int vectorLength, std::vector<llvm::PHINode *> &seenPhis,
964  llvm::Value **splatValue) {
965  if (vectorLength == 1)
966  return true;
967 
968  if (llvm::isa<llvm::ConstantAggregateZero>(v)) {
969  if (splatValue) {
970  llvm::ConstantAggregateZero *caz = llvm::dyn_cast<llvm::ConstantAggregateZero>(v);
971  *splatValue = caz->getSequentialElement();
972  }
973  return true;
974  }
975 
976  llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v);
977  if (cv != NULL) {
978  llvm::Value *splat = cv->getSplatValue();
979  if (splat != NULL && splatValue) {
980  *splatValue = splat;
981  }
982  return (splat != NULL);
983  }
984 
985  llvm::ConstantDataVector *cdv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
986  if (cdv != NULL) {
987  llvm::Value *splat = cdv->getSplatValue();
988  if (splat != NULL && splatValue) {
989  *splatValue = splat;
990  }
991  return (splat != NULL);
992  }
993 
994  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
995  if (bop != NULL) {
996  // Easy case: both operands are all equal -> return true
997  if (lVectorValuesAllEqual(bop->getOperand(0), vectorLength, seenPhis) &&
998  lVectorValuesAllEqual(bop->getOperand(1), vectorLength, seenPhis))
999  return true;
1000 
1001  // If it's a shift, take a special path that tries to check if the
1002  // high (surviving) bits of the values are equal.
1003  if (bop->getOpcode() == llvm::Instruction::AShr || bop->getOpcode() == llvm::Instruction::LShr)
1004  return lVectorShiftRightAllEqual(bop->getOperand(0), bop->getOperand(1), vectorLength);
1005 
1006  return false;
1007  }
1008 
1009  llvm::CastInst *cast = llvm::dyn_cast<llvm::CastInst>(v);
1010  if (cast != NULL)
1011  return lVectorValuesAllEqual(cast->getOperand(0), vectorLength, seenPhis);
1012 
1013  llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(v);
1014  if (ie != NULL) {
1015  return (LLVMFlattenInsertChain(ie, vectorLength) != NULL);
1016  }
1017 
1018  llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
1019  if (phi) {
1020  for (unsigned int i = 0; i < seenPhis.size(); ++i)
1021  if (seenPhis[i] == phi)
1022  return true;
1023 
1024  seenPhis.push_back(phi);
1025 
1026  unsigned int numIncoming = phi->getNumIncomingValues();
1027  // Check all of the incoming values: if all of them are all equal,
1028  // then we're good.
1029  for (unsigned int i = 0; i < numIncoming; ++i) {
1030  if (!lVectorValuesAllEqual(phi->getIncomingValue(i), vectorLength, seenPhis)) {
1031  seenPhis.pop_back();
1032  return false;
1033  }
1034  }
1035 
1036  seenPhis.pop_back();
1037  return true;
1038  }
1039 
1040  if (llvm::isa<llvm::UndefValue>(v))
1041  // ?
1042  return false;
1043 
1044  Assert(!llvm::isa<llvm::Constant>(v));
1045 
1046  if (llvm::isa<llvm::CallInst>(v) || llvm::isa<llvm::LoadInst>(v) || !llvm::isa<llvm::Instruction>(v))
1047  return false;
1048 
1049  llvm::ShuffleVectorInst *shuffle = llvm::dyn_cast<llvm::ShuffleVectorInst>(v);
1050  if (shuffle != NULL) {
1051  llvm::Value *indices = shuffle->getOperand(2);
1052  if (lVectorValuesAllEqual(indices, vectorLength, seenPhis))
1053  // The easy case--just a smear of the same element across the
1054  // whole vector.
1055  return true;
1056 
1057  // TODO: handle more general cases?
1058  return false;
1059  }
1060 
1061 #if 0
1062  fprintf(stderr, "all equal: ");
1063  v->dump();
1064  fprintf(stderr, "\n");
1065  llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
1066  if (inst) {
1067  inst->getParent()->dump();
1068  fprintf(stderr, "\n");
1069  fprintf(stderr, "\n");
1070  }
1071 #endif
1072 
1073  return false;
1074 }
1075 
1076 /** Tests to see if all of the elements of the vector in the 'v' parameter
1077  are equal. This is a conservative test and may return false for arrays
1078  where the values are actually all equal.
1079 */
1080 bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat) {
1081  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(v->getType());
1082  Assert(vt != NULL);
1083  int vectorLength = vt->getNumElements();
1084 
1085  std::vector<llvm::PHINode *> seenPhis;
1086  bool equal = lVectorValuesAllEqual(v, vectorLength, seenPhis, splat);
1087 
1088  Debug(SourcePos(), "LLVMVectorValuesAllEqual(%s) -> %s.", v->getName().str().c_str(), equal ? "true" : "false");
1089 #ifndef ISPC_NO_DUMPS
1090  if (g->debugPrint)
1091  LLVMDumpValue(v);
1092 #endif
1093 
1094  return equal;
1095 }
1096 
1097 /** Tests to see if a binary operator has an OR which is equivalent to an ADD.*/
1098 bool IsOrEquivalentToAdd(llvm::Value *op) {
1099  bool isEq = false;
1100  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(op);
1101  if (bop != NULL && bop->getOpcode() == llvm::Instruction::Or) {
1102  // Special case when A+B --> A|B transformation is triggered
1103  // We need to prove that A|B == A+B
1104  llvm::Module *module = bop->getParent()->getParent()->getParent();
1105  llvm::Value *op0 = bop->getOperand(0), *op1 = bop->getOperand(1);
1106  if (!haveNoCommonBitsSet(op0, op1, module->getDataLayout()) == false) {
1107  // Fallback to A+B case
1108  isEq = true;
1109  }
1110  }
1111  return isEq;
1112 }
1113 
1114 static bool lVectorIsLinear(llvm::Value *v, int vectorLength, int stride, std::vector<llvm::PHINode *> &seenPhis);
1115 
1116 /** Given a vector of compile-time constant integer values, test to see if
1117  they are a linear sequence of constant integers starting from an
1118  arbirary value but then having a step of value "stride" between
1119  elements.
1120  */
1121 static bool lVectorIsLinearConstantInts(llvm::ConstantDataVector *cv, int vectorLength, int stride) {
1122  // Flatten the vector out into the elements array
1123  llvm::SmallVector<llvm::Constant *, ISPC_MAX_NVEC> elements;
1124  for (int i = 0; i < (int)cv->getNumElements(); ++i)
1125  elements.push_back(cv->getElementAsConstant(i));
1126  Assert((int)elements.size() == vectorLength);
1127 
1128  llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(elements[0]);
1129  if (ci == NULL)
1130  // Not a vector of integers
1131  return false;
1132 
1133  int64_t prevVal = ci->getSExtValue();
1134 
1135  // For each element in the array, see if it is both a ConstantInt and
1136  // if the difference between it and the value of the previous element
1137  // is stride. If not, fail.
1138  for (int i = 1; i < vectorLength; ++i) {
1139  ci = llvm::dyn_cast<llvm::ConstantInt>(elements[i]);
1140  if (ci == NULL)
1141  return false;
1142 
1143  int64_t nextVal = ci->getSExtValue();
1144  if (prevVal + stride != nextVal)
1145  return false;
1146 
1147  prevVal = nextVal;
1148  }
1149  return true;
1150 }
1151 
1152 /** Checks to see if (op0 * op1) is a linear vector where the result is a
1153  vector with values that increase by stride.
1154  */
1155 static bool lCheckMulForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride,
1156  std::vector<llvm::PHINode *> &seenPhis) {
1157  // Is the first operand a constant integer value splatted across all of
1158  // the lanes?
1159  llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(op0);
1160  if (cv == NULL)
1161  return false;
1162 
1163  llvm::Constant *csplat = cv->getSplatValue();
1164  if (csplat == NULL)
1165  return false;
1166 
1167  llvm::ConstantInt *splat = llvm::dyn_cast<llvm::ConstantInt>(csplat);
1168  if (splat == NULL)
1169  return false;
1170 
1171  // If the splat value doesn't evenly divide the stride we're looking
1172  // for, there's no way that we can get the linear sequence we're
1173  // looking or.
1174  int64_t splatVal = splat->getSExtValue();
1175  if (splatVal == 0 || splatVal > stride || (stride % splatVal) != 0)
1176  return false;
1177 
1178  // Check to see if the other operand is a linear vector with stride
1179  // given by stride/splatVal.
1180  return lVectorIsLinear(op1, vectorLength, (int)(stride / splatVal), seenPhis);
1181 }
1182 
1183 /** Checks to see if (op0 << op1) is a linear vector where the result is a
1184  vector with values that increase by stride.
1185  */
1186 static bool lCheckShlForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride,
1187  std::vector<llvm::PHINode *> &seenPhis) {
1188  // Is the second operand a constant integer value splatted across all of
1189  // the lanes?
1190  llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(op1);
1191  if (cv == NULL)
1192  return false;
1193 
1194  llvm::Constant *csplat = cv->getSplatValue();
1195  if (csplat == NULL)
1196  return false;
1197 
1198  llvm::ConstantInt *splat = llvm::dyn_cast<llvm::ConstantInt>(csplat);
1199  if (splat == NULL)
1200  return false;
1201 
1202  // If (1 << the splat value) doesn't evenly divide the stride we're
1203  // looking for, there's no way that we can get the linear sequence
1204  // we're looking or.
1205  int64_t equivalentMul = (1LL << splat->getSExtValue());
1206  if (equivalentMul > stride || (stride % equivalentMul) != 0)
1207  return false;
1208 
1209  // Check to see if the first operand is a linear vector with stride
1210  // given by stride/splatVal.
1211  return lVectorIsLinear(op0, vectorLength, (int)(stride / equivalentMul), seenPhis);
1212 }
1213 
1214 /** Given (op0 AND op1), try and see if we can determine if the result is a
1215  linear sequence with a step of "stride" between values. Returns true
1216  if so and false otherwise. This pattern comes up when accessing SOA
1217  data.
1218  */
1219 static bool lCheckAndForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride,
1220  std::vector<llvm::PHINode *> &seenPhis) {
1221  // Require op1 to be a compile-time constant
1222  int64_t maskValue[ISPC_MAX_NVEC];
1223  int nElts;
1224  if (LLVMExtractVectorInts(op1, maskValue, &nElts) == false)
1225  return false;
1226  Assert(nElts == vectorLength);
1227 
1228  // Is op1 a smear of the same value across all lanes? Give up if not.
1229  for (int i = 1; i < vectorLength; ++i)
1230  if (maskValue[i] != maskValue[0])
1231  return false;
1232 
1233  // If the op1 value isn't power of 2 minus one, then also give up.
1234  int64_t maskPlusOne = maskValue[0] + 1;
1235  bool isPowTwo = (maskPlusOne & (maskPlusOne - 1)) == 0;
1236  if (isPowTwo == false)
1237  return false;
1238 
1239  // The case we'll covert here is op0 being a linear vector with desired
1240  // stride, and where all of the values of op0, when divided by
1241  // maskPlusOne, have the same value.
1242  if (lVectorIsLinear(op0, vectorLength, stride, seenPhis) == false)
1243  return false;
1244 
1245  bool canAdd = true;
1246  bool isMult = lAllDivBaseEqual(op0, maskPlusOne, vectorLength, seenPhis, canAdd);
1247  return isMult;
1248 }
1249 
1250 static bool lVectorIsLinear(llvm::Value *v, int vectorLength, int stride, std::vector<llvm::PHINode *> &seenPhis) {
1251  // First try the easy case: if the values are all just constant
1252  // integers and have the expected stride between them, then we're done.
1253  llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
1254  if (cv != NULL)
1255  return lVectorIsLinearConstantInts(cv, vectorLength, stride);
1256 
1257  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
1258  if (bop != NULL) {
1259  // FIXME: is it right to pass the seenPhis to the all equal check as well??
1260  llvm::Value *op0 = bop->getOperand(0), *op1 = bop->getOperand(1);
1261  if ((bop->getOpcode() == llvm::Instruction::Add) || IsOrEquivalentToAdd(bop)) {
1262  // There are two cases to check if we have an add:
1263  //
1264  // programIndex + unif -> ascending linear seqeuence
1265  // unif + programIndex -> ascending linear sequence
1266  bool l0 = lVectorIsLinear(op0, vectorLength, stride, seenPhis);
1267  bool e1 = lVectorValuesAllEqual(op1, vectorLength, seenPhis);
1268  if (l0 && e1)
1269  return true;
1270 
1271  bool e0 = lVectorValuesAllEqual(op0, vectorLength, seenPhis);
1272  bool l1 = lVectorIsLinear(op1, vectorLength, stride, seenPhis);
1273  return (e0 && l1);
1274  } else if (bop->getOpcode() == llvm::Instruction::Sub)
1275  // For subtraction, we only match:
1276  // programIndex - unif -> ascending linear seqeuence
1277  return (lVectorIsLinear(bop->getOperand(0), vectorLength, stride, seenPhis) &&
1278  lVectorValuesAllEqual(bop->getOperand(1), vectorLength, seenPhis));
1279  else if (bop->getOpcode() == llvm::Instruction::Mul) {
1280  // Multiplies are a bit trickier, so are handled in a separate
1281  // function.
1282  bool m0 = lCheckMulForLinear(op0, op1, vectorLength, stride, seenPhis);
1283  if (m0)
1284  return true;
1285  bool m1 = lCheckMulForLinear(op1, op0, vectorLength, stride, seenPhis);
1286  return m1;
1287  } else if (bop->getOpcode() == llvm::Instruction::Shl) {
1288  // Sometimes multiplies come in as shift lefts (especially in
1289  // LLVM 3.4+).
1290  bool linear = lCheckShlForLinear(op0, op1, vectorLength, stride, seenPhis);
1291  return linear;
1292  } else if (bop->getOpcode() == llvm::Instruction::And) {
1293  // Special case for some AND-related patterns that come up when
1294  // looping over SOA data
1295  bool linear = lCheckAndForLinear(op0, op1, vectorLength, stride, seenPhis);
1296  return linear;
1297  } else
1298  return false;
1299  }
1300 
1301  llvm::CastInst *ci = llvm::dyn_cast<llvm::CastInst>(v);
1302  if (ci != NULL)
1303  return lVectorIsLinear(ci->getOperand(0), vectorLength, stride, seenPhis);
1304 
1305  if (llvm::isa<llvm::CallInst>(v) || llvm::isa<llvm::LoadInst>(v))
1306  return false;
1307 
1308  llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
1309  if (phi != NULL) {
1310  for (unsigned int i = 0; i < seenPhis.size(); ++i)
1311  if (seenPhis[i] == phi)
1312  return true;
1313 
1314  seenPhis.push_back(phi);
1315 
1316  unsigned int numIncoming = phi->getNumIncomingValues();
1317  // Check all of the incoming values: if all of them are all equal,
1318  // then we're good.
1319  for (unsigned int i = 0; i < numIncoming; ++i) {
1320  if (!lVectorIsLinear(phi->getIncomingValue(i), vectorLength, stride, seenPhis)) {
1321  seenPhis.pop_back();
1322  return false;
1323  }
1324  }
1325 
1326  seenPhis.pop_back();
1327  return true;
1328  }
1329 
1330  // TODO: is any reason to worry about these?
1331  if (llvm::isa<llvm::InsertElementInst>(v))
1332  return false;
1333 
1334  // TODO: we could also handle shuffles, but we haven't yet seen any
1335  // cases where doing so would detect cases where actually have a linear
1336  // vector.
1337  llvm::ShuffleVectorInst *shuffle = llvm::dyn_cast<llvm::ShuffleVectorInst>(v);
1338  if (shuffle != NULL)
1339  return false;
1340 
1341 #if 0
1342  fprintf(stderr, "linear check: ");
1343  v->dump();
1344  fprintf(stderr, "\n");
1345  llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
1346  if (inst) {
1347  inst->getParent()->dump();
1348  fprintf(stderr, "\n");
1349  fprintf(stderr, "\n");
1350  }
1351 #endif
1352 
1353  return false;
1354 }
1355 
1356 /** Given vector of integer-typed values, see if the elements of the array
1357  have a step of 'stride' between their values. This function tries to
1358  handle as many possibilities as possible, including things like all
1359  elements equal to some non-constant value plus an integer offset, etc.
1360 */
1361 bool LLVMVectorIsLinear(llvm::Value *v, int stride) {
1362  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(v->getType());
1363  Assert(vt != NULL);
1364  int vectorLength = vt->getNumElements();
1365 
1366  std::vector<llvm::PHINode *> seenPhis;
1367  bool linear = lVectorIsLinear(v, vectorLength, stride, seenPhis);
1368  Debug(SourcePos(), "LLVMVectorIsLinear(%s) -> %s.", v->getName().str().c_str(), linear ? "true" : "false");
1369 #ifndef ISPC_NO_DUMPS
1370  if (g->debugPrint)
1371  LLVMDumpValue(v);
1372 #endif
1373 
1374  return linear;
1375 }
1376 
1377 #ifndef ISPC_NO_DUMPS
1378 static void lDumpValue(llvm::Value *v, std::set<llvm::Value *> &done) {
1379  if (done.find(v) != done.end())
1380  return;
1381 
1382  llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
1383  if (done.size() > 0 && inst == NULL)
1384  return;
1385 
1386  fprintf(stderr, " ");
1387  v->dump();
1388  done.insert(v);
1389 
1390  if (inst == NULL)
1391  return;
1392 
1393  for (unsigned i = 0; i < inst->getNumOperands(); ++i)
1394  lDumpValue(inst->getOperand(i), done);
1395 }
1396 
1397 void LLVMDumpValue(llvm::Value *v) {
1398  std::set<llvm::Value *> done;
1399  lDumpValue(v, done);
1400  fprintf(stderr, "----\n");
1401 }
1402 #endif
1403 
1404 static llvm::Value *lExtractFirstVectorElement(llvm::Value *v, std::map<llvm::PHINode *, llvm::PHINode *> &phiMap) {
1405  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(v->getType());
1406  Assert(vt != NULL);
1407 
1408  // First, handle various constant types; do the extraction manually, as
1409  // appropriate.
1410  if (llvm::isa<llvm::ConstantAggregateZero>(v) == true) {
1411  return llvm::Constant::getNullValue(vt->getElementType());
1412  }
1413  if (llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v)) {
1414  return cv->getOperand(0);
1415  }
1416  if (llvm::ConstantDataVector *cdv = llvm::dyn_cast<llvm::ConstantDataVector>(v))
1417  return cdv->getElementAsConstant(0);
1418 
1419  // Otherwise, all that we should have at this point is an instruction
1420  // of some sort
1421  Assert(llvm::isa<llvm::Constant>(v) == false);
1422  Assert(llvm::isa<llvm::Instruction>(v) == true);
1423 
1424  std::string newName = v->getName().str() + std::string(".elt0");
1425 
1426  // Rewrite regular binary operators and casts to the scalarized
1427  // equivalent.
1428  llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
1429  if (bop != NULL) {
1430  llvm::Value *v0 = lExtractFirstVectorElement(bop->getOperand(0), phiMap);
1431  llvm::Value *v1 = lExtractFirstVectorElement(bop->getOperand(1), phiMap);
1432  Assert(v0 != NULL);
1433  Assert(v1 != NULL);
1434  // Note that the new binary operator is inserted immediately before
1435  // the previous vector one
1436  return llvm::BinaryOperator::Create(bop->getOpcode(), v0, v1, newName, bop);
1437  }
1438 
1439  llvm::CastInst *cast = llvm::dyn_cast<llvm::CastInst>(v);
1440  if (cast != NULL) {
1441  llvm::Value *v = lExtractFirstVectorElement(cast->getOperand(0), phiMap);
1442  // Similarly, the equivalent scalar cast instruction goes right
1443  // before the vector cast
1444  return llvm::CastInst::Create(cast->getOpcode(), v, vt->getElementType(), newName, cast);
1445  }
1446 
1447  llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
1448  if (phi != NULL) {
1449  // For PHI notes, recursively scalarize them.
1450  if (phiMap.find(phi) != phiMap.end())
1451  return phiMap[phi];
1452 
1453  // We need to create the new scalar PHI node immediately, though,
1454  // and put it in the map<>, so that if we come back to this node
1455  // via a recursive lExtractFirstVectorElement() call, then we can
1456  // return the pointer and not get stuck in an infinite loop.
1457  //
1458  // The insertion point for the new phi node also has to be the
1459  // start of the bblock of the original phi node.
1460 
1461  llvm::Instruction *phiInsertPos = &*(phi->getParent()->begin());
1462  llvm::PHINode *scalarPhi =
1463  llvm::PHINode::Create(vt->getElementType(), phi->getNumIncomingValues(), newName, phiInsertPos);
1464  phiMap[phi] = scalarPhi;
1465 
1466  for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
1467  llvm::Value *v = lExtractFirstVectorElement(phi->getIncomingValue(i), phiMap);
1468  scalarPhi->addIncoming(v, phi->getIncomingBlock(i));
1469  }
1470 
1471  return scalarPhi;
1472  }
1473 
1474  // We should consider "shuffle" case and "insertElement" case separately.
1475  // For example we can have shuffle(mul, undef, zero) but function
1476  // "LLVMFlattenInsertChain" can handle only case shuffle(insertElement, undef, zero).
1477  // Also if we have insertElement under shuffle we will handle it the next call of
1478  // "lExtractFirstVectorElement" function.
1479  if (llvm::isa<llvm::ShuffleVectorInst>(v)) {
1480  llvm::ShuffleVectorInst *shuf = llvm::dyn_cast<llvm::ShuffleVectorInst>(v);
1481  llvm::Value *indices = shuf->getOperand(2);
1482  if (llvm::isa<llvm::ConstantAggregateZero>(indices)) {
1483  return lExtractFirstVectorElement(shuf->getOperand(0), phiMap);
1484  }
1485  }
1486 
1487  // If we have a chain of insertelement instructions, then we can just
1488  // flatten them out and grab the value for the first one.
1489  if (llvm::isa<llvm::InsertElementInst>(v)) {
1490  return LLVMFlattenInsertChain(v, vt->getNumElements(), false);
1491  }
1492 
1493  // Worst case, for everything else, just do a regular extract element
1494  // instruction, which we insert immediately after the instruction we
1495  // have here.
1496  llvm::Instruction *insertAfter = llvm::dyn_cast<llvm::Instruction>(v);
1497  Assert(insertAfter != NULL);
1498  llvm::Instruction *ee = llvm::ExtractElementInst::Create(v, LLVMInt32(0), "first_elt", (llvm::Instruction *)NULL);
1499  ee->insertAfter(insertAfter);
1500  return ee;
1501 }
1502 
1503 llvm::Value *LLVMExtractFirstVectorElement(llvm::Value *v) {
1504  std::map<llvm::PHINode *, llvm::PHINode *> phiMap;
1505  llvm::Value *ret = lExtractFirstVectorElement(v, phiMap);
1506  return ret;
1507 }
1508 
1509 /** Given two vectors of the same type, concatenate them into a vector that
1510  has twice as many elements, where the first half has the elements from
1511  the first vector and the second half has the elements from the second
1512  vector.
1513  */
1514 llvm::Value *LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore) {
1515  Assert(v1->getType() == v2->getType());
1516 
1517  llvm::VectorType *vt = llvm::dyn_cast<llvm::VectorType>(v1->getType());
1518  Assert(vt != NULL);
1519 
1520  int32_t identity[ISPC_MAX_NVEC];
1521  int resultSize = 2 * vt->getNumElements();
1522  Assert(resultSize <= ISPC_MAX_NVEC);
1523  for (int i = 0; i < resultSize; ++i)
1524  identity[i] = i;
1525 
1526  return LLVMShuffleVectors(v1, v2, identity, resultSize, insertBefore);
1527 }
1528 
1529 /** Shuffle two vectors together with a ShuffleVectorInst, returning a
1530  vector with shufSize elements, where the shuf[] array offsets are used
1531  to determine which element from the two given vectors is used for each
1532  result element. */
1533 llvm::Value *LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize,
1534  llvm::Instruction *insertBefore) {
1535  std::vector<llvm::Constant *> shufVec;
1536  for (int i = 0; i < shufSize; ++i) {
1537  if (shuf[i] == -1)
1538  shufVec.push_back(llvm::UndefValue::get(LLVMTypes::Int32Type));
1539  else
1540  shufVec.push_back(LLVMInt32(shuf[i]));
1541  }
1542 
1543  llvm::ArrayRef<llvm::Constant *> aref(&shufVec[0], &shufVec[shufSize]);
1544  llvm::Value *vec = llvm::ConstantVector::get(aref);
1545 
1546  return new llvm::ShuffleVectorInst(v1, v2, vec, "shuffle", insertBefore);
1547 }
1548 
1549 const char *LLVMGetName(llvm::Value *v, const char *s) {
1550  if (v == NULL)
1551  return s;
1552  std::string ret = std::string(v->getName());
1553  ret += s;
1554  return strdup(ret.c_str());
1555 }
1556 
1557 const char *LLVMGetName(const char *op, llvm::Value *v1, llvm::Value *v2) {
1558  std::string r = op;
1559  r += "_";
1560  r += v1->getName().str();
1561  r += "_";
1562  r += v2->getName().str();
1563  return strdup(r.c_str());
1564 }
llvm::Constant * LLVMIntAsType(int64_t val, llvm::Type *type)
Definition: llvmutil.cpp:463
static llvm::Type * FloatType
Definition: llvmutil.h:69
static llvm::Type * Int32VectorPointerType
Definition: llvmutil.h:93
llvm::Constant * LLVMUInt64Vector(uint64_t ival)
Definition: llvmutil.cpp:388
static int lRoundUpPow2(int v)
Definition: llvmutil.cpp:798
static bool lIsFirstElementConstVector(llvm::Value *v)
Definition: llvmutil.cpp:569
llvm::Constant * LLVMUInt32Vector(uint32_t ival)
Definition: llvmutil.cpp:328
llvm::ConstantInt * LLVMUInt32(uint32_t ival)
Definition: llvmutil.cpp:237
static bool lVectorIsLinear(llvm::Value *v, int vectorLength, int stride, std::vector< llvm::PHINode *> &seenPhis)
Definition: llvmutil.cpp:1250
static void lDumpValue(llvm::Value *v, std::set< llvm::Value *> &done)
Definition: llvmutil.cpp:1378
llvm::Constant * LLVMBoolVector(bool b)
Definition: llvmutil.cpp:403
static llvm::Type * DoubleType
Definition: llvmutil.h:70
static bool lVectorShiftRightAllEqual(llvm::Value *val, llvm::Value *shift, int vectorLength)
Definition: llvmutil.cpp:934
llvm::ConstantInt * LLVMInt8(int8_t ival)
Definition: llvmutil.cpp:217
llvm::Constant * LLVMInt64Vector(int64_t ival)
Definition: llvmutil.cpp:373
llvm::ConstantInt * LLVMUInt8(uint8_t ival)
Definition: llvmutil.cpp:221
Structure that defines a compilation target.
Definition: ispc.h:149
Target * target
Definition: ispc.h:512
static llvm::VectorType * VoidPointerVectorType
Definition: llvmutil.h:98
static llvm::VectorType * BoolVectorType
Definition: llvmutil.h:81
static bool lIsExactMultiple(llvm::Value *val, int baseValue, int vectorLength, std::vector< llvm::PHINode *> &seenPhis)
Definition: llvmutil.cpp:741
bool LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts)
Definition: llvmutil.cpp:709
const char * LLVMGetName(llvm::Value *v, const char *s)
Definition: llvmutil.cpp:1549
llvm::Constant * LLVMMaskAllOn
Definition: llvmutil.cpp:94
static llvm::Type * BoolType
Definition: llvmutil.h:62
llvm::ConstantInt * LLVMInt16(int16_t ival)
Definition: llvmutil.cpp:225
static llvm::VectorType * Int32VectorType
Definition: llvmutil.h:86
int getMaskBitCount() const
Definition: ispc.h:251
void InitLLVMUtil(llvm::LLVMContext *ctx, Target &target)
Definition: llvmutil.cpp:97
static llvm::VectorType * BoolVectorStorageType
Definition: llvmutil.h:82
llvm::Constant * LLVMFalseInStorage
Definition: llvmutil.cpp:93
static llvm::Type * FloatVectorPointerType
Definition: llvmutil.h:95
static llvm::Type * Int8PointerType
Definition: llvmutil.h:72
static llvm::Type * Int32PointerType
Definition: llvmutil.h:74
llvm::Constant * LLVMUIntAsType(uint64_t val, llvm::Type *type)
Definition: llvmutil.cpp:476
static llvm::Type * Int16VectorPointerType
Definition: llvmutil.h:92
static bool lValuesAreEqual(llvm::Value *v0, llvm::Value *v1, std::vector< llvm::PHINode *> &seenPhi0, std::vector< llvm::PHINode *> &seenPhi1)
Definition: llvmutil.cpp:494
static llvm::Type * Int16Type
Definition: llvmutil.h:66
static llvm::Type * DoubleVectorPointerType
Definition: llvmutil.h:96
static llvm::VectorType * Int1VectorType
Definition: llvmutil.h:83
llvm::Constant * LLVMFloatVector(float fval)
Definition: llvmutil.cpp:343
static llvm::Type * VoidType
Definition: llvmutil.h:59
llvm::Constant * LLVMDoubleVector(double dval)
Definition: llvmutil.cpp:358
static llvm::Type * Int8VectorPointerType
Definition: llvmutil.h:91
bool debugPrint
Definition: ispc.h:536
static llvm::VectorType * Int8VectorType
Definition: llvmutil.h:84
llvm::Constant * LLVMInt32Vector(int32_t ival)
Definition: llvmutil.cpp:313
static bool lAllDivBaseEqual(llvm::Value *val, int64_t baseValue, int vectorLength, std::vector< llvm::PHINode *> &seenPhis, bool &canAdd)
Definition: llvmutil.cpp:815
llvm::Constant * LLVMTrue
Definition: llvmutil.cpp:90
static llvm::VectorType * FloatVectorType
Definition: llvmutil.h:88
static llvm::Type * Int64Type
Definition: llvmutil.h:68
static llvm::Type * Int8Type
Definition: llvmutil.h:65
bool IsOrEquivalentToAdd(llvm::Value *op)
Definition: llvmutil.cpp:1098
static llvm::VectorType * Int64VectorType
Definition: llvmutil.h:87
Header file with declarations for various LLVM utility stuff.
static llvm::Type * Int64PointerType
Definition: llvmutil.h:75
static bool lCheckShlForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride, std::vector< llvm::PHINode *> &seenPhis)
Definition: llvmutil.cpp:1186
llvm::ConstantInt * LLVMUInt64(uint64_t ival)
Definition: llvmutil.cpp:245
llvm::Constant * LLVMBoolVectorInStorage(bool b)
Definition: llvmutil.cpp:446
static bool lCheckMulForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride, std::vector< llvm::PHINode *> &seenPhis)
Definition: llvmutil.cpp:1155
static llvm::Type * FloatPointerType
Definition: llvmutil.h:76
llvm::Constant * LLVMMaskAllOff
Definition: llvmutil.cpp:95
Representation of a range of positions in a source file.
Definition: ispc.h:123
static llvm::Type * Int16PointerType
Definition: llvmutil.h:73
llvm::Value * LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1514
llvm::ConstantInt * LLVMInt32(int32_t ival)
Definition: llvmutil.cpp:233
bool LLVMVectorIsLinear(llvm::Value *v, int stride)
Definition: llvmutil.cpp:1361
llvm::Constant * LLVMUInt8Vector(uint8_t ival)
Definition: llvmutil.cpp:268
static llvm::Type * PointerIntType
Definition: llvmutil.h:61
static llvm::PointerType * VoidPointerType
Definition: llvmutil.h:60
llvm::Constant * LLVMFloat(float fval)
Definition: llvmutil.cpp:249
int getVectorWidth() const
Definition: ispc.h:245
#define FATAL(message)
Definition: util.h:116
llvm::Constant * LLVMDouble(double dval)
Definition: llvmutil.cpp:251
void LLVMDumpValue(llvm::Value *v)
Definition: llvmutil.cpp:1397
static llvm::Type * Int64VectorPointerType
Definition: llvmutil.h:94
static llvm::Type * Int32Type
Definition: llvmutil.h:67
static llvm::Type * DoublePointerType
Definition: llvmutil.h:77
#define ISPC_MAX_NVEC
Definition: ispc.h:69
static llvm::Type * BoolStorageType
Definition: llvmutil.h:63
#define Assert(expr)
Definition: util.h:128
static llvm::Value * lExtractFirstVectorElement(llvm::Value *v, std::map< llvm::PHINode *, llvm::PHINode *> &phiMap)
Definition: llvmutil.cpp:1404
llvm::Constant * LLVMFalse
Definition: llvmutil.cpp:91
llvm::Value * LLVMExtractFirstVectorElement(llvm::Value *v)
Definition: llvmutil.cpp:1503
Globals * g
Definition: ispc.cpp:72
llvm::Constant * LLVMInt16Vector(int16_t ival)
Definition: llvmutil.cpp:283
static llvm::VectorType * MaskType
Definition: llvmutil.h:79
void Debug(SourcePos p, const char *fmt,...)
Definition: util.cpp:366
llvm::ConstantInt * LLVMUInt16(uint16_t ival)
Definition: llvmutil.cpp:229
bool LLVMVectorValuesAllEqual(llvm::Value *v, llvm::Value **splat)
Definition: llvmutil.cpp:1080
static llvm::VectorType * DoubleVectorType
Definition: llvmutil.h:89
llvm::Constant * LLVMInt8Vector(int8_t ival)
Definition: llvmutil.cpp:253
llvm::Constant * LLVMUInt16Vector(uint16_t ival)
Definition: llvmutil.cpp:298
static int64_t lGetIntValue(llvm::Value *offset)
Definition: llvmutil.cpp:560
llvm::ConstantInt * LLVMInt64(int64_t ival)
Definition: llvmutil.cpp:241
static llvm::VectorType * Int16VectorType
Definition: llvmutil.h:85
static bool lVectorValuesAllEqual(llvm::Value *v, int vectorLength, std::vector< llvm::PHINode *> &seenPhis, llvm::Value **splatValue=NULL)
Definition: llvmutil.cpp:963
bool is32Bit() const
Definition: ispc.h:235
llvm::LLVMContext * ctx
Definition: ispc.h:611
Main ispc.header file. Defines Target, Globals and Opt classes.
llvm::Value * LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[], int shufSize, llvm::Instruction *insertBefore)
Definition: llvmutil.cpp:1533
static bool lCheckAndForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, int stride, std::vector< llvm::PHINode *> &seenPhis)
Definition: llvmutil.cpp:1219
static bool lVectorIsLinearConstantInts(llvm::ConstantDataVector *cv, int vectorLength, int stride)
Definition: llvmutil.cpp:1121
File with declarations for classes related to type representation.
llvm::Value * LLVMFlattenInsertChain(llvm::Value *inst, int vectorWidth, bool compare, bool undef, bool searchFirstUndef)
Definition: llvmutil.cpp:587
llvm::Constant * LLVMTrueInStorage
Definition: llvmutil.cpp:92