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