Intel SPMD Program Compiler  1.11.0
expr.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2019, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file expr.cpp
35  @brief Implementations of expression classes
36 */
37 
38 #include "expr.h"
39 #include "ast.h"
40 #include "ctx.h"
41 #include "llvmutil.h"
42 #include "module.h"
43 #include "sym.h"
44 #include "type.h"
45 #include "util.h"
46 #ifndef _MSC_VER
47 #include <inttypes.h>
48 #endif
49 #ifndef PRId64
50 #define PRId64 "lld"
51 #endif
52 #ifndef PRIu64
53 #define PRIu64 "llu"
54 #endif
55 
56 #include <list>
57 #include <set>
58 #include <stdio.h>
59 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
60 #include <llvm/CallingConv.h>
61 #include <llvm/DerivedTypes.h>
62 #include <llvm/Function.h>
63 #include <llvm/Instructions.h>
64 #include <llvm/LLVMContext.h>
65 #include <llvm/Module.h>
66 #include <llvm/Type.h>
67 #else
68 #include <llvm/IR/CallingConv.h>
69 #include <llvm/IR/DerivedTypes.h>
70 #include <llvm/IR/Function.h>
71 #include <llvm/IR/Instructions.h>
72 #include <llvm/IR/LLVMContext.h>
73 #include <llvm/IR/Module.h>
74 #include <llvm/IR/Type.h>
75 #endif
76 #include <llvm/ExecutionEngine/GenericValue.h>
77 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
78 #include <llvm/IR/InstIterator.h>
79 #else
80 #include <llvm/Support/InstIterator.h>
81 #endif
82 
83 /////////////////////////////////////////////////////////////////////////////////////
84 // Expr
85 
86 llvm::Value *Expr::GetLValue(FunctionEmitContext *ctx) const {
87  // Expressions that can't provide an lvalue can just return NULL
88  return NULL;
89 }
90 
91 const Type *Expr::GetLValueType() const {
92  // This also only needs to be overrided by Exprs that implement the
93  // GetLValue() method.
94  return NULL;
95 }
96 
97 llvm::Constant *Expr::GetConstant(const Type *type) const {
98  // The default is failure; just return NULL
99  return NULL;
100 }
101 
103  // Not all expressions can do this, so provide a generally-useful
104  // default implementation.
105  return NULL;
106 }
107 
108 #if 0
109 /** If a conversion from 'fromAtomicType' to 'toAtomicType' may cause lost
110  precision, issue a warning. Don't warn for conversions to bool and
111  conversions between signed and unsigned integers of the same size.
112  */
113 static void
114 lMaybeIssuePrecisionWarning(const AtomicType *toAtomicType,
115  const AtomicType *fromAtomicType,
116  SourcePos pos, const char *errorMsgBase) {
117  switch (toAtomicType->basicType) {
129  if ((int)toAtomicType->basicType < (int)fromAtomicType->basicType &&
130  toAtomicType->basicType != AtomicType::TYPE_BOOL &&
131  !(toAtomicType->basicType == AtomicType::TYPE_INT8 &&
132  fromAtomicType->basicType == AtomicType::TYPE_UINT8) &&
133  !(toAtomicType->basicType == AtomicType::TYPE_INT16 &&
134  fromAtomicType->basicType == AtomicType::TYPE_UINT16) &&
135  !(toAtomicType->basicType == AtomicType::TYPE_INT32 &&
136  fromAtomicType->basicType == AtomicType::TYPE_UINT32) &&
137  !(toAtomicType->basicType == AtomicType::TYPE_INT64 &&
138  fromAtomicType->basicType == AtomicType::TYPE_UINT64))
139  Warning(pos, "Conversion from type \"%s\" to type \"%s\" for %s"
140  " may lose information.",
141  fromAtomicType->GetString().c_str(), toAtomicType->GetString().c_str(),
142  errorMsgBase);
143  break;
144  default:
145  FATAL("logic error in lMaybeIssuePrecisionWarning()");
146  }
147 }
148 #endif
149 
150 ///////////////////////////////////////////////////////////////////////////
151 
152 static Expr *lArrayToPointer(Expr *expr) {
153  Assert(expr != NULL);
154  AssertPos(expr->pos, CastType<ArrayType>(expr->GetType()));
155 
156  Expr *zero = new ConstExpr(AtomicType::UniformInt32, 0, expr->pos);
157  Expr *index = new IndexExpr(expr, zero, expr->pos);
158  Expr *addr = new AddressOfExpr(index, expr->pos);
159  addr = TypeCheck(addr);
160  Assert(addr != NULL);
161  addr = Optimize(addr);
162  Assert(addr != NULL);
163  return addr;
164 }
165 
166 static bool lIsAllIntZeros(Expr *expr) {
167  const Type *type = expr->GetType();
168  if (type == NULL || type->IsIntType() == false)
169  return false;
170 
171  ConstExpr *ce = llvm::dyn_cast<ConstExpr>(expr);
172  if (ce == NULL)
173  return false;
174 
175  uint64_t vals[ISPC_MAX_NVEC];
176  int count = ce->GetValues(vals);
177  if (count == 1)
178  return (vals[0] == 0);
179  else {
180  for (int i = 0; i < count; ++i)
181  if (vals[i] != 0)
182  return false;
183  }
184  return true;
185 }
186 
187 static bool lDoTypeConv(const Type *fromType, const Type *toType, Expr **expr, bool failureOk, const char *errorMsgBase,
188  SourcePos pos) {
189  /* This function is way too long and complex. Is type conversion stuff
190  always this messy, or can this be cleaned up somehow? */
191  AssertPos(pos, failureOk || errorMsgBase != NULL);
192 
193  if (toType == NULL || fromType == NULL)
194  return false;
195 
196  // The types are equal; there's nothing to do
197  if (Type::Equal(toType, fromType))
198  return true;
199 
200  if (fromType->IsVoidType()) {
201  if (!failureOk)
202  Error(pos, "Can't convert from \"void\" to \"%s\" for %s.", toType->GetString().c_str(), errorMsgBase);
203  return false;
204  }
205 
206  if (toType->IsVoidType()) {
207  if (!failureOk)
208  Error(pos, "Can't convert type \"%s\" to \"void\" for %s.", fromType->GetString().c_str(), errorMsgBase);
209  return false;
210  }
211 
212  if (CastType<FunctionType>(fromType)) {
213  if (CastType<PointerType>(toType) != NULL) {
214  // Convert function type to pointer to function type
215  if (expr != NULL) {
216  Expr *aoe = new AddressOfExpr(*expr, (*expr)->pos);
217  if (lDoTypeConv(aoe->GetType(), toType, &aoe, failureOk, errorMsgBase, pos)) {
218  *expr = aoe;
219  return true;
220  }
221  } else
222  return lDoTypeConv(PointerType::GetUniform(fromType), toType, NULL, failureOk, errorMsgBase, pos);
223  } else {
224  if (!failureOk)
225  Error(pos, "Can't convert function type \"%s\" to \"%s\" for %s.", fromType->GetString().c_str(),
226  toType->GetString().c_str(), errorMsgBase);
227  return false;
228  }
229  }
230  if (CastType<FunctionType>(toType)) {
231  if (!failureOk)
232  Error(pos,
233  "Can't convert from type \"%s\" to function type \"%s\" "
234  "for %s.",
235  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
236  return false;
237  }
238 
239  if ((toType->GetSOAWidth() > 0 || fromType->GetSOAWidth() > 0) &&
240  Type::Equal(toType->GetAsUniformType(), fromType->GetAsUniformType()) &&
241  toType->GetSOAWidth() != fromType->GetSOAWidth()) {
242  if (!failureOk)
243  Error(pos,
244  "Can't convert between types \"%s\" and \"%s\" with "
245  "different SOA widths for %s.",
246  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
247  return false;
248  }
249 
250  const ArrayType *toArrayType = CastType<ArrayType>(toType);
251  const ArrayType *fromArrayType = CastType<ArrayType>(fromType);
252  const VectorType *toVectorType = CastType<VectorType>(toType);
253  const VectorType *fromVectorType = CastType<VectorType>(fromType);
254  const StructType *toStructType = CastType<StructType>(toType);
255  const StructType *fromStructType = CastType<StructType>(fromType);
256  const EnumType *toEnumType = CastType<EnumType>(toType);
257  const EnumType *fromEnumType = CastType<EnumType>(fromType);
258  const AtomicType *toAtomicType = CastType<AtomicType>(toType);
259  const AtomicType *fromAtomicType = CastType<AtomicType>(fromType);
260  const PointerType *fromPointerType = CastType<PointerType>(fromType);
261  const PointerType *toPointerType = CastType<PointerType>(toType);
262 
263  // Do this early, since for the case of a conversion like
264  // "float foo[10]" -> "float * uniform foo", we have what's seemingly
265  // a varying to uniform conversion (but not really)
266  if (fromArrayType != NULL && toPointerType != NULL) {
267  // can convert any array to a void pointer (both uniform and
268  // varying).
269  if (PointerType::IsVoidPointer(toPointerType))
270  goto typecast_ok;
271 
272  // array to pointer to array element type
273  const Type *eltType = fromArrayType->GetElementType();
274  if (toPointerType->GetBaseType()->IsConstType())
275  eltType = eltType->GetAsConstType();
276 
277  PointerType pt(eltType, toPointerType->GetVariability(), toPointerType->IsConstType());
278  if (Type::Equal(toPointerType, &pt))
279  goto typecast_ok;
280  else {
281  if (!failureOk)
282  Error(pos,
283  "Can't convert from incompatible array type \"%s\" "
284  "to pointer type \"%s\" for %s.",
285  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
286  return false;
287  }
288  }
289 
290  if (toType->IsUniformType() && fromType->IsVaryingType()) {
291  if (!failureOk)
292  Error(pos, "Can't convert from type \"%s\" to type \"%s\" for %s.", fromType->GetString().c_str(),
293  toType->GetString().c_str(), errorMsgBase);
294  return false;
295  }
296 
297  if (fromPointerType != NULL) {
298  if (CastType<AtomicType>(toType) != NULL && toType->IsBoolType())
299  // Allow implicit conversion of pointers to bools
300  goto typecast_ok;
301 
302  if (toArrayType != NULL && Type::Equal(fromType->GetBaseType(), toArrayType->GetElementType())) {
303  // Can convert pointers to arrays of the same type
304  goto typecast_ok;
305  }
306  if (toPointerType == NULL) {
307  if (!failureOk)
308  Error(pos,
309  "Can't convert between from pointer type "
310  "\"%s\" to non-pointer type \"%s\" for %s.",
311  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
312  return false;
313  } else if (fromPointerType->IsSlice() == true && toPointerType->IsSlice() == false) {
314  if (!failureOk)
315  Error(pos,
316  "Can't convert from pointer to SOA type "
317  "\"%s\" to pointer to non-SOA type \"%s\" for %s.",
318  fromPointerType->GetAsNonSlice()->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
319  return false;
320  } else if (PointerType::IsVoidPointer(toPointerType)) {
321  if (fromPointerType->GetBaseType()->IsConstType() && !(toPointerType->GetBaseType()->IsConstType())) {
322  if (!failureOk)
323  Error(pos, "Can't convert pointer to const \"%s\" to void pointer.",
324  fromPointerType->GetString().c_str());
325  return false;
326  }
327  // any pointer type can be converted to a void *
328  // ...almost. #731
329  goto typecast_ok;
330  } else if (PointerType::IsVoidPointer(fromPointerType) && expr != NULL &&
331  llvm::dyn_cast<NullPointerExpr>(*expr) != NULL) {
332  // and a NULL convert to any other pointer type
333  goto typecast_ok;
334  } else if (!Type::Equal(fromPointerType->GetBaseType(), toPointerType->GetBaseType()) &&
335  !Type::Equal(fromPointerType->GetBaseType()->GetAsConstType(), toPointerType->GetBaseType())) {
336  if (!failureOk)
337  Error(pos,
338  "Can't convert from pointer type \"%s\" to "
339  "incompatible pointer type \"%s\" for %s.",
340  fromPointerType->GetString().c_str(), toPointerType->GetString().c_str(), errorMsgBase);
341  return false;
342  }
343 
344  if (toType->IsVaryingType() && fromType->IsUniformType())
345  goto typecast_ok;
346 
347  if (toPointerType->IsSlice() == true && fromPointerType->IsSlice() == false)
348  goto typecast_ok;
349 
350  // Otherwise there's nothing to do
351  return true;
352  }
353 
354  if (toPointerType != NULL && fromAtomicType != NULL && fromAtomicType->IsIntType() && expr != NULL &&
355  lIsAllIntZeros(*expr)) {
356  // We have a zero-valued integer expression, which can also be
357  // treated as a NULL pointer that can be converted to any other
358  // pointer type.
359  Expr *npe = new NullPointerExpr(pos);
360  if (lDoTypeConv(PointerType::Void, toType, &npe, failureOk, errorMsgBase, pos)) {
361  *expr = npe;
362  return true;
363  }
364  return false;
365  }
366 
367  // Need to check this early, since otherwise the [sic] "unbound"
368  // variability of SOA struct types causes things to get messy if that
369  // hasn't been detected...
370  if (toStructType && fromStructType && (toStructType->GetSOAWidth() != fromStructType->GetSOAWidth())) {
371  if (!failureOk)
372  Error(pos,
373  "Can't convert between incompatible struct types \"%s\" "
374  "and \"%s\" for %s.",
375  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
376  return false;
377  }
378 
379  // Convert from type T -> const T; just return a TypeCast expr, which
380  // can handle this
381  if (Type::EqualIgnoringConst(toType, fromType) && toType->IsConstType() == true && fromType->IsConstType() == false)
382  goto typecast_ok;
383 
384  if (CastType<ReferenceType>(fromType)) {
385  if (CastType<ReferenceType>(toType)) {
386  // Convert from a reference to a type to a const reference to a type;
387  // this is handled by TypeCastExpr
388  if (Type::Equal(toType->GetReferenceTarget(), fromType->GetReferenceTarget()->GetAsConstType()))
389  goto typecast_ok;
390 
391  const ArrayType *atFrom = CastType<ArrayType>(fromType->GetReferenceTarget());
392  const ArrayType *atTo = CastType<ArrayType>(toType->GetReferenceTarget());
393 
394  if (atFrom != NULL && atTo != NULL && Type::Equal(atFrom->GetElementType(), atTo->GetElementType())) {
395  goto typecast_ok;
396  } else {
397  if (!failureOk)
398  Error(pos,
399  "Can't convert between incompatible reference types \"%s\" "
400  "and \"%s\" for %s.",
401  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
402  return false;
403  }
404  } else {
405  // convert from a reference T -> T
406  if (expr != NULL) {
407  Expr *drExpr = new RefDerefExpr(*expr, pos);
408  if (lDoTypeConv(drExpr->GetType(), toType, &drExpr, failureOk, errorMsgBase, pos) == true) {
409  *expr = drExpr;
410  return true;
411  }
412  return false;
413  } else
414  return lDoTypeConv(fromType->GetReferenceTarget(), toType, NULL, failureOk, errorMsgBase, pos);
415  }
416  } else if (CastType<ReferenceType>(toType)) {
417  // T -> reference T
418  if (expr != NULL) {
419  Expr *rExpr = new ReferenceExpr(*expr, pos);
420  if (lDoTypeConv(rExpr->GetType(), toType, &rExpr, failureOk, errorMsgBase, pos) == true) {
421  *expr = rExpr;
422  return true;
423  }
424  return false;
425  } else {
426  ReferenceType rt(fromType);
427  return lDoTypeConv(&rt, toType, NULL, failureOk, errorMsgBase, pos);
428  }
429  } else if (Type::Equal(toType, fromType->GetAsNonConstType()))
430  // convert: const T -> T (as long as T isn't a reference)
431  goto typecast_ok;
432 
433  fromType = fromType->GetReferenceTarget();
434  toType = toType->GetReferenceTarget();
435  if (toArrayType && fromArrayType) {
436  if (Type::Equal(toArrayType->GetElementType(), fromArrayType->GetElementType())) {
437  // the case of different element counts should have returned
438  // successfully earlier, yes??
439  AssertPos(pos, toArrayType->GetElementCount() != fromArrayType->GetElementCount());
440  goto typecast_ok;
441  } else if (Type::Equal(toArrayType->GetElementType(), fromArrayType->GetElementType()->GetAsConstType())) {
442  // T[x] -> const T[x]
443  goto typecast_ok;
444  } else {
445  if (!failureOk)
446  Error(pos, "Array type \"%s\" can't be converted to type \"%s\" for %s.", fromType->GetString().c_str(),
447  toType->GetString().c_str(), errorMsgBase);
448  return false;
449  }
450  }
451 
452  if (toVectorType && fromVectorType) {
453  // converting e.g. int<n> -> float<n>
454  if (fromVectorType->GetElementCount() != toVectorType->GetElementCount()) {
455  if (!failureOk)
456  Error(pos,
457  "Can't convert between differently sized vector types "
458  "\"%s\" -> \"%s\" for %s.",
459  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
460  return false;
461  }
462  goto typecast_ok;
463  }
464 
465  if (toStructType && fromStructType) {
466  if (!Type::Equal(toStructType->GetAsUniformType()->GetAsConstType(),
467  fromStructType->GetAsUniformType()->GetAsConstType())) {
468  if (!failureOk)
469  Error(pos,
470  "Can't convert between different struct types "
471  "\"%s\" and \"%s\" for %s.",
472  fromStructType->GetString().c_str(), toStructType->GetString().c_str(), errorMsgBase);
473  return false;
474  }
475  goto typecast_ok;
476  }
477 
478  if (toEnumType != NULL && fromEnumType != NULL) {
479  // No implicit conversions between different enum types
480  if (!Type::EqualIgnoringConst(toEnumType->GetAsUniformType(), fromEnumType->GetAsUniformType())) {
481  if (!failureOk)
482  Error(pos,
483  "Can't convert between different enum types "
484  "\"%s\" and \"%s\" for %s",
485  fromEnumType->GetString().c_str(), toEnumType->GetString().c_str(), errorMsgBase);
486  return false;
487  }
488  goto typecast_ok;
489  }
490 
491  // enum -> atomic (integer, generally...) is always ok
492  if (fromEnumType != NULL) {
493  // Cannot convert to anything other than atomic
494  if (toAtomicType == NULL && toVectorType == NULL) {
495  if (!failureOk)
496  Error(pos,
497  "Type conversion from \"%s\" to \"%s\" for %s is not "
498  "possible.",
499  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
500  return false;
501  }
502  goto typecast_ok;
503  }
504 
505  // from here on out, the from type can only be atomic something or
506  // other...
507  if (fromAtomicType == NULL) {
508  if (!failureOk)
509  Error(pos,
510  "Type conversion from \"%s\" to \"%s\" for %s is not "
511  "possible.",
512  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
513  return false;
514  }
515 
516  // scalar -> short-vector conversions
517  if (toVectorType != NULL && (fromType->GetSOAWidth() == toType->GetSOAWidth()))
518  goto typecast_ok;
519 
520  // ok, it better be a scalar->scalar conversion of some sort by now
521  if (toAtomicType == NULL) {
522  if (!failureOk)
523  Error(pos,
524  "Type conversion from \"%s\" to \"%s\" for %s is "
525  "not possible",
526  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
527  return false;
528  }
529 
530  if (fromType->GetSOAWidth() != toType->GetSOAWidth()) {
531  if (!failureOk)
532  Error(pos,
533  "Can't convert between types \"%s\" and \"%s\" with "
534  "different SOA widths for %s.",
535  fromType->GetString().c_str(), toType->GetString().c_str(), errorMsgBase);
536  return false;
537  }
538 
539 typecast_ok:
540  if (expr != NULL)
541  *expr = new TypeCastExpr(toType, *expr, pos);
542  return true;
543 }
544 
545 bool CanConvertTypes(const Type *fromType, const Type *toType, const char *errorMsgBase, SourcePos pos) {
546  return lDoTypeConv(fromType, toType, NULL, errorMsgBase == NULL, errorMsgBase, pos);
547 }
548 
549 Expr *TypeConvertExpr(Expr *expr, const Type *toType, const char *errorMsgBase) {
550  if (expr == NULL)
551  return NULL;
552 
553 #if 0
554  Debug(expr->pos, "type convert %s -> %s.", expr->GetType()->GetString().c_str(),
555  toType->GetString().c_str());
556 #endif
557 
558  const Type *fromType = expr->GetType();
559  Expr *e = expr;
560  if (lDoTypeConv(fromType, toType, &e, false, errorMsgBase, expr->pos))
561  return e;
562  else
563  return NULL;
564 }
565 
566 bool PossiblyResolveFunctionOverloads(Expr *expr, const Type *type) {
567  FunctionSymbolExpr *fse = NULL;
568  const FunctionType *funcType = NULL;
569  if (CastType<PointerType>(type) != NULL && (funcType = CastType<FunctionType>(type->GetBaseType())) &&
570  (fse = llvm::dyn_cast<FunctionSymbolExpr>(expr)) != NULL) {
571  // We're initializing a function pointer with a function symbol,
572  // which in turn may represent an overloaded function. So we need
573  // to try to resolve the overload based on the type of the symbol
574  // we're initializing here.
575  std::vector<const Type *> paramTypes;
576  for (int i = 0; i < funcType->GetNumParameters(); ++i)
577  paramTypes.push_back(funcType->GetParameterType(i));
578 
579  if (fse->ResolveOverloads(expr->pos, paramTypes) == false)
580  return false;
581  }
582  return true;
583 }
584 
585 /** Utility routine that emits code to initialize a symbol given an
586  initializer expression.
587 
588  @param ptr Memory location of storage for the symbol's data
589  @param symName Name of symbol (used in error messages)
590  @param symType Type of variable being initialized
591  @param initExpr Expression for the initializer
592  @param ctx FunctionEmitContext to use for generating instructions
593  @param pos Source file position of the variable being initialized
594 */
595 void InitSymbol(llvm::Value *ptr, const Type *symType, Expr *initExpr, FunctionEmitContext *ctx, SourcePos pos) {
596  if (initExpr == NULL)
597  // leave it uninitialized
598  return;
599 
600  // See if we have a constant initializer a this point
601  llvm::Constant *constValue = initExpr->GetConstant(symType);
602  if (constValue != NULL) {
603  // It'd be nice if we could just do a StoreInst(constValue, ptr)
604  // at this point, but unfortunately that doesn't generate great
605  // code (e.g. a bunch of scalar moves for a constant array.) So
606  // instead we'll make a constant static global that holds the
607  // constant value and emit a memcpy to put its value into the
608  // pointer we have.
609  llvm::Type *llvmType = symType->LLVMType(g->ctx);
610  if (llvmType == NULL) {
611  AssertPos(pos, m->errorCount > 0);
612  return;
613  }
614 
615  if (Type::IsBasicType(symType))
616  ctx->StoreInst(constValue, ptr);
617  else {
618  llvm::Value *constPtr =
619  new llvm::GlobalVariable(*m->module, llvmType, true /* const */, llvm::GlobalValue::InternalLinkage,
620  constValue, "const_initializer");
621  llvm::Value *size = g->target->SizeOf(llvmType, ctx->GetCurrentBasicBlock());
622  ctx->MemcpyInst(ptr, constPtr, size);
623  }
624 
625  return;
626  }
627 
628  // If the initializer is a straight up expression that isn't an
629  // ExprList, then we'll see if we can type convert it to the type of
630  // the variable.
631  if (llvm::dyn_cast<ExprList>(initExpr) == NULL) {
632  if (PossiblyResolveFunctionOverloads(initExpr, symType) == false)
633  return;
634  initExpr = TypeConvertExpr(initExpr, symType, "initializer");
635 
636  if (initExpr == NULL)
637  return;
638 
639  llvm::Value *initializerValue = initExpr->GetValue(ctx);
640  if (initializerValue != NULL)
641  // Bingo; store the value in the variable's storage
642  ctx->StoreInst(initializerValue, ptr);
643  return;
644  }
645 
646  // Atomic types and enums can be initialized with { ... } initializer
647  // expressions if they have a single element (except for SOA types,
648  // which are handled below).
649  if (symType->IsSOAType() == false && Type::IsBasicType(symType)) {
650  ExprList *elist = llvm::dyn_cast<ExprList>(initExpr);
651  if (elist != NULL) {
652  if (elist->exprs.size() == 1)
653  InitSymbol(ptr, symType, elist->exprs[0], ctx, pos);
654  else
655  Error(initExpr->pos,
656  "Expression list initializers with "
657  "multiple values can't be used with type \"%s\".",
658  symType->GetString().c_str());
659  }
660  return;
661  }
662 
663  const ReferenceType *rt = CastType<ReferenceType>(symType);
664  if (rt) {
665  if (!Type::Equal(initExpr->GetType(), rt)) {
666  Error(initExpr->pos,
667  "Initializer for reference type \"%s\" must have same "
668  "reference type itself. \"%s\" is incompatible.",
669  rt->GetString().c_str(), initExpr->GetType()->GetString().c_str());
670  return;
671  }
672 
673  llvm::Value *initializerValue = initExpr->GetValue(ctx);
674  if (initializerValue)
675  ctx->StoreInst(initializerValue, ptr);
676  return;
677  }
678 
679  // Handle initiailizers for SOA types as well as for structs, arrays,
680  // and vectors.
681  const CollectionType *collectionType = CastType<CollectionType>(symType);
682  if (collectionType != NULL || symType->IsSOAType()) {
683  int nElements = collectionType ? collectionType->GetElementCount() : symType->GetSOAWidth();
684 
685  std::string name;
686  if (CastType<StructType>(symType) != NULL)
687  name = "struct";
688  else if (CastType<ArrayType>(symType) != NULL)
689  name = "array";
690  else if (CastType<VectorType>(symType) != NULL)
691  name = "vector";
692  else if (symType->IsSOAType())
693  name = symType->GetVariability().GetString();
694  else
695  FATAL("Unexpected CollectionType in InitSymbol()");
696 
697  // There are two cases for initializing these types; either a
698  // single initializer may be provided (float foo[3] = 0;), in which
699  // case all of the elements are initialized to the given value, or
700  // an initializer list may be provided (float foo[3] = { 1,2,3 }),
701  // in which case the elements are initialized with the
702  // corresponding values.
703  ExprList *exprList = llvm::dyn_cast<ExprList>(initExpr);
704  if (exprList != NULL) {
705  // The { ... } case; make sure we have the no more expressions
706  // in the ExprList as we have struct members
707  int nInits = exprList->exprs.size();
708  if (nInits > nElements) {
709  Error(initExpr->pos,
710  "Initializer for %s type \"%s\" requires "
711  "no more than %d values; %d provided.",
712  name.c_str(), symType->GetString().c_str(), nElements, nInits);
713  return;
714  }
715 
716  // Initialize each element with the corresponding value from
717  // the ExprList
718  for (int i = 0; i < nElements; ++i) {
719  // For SOA types, the element type is the uniform variant
720  // of the underlying type
721  const Type *elementType =
722  collectionType ? collectionType->GetElementType(i) : symType->GetAsUniformType();
723  if (elementType == NULL) {
724  AssertPos(pos, m->errorCount > 0);
725  return;
726  }
727 
728  llvm::Value *ep;
729  if (CastType<StructType>(symType) != NULL)
730  ep = ctx->AddElementOffset(ptr, i, NULL, "element");
731  else
732  ep = ctx->GetElementPtrInst(ptr, LLVMInt32(0), LLVMInt32(i), PointerType::GetUniform(elementType),
733  "gep");
734 
735  if (i < nInits)
736  InitSymbol(ep, elementType, exprList->exprs[i], ctx, pos);
737  else {
738  // If we don't have enough initializer values, initialize the
739  // rest as zero.
740  llvm::Type *llvmType = elementType->LLVMType(g->ctx);
741  if (llvmType == NULL) {
742  AssertPos(pos, m->errorCount > 0);
743  return;
744  }
745 
746  llvm::Constant *zeroInit = llvm::Constant::getNullValue(llvmType);
747  ctx->StoreInst(zeroInit, ep);
748  }
749  }
750  } else
751  Error(initExpr->pos, "Can't assign type \"%s\" to \"%s\".", initExpr->GetType()->GetString().c_str(),
752  collectionType->GetString().c_str());
753  return;
754  }
755 
756  FATAL("Unexpected Type in InitSymbol()");
757 }
758 
759 ///////////////////////////////////////////////////////////////////////////
760 
761 /** Given an atomic or vector type, this returns a boolean type with the
762  same "shape". In other words, if the given type is a vector type of
763  three uniform ints, the returned type is a vector type of three uniform
764  bools. */
765 static const Type *lMatchingBoolType(const Type *type) {
766  bool uniformTest = type->IsUniformType();
767  const AtomicType *boolBase = uniformTest ? AtomicType::UniformBool : AtomicType::VaryingBool;
768  const VectorType *vt = CastType<VectorType>(type);
769  if (vt != NULL)
770  return new VectorType(boolBase, vt->GetElementCount());
771  else {
772  Assert(Type::IsBasicType(type));
773  return boolBase;
774  }
775 }
776 
777 ///////////////////////////////////////////////////////////////////////////
778 // UnaryExpr
779 
780 static llvm::Constant *lLLVMConstantValue(const Type *type, llvm::LLVMContext *ctx, double value) {
781  const AtomicType *atomicType = CastType<AtomicType>(type);
782  const EnumType *enumType = CastType<EnumType>(type);
783  const VectorType *vectorType = CastType<VectorType>(type);
784  const PointerType *pointerType = CastType<PointerType>(type);
785 
786  // This function is only called with, and only works for atomic, enum,
787  // and vector types.
788  Assert(atomicType != NULL || enumType != NULL || vectorType != NULL || pointerType != NULL);
789 
790  if (atomicType != NULL || enumType != NULL) {
791  // If it's an atomic or enuemrator type, then figure out which of
792  // the llvmutil.h functions to call to get the corresponding
793  // constant and then call it...
794  bool isUniform = type->IsUniformType();
795  AtomicType::BasicType basicType = (enumType != NULL) ? AtomicType::TYPE_UINT32 : atomicType->basicType;
796 
797  switch (basicType) {
799  FATAL("can't get constant value for void type");
800  return NULL;
802  if (isUniform)
803  return (value != 0.) ? LLVMTrue : LLVMFalse;
804  else
805  return LLVMBoolVector(value != 0.);
806  case AtomicType::TYPE_INT8: {
807  int i = (int)value;
808  Assert((double)i == value);
809  return isUniform ? LLVMInt8(i) : LLVMInt8Vector(i);
810  }
811  case AtomicType::TYPE_UINT8: {
812  unsigned int i = (unsigned int)value;
813  return isUniform ? LLVMUInt8(i) : LLVMUInt8Vector(i);
814  }
815  case AtomicType::TYPE_INT16: {
816  int i = (int)value;
817  Assert((double)i == value);
818  return isUniform ? LLVMInt16(i) : LLVMInt16Vector(i);
819  }
821  unsigned int i = (unsigned int)value;
822  return isUniform ? LLVMUInt16(i) : LLVMUInt16Vector(i);
823  }
824  case AtomicType::TYPE_INT32: {
825  int i = (int)value;
826  Assert((double)i == value);
827  return isUniform ? LLVMInt32(i) : LLVMInt32Vector(i);
828  }
830  unsigned int i = (unsigned int)value;
831  return isUniform ? LLVMUInt32(i) : LLVMUInt32Vector(i);
832  }
834  return isUniform ? LLVMFloat((float)value) : LLVMFloatVector((float)value);
836  uint64_t i = (uint64_t)value;
837  Assert(value == (int64_t)i);
838  return isUniform ? LLVMUInt64(i) : LLVMUInt64Vector(i);
839  }
840  case AtomicType::TYPE_INT64: {
841  int64_t i = (int64_t)value;
842  Assert((double)i == value);
843  return isUniform ? LLVMInt64(i) : LLVMInt64Vector(i);
844  }
846  return isUniform ? LLVMDouble(value) : LLVMDoubleVector(value);
847  default:
848  FATAL("logic error in lLLVMConstantValue");
849  return NULL;
850  }
851  } else if (pointerType != NULL) {
852  Assert(value == 0);
853  if (pointerType->IsUniformType())
854  return llvm::Constant::getNullValue(LLVMTypes::VoidPointerType);
855  else
856  return llvm::Constant::getNullValue(LLVMTypes::VoidPointerVectorType);
857  } else {
858  // For vector types, first get the LLVM constant for the basetype with
859  // a recursive call to lLLVMConstantValue().
860  const Type *baseType = vectorType->GetBaseType();
861  llvm::Constant *constElement = lLLVMConstantValue(baseType, ctx, value);
862  llvm::Type *llvmVectorType = vectorType->LLVMType(ctx);
863 
864  // Now create a constant version of the corresponding LLVM type that we
865  // use to represent the VectorType.
866  // FIXME: this is a little ugly in that the fact that ispc represents
867  // uniform VectorTypes as LLVM VectorTypes and varying VectorTypes as
868  // LLVM ArrayTypes leaks into the code here; it feels like this detail
869  // should be better encapsulated?
870  if (baseType->IsUniformType()) {
871  llvm::VectorType *lvt = llvm::dyn_cast<llvm::VectorType>(llvmVectorType);
872  Assert(lvt != NULL);
873  std::vector<llvm::Constant *> vals;
874  for (unsigned int i = 0; i < lvt->getNumElements(); ++i)
875  vals.push_back(constElement);
876  return llvm::ConstantVector::get(vals);
877  } else {
878  llvm::ArrayType *lat = llvm::dyn_cast<llvm::ArrayType>(llvmVectorType);
879  Assert(lat != NULL);
880  std::vector<llvm::Constant *> vals;
881  for (unsigned int i = 0; i < lat->getNumElements(); ++i)
882  vals.push_back(constElement);
883  return llvm::ConstantArray::get(lat, vals);
884  }
885  }
886 }
887 
888 static llvm::Value *lMaskForSymbol(Symbol *baseSym, FunctionEmitContext *ctx) {
889  if (baseSym == NULL)
890  return ctx->GetFullMask();
891 
892  if (CastType<PointerType>(baseSym->type) != NULL || CastType<ReferenceType>(baseSym->type) != NULL)
893  // FIXME: for pointers, we really only want to do this for
894  // dereferencing the pointer, not for things like pointer
895  // arithmetic, when we may be able to use the internal mask,
896  // depending on context...
897  return ctx->GetFullMask();
898 
899  llvm::Value *mask = (baseSym->parentFunction == ctx->GetFunction() && baseSym->storageClass != SC_STATIC)
900  ? ctx->GetInternalMask()
901  : ctx->GetFullMask();
902  return mask;
903 }
904 
905 /** Store the result of an assignment to the given location.
906  */
907 static void lStoreAssignResult(llvm::Value *value, llvm::Value *ptr, const Type *valueType, const Type *ptrType,
908  FunctionEmitContext *ctx, Symbol *baseSym) {
909  Assert(baseSym == NULL || baseSym->varyingCFDepth <= ctx->VaryingCFDepth());
910  if (!g->opt.disableMaskedStoreToStore && !g->opt.disableMaskAllOnOptimizations && baseSym != NULL &&
911  baseSym->varyingCFDepth == ctx->VaryingCFDepth() && baseSym->storageClass != SC_STATIC &&
912  CastType<ReferenceType>(baseSym->type) == NULL && CastType<PointerType>(baseSym->type) == NULL) {
913  // If the variable is declared at the same varying control flow
914  // depth as where it's being assigned, then we don't need to do any
915  // masking but can just do the assignment as if all the lanes were
916  // known to be on. While this may lead to random/garbage values
917  // written into the lanes that are off, by definition they will
918  // never be accessed, since those lanes aren't executing, and won't
919  // be executing at this scope or any other one before the variable
920  // goes out of scope.
921  ctx->StoreInst(value, ptr, LLVMMaskAllOn, valueType, ptrType);
922  } else {
923  ctx->StoreInst(value, ptr, lMaskForSymbol(baseSym, ctx), valueType, ptrType);
924  }
925 }
926 
927 /** Utility routine to emit code to do a {pre,post}-{inc,dec}rement of the
928  given expresion.
929  */
930 static llvm::Value *lEmitPrePostIncDec(UnaryExpr::Op op, Expr *expr, SourcePos pos, FunctionEmitContext *ctx) {
931  const Type *type = expr->GetType();
932  if (type == NULL)
933  return NULL;
934 
935  // Get both the lvalue and the rvalue of the given expression
936  llvm::Value *lvalue = NULL, *rvalue = NULL;
937  const Type *lvalueType = NULL;
938  if (CastType<ReferenceType>(type) != NULL) {
939  lvalueType = type;
940  type = type->GetReferenceTarget();
941  lvalue = expr->GetValue(ctx);
942 
943  Expr *deref = new RefDerefExpr(expr, expr->pos);
944  rvalue = deref->GetValue(ctx);
945  } else {
946  lvalue = expr->GetLValue(ctx);
947  lvalueType = expr->GetLValueType();
948  rvalue = expr->GetValue(ctx);
949  }
950 
951  if (lvalue == NULL) {
952  // If we can't get a lvalue, then we have an error here
953  const char *prepost = (op == UnaryExpr::PreInc || op == UnaryExpr::PreDec) ? "pre" : "post";
954  const char *incdec = (op == UnaryExpr::PreInc || op == UnaryExpr::PostInc) ? "increment" : "decrement";
955  Error(pos, "Can't %s-%s non-lvalues.", prepost, incdec);
956  return NULL;
957  }
958 
959  // Emit code to do the appropriate addition/subtraction to the
960  // expression's old value
961  ctx->SetDebugPos(pos);
962  llvm::Value *binop = NULL;
963  int delta = (op == UnaryExpr::PreInc || op == UnaryExpr::PostInc) ? 1 : -1;
964 
965  std::string opName = rvalue->getName().str();
966  if (op == UnaryExpr::PreInc || op == UnaryExpr::PostInc)
967  opName += "_plus1";
968  else
969  opName += "_minus1";
970 
971  if (CastType<PointerType>(type) != NULL) {
973  llvm::Constant *dval = lLLVMConstantValue(incType, g->ctx, delta);
974  binop = ctx->GetElementPtrInst(rvalue, dval, type, opName.c_str());
975  } else {
976  llvm::Constant *dval = lLLVMConstantValue(type, g->ctx, delta);
977  if (type->IsFloatType())
978  binop = ctx->BinaryOperator(llvm::Instruction::FAdd, rvalue, dval, opName.c_str());
979  else
980  binop = ctx->BinaryOperator(llvm::Instruction::Add, rvalue, dval, opName.c_str());
981  }
982 
983  // And store the result out to the lvalue
984  Symbol *baseSym = expr->GetBaseSymbol();
985  lStoreAssignResult(binop, lvalue, type, lvalueType, ctx, baseSym);
986 
987  // And then if it's a pre increment/decrement, return the final
988  // computed result; otherwise return the previously-grabbed expression
989  // value.
990  return (op == UnaryExpr::PreInc || op == UnaryExpr::PreDec) ? binop : rvalue;
991 }
992 
993 /** Utility routine to emit code to negate the given expression.
994  */
995 static llvm::Value *lEmitNegate(Expr *arg, SourcePos pos, FunctionEmitContext *ctx) {
996  const Type *type = arg->GetType();
997  llvm::Value *argVal = arg->GetValue(ctx);
998  if (type == NULL || argVal == NULL)
999  return NULL;
1000 
1001  // Negate by subtracting from zero...
1002  llvm::Value *zero = lLLVMConstantValue(type, g->ctx, 0.);
1003  ctx->SetDebugPos(pos);
1004  if (type->IsFloatType())
1005  return ctx->BinaryOperator(llvm::Instruction::FSub, zero, argVal, LLVMGetName(argVal, "_negate"));
1006  else {
1007  AssertPos(pos, type->IsIntType());
1008  return ctx->BinaryOperator(llvm::Instruction::Sub, zero, argVal, LLVMGetName(argVal, "_negate"));
1009  }
1010 }
1011 
1012 UnaryExpr::UnaryExpr(Op o, Expr *e, SourcePos p) : Expr(p, UnaryExprID), op(o) { expr = e; }
1013 
1014 llvm::Value *UnaryExpr::GetValue(FunctionEmitContext *ctx) const {
1015  if (expr == NULL)
1016  return NULL;
1017 
1018  ctx->SetDebugPos(pos);
1019 
1020  switch (op) {
1021  case PreInc:
1022  case PreDec:
1023  case PostInc:
1024  case PostDec:
1025  return lEmitPrePostIncDec(op, expr, pos, ctx);
1026  case Negate:
1027  return lEmitNegate(expr, pos, ctx);
1028  case LogicalNot: {
1029  llvm::Value *argVal = expr->GetValue(ctx);
1030  return ctx->NotOperator(argVal, LLVMGetName(argVal, "_logicalnot"));
1031  }
1032  case BitNot: {
1033  llvm::Value *argVal = expr->GetValue(ctx);
1034  return ctx->NotOperator(argVal, LLVMGetName(argVal, "_bitnot"));
1035  }
1036  default:
1037  FATAL("logic error");
1038  return NULL;
1039  }
1040 }
1041 
1042 const Type *UnaryExpr::GetType() const {
1043  if (expr == NULL)
1044  return NULL;
1045 
1046  const Type *type = expr->GetType();
1047  if (type == NULL)
1048  return NULL;
1049 
1050  // For all unary expressions besides logical not, the returned type is
1051  // the same as the source type. Logical not always returns a bool
1052  // type, with the same shape as the input type.
1053  switch (op) {
1054  case PreInc:
1055  case PreDec:
1056  case PostInc:
1057  case PostDec:
1058  case Negate:
1059  case BitNot:
1060  return type;
1061  case LogicalNot:
1062  return lMatchingBoolType(type);
1063  default:
1064  FATAL("error");
1065  return NULL;
1066  }
1067 }
1068 
1069 template <typename T> static Expr *lOptimizeBitNot(ConstExpr *constExpr, const Type *type, SourcePos pos) {
1070  T v[ISPC_MAX_NVEC];
1071  int count = constExpr->GetValues(v);
1072  for (int i = 0; i < count; ++i)
1073  v[i] = ~v[i];
1074  return new ConstExpr(type, v, pos);
1075 }
1076 
1077 template <typename T> static Expr *lOptimizeNegate(ConstExpr *constExpr, const Type *type, SourcePos pos) {
1078  T v[ISPC_MAX_NVEC];
1079  int count = constExpr->GetValues(v);
1080  for (int i = 0; i < count; ++i)
1081  v[i] = -v[i];
1082  return new ConstExpr(type, v, pos);
1083 }
1084 
1086  ConstExpr *constExpr = llvm::dyn_cast<ConstExpr>(expr);
1087  // If the operand isn't a constant, then we can't do any optimization
1088  // here...
1089  if (constExpr == NULL)
1090  return this;
1091 
1092  const Type *type = constExpr->GetType();
1093  bool isEnumType = CastType<EnumType>(type) != NULL;
1094 
1095  switch (op) {
1096  case PreInc:
1097  case PreDec:
1098  case PostInc:
1099  case PostDec:
1100  // this shouldn't happen--it's illegal to modify a contant value..
1101  // An error will be issued elsewhere...
1102  return this;
1103  case Negate: {
1106  return lOptimizeNegate<int64_t>(constExpr, type, pos);
1109  return lOptimizeNegate<uint64_t>(constExpr, type, pos);
1112  return lOptimizeNegate<int32_t>(constExpr, type, pos);
1115  return lOptimizeNegate<uint32_t>(constExpr, type, pos);
1118  return lOptimizeNegate<int16_t>(constExpr, type, pos);
1121  return lOptimizeNegate<uint16_t>(constExpr, type, pos);
1124  return lOptimizeNegate<int8_t>(constExpr, type, pos);
1127  return lOptimizeNegate<uint8_t>(constExpr, type, pos);
1128  } else {
1129  // For all the other types, it's safe to stuff whatever we have
1130  // into a double, do the negate as a double, and then return a
1131  // ConstExpr with the same type as the original...
1132  double v[ISPC_MAX_NVEC];
1133  int count = constExpr->GetValues(v);
1134  for (int i = 0; i < count; ++i)
1135  v[i] = -v[i];
1136  return new ConstExpr(constExpr, v);
1137  }
1138  }
1139  case BitNot: {
1142  return lOptimizeBitNot<int8_t>(constExpr, type, pos);
1145  return lOptimizeBitNot<uint8_t>(constExpr, type, pos);
1148  return lOptimizeBitNot<int16_t>(constExpr, type, pos);
1151  return lOptimizeBitNot<uint16_t>(constExpr, type, pos);
1154  return lOptimizeBitNot<int32_t>(constExpr, type, pos);
1156  Type::EqualIgnoringConst(type, AtomicType::VaryingUInt32) || isEnumType == true) {
1157  return lOptimizeBitNot<uint32_t>(constExpr, type, pos);
1160  return lOptimizeBitNot<int64_t>(constExpr, type, pos);
1162  Type::EqualIgnoringConst(type, AtomicType::VaryingUInt64) || isEnumType == true) {
1163  return lOptimizeBitNot<uint64_t>(constExpr, type, pos);
1164  } else
1165  FATAL("unexpected type in UnaryExpr::Optimize() / BitNot case");
1166  }
1167  case LogicalNot: {
1170  bool v[ISPC_MAX_NVEC];
1171  int count = constExpr->GetValues(v);
1172  for (int i = 0; i < count; ++i)
1173  v[i] = !v[i];
1174  return new ConstExpr(type, v, pos);
1175  }
1176  default:
1177  FATAL("unexpected op in UnaryExpr::Optimize()");
1178  return NULL;
1179  }
1180 }
1181 
1183  const Type *type;
1184  if (expr == NULL || (type = expr->GetType()) == NULL)
1185  // something went wrong in type checking...
1186  return NULL;
1187 
1188  if (type->IsSOAType()) {
1189  Error(pos, "Can't apply unary operator to SOA type \"%s\".", type->GetString().c_str());
1190  return NULL;
1191  }
1192 
1193  if (op == PreInc || op == PreDec || op == PostInc || op == PostDec) {
1194  if (type->IsConstType()) {
1195  Error(pos,
1196  "Can't assign to type \"%s\" on left-hand side of "
1197  "expression.",
1198  type->GetString().c_str());
1199  return NULL;
1200  }
1201 
1202  if (type->IsNumericType())
1203  return this;
1204 
1205  const PointerType *pt = CastType<PointerType>(type);
1206  if (pt == NULL) {
1207  Error(expr->pos,
1208  "Can only pre/post increment numeric and "
1209  "pointer types, not \"%s\".",
1210  type->GetString().c_str());
1211  return NULL;
1212  }
1213 
1214  if (PointerType::IsVoidPointer(type)) {
1215  Error(expr->pos, "Illegal to pre/post increment \"%s\" type.", type->GetString().c_str());
1216  return NULL;
1217  }
1218  if (CastType<UndefinedStructType>(pt->GetBaseType())) {
1219  Error(expr->pos,
1220  "Illegal to pre/post increment pointer to "
1221  "undefined struct type \"%s\".",
1222  type->GetString().c_str());
1223  return NULL;
1224  }
1225 
1226  return this;
1227  }
1228 
1229  // don't do this for pre/post increment/decrement
1230  if (CastType<ReferenceType>(type)) {
1231  expr = new RefDerefExpr(expr, pos);
1232  type = expr->GetType();
1233  }
1234 
1235  if (op == Negate) {
1236  if (!type->IsNumericType()) {
1237  Error(expr->pos, "Negate not allowed for non-numeric type \"%s\".", type->GetString().c_str());
1238  return NULL;
1239  }
1240  } else if (op == LogicalNot) {
1241  const Type *boolType = lMatchingBoolType(type);
1242  expr = TypeConvertExpr(expr, boolType, "logical not");
1243  if (expr == NULL)
1244  return NULL;
1245  } else if (op == BitNot) {
1246  if (!type->IsIntType()) {
1247  Error(expr->pos,
1248  "~ operator can only be used with integer types, "
1249  "not \"%s\".",
1250  type->GetString().c_str());
1251  return NULL;
1252  }
1253  }
1254  return this;
1255 }
1256 
1258  if (llvm::dyn_cast<ConstExpr>(expr) != NULL)
1259  return 0;
1260 
1262 }
1263 
1264 void UnaryExpr::Print() const {
1265  if (!expr || !GetType())
1266  return;
1267 
1268  printf("[ %s ] (", GetType()->GetString().c_str());
1269  if (op == PreInc)
1270  printf("++");
1271  if (op == PreDec)
1272  printf("--");
1273  if (op == Negate)
1274  printf("-");
1275  if (op == LogicalNot)
1276  printf("!");
1277  if (op == BitNot)
1278  printf("~");
1279  printf("(");
1280  expr->Print();
1281  printf(")");
1282  if (op == PostInc)
1283  printf("++");
1284  if (op == PostDec)
1285  printf("--");
1286  printf(")");
1287  pos.Print();
1288 }
1289 
1290 ///////////////////////////////////////////////////////////////////////////
1291 // BinaryExpr
1292 
1293 static const char *lOpString(BinaryExpr::Op op) {
1294  switch (op) {
1295  case BinaryExpr::Add:
1296  return "+";
1297  case BinaryExpr::Sub:
1298  return "-";
1299  case BinaryExpr::Mul:
1300  return "*";
1301  case BinaryExpr::Div:
1302  return "/";
1303  case BinaryExpr::Mod:
1304  return "%";
1305  case BinaryExpr::Shl:
1306  return "<<";
1307  case BinaryExpr::Shr:
1308  return ">>";
1309  case BinaryExpr::Lt:
1310  return "<";
1311  case BinaryExpr::Gt:
1312  return ">";
1313  case BinaryExpr::Le:
1314  return "<=";
1315  case BinaryExpr::Ge:
1316  return ">=";
1317  case BinaryExpr::Equal:
1318  return "==";
1319  case BinaryExpr::NotEqual:
1320  return "!=";
1321  case BinaryExpr::BitAnd:
1322  return "&";
1323  case BinaryExpr::BitXor:
1324  return "^";
1325  case BinaryExpr::BitOr:
1326  return "|";
1328  return "&&";
1329  case BinaryExpr::LogicalOr:
1330  return "||";
1331  case BinaryExpr::Comma:
1332  return ",";
1333  default:
1334  FATAL("unimplemented case in lOpString()");
1335  return "";
1336  }
1337 }
1338 
1339 /** Utility routine to emit the binary bitwise operator corresponding to
1340  the given BinaryExpr::Op.
1341 */
1342 static llvm::Value *lEmitBinaryBitOp(BinaryExpr::Op op, llvm::Value *arg0Val, llvm::Value *arg1Val, bool isUnsigned,
1343  FunctionEmitContext *ctx) {
1344  llvm::Instruction::BinaryOps inst;
1345  switch (op) {
1346  case BinaryExpr::Shl:
1347  inst = llvm::Instruction::Shl;
1348  break;
1349  case BinaryExpr::Shr:
1350  if (isUnsigned)
1351  inst = llvm::Instruction::LShr;
1352  else
1353  inst = llvm::Instruction::AShr;
1354  break;
1355  case BinaryExpr::BitAnd:
1356  inst = llvm::Instruction::And;
1357  break;
1358  case BinaryExpr::BitXor:
1359  inst = llvm::Instruction::Xor;
1360  break;
1361  case BinaryExpr::BitOr:
1362  inst = llvm::Instruction::Or;
1363  break;
1364  default:
1365  FATAL("logic error in lEmitBinaryBitOp()");
1366  return NULL;
1367  }
1368 
1369  return ctx->BinaryOperator(inst, arg0Val, arg1Val, "bitop");
1370 }
1371 
1372 static llvm::Value *lEmitBinaryPointerArith(BinaryExpr::Op op, llvm::Value *value0, llvm::Value *value1,
1373  const Type *type0, const Type *type1, FunctionEmitContext *ctx,
1374  SourcePos pos) {
1375  const PointerType *ptrType = CastType<PointerType>(type0);
1376  AssertPos(pos, ptrType != NULL);
1377  switch (op) {
1378  case BinaryExpr::Add:
1379  // ptr + integer
1380  return ctx->GetElementPtrInst(value0, value1, ptrType, "ptrmath");
1381  break;
1382  case BinaryExpr::Sub: {
1383  if (CastType<PointerType>(type1) != NULL) {
1384  AssertPos(pos, Type::EqualIgnoringConst(type0, type1));
1385 
1386  if (ptrType->IsSlice()) {
1387  llvm::Value *p0 = ctx->ExtractInst(value0, 0);
1388  llvm::Value *p1 = ctx->ExtractInst(value1, 0);
1389  const Type *majorType = ptrType->GetAsNonSlice();
1390  llvm::Value *majorDelta = lEmitBinaryPointerArith(op, p0, p1, majorType, majorType, ctx, pos);
1391 
1392  int soaWidth = ptrType->GetBaseType()->GetSOAWidth();
1393  AssertPos(pos, soaWidth > 0);
1394  llvm::Value *soaScale = LLVMIntAsType(soaWidth, majorDelta->getType());
1395 
1396  llvm::Value *majorScale =
1397  ctx->BinaryOperator(llvm::Instruction::Mul, majorDelta, soaScale, "major_soa_scaled");
1398 
1399  llvm::Value *m0 = ctx->ExtractInst(value0, 1);
1400  llvm::Value *m1 = ctx->ExtractInst(value1, 1);
1401  llvm::Value *minorDelta = ctx->BinaryOperator(llvm::Instruction::Sub, m0, m1, "minor_soa_delta");
1402 
1403  ctx->MatchIntegerTypes(&majorScale, &minorDelta);
1404  return ctx->BinaryOperator(llvm::Instruction::Add, majorScale, minorDelta, "soa_ptrdiff");
1405  }
1406 
1407  // ptr - ptr
1408  if (ptrType->IsUniformType()) {
1409  value0 = ctx->PtrToIntInst(value0);
1410  value1 = ctx->PtrToIntInst(value1);
1411  }
1412 
1413  // Compute the difference in bytes
1414  llvm::Value *delta = ctx->BinaryOperator(llvm::Instruction::Sub, value0, value1, "ptr_diff");
1415 
1416  // Now divide by the size of the type that the pointer
1417  // points to in order to return the difference in elements.
1418  llvm::Type *llvmElementType = ptrType->GetBaseType()->LLVMType(g->ctx);
1419  llvm::Value *size = g->target->SizeOf(llvmElementType, ctx->GetCurrentBasicBlock());
1420  if (ptrType->IsVaryingType())
1421  size = ctx->SmearUniform(size);
1422 
1423  if (g->target->is32Bit() == false && g->opt.force32BitAddressing == true) {
1424  // If we're doing 32-bit addressing math on a 64-bit
1425  // target, then trunc the delta down to a 32-bit value.
1426  // (Thus also matching what will be a 32-bit value
1427  // returned from SizeOf above.)
1428  if (ptrType->IsUniformType())
1429  delta = ctx->TruncInst(delta, LLVMTypes::Int32Type, "trunc_ptr_delta");
1430  else
1431  delta = ctx->TruncInst(delta, LLVMTypes::Int32VectorType, "trunc_ptr_delta");
1432  }
1433 
1434  // And now do the actual division
1435  return ctx->BinaryOperator(llvm::Instruction::SDiv, delta, size, "element_diff");
1436  } else {
1437  // ptr - integer
1438  llvm::Value *zero = lLLVMConstantValue(type1, g->ctx, 0.);
1439  llvm::Value *negOffset = ctx->BinaryOperator(llvm::Instruction::Sub, zero, value1, "negate");
1440  // Do a GEP as ptr + -integer
1441  return ctx->GetElementPtrInst(value0, negOffset, ptrType, "ptrmath");
1442  }
1443  }
1444  default:
1445  FATAL("Logic error in lEmitBinaryArith() for pointer type case");
1446  return NULL;
1447  }
1448 }
1449 
1450 /** Utility routine to emit binary arithmetic operator based on the given
1451  BinaryExpr::Op.
1452 */
1453 static llvm::Value *lEmitBinaryArith(BinaryExpr::Op op, llvm::Value *value0, llvm::Value *value1, const Type *type0,
1454  const Type *type1, FunctionEmitContext *ctx, SourcePos pos) {
1455  const PointerType *ptrType = CastType<PointerType>(type0);
1456 
1457  if (ptrType != NULL)
1458  return lEmitBinaryPointerArith(op, value0, value1, type0, type1, ctx, pos);
1459  else {
1460  AssertPos(pos, Type::EqualIgnoringConst(type0, type1));
1461 
1462  llvm::Instruction::BinaryOps inst;
1463  bool isFloatOp = type0->IsFloatType();
1464  bool isUnsignedOp = type0->IsUnsignedType();
1465 
1466  const char *opName = NULL;
1467  switch (op) {
1468  case BinaryExpr::Add:
1469  opName = "add";
1470  inst = isFloatOp ? llvm::Instruction::FAdd : llvm::Instruction::Add;
1471  break;
1472  case BinaryExpr::Sub:
1473  opName = "sub";
1474  inst = isFloatOp ? llvm::Instruction::FSub : llvm::Instruction::Sub;
1475  break;
1476  case BinaryExpr::Mul:
1477  opName = "mul";
1478  inst = isFloatOp ? llvm::Instruction::FMul : llvm::Instruction::Mul;
1479  break;
1480  case BinaryExpr::Div:
1481  opName = "div";
1482  if (type0->IsVaryingType() && !isFloatOp)
1483  PerformanceWarning(pos, "Division with varying integer types is "
1484  "very inefficient.");
1485  inst = isFloatOp ? llvm::Instruction::FDiv
1486  : (isUnsignedOp ? llvm::Instruction::UDiv : llvm::Instruction::SDiv);
1487  break;
1488  case BinaryExpr::Mod:
1489  opName = "mod";
1490  if (type0->IsVaryingType() && !isFloatOp)
1491  PerformanceWarning(pos, "Modulus operator with varying types is "
1492  "very inefficient.");
1493  inst = isFloatOp ? llvm::Instruction::FRem
1494  : (isUnsignedOp ? llvm::Instruction::URem : llvm::Instruction::SRem);
1495  break;
1496  default:
1497  FATAL("Invalid op type passed to lEmitBinaryArith()");
1498  return NULL;
1499  }
1500 
1501  return ctx->BinaryOperator(inst, value0, value1, LLVMGetName(opName, value0, value1));
1502  }
1503 }
1504 
1505 /** Utility routine to emit a binary comparison operator based on the given
1506  BinaryExpr::Op.
1507  */
1508 static llvm::Value *lEmitBinaryCmp(BinaryExpr::Op op, llvm::Value *e0Val, llvm::Value *e1Val, const Type *type,
1509  FunctionEmitContext *ctx, SourcePos pos) {
1510  bool isFloatOp = type->IsFloatType();
1511  bool isUnsignedOp = type->IsUnsignedType();
1512 
1513  llvm::CmpInst::Predicate pred;
1514  const char *opName = NULL;
1515  switch (op) {
1516  case BinaryExpr::Lt:
1517  opName = "less";
1518  pred = isFloatOp ? llvm::CmpInst::FCMP_ULT : (isUnsignedOp ? llvm::CmpInst::ICMP_ULT : llvm::CmpInst::ICMP_SLT);
1519  break;
1520  case BinaryExpr::Gt:
1521  opName = "greater";
1522  pred = isFloatOp ? llvm::CmpInst::FCMP_UGT : (isUnsignedOp ? llvm::CmpInst::ICMP_UGT : llvm::CmpInst::ICMP_SGT);
1523  break;
1524  case BinaryExpr::Le:
1525  opName = "lessequal";
1526  pred = isFloatOp ? llvm::CmpInst::FCMP_ULE : (isUnsignedOp ? llvm::CmpInst::ICMP_ULE : llvm::CmpInst::ICMP_SLE);
1527  break;
1528  case BinaryExpr::Ge:
1529  opName = "greaterequal";
1530  pred = isFloatOp ? llvm::CmpInst::FCMP_UGE : (isUnsignedOp ? llvm::CmpInst::ICMP_UGE : llvm::CmpInst::ICMP_SGE);
1531  break;
1532  case BinaryExpr::Equal:
1533  opName = "equal";
1534  pred = isFloatOp ? llvm::CmpInst::FCMP_UEQ : llvm::CmpInst::ICMP_EQ;
1535  break;
1536  case BinaryExpr::NotEqual:
1537  opName = "notequal";
1538  pred = isFloatOp ? llvm::CmpInst::FCMP_UNE : llvm::CmpInst::ICMP_NE;
1539  break;
1540  default:
1541  FATAL("error in lEmitBinaryCmp()");
1542  return NULL;
1543  }
1544 
1545  llvm::Value *cmp = ctx->CmpInst(isFloatOp ? llvm::Instruction::FCmp : llvm::Instruction::ICmp, pred, e0Val, e1Val,
1546  LLVMGetName(opName, e0Val, e1Val));
1547  // This is a little ugly: CmpInst returns i1 values, but we use vectors
1548  // of i32s for varying bool values; type convert the result here if
1549  // needed.
1550  if (type->IsVaryingType())
1551  cmp = ctx->I1VecToBoolVec(cmp);
1552 
1553  return cmp;
1554 }
1555 
1556 BinaryExpr::BinaryExpr(Op o, Expr *a, Expr *b, SourcePos p) : Expr(p, BinaryExprID), op(o) {
1557  arg0 = a;
1558  arg1 = b;
1559 }
1560 
1562  if ((a0 == NULL) || (a1 == NULL)) {
1563  return NULL;
1564  }
1565  Expr *arg0 = a0->TypeCheck();
1566  Expr *arg1 = a1->TypeCheck();
1567  if ((arg0 == NULL) || (arg1 == NULL)) {
1568  return NULL;
1569  }
1570  const Type *type0 = arg0->GetType();
1571  const Type *type1 = arg1->GetType();
1572 
1573  // If either operand is a reference, dereference it before we move
1574  // forward
1575  if (CastType<ReferenceType>(type0) != NULL) {
1576  arg0 = new RefDerefExpr(arg0, arg0->pos);
1577  type0 = arg0->GetType();
1578  }
1579  if (CastType<ReferenceType>(type1) != NULL) {
1580  arg1 = new RefDerefExpr(arg1, arg1->pos);
1581  type1 = arg1->GetType();
1582  }
1583  if ((type0 == NULL) || (type1 == NULL)) {
1584  return NULL;
1585  }
1586  if (CastType<StructType>(type0) != NULL || CastType<StructType>(type1) != NULL) {
1587  std::string opName = std::string("operator") + lOpString(bop);
1588  std::vector<Symbol *> funs;
1589  m->symbolTable->LookupFunction(opName.c_str(), &funs);
1590  if (funs.size() == 0) {
1591  Error(sp, "operator %s(%s, %s) is not defined.", opName.c_str(), (type0->GetString()).c_str(),
1592  (type1->GetString()).c_str());
1593  return NULL;
1594  }
1595  Expr *func = new FunctionSymbolExpr(opName.c_str(), funs, sp);
1596  ExprList *args = new ExprList(sp);
1597  args->exprs.push_back(arg0);
1598  args->exprs.push_back(arg1);
1599  Expr *opCallExpr = new FunctionCallExpr(func, args, sp);
1600  return opCallExpr;
1601  }
1602  return NULL;
1603 }
1604 
1606  Expr *op = lCreateBinaryOperatorCall(o, a, b, p);
1607  if (op != NULL) {
1608  return op;
1609  }
1610  op = new BinaryExpr(o, a, b, p);
1611  return op;
1612 }
1613 
1614 /** Emit code for a && or || logical operator. In particular, the code
1615  here handles "short-circuit" evaluation, where the second expression
1616  isn't evaluated if the value of the first one determines the value of
1617  the result.
1618 */
1619 llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionEmitContext *ctx, SourcePos pos) {
1620 
1621  const Type *type0 = arg0->GetType(), *type1 = arg1->GetType();
1622  if (type0 == NULL || type1 == NULL) {
1623  AssertPos(pos, m->errorCount > 0);
1624  return NULL;
1625  }
1626 
1627  // There is overhead (branches, etc.), to short-circuiting, so if the
1628  // right side of the expression is a) relatively simple, and b) can be
1629  // safely executed with an all-off execution mask, then we just
1630  // evaluate both sides and then the logical operator in that case.
1631  // FIXME: not sure what we should do about vector types here...
1632  bool shortCircuit =
1634  CastType<VectorType>(type0) != NULL || CastType<VectorType>(type1) != NULL);
1635  if (shortCircuit == false) {
1636  // If one of the operands is uniform but the other is varying,
1637  // promote the uniform one to varying
1638  if (type0->IsUniformType() && type1->IsVaryingType()) {
1640  AssertPos(pos, arg0 != NULL);
1641  }
1642  if (type1->IsUniformType() && type0->IsVaryingType()) {
1644  AssertPos(pos, arg1 != NULL);
1645  }
1646 
1647  llvm::Value *value0 = arg0->GetValue(ctx);
1648  llvm::Value *value1 = arg1->GetValue(ctx);
1649  if (value0 == NULL || value1 == NULL) {
1650  AssertPos(pos, m->errorCount > 0);
1651  return NULL;
1652  }
1653 
1654  if (op == BinaryExpr::LogicalAnd)
1655  return ctx->BinaryOperator(llvm::Instruction::And, value0, value1, "logical_and");
1656  else {
1657  AssertPos(pos, op == BinaryExpr::LogicalOr);
1658  return ctx->BinaryOperator(llvm::Instruction::Or, value0, value1, "logical_or");
1659  }
1660  }
1661 
1662  // Allocate temporary storage for the return value
1663  const Type *retType = Type::MoreGeneralType(type0, type1, pos, lOpString(op));
1664  llvm::Type *llvmRetType = retType->LLVMType(g->ctx);
1665  llvm::Value *retPtr = ctx->AllocaInst(llvmRetType, "logical_op_mem");
1666 
1667  llvm::BasicBlock *bbSkipEvalValue1 = ctx->CreateBasicBlock("skip_eval_1");
1668  llvm::BasicBlock *bbEvalValue1 = ctx->CreateBasicBlock("eval_1");
1669  llvm::BasicBlock *bbLogicalDone = ctx->CreateBasicBlock("logical_op_done");
1670 
1671  // Evaluate the first operand
1672  llvm::Value *value0 = arg0->GetValue(ctx);
1673  if (value0 == NULL) {
1674  AssertPos(pos, m->errorCount > 0);
1675  return NULL;
1676  }
1677 
1678  if (type0->IsUniformType()) {
1679  // Check to see if the value of the first operand is true or false
1680  llvm::Value *value0True = ctx->CmpInst(llvm::Instruction::ICmp, llvm::CmpInst::ICMP_EQ, value0, LLVMTrue);
1681 
1682  if (op == BinaryExpr::LogicalOr) {
1683  // For ||, if value0 is true, then we skip evaluating value1
1684  // entirely.
1685  ctx->BranchInst(bbSkipEvalValue1, bbEvalValue1, value0True);
1686 
1687  // If value0 is true, the complete result is true (either
1688  // uniform or varying)
1689  ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
1690  llvm::Value *trueValue = retType->IsUniformType() ? LLVMTrue : LLVMMaskAllOn;
1691  ctx->StoreInst(trueValue, retPtr);
1692  ctx->BranchInst(bbLogicalDone);
1693  } else {
1694  AssertPos(pos, op == BinaryExpr::LogicalAnd);
1695 
1696  // Conversely, for &&, if value0 is false, we skip evaluating
1697  // value1.
1698  ctx->BranchInst(bbEvalValue1, bbSkipEvalValue1, value0True);
1699 
1700  // In this case, the complete result is false (again, either a
1701  // uniform or varying false).
1702  ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
1703  llvm::Value *falseValue = retType->IsUniformType() ? LLVMFalse : LLVMMaskAllOff;
1704  ctx->StoreInst(falseValue, retPtr);
1705  ctx->BranchInst(bbLogicalDone);
1706  }
1707 
1708  // Both || and && are in the same situation if the first operand's
1709  // value didn't resolve the final result: they need to evaluate the
1710  // value of the second operand, which in turn gives the value for
1711  // the full expression.
1712  ctx->SetCurrentBasicBlock(bbEvalValue1);
1713  if (type1->IsUniformType() && retType->IsVaryingType()) {
1714  arg1 = TypeConvertExpr(arg1, AtomicType::VaryingBool, "logical op");
1715  AssertPos(pos, arg1 != NULL);
1716  }
1717 
1718  llvm::Value *value1 = arg1->GetValue(ctx);
1719  if (value1 == NULL) {
1720  AssertPos(pos, m->errorCount > 0);
1721  return NULL;
1722  }
1723  ctx->StoreInst(value1, retPtr);
1724  ctx->BranchInst(bbLogicalDone);
1725 
1726  // In all cases, we end up at the bbLogicalDone basic block;
1727  // loading the value stored in retPtr in turn gives the overall
1728  // result.
1729  ctx->SetCurrentBasicBlock(bbLogicalDone);
1730  return ctx->LoadInst(retPtr);
1731  } else {
1732  // Otherwise, the first operand is varying... Save the current
1733  // value of the mask so that we can restore it at the end.
1734  llvm::Value *oldMask = ctx->GetInternalMask();
1735  llvm::Value *oldFullMask = ctx->GetFullMask();
1736 
1737  // Convert the second operand to be varying as well, so that we can
1738  // perform logical vector ops with its value.
1739  if (type1->IsUniformType()) {
1740  arg1 = TypeConvertExpr(arg1, AtomicType::VaryingBool, "logical op");
1741  AssertPos(pos, arg1 != NULL);
1742  type1 = arg1->GetType();
1743  }
1744 
1745  if (op == BinaryExpr::LogicalOr) {
1746  // See if value0 is true for all currently executing
1747  // lanes--i.e. if (value0 & mask) == mask. If so, we don't
1748  // need to evaluate the second operand of the expression.
1749  llvm::Value *value0AndMask = ctx->BinaryOperator(llvm::Instruction::And, value0, oldFullMask, "op&mask");
1750  llvm::Value *equalsMask = ctx->CmpInst(llvm::Instruction::ICmp, llvm::CmpInst::ICMP_EQ, value0AndMask,
1751  oldFullMask, "value0&mask==mask");
1752  equalsMask = ctx->I1VecToBoolVec(equalsMask);
1753  llvm::Value *allMatch = ctx->All(equalsMask);
1754  ctx->BranchInst(bbSkipEvalValue1, bbEvalValue1, allMatch);
1755 
1756  // value0 is true for all running lanes, so it can be used for
1757  // the final result
1758  ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
1759  ctx->StoreInst(value0, retPtr);
1760  ctx->BranchInst(bbLogicalDone);
1761 
1762  // Otherwise, we need to valuate arg1. However, first we need
1763  // to set the execution mask to be (oldMask & ~a); in other
1764  // words, only execute the instances where value0 is false.
1765  // For the instances where value0 was true, we need to inhibit
1766  // execution.
1767  ctx->SetCurrentBasicBlock(bbEvalValue1);
1768  ctx->SetInternalMaskAndNot(oldMask, value0);
1769 
1770  llvm::Value *value1 = arg1->GetValue(ctx);
1771  if (value1 == NULL) {
1772  AssertPos(pos, m->errorCount > 0);
1773  return NULL;
1774  }
1775 
1776  // We need to compute the result carefully, since vector
1777  // elements that were computed when the corresponding lane was
1778  // disabled have undefined values:
1779  // result = (value0 & old_mask) | (value1 & current_mask)
1780  llvm::Value *value1AndMask =
1781  ctx->BinaryOperator(llvm::Instruction::And, value1, ctx->GetInternalMask(), "op&mask");
1782  llvm::Value *result = ctx->BinaryOperator(llvm::Instruction::Or, value0AndMask, value1AndMask, "or_result");
1783  ctx->StoreInst(result, retPtr);
1784  ctx->BranchInst(bbLogicalDone);
1785  } else {
1786  AssertPos(pos, op == BinaryExpr::LogicalAnd);
1787 
1788  // If value0 is false for all currently running lanes, the
1789  // overall result must be false: this corresponds to checking
1790  // if (mask & ~value0) == mask.
1791  llvm::Value *notValue0 = ctx->NotOperator(value0, "not_value0");
1792  llvm::Value *notValue0AndMask =
1793  ctx->BinaryOperator(llvm::Instruction::And, notValue0, oldFullMask, "not_value0&mask");
1794  llvm::Value *equalsMask = ctx->CmpInst(llvm::Instruction::ICmp, llvm::CmpInst::ICMP_EQ, notValue0AndMask,
1795  oldFullMask, "not_value0&mask==mask");
1796  equalsMask = ctx->I1VecToBoolVec(equalsMask);
1797  llvm::Value *allMatch = ctx->All(equalsMask);
1798  ctx->BranchInst(bbSkipEvalValue1, bbEvalValue1, allMatch);
1799 
1800  // value0 was false for all running lanes, so use its value as
1801  // the overall result.
1802  ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
1803  ctx->StoreInst(value0, retPtr);
1804  ctx->BranchInst(bbLogicalDone);
1805 
1806  // Otherwise we need to evaluate value1, but again with the
1807  // mask set to only be on for the lanes where value0 was true.
1808  // For the lanes where value0 was false, execution needs to be
1809  // disabled: mask = (mask & value0).
1810  ctx->SetCurrentBasicBlock(bbEvalValue1);
1811  ctx->SetInternalMaskAnd(oldMask, value0);
1812 
1813  llvm::Value *value1 = arg1->GetValue(ctx);
1814  if (value1 == NULL) {
1815  AssertPos(pos, m->errorCount > 0);
1816  return NULL;
1817  }
1818 
1819  // And as in the || case, we compute the overall result by
1820  // masking off the valid lanes before we AND them together:
1821  // result = (value0 & old_mask) & (value1 & current_mask)
1822  llvm::Value *value0AndMask = ctx->BinaryOperator(llvm::Instruction::And, value0, oldFullMask, "op&mask");
1823  llvm::Value *value1AndMask =
1824  ctx->BinaryOperator(llvm::Instruction::And, value1, ctx->GetInternalMask(), "value1&mask");
1825  llvm::Value *result =
1826  ctx->BinaryOperator(llvm::Instruction::And, value0AndMask, value1AndMask, "or_result");
1827  ctx->StoreInst(result, retPtr);
1828  ctx->BranchInst(bbLogicalDone);
1829  }
1830 
1831  // And finally we always end up in bbLogicalDone, where we restore
1832  // the old mask and return the computed result
1833  ctx->SetCurrentBasicBlock(bbLogicalDone);
1834  ctx->SetInternalMask(oldMask);
1835  return ctx->LoadInst(retPtr);
1836  }
1837 }
1838 
1839 /* Returns true if shifting right by the given amount will lead to
1840  inefficient code. (Assumes x86 target. May also warn inaccurately if
1841  later optimization simplify the shift amount more than we are able to
1842  see at this point.) */
1843 static bool lIsDifficultShiftAmount(Expr *expr) {
1844  // Uniform shifts (of uniform values) are no problem.
1845  if (expr->GetType()->IsVaryingType() == false)
1846  return false;
1847 
1848  ConstExpr *ce = llvm::dyn_cast<ConstExpr>(expr);
1849  if (ce) {
1850  // If the shift is by a constant amount, *and* it's the same amount
1851  // in all vector lanes, we're in good shape.
1852  uint32_t amount[ISPC_MAX_NVEC];
1853  int count = ce->GetValues(amount);
1854  for (int i = 1; i < count; ++i)
1855  if (amount[i] != amount[0])
1856  return true;
1857  return false;
1858  }
1859 
1860  TypeCastExpr *tce = llvm::dyn_cast<TypeCastExpr>(expr);
1861  if (tce && tce->expr) {
1862  // Finally, if the shift amount is given by a uniform value that's
1863  // been smeared out into a varying, we have the same shift for all
1864  // lanes and are also in good shape.
1865  return (tce->expr->GetType()->IsUniformType() == false);
1866  }
1867 
1868  return true;
1869 }
1870 
1871 llvm::Value *BinaryExpr::GetValue(FunctionEmitContext *ctx) const {
1872  if (!arg0 || !arg1) {
1873  AssertPos(pos, m->errorCount > 0);
1874  return NULL;
1875  }
1876 
1877  // Handle these specially, since we want to short-circuit their evaluation...
1878  if (op == LogicalAnd || op == LogicalOr)
1879  return lEmitLogicalOp(op, arg0, arg1, ctx, pos);
1880 
1881  llvm::Value *value0 = arg0->GetValue(ctx);
1882  llvm::Value *value1 = arg1->GetValue(ctx);
1883  if (value0 == NULL || value1 == NULL) {
1884  AssertPos(pos, m->errorCount > 0);
1885  return NULL;
1886  }
1887 
1888  ctx->SetDebugPos(pos);
1889 
1890  switch (op) {
1891  case Add:
1892  case Sub:
1893  case Mul:
1894  case Div:
1895  case Mod:
1896  return lEmitBinaryArith(op, value0, value1, arg0->GetType(), arg1->GetType(), ctx, pos);
1897  case Lt:
1898  case Gt:
1899  case Le:
1900  case Ge:
1901  case Equal:
1902  case NotEqual:
1903  return lEmitBinaryCmp(op, value0, value1, arg0->GetType(), ctx, pos);
1904  case Shl:
1905  case Shr:
1906  case BitAnd:
1907  case BitXor:
1908  case BitOr: {
1909  if (op == Shr && lIsDifficultShiftAmount(arg1))
1910  PerformanceWarning(pos, "Shift right is inefficient for "
1911  "varying shift amounts.");
1912  return lEmitBinaryBitOp(op, value0, value1, arg0->GetType()->IsUnsignedType(), ctx);
1913  }
1914  case Comma:
1915  return value1;
1916  default:
1917  FATAL("logic error");
1918  return NULL;
1919  }
1920 }
1921 
1922 const Type *BinaryExpr::GetType() const {
1923  if (arg0 == NULL || arg1 == NULL)
1924  return NULL;
1925 
1926  const Type *type0 = arg0->GetType(), *type1 = arg1->GetType();
1927  if (type0 == NULL || type1 == NULL)
1928  return NULL;
1929 
1930  // If this hits, it means that our TypeCheck() method hasn't been
1931  // called before GetType() was called; adding two pointers is illegal
1932  // and will fail type checking and (int + ptr) should be canonicalized
1933  // into (ptr + int) by type checking.
1934  if (op == Add)
1935  AssertPos(pos, CastType<PointerType>(type1) == NULL);
1936 
1937  if (op == Comma)
1938  return arg1->GetType();
1939 
1940  if (CastType<PointerType>(type0) != NULL) {
1941  if (op == Add)
1942  // ptr + int -> ptr
1943  return type0;
1944  else if (op == Sub) {
1945  if (CastType<PointerType>(type1) != NULL) {
1946  // ptr - ptr -> ~ptrdiff_t
1947  const Type *diffType = (g->target->is32Bit() || g->opt.force32BitAddressing) ? AtomicType::UniformInt32
1949  if (type0->IsVaryingType() || type1->IsVaryingType())
1950  diffType = diffType->GetAsVaryingType();
1951  return diffType;
1952  } else
1953  // ptr - int -> ptr
1954  return type0;
1955  }
1956 
1957  // otherwise fall through for these...
1958  AssertPos(pos, op == Lt || op == Gt || op == Le || op == Ge || op == Equal || op == NotEqual);
1959  }
1960 
1961  const Type *exprType = Type::MoreGeneralType(type0, type1, pos, lOpString(op));
1962  // I don't think that MoreGeneralType should be able to fail after the
1963  // checks done in BinaryExpr::TypeCheck().
1964  AssertPos(pos, exprType != NULL);
1965 
1966  switch (op) {
1967  case Add:
1968  case Sub:
1969  case Mul:
1970  case Div:
1971  case Mod:
1972  return exprType;
1973  case Lt:
1974  case Gt:
1975  case Le:
1976  case Ge:
1977  case Equal:
1978  case NotEqual:
1979  case LogicalAnd:
1980  case LogicalOr:
1981  return lMatchingBoolType(exprType);
1982  case Shl:
1983  case Shr:
1984  return type1->IsVaryingType() ? type0->GetAsVaryingType() : type0;
1985  case BitAnd:
1986  case BitXor:
1987  case BitOr:
1988  return exprType;
1989  case Comma:
1990  // handled above, so fall through here just in case
1991  default:
1992  FATAL("logic error in BinaryExpr::GetType()");
1993  return NULL;
1994  }
1995 }
1996 
1997 #define FOLD_OP(O, E) \
1998  case O: \
1999  for (int i = 0; i < count; ++i) \
2000  result[i] = (v0[i] E v1[i]); \
2001  break
2002 
2003 #define FOLD_OP_REF(O, E, TRef) \
2004  case O: \
2005  for (int i = 0; i < count; ++i) { \
2006  result[i] = (v0[i] E v1[i]); \
2007  TRef r = (TRef)v0[i] E(TRef) v1[i]; \
2008  if (result[i] != r) \
2009  Warning(pos, "Binary expression with type \"%s\" can't represent value.", \
2010  carg0->GetType()->GetString().c_str()); \
2011  } \
2012  break
2013 
2014 /** Constant fold the binary integer operations that aren't also applicable
2015  to floating-point types.
2016 */
2017 template <typename T, typename TRef>
2018 static ConstExpr *lConstFoldBinaryIntOp(BinaryExpr::Op op, const T *v0, const T *v1, ConstExpr *carg0, SourcePos pos) {
2019  T result[ISPC_MAX_NVEC];
2020  int count = carg0->Count();
2021 
2022  switch (op) {
2023  FOLD_OP_REF(BinaryExpr::Mod, %, TRef);
2024  FOLD_OP_REF(BinaryExpr::Shl, <<, TRef);
2025  FOLD_OP_REF(BinaryExpr::Shr, >>, TRef);
2026  FOLD_OP_REF(BinaryExpr::BitAnd, &, TRef);
2027  FOLD_OP_REF(BinaryExpr::BitXor, ^, TRef);
2028  FOLD_OP_REF(BinaryExpr::BitOr, |, TRef);
2029  default:
2030  return NULL;
2031  }
2032 
2033  return new ConstExpr(carg0->GetType(), result, carg0->pos);
2034 }
2035 
2036 /** Constant fold the binary logical ops.
2037  */
2038 template <typename T>
2039 static ConstExpr *lConstFoldBinaryLogicalOp(BinaryExpr::Op op, const T *v0, const T *v1, ConstExpr *carg0) {
2040  bool result[ISPC_MAX_NVEC];
2041  int count = carg0->Count();
2042 
2043  switch (op) {
2044  FOLD_OP(BinaryExpr::Lt, <);
2045  FOLD_OP(BinaryExpr::Gt, >);
2046  FOLD_OP(BinaryExpr::Le, <=);
2047  FOLD_OP(BinaryExpr::Ge, >=);
2052  default:
2053  return NULL;
2054  }
2055 
2057  return new ConstExpr(rType, result, carg0->pos);
2058 }
2059 
2060 /** Constant fold binary arithmetic ops.
2061  */
2062 template <typename T, typename TRef>
2063 static ConstExpr *lConstFoldBinaryArithOp(BinaryExpr::Op op, const T *v0, const T *v1, ConstExpr *carg0,
2064  SourcePos pos) {
2065  T result[ISPC_MAX_NVEC];
2066  int count = carg0->Count();
2067 
2068  switch (op) {
2069  FOLD_OP_REF(BinaryExpr::Add, +, TRef);
2070  FOLD_OP_REF(BinaryExpr::Sub, -, TRef);
2071  FOLD_OP_REF(BinaryExpr::Mul, *, TRef);
2072  case BinaryExpr::Div:
2073  for (int i = 0; i < count; ++i) {
2074  if (v1[i] == 0) {
2075  Error(pos, "Division by zero encountered in expression.");
2076  result[i] = 0;
2077  } else
2078  result[i] = (v0[i] / v1[i]);
2079  }
2080  break;
2081  default:
2082  return NULL;
2083  }
2084 
2085  return new ConstExpr(carg0->GetType(), result, carg0->pos);
2086 }
2087 
2088 /** Constant fold the various boolean binary ops.
2089  */
2090 static ConstExpr *lConstFoldBoolBinaryOp(BinaryExpr::Op op, const bool *v0, const bool *v1, ConstExpr *carg0) {
2091  bool result[ISPC_MAX_NVEC];
2092  int count = carg0->Count();
2093 
2094  switch (op) {
2098  FOLD_OP(BinaryExpr::Lt, <);
2099  FOLD_OP(BinaryExpr::Gt, >);
2100  FOLD_OP(BinaryExpr::Le, <=);
2101  FOLD_OP(BinaryExpr::Ge, >=);
2106  default:
2107  return NULL;
2108  }
2109 
2110  return new ConstExpr(carg0->GetType(), result, carg0->pos);
2111 }
2112 
2113 template <typename T>
2114 static Expr *lConstFoldBinaryFPOp(ConstExpr *constArg0, ConstExpr *constArg1, BinaryExpr::Op op, BinaryExpr *origExpr,
2115  SourcePos pos) {
2116  T v0[ISPC_MAX_NVEC], v1[ISPC_MAX_NVEC];
2117  constArg0->GetValues(v0);
2118  constArg1->GetValues(v1);
2119  ConstExpr *ret;
2120  if ((ret = lConstFoldBinaryArithOp<T, T>(op, v0, v1, constArg0, pos)) != NULL)
2121  return ret;
2122  else if ((ret = lConstFoldBinaryLogicalOp(op, v0, v1, constArg0)) != NULL)
2123  return ret;
2124  else
2125  return origExpr;
2126 }
2127 
2128 template <typename T, typename TRef>
2129 static Expr *lConstFoldBinaryIntOp(ConstExpr *constArg0, ConstExpr *constArg1, BinaryExpr::Op op, BinaryExpr *origExpr,
2130  SourcePos pos) {
2131  T v0[ISPC_MAX_NVEC], v1[ISPC_MAX_NVEC];
2132  constArg0->GetValues(v0);
2133  constArg1->GetValues(v1);
2134  ConstExpr *ret;
2135  if ((ret = lConstFoldBinaryArithOp<T, TRef>(op, v0, v1, constArg0, pos)) != NULL)
2136  return ret;
2137  else if ((ret = lConstFoldBinaryIntOp<T, TRef>(op, v0, v1, constArg0, pos)) != NULL)
2138  return ret;
2139  else if ((ret = lConstFoldBinaryLogicalOp(op, v0, v1, constArg0)) != NULL)
2140  return ret;
2141  else
2142  return origExpr;
2143 }
2144 
2145 /* Returns true if the given arguments (which are assumed to be the
2146  operands of a divide) represent a divide that can be performed by one of
2147  the __fast_idiv functions.
2148  */
2149 static bool lCanImproveVectorDivide(Expr *arg0, Expr *arg1, int *divisor) {
2150  const Type *type = arg0->GetType();
2151  if (!type)
2152  return false;
2153 
2154  // The value being divided must be an int8/16/32.
2161  return false;
2162 
2163  // The divisor must be the same compile-time constant value for all of
2164  // the vector lanes.
2165  ConstExpr *ce = llvm::dyn_cast<ConstExpr>(arg1);
2166  if (!ce)
2167  return false;
2168  int64_t div[ISPC_MAX_NVEC];
2169  int count = ce->GetValues(div);
2170  for (int i = 1; i < count; ++i)
2171  if (div[i] != div[0])
2172  return false;
2173  *divisor = div[0];
2174 
2175  // And finally, the divisor must be >= 2 and <128 (for 8-bit divides),
2176  // and <256 otherwise.
2177  if (*divisor < 2)
2178  return false;
2181  return *divisor < 128;
2182  else
2183  return *divisor < 256;
2184 }
2185 
2187  if (arg0 == NULL || arg1 == NULL)
2188  return NULL;
2189 
2190  ConstExpr *constArg0 = llvm::dyn_cast<ConstExpr>(arg0);
2191  ConstExpr *constArg1 = llvm::dyn_cast<ConstExpr>(arg1);
2192 
2193  if (g->opt.fastMath) {
2194  // optimizations related to division by floats..
2195 
2196  // transform x / const -> x * (1/const)
2197  if (op == Div && constArg1 != NULL) {
2198  const Type *type1 = constArg1->GetType();
2201  float inv[ISPC_MAX_NVEC];
2202  int count = constArg1->GetValues(inv);
2203  for (int i = 0; i < count; ++i)
2204  inv[i] = 1.f / inv[i];
2205  Expr *einv = new ConstExpr(type1, inv, constArg1->pos);
2206  Expr *e = new BinaryExpr(Mul, arg0, einv, pos);
2207  e = ::TypeCheck(e);
2208  if (e == NULL)
2209  return NULL;
2210  return ::Optimize(e);
2211  }
2212  }
2213 
2214  // transform x / y -> x * rcp(y)
2215  if (op == Div) {
2216  const Type *type1 = arg1->GetType();
2219  // Get the symbol for the appropriate builtin
2220  std::vector<Symbol *> rcpFuns;
2221  m->symbolTable->LookupFunction("rcp", &rcpFuns);
2222  if (rcpFuns.size() > 0) {
2223  Expr *rcpSymExpr = new FunctionSymbolExpr("rcp", rcpFuns, pos);
2224  ExprList *args = new ExprList(arg1, arg1->pos);
2225  Expr *rcpCall = new FunctionCallExpr(rcpSymExpr, args, arg1->pos);
2226  rcpCall = ::TypeCheck(rcpCall);
2227  if (rcpCall == NULL)
2228  return NULL;
2229  rcpCall = ::Optimize(rcpCall);
2230  if (rcpCall == NULL)
2231  return NULL;
2232 
2233  Expr *ret = new BinaryExpr(Mul, arg0, rcpCall, pos);
2234  ret = ::TypeCheck(ret);
2235  if (ret == NULL)
2236  return NULL;
2237  return ::Optimize(ret);
2238  } else
2239  Warning(pos, "rcp() not found from stdlib. Can't apply "
2240  "fast-math rcp optimization.");
2241  }
2242  }
2243  }
2244 
2245  int divisor;
2246  if (op == Div && lCanImproveVectorDivide(arg0, arg1, &divisor)) {
2247  Debug(pos, "Improving vector divide by constant %d", divisor);
2248 
2249  std::vector<Symbol *> idivFuns;
2250  m->symbolTable->LookupFunction("__fast_idiv", &idivFuns);
2251  if (idivFuns.size() == 0) {
2252  Warning(pos, "Couldn't find __fast_idiv to optimize integer divide. "
2253  "Are you compiling with --nostdlib?");
2254  return this;
2255  }
2256 
2257  Expr *idivSymExpr = new FunctionSymbolExpr("__fast_idiv", idivFuns, pos);
2258  ExprList *args = new ExprList(arg0, pos);
2259  args->exprs.push_back(new ConstExpr(AtomicType::UniformInt32, divisor, arg1->pos));
2260  Expr *idivCall = new FunctionCallExpr(idivSymExpr, args, pos);
2261 
2262  idivCall = ::TypeCheck(idivCall);
2263  if (idivCall == NULL)
2264  return NULL;
2265 
2267  idivCall = new TypeCastExpr(GetType(), idivCall, pos);
2268  return ::Optimize(idivCall);
2269  }
2270 
2271  // From here on out, we're just doing constant folding, so if both args
2272  // aren't constants then we're done...
2273  if (constArg0 == NULL || constArg1 == NULL)
2274  return this;
2275 
2277  const Type *type = arg0->GetType()->GetAsNonConstType();
2279  return lConstFoldBinaryFPOp<float>(constArg0, constArg1, op, this, pos);
2281  return lConstFoldBinaryFPOp<double>(constArg0, constArg1, op, this, pos);
2283  return lConstFoldBinaryIntOp<int8_t, int64_t>(constArg0, constArg1, op, this, pos);
2285  return lConstFoldBinaryIntOp<uint8_t, uint64_t>(constArg0, constArg1, op, this, pos);
2287  return lConstFoldBinaryIntOp<int16_t, int64_t>(constArg0, constArg1, op, this, pos);
2289  return lConstFoldBinaryIntOp<uint16_t, uint64_t>(constArg0, constArg1, op, this, pos);
2291  return lConstFoldBinaryIntOp<int32_t, int64_t>(constArg0, constArg1, op, this, pos);
2293  return lConstFoldBinaryIntOp<uint32_t, uint64_t>(constArg0, constArg1, op, this, pos);
2295  return lConstFoldBinaryIntOp<int64_t, int64_t>(constArg0, constArg1, op, this, pos);
2297  return lConstFoldBinaryIntOp<uint64_t, uint64_t>(constArg0, constArg1, op, this, pos);
2299  bool v0[ISPC_MAX_NVEC], v1[ISPC_MAX_NVEC];
2300  constArg0->GetValues(v0);
2301  constArg1->GetValues(v1);
2302  ConstExpr *ret;
2303  if ((ret = lConstFoldBoolBinaryOp(op, v0, v1, constArg0)) != NULL)
2304  return ret;
2305  else if ((ret = lConstFoldBinaryLogicalOp(op, v0, v1, constArg0)) != NULL)
2306  return ret;
2307  else
2308  return this;
2309  } else
2310  return this;
2311 }
2312 
2314  if (arg0 == NULL || arg1 == NULL)
2315  return NULL;
2316 
2317  const Type *type0 = arg0->GetType(), *type1 = arg1->GetType();
2318  if (type0 == NULL || type1 == NULL)
2319  return NULL;
2320 
2321  // If either operand is a reference, dereference it before we move
2322  // forward
2323  if (CastType<ReferenceType>(type0) != NULL) {
2324  arg0 = new RefDerefExpr(arg0, arg0->pos);
2325  type0 = arg0->GetType();
2326  AssertPos(pos, type0 != NULL);
2327  }
2328  if (CastType<ReferenceType>(type1) != NULL) {
2329  arg1 = new RefDerefExpr(arg1, arg1->pos);
2330  type1 = arg1->GetType();
2331  AssertPos(pos, type1 != NULL);
2332  }
2333 
2334  // Convert arrays to pointers to their first elements
2335  if (CastType<ArrayType>(type0) != NULL) {
2337  type0 = arg0->GetType();
2338  }
2339  if (CastType<ArrayType>(type1) != NULL) {
2341  type1 = arg1->GetType();
2342  }
2343 
2344  // Prohibit binary operators with SOA types
2345  if (type0->GetSOAWidth() > 0) {
2346  Error(arg0->pos,
2347  "Illegal to use binary operator %s with SOA type "
2348  "\"%s\".",
2349  lOpString(op), type0->GetString().c_str());
2350  return NULL;
2351  }
2352  if (type1->GetSOAWidth() > 0) {
2353  Error(arg1->pos,
2354  "Illegal to use binary operator %s with SOA type "
2355  "\"%s\".",
2356  lOpString(op), type1->GetString().c_str());
2357  return NULL;
2358  }
2359 
2360  const PointerType *pt0 = CastType<PointerType>(type0);
2361  const PointerType *pt1 = CastType<PointerType>(type1);
2362  if (pt0 != NULL && pt1 != NULL && op == Sub) {
2363  // Pointer subtraction
2364  if (PointerType::IsVoidPointer(type0)) {
2365  Error(pos,
2366  "Illegal to perform pointer arithmetic "
2367  "on \"%s\" type.",
2368  type0->GetString().c_str());
2369  return NULL;
2370  }
2371  if (PointerType::IsVoidPointer(type1)) {
2372  Error(pos,
2373  "Illegal to perform pointer arithmetic "
2374  "on \"%s\" type.",
2375  type1->GetString().c_str());
2376  return NULL;
2377  }
2378  if (CastType<UndefinedStructType>(pt0->GetBaseType())) {
2379  Error(pos,
2380  "Illegal to perform pointer arithmetic "
2381  "on undefined struct type \"%s\".",
2382  pt0->GetString().c_str());
2383  return NULL;
2384  }
2385  if (CastType<UndefinedStructType>(pt1->GetBaseType())) {
2386  Error(pos,
2387  "Illegal to perform pointer arithmetic "
2388  "on undefined struct type \"%s\".",
2389  pt1->GetString().c_str());
2390  return NULL;
2391  }
2392 
2393  const Type *t = Type::MoreGeneralType(type0, type1, pos, "-");
2394  if (t == NULL)
2395  return NULL;
2396 
2397  arg0 = TypeConvertExpr(arg0, t, "pointer subtraction");
2398  arg1 = TypeConvertExpr(arg1, t, "pointer subtraction");
2399  if (arg0 == NULL || arg1 == NULL)
2400  return NULL;
2401 
2402  return this;
2403  } else if (((pt0 != NULL || pt1 != NULL) && op == Add) || (pt0 != NULL && op == Sub)) {
2404  // Handle ptr + int, int + ptr, ptr - int
2405  if (pt0 != NULL && pt1 != NULL) {
2406  Error(pos, "Illegal to add two pointer types \"%s\" and \"%s\".", pt0->GetString().c_str(),
2407  pt1->GetString().c_str());
2408  return NULL;
2409  } else if (pt1 != NULL) {
2410  // put in canonical order with the pointer as the first operand
2411  // for GetValue()
2412  std::swap(arg0, arg1);
2413  std::swap(type0, type1);
2414  std::swap(pt0, pt1);
2415  }
2416 
2417  AssertPos(pos, pt0 != NULL);
2418 
2419  if (PointerType::IsVoidPointer(pt0)) {
2420  Error(pos,
2421  "Illegal to perform pointer arithmetic "
2422  "on \"%s\" type.",
2423  pt0->GetString().c_str());
2424  return NULL;
2425  }
2426  if (CastType<UndefinedStructType>(pt0->GetBaseType())) {
2427  Error(pos,
2428  "Illegal to perform pointer arithmetic "
2429  "on undefined struct type \"%s\".",
2430  pt0->GetString().c_str());
2431  return NULL;
2432  }
2433 
2435  if (pt0->IsVaryingType())
2436  offsetType = offsetType->GetAsVaryingType();
2437  if (type1->IsVaryingType()) {
2438  arg0 = TypeConvertExpr(arg0, type0->GetAsVaryingType(), "pointer addition");
2439  offsetType = offsetType->GetAsVaryingType();
2440  AssertPos(pos, arg0 != NULL);
2441  }
2442 
2443  arg1 = TypeConvertExpr(arg1, offsetType, lOpString(op));
2444  if (arg1 == NULL)
2445  return NULL;
2446 
2447  return this;
2448  }
2449 
2450  switch (op) {
2451  case Shl:
2452  case Shr:
2453  case BitAnd:
2454  case BitXor:
2455  case BitOr: {
2456  // Must have integer or bool-typed operands for these bit-related
2457  // ops; don't do any implicit conversions from floats here...
2458  if (!type0->IsIntType() && !type0->IsBoolType()) {
2459  Error(arg0->pos,
2460  "First operand to binary operator \"%s\" must be "
2461  "an integer or bool.",
2462  lOpString(op));
2463  return NULL;
2464  }
2465  if (!type1->IsIntType() && !type1->IsBoolType()) {
2466  Error(arg1->pos,
2467  "Second operand to binary operator \"%s\" must be "
2468  "an integer or bool.",
2469  lOpString(op));
2470  return NULL;
2471  }
2472 
2473  if (op == Shl || op == Shr) {
2474  bool isVarying = (type0->IsVaryingType() || type1->IsVaryingType());
2475  if (isVarying) {
2476  arg0 = TypeConvertExpr(arg0, type0->GetAsVaryingType(), "shift operator");
2477  if (arg0 == NULL)
2478  return NULL;
2479  type0 = arg0->GetType();
2480  }
2481  arg1 = TypeConvertExpr(arg1, type0, "shift operator");
2482  if (arg1 == NULL)
2483  return NULL;
2484  } else {
2485  const Type *promotedType = Type::MoreGeneralType(type0, type1, arg0->pos, "binary bit op");
2486  if (promotedType == NULL)
2487  return NULL;
2488 
2489  arg0 = TypeConvertExpr(arg0, promotedType, "binary bit op");
2490  arg1 = TypeConvertExpr(arg1, promotedType, "binary bit op");
2491  if (arg0 == NULL || arg1 == NULL)
2492  return NULL;
2493  }
2494  return this;
2495  }
2496  case Add:
2497  case Sub:
2498  case Mul:
2499  case Div:
2500  case Mod: {
2501  // Must be numeric type for these. (And mod is special--can't be float)
2502  if (!type0->IsNumericType() || (op == Mod && type0->IsFloatType())) {
2503  Error(arg0->pos,
2504  "First operand to binary operator \"%s\" is of "
2505  "invalid type \"%s\".",
2506  lOpString(op), type0->GetString().c_str());
2507  return NULL;
2508  }
2509  if (!type1->IsNumericType() || (op == Mod && type1->IsFloatType())) {
2510  Error(arg1->pos,
2511  "First operand to binary operator \"%s\" is of "
2512  "invalid type \"%s\".",
2513  lOpString(op), type1->GetString().c_str());
2514  return NULL;
2515  }
2516 
2517  const Type *promotedType = Type::MoreGeneralType(type0, type1, Union(arg0->pos, arg1->pos), lOpString(op));
2518  if (promotedType == NULL)
2519  return NULL;
2520 
2521  arg0 = TypeConvertExpr(arg0, promotedType, lOpString(op));
2522  arg1 = TypeConvertExpr(arg1, promotedType, lOpString(op));
2523  if (arg0 == NULL || arg1 == NULL)
2524  return NULL;
2525  return this;
2526  }
2527  case Lt:
2528  case Gt:
2529  case Le:
2530  case Ge:
2531  case Equal:
2532  case NotEqual: {
2533  const PointerType *pt0 = CastType<PointerType>(type0);
2534  const PointerType *pt1 = CastType<PointerType>(type1);
2535 
2536  // Convert '0' in expressions where the other expression is a
2537  // pointer type to a NULL pointer.
2538  if (pt0 != NULL && lIsAllIntZeros(arg1)) {
2539  arg1 = new NullPointerExpr(pos);
2540  type1 = arg1->GetType();
2541  pt1 = CastType<PointerType>(type1);
2542  } else if (pt1 != NULL && lIsAllIntZeros(arg0)) {
2543  arg0 = new NullPointerExpr(pos);
2544  type0 = arg1->GetType();
2545  pt0 = CastType<PointerType>(type0);
2546  }
2547 
2548  if (pt0 == NULL && pt1 == NULL) {
2549  if (!type0->IsBoolType() && !type0->IsNumericType()) {
2550  Error(arg0->pos,
2551  "First operand to operator \"%s\" is of "
2552  "non-comparable type \"%s\".",
2553  lOpString(op), type0->GetString().c_str());
2554  return NULL;
2555  }
2556  if (!type1->IsBoolType() && !type1->IsNumericType()) {
2557  Error(arg1->pos,
2558  "Second operand to operator \"%s\" is of "
2559  "non-comparable type \"%s\".",
2560  lOpString(op), type1->GetString().c_str());
2561  return NULL;
2562  }
2563  }
2564 
2565  const Type *promotedType = Type::MoreGeneralType(type0, type1, arg0->pos, lOpString(op));
2566  if (promotedType == NULL)
2567  return NULL;
2568 
2569  arg0 = TypeConvertExpr(arg0, promotedType, lOpString(op));
2570  arg1 = TypeConvertExpr(arg1, promotedType, lOpString(op));
2571  if (arg0 == NULL || arg1 == NULL)
2572  return NULL;
2573  return this;
2574  }
2575  case LogicalAnd:
2576  case LogicalOr: {
2577  // For now, we just type convert to boolean types, of the same
2578  // variability as the original types. (When generating code, it's
2579  // useful to have preserved the uniform/varying distinction.)
2582 
2583  const Type *destType0 = NULL, *destType1 = NULL;
2584  const VectorType *vtype0 = CastType<VectorType>(type0);
2585  const VectorType *vtype1 = CastType<VectorType>(type1);
2586  if (vtype0 && vtype1) {
2587  int sz0 = vtype0->GetElementCount(), sz1 = vtype1->GetElementCount();
2588  if (sz0 != sz1) {
2589  Error(pos,
2590  "Can't do logical operation \"%s\" between vector types of "
2591  "different sizes (%d vs. %d).",
2592  lOpString(op), sz0, sz1);
2593  return NULL;
2594  }
2595  destType0 = new VectorType(boolType0, sz0);
2596  destType1 = new VectorType(boolType1, sz1);
2597  } else if (vtype0 != NULL) {
2598  destType0 = new VectorType(boolType0, vtype0->GetElementCount());
2599  destType1 = new VectorType(boolType1, vtype0->GetElementCount());
2600  } else if (vtype1 != NULL) {
2601  destType0 = new VectorType(boolType0, vtype1->GetElementCount());
2602  destType1 = new VectorType(boolType1, vtype1->GetElementCount());
2603  } else {
2604  destType0 = boolType0;
2605  destType1 = boolType1;
2606  }
2607 
2608  arg0 = TypeConvertExpr(arg0, destType0, lOpString(op));
2609  arg1 = TypeConvertExpr(arg1, destType1, lOpString(op));
2610  if (arg0 == NULL || arg1 == NULL)
2611  return NULL;
2612  return this;
2613  }
2614  case Comma:
2615  return this;
2616  default:
2617  FATAL("logic error");
2618  return NULL;
2619  }
2620 }
2621 
2623  const Type *t = GetType();
2624  if (CastType<PointerType>(t) != NULL) {
2625  // Are we doing something like (basePtr + offset)[...] = ...
2626  return t;
2627  } else {
2628  return NULL;
2629  }
2630 }
2631 
2633  if (llvm::dyn_cast<ConstExpr>(arg0) != NULL && llvm::dyn_cast<ConstExpr>(arg1) != NULL)
2634  return 0;
2635 
2637 }
2638 
2639 void BinaryExpr::Print() const {
2640  if (!arg0 || !arg1 || !GetType())
2641  return;
2642 
2643  printf("[ %s ] (", GetType()->GetString().c_str());
2644  arg0->Print();
2645  printf(" %s ", lOpString(op));
2646  arg1->Print();
2647  printf(")");
2648  pos.Print();
2649 }
2650 
2651 ///////////////////////////////////////////////////////////////////////////
2652 // AssignExpr
2653 
2654 static const char *lOpString(AssignExpr::Op op) {
2655  switch (op) {
2656  case AssignExpr::Assign:
2657  return "assignment operator";
2658  case AssignExpr::MulAssign:
2659  return "*=";
2660  case AssignExpr::DivAssign:
2661  return "/=";
2662  case AssignExpr::ModAssign:
2663  return "%%=";
2664  case AssignExpr::AddAssign:
2665  return "+=";
2666  case AssignExpr::SubAssign:
2667  return "-=";
2668  case AssignExpr::ShlAssign:
2669  return "<<=";
2670  case AssignExpr::ShrAssign:
2671  return ">>=";
2672  case AssignExpr::AndAssign:
2673  return "&=";
2674  case AssignExpr::XorAssign:
2675  return "^=";
2676  case AssignExpr::OrAssign:
2677  return "|=";
2678  default:
2679  FATAL("Missing op in lOpstring");
2680  return "";
2681  }
2682 }
2683 
2684 /** Emit code to do an "assignment + operation" operator, e.g. "+=".
2685  */
2686 static llvm::Value *lEmitOpAssign(AssignExpr::Op op, Expr *arg0, Expr *arg1, const Type *type, Symbol *baseSym,
2687  SourcePos pos, FunctionEmitContext *ctx) {
2688  llvm::Value *lv = arg0->GetLValue(ctx);
2689  if (!lv) {
2690  // FIXME: I think this test is unnecessary and that this case
2691  // should be caught during typechecking
2692  Error(pos, "Can't assign to left-hand side of expression.");
2693  return NULL;
2694  }
2695  const Type *lvalueType = arg0->GetLValueType();
2696  const Type *resultType = arg0->GetType();
2697  if (lvalueType == NULL || resultType == NULL)
2698  return NULL;
2699 
2700  // Get the value on the right-hand side of the assignment+operation
2701  // operator and load the current value on the left-hand side.
2702  llvm::Value *rvalue = arg1->GetValue(ctx);
2703  llvm::Value *mask = lMaskForSymbol(baseSym, ctx);
2704  ctx->SetDebugPos(arg0->pos);
2705  llvm::Value *oldLHS = ctx->LoadInst(lv, mask, lvalueType);
2706  ctx->SetDebugPos(pos);
2707 
2708  // Map the operator to the corresponding BinaryExpr::Op operator
2709  BinaryExpr::Op basicop;
2710  switch (op) {
2711  case AssignExpr::MulAssign:
2712  basicop = BinaryExpr::Mul;
2713  break;
2714  case AssignExpr::DivAssign:
2715  basicop = BinaryExpr::Div;
2716  break;
2717  case AssignExpr::ModAssign:
2718  basicop = BinaryExpr::Mod;
2719  break;
2720  case AssignExpr::AddAssign:
2721  basicop = BinaryExpr::Add;
2722  break;
2723  case AssignExpr::SubAssign:
2724  basicop = BinaryExpr::Sub;
2725  break;
2726  case AssignExpr::ShlAssign:
2727  basicop = BinaryExpr::Shl;
2728  break;
2729  case AssignExpr::ShrAssign:
2730  basicop = BinaryExpr::Shr;
2731  break;
2732  case AssignExpr::AndAssign:
2733  basicop = BinaryExpr::BitAnd;
2734  break;
2735  case AssignExpr::XorAssign:
2736  basicop = BinaryExpr::BitXor;
2737  break;
2738  case AssignExpr::OrAssign:
2739  basicop = BinaryExpr::BitOr;
2740  break;
2741  default:
2742  FATAL("logic error in lEmitOpAssign()");
2743  return NULL;
2744  }
2745 
2746  // Emit the code to compute the new value
2747  llvm::Value *newValue = NULL;
2748  switch (op) {
2749  case AssignExpr::MulAssign:
2750  case AssignExpr::DivAssign:
2751  case AssignExpr::ModAssign:
2752  case AssignExpr::AddAssign:
2753  case AssignExpr::SubAssign:
2754  newValue = lEmitBinaryArith(basicop, oldLHS, rvalue, type, arg1->GetType(), ctx, pos);
2755  break;
2756  case AssignExpr::ShlAssign:
2757  case AssignExpr::ShrAssign:
2758  case AssignExpr::AndAssign:
2759  case AssignExpr::XorAssign:
2760  case AssignExpr::OrAssign:
2761  newValue = lEmitBinaryBitOp(basicop, oldLHS, rvalue, arg0->GetType()->IsUnsignedType(), ctx);
2762  break;
2763  default:
2764  FATAL("logic error in lEmitOpAssign");
2765  return NULL;
2766  }
2767 
2768  // And store the result back to the lvalue.
2769  ctx->SetDebugPos(arg0->pos);
2770  lStoreAssignResult(newValue, lv, resultType, lvalueType, ctx, baseSym);
2771 
2772  return newValue;
2773 }
2774 
2775 AssignExpr::AssignExpr(AssignExpr::Op o, Expr *a, Expr *b, SourcePos p) : Expr(p, AssignExprID), op(o) {
2776  lvalue = a;
2777  rvalue = b;
2778 }
2779 
2780 llvm::Value *AssignExpr::GetValue(FunctionEmitContext *ctx) const {
2781  const Type *type = NULL;
2782  if (lvalue == NULL || rvalue == NULL || (type = GetType()) == NULL)
2783  return NULL;
2784 
2785  ctx->SetDebugPos(pos);
2786 
2787  Symbol *baseSym = lvalue->GetBaseSymbol();
2788 
2789  switch (op) {
2790  case Assign: {
2791  llvm::Value *ptr = lvalue->GetLValue(ctx);
2792  if (ptr == NULL) {
2793  Error(lvalue->pos, "Left hand side of assignment expression can't "
2794  "be assigned to.");
2795  return NULL;
2796  }
2797  const Type *ptrType = lvalue->GetLValueType();
2798  const Type *valueType = rvalue->GetType();
2799  if (ptrType == NULL || valueType == NULL) {
2800  AssertPos(pos, m->errorCount > 0);
2801  return NULL;
2802  }
2803 
2804  llvm::Value *value = rvalue->GetValue(ctx);
2805  if (value == NULL) {
2806  AssertPos(pos, m->errorCount > 0);
2807  return NULL;
2808  }
2809 
2810  ctx->SetDebugPos(lvalue->pos);
2811 
2812  lStoreAssignResult(value, ptr, valueType, ptrType, ctx, baseSym);
2813 
2814  return value;
2815  }
2816  case MulAssign:
2817  case DivAssign:
2818  case ModAssign:
2819  case AddAssign:
2820  case SubAssign:
2821  case ShlAssign:
2822  case ShrAssign:
2823  case AndAssign:
2824  case XorAssign:
2825  case OrAssign: {
2826  // This should be caught during type checking
2827  AssertPos(pos, !CastType<ArrayType>(type) && !CastType<StructType>(type));
2828  return lEmitOpAssign(op, lvalue, rvalue, type, baseSym, pos, ctx);
2829  }
2830  default:
2831  FATAL("logic error in AssignExpr::GetValue()");
2832  return NULL;
2833  }
2834 }
2835 
2837  if (lvalue == NULL || rvalue == NULL)
2838  return NULL;
2839  return this;
2840 }
2841 
2842 const Type *AssignExpr::GetType() const { return lvalue ? lvalue->GetType() : NULL; }
2843 
2844 /** Recursively checks a structure type to see if it (or any struct type
2845  that it holds) has a const-qualified member. */
2846 static bool lCheckForConstStructMember(SourcePos pos, const StructType *structType, const StructType *initialType) {
2847  for (int i = 0; i < structType->GetElementCount(); ++i) {
2848  const Type *t = structType->GetElementType(i);
2849  if (t->IsConstType()) {
2850  if (structType == initialType)
2851  Error(pos,
2852  "Illegal to assign to type \"%s\" due to element "
2853  "\"%s\" with type \"%s\".",
2854  structType->GetString().c_str(), structType->GetElementName(i).c_str(), t->GetString().c_str());
2855  else
2856  Error(pos,
2857  "Illegal to assign to type \"%s\" in type \"%s\" "
2858  "due to element \"%s\" with type \"%s\".",
2859  structType->GetString().c_str(), initialType->GetString().c_str(),
2860  structType->GetElementName(i).c_str(), t->GetString().c_str());
2861  return true;
2862  }
2863 
2864  const StructType *st = CastType<StructType>(t);
2865  if (st != NULL && lCheckForConstStructMember(pos, st, initialType))
2866  return true;
2867  }
2868  return false;
2869 }
2870 
2872  if (lvalue == NULL || rvalue == NULL)
2873  return NULL;
2874 
2875  bool lvalueIsReference = CastType<ReferenceType>(lvalue->GetType()) != NULL;
2876  if (lvalueIsReference)
2877  lvalue = new RefDerefExpr(lvalue, lvalue->pos);
2878 
2880  Error(pos, "Unable to find overloaded function for function "
2881  "pointer assignment.");
2882  return NULL;
2883  }
2884 
2885  const Type *lhsType = lvalue->GetType();
2886  if (lhsType == NULL) {
2887  AssertPos(pos, m->errorCount > 0);
2888  return NULL;
2889  }
2890 
2891  if (lhsType->IsConstType()) {
2892  Error(lvalue->pos,
2893  "Can't assign to type \"%s\" on left-hand side of "
2894  "expression.",
2895  lhsType->GetString().c_str());
2896  return NULL;
2897  }
2898 
2899  if (CastType<PointerType>(lhsType) != NULL) {
2900  if (op == AddAssign || op == SubAssign) {
2901  if (PointerType::IsVoidPointer(lhsType)) {
2902  Error(pos,
2903  "Illegal to perform pointer arithmetic on \"%s\" "
2904  "type.",
2905  lhsType->GetString().c_str());
2906  return NULL;
2907  }
2908 
2910  if (lhsType->IsVaryingType())
2911  deltaType = deltaType->GetAsVaryingType();
2912  rvalue = TypeConvertExpr(rvalue, deltaType, lOpString(op));
2913  } else if (op == Assign)
2914  rvalue = TypeConvertExpr(rvalue, lhsType, "assignment");
2915  else {
2916  Error(lvalue->pos, "Assignment operator \"%s\" is illegal with pointer types.", lOpString(op));
2917  return NULL;
2918  }
2919  } else if (CastType<ArrayType>(lhsType) != NULL) {
2920  Error(lvalue->pos, "Illegal to assign to array type \"%s\".", lhsType->GetString().c_str());
2921  return NULL;
2922  } else
2923  rvalue = TypeConvertExpr(rvalue, lhsType, lOpString(op));
2924 
2925  if (rvalue == NULL)
2926  return NULL;
2927 
2928  if (lhsType->IsFloatType() == true &&
2929  (op == ShlAssign || op == ShrAssign || op == AndAssign || op == XorAssign || op == OrAssign)) {
2930  Error(pos,
2931  "Illegal to use %s operator with floating-point "
2932  "operands.",
2933  lOpString(op));
2934  return NULL;
2935  }
2936 
2937  const StructType *st = CastType<StructType>(lhsType);
2938  if (st != NULL) {
2939  // Make sure we're not assigning to a struct that has a constant member
2940  if (lCheckForConstStructMember(pos, st, st))
2941  return NULL;
2942 
2943  if (op != Assign) {
2944  Error(lvalue->pos,
2945  "Assignment operator \"%s\" is illegal with struct "
2946  "type \"%s\".",
2947  lOpString(op), st->GetString().c_str());
2948  return NULL;
2949  }
2950  }
2951 
2952  return this;
2953 }
2954 
2956  if (op == Assign)
2957  return COST_ASSIGN;
2958  if (op == DivAssign || op == ModAssign)
2960  else
2962 }
2963 
2964 void AssignExpr::Print() const {
2965  if (!lvalue || !rvalue || !GetType())
2966  return;
2967 
2968  printf("[%s] assign (", GetType()->GetString().c_str());
2969  lvalue->Print();
2970  printf(" %s ", lOpString(op));
2971  rvalue->Print();
2972  printf(")");
2973  pos.Print();
2974 }
2975 
2976 ///////////////////////////////////////////////////////////////////////////
2977 // SelectExpr
2978 
2979 SelectExpr::SelectExpr(Expr *t, Expr *e1, Expr *e2, SourcePos p) : Expr(p, SelectExprID) {
2980  test = t;
2981  expr1 = e1;
2982  expr2 = e2;
2983 }
2984 
2985 /** Emit code to select between two varying values based on a varying test
2986  value.
2987  */
2988 static llvm::Value *lEmitVaryingSelect(FunctionEmitContext *ctx, llvm::Value *test, llvm::Value *expr1,
2989  llvm::Value *expr2, const Type *type) {
2990 
2991  llvm::Value *resultPtr = ctx->AllocaInst(expr1->getType(), "selectexpr_tmp");
2992  // Don't need to worry about masking here
2993  ctx->StoreInst(expr2, resultPtr);
2994  // Use masking to conditionally store the expr1 values
2995  Assert(resultPtr->getType() == PointerType::GetUniform(type)->LLVMType(g->ctx));
2996  ctx->StoreInst(expr1, resultPtr, test, type, PointerType::GetUniform(type));
2997  return ctx->LoadInst(resultPtr, "selectexpr_final");
2998 }
2999 
3000 static void lEmitSelectExprCode(FunctionEmitContext *ctx, llvm::Value *testVal, llvm::Value *oldMask,
3001  llvm::Value *fullMask, Expr *expr, llvm::Value *exprPtr) {
3002  llvm::BasicBlock *bbEval = ctx->CreateBasicBlock("select_eval_expr");
3003  llvm::BasicBlock *bbDone = ctx->CreateBasicBlock("select_done");
3004 
3005  // Check to see if the test was true for any of the currently executing
3006  // program instances.
3007  llvm::Value *testAndFullMask = ctx->BinaryOperator(llvm::Instruction::And, testVal, fullMask, "test&mask");
3008  llvm::Value *anyOn = ctx->Any(testAndFullMask);
3009  ctx->BranchInst(bbEval, bbDone, anyOn);
3010 
3011  ctx->SetCurrentBasicBlock(bbEval);
3012  llvm::Value *testAndMask = ctx->BinaryOperator(llvm::Instruction::And, testVal, oldMask, "test&mask");
3013  ctx->SetInternalMask(testAndMask);
3014  llvm::Value *exprVal = expr->GetValue(ctx);
3015  ctx->StoreInst(exprVal, exprPtr);
3016  ctx->BranchInst(bbDone);
3017 
3018  ctx->SetCurrentBasicBlock(bbDone);
3019 }
3020 
3021 llvm::Value *SelectExpr::GetValue(FunctionEmitContext *ctx) const {
3022  if (!expr1 || !expr2 || !test)
3023  return NULL;
3024 
3025  ctx->SetDebugPos(pos);
3026 
3027  const Type *testType = test->GetType()->GetAsNonConstType();
3028  // This should be taken care of during typechecking
3031 
3032  const Type *type = expr1->GetType();
3033 
3034  if (Type::Equal(testType, AtomicType::UniformBool)) {
3035  // Simple case of a single uniform bool test expression; we just
3036  // want one of the two expressions. In this case, we can be
3037  // careful to evaluate just the one of the expressions that we need
3038  // the value of so that if the other one has side-effects or
3039  // accesses invalid memory, it doesn't execute.
3040  llvm::Value *testVal = test->GetValue(ctx);
3041  llvm::BasicBlock *testTrue = ctx->CreateBasicBlock("select_true");
3042  llvm::BasicBlock *testFalse = ctx->CreateBasicBlock("select_false");
3043  llvm::BasicBlock *testDone = ctx->CreateBasicBlock("select_done");
3044  ctx->BranchInst(testTrue, testFalse, testVal);
3045 
3046  ctx->SetCurrentBasicBlock(testTrue);
3047  llvm::Value *expr1Val = expr1->GetValue(ctx);
3048  // Note that truePred won't be necessarily equal to testTrue, in
3049  // case the expr1->GetValue() call changes the current basic block.
3050  llvm::BasicBlock *truePred = ctx->GetCurrentBasicBlock();
3051  ctx->BranchInst(testDone);
3052 
3053  ctx->SetCurrentBasicBlock(testFalse);
3054  llvm::Value *expr2Val = expr2->GetValue(ctx);
3055  // See comment above truePred for why we can't just assume we're in
3056  // the testFalse basic block here.
3057  llvm::BasicBlock *falsePred = ctx->GetCurrentBasicBlock();
3058  ctx->BranchInst(testDone);
3059 
3060  ctx->SetCurrentBasicBlock(testDone);
3061  llvm::PHINode *ret = ctx->PhiNode(expr1Val->getType(), 2, "select");
3062  ret->addIncoming(expr1Val, truePred);
3063  ret->addIncoming(expr2Val, falsePred);
3064  return ret;
3065  } else if (CastType<VectorType>(testType) == NULL) {
3066  // the test is a varying bool type
3067  llvm::Value *testVal = test->GetValue(ctx);
3068  AssertPos(pos, testVal->getType() == LLVMTypes::MaskType);
3069  llvm::Value *oldMask = ctx->GetInternalMask();
3070  llvm::Value *fullMask = ctx->GetFullMask();
3071 
3072  // We don't want to incur the overhead for short-circuit evaluation
3073  // for expressions that are both computationally simple and safe to
3074  // run with an "all off" mask.
3075  bool shortCircuit1 =
3077  bool shortCircuit2 =
3079 
3080  Debug(expr1->pos, "%sshort circuiting evaluation for select expr", shortCircuit1 ? "" : "Not ");
3081  Debug(expr2->pos, "%sshort circuiting evaluation for select expr", shortCircuit2 ? "" : "Not ");
3082 
3083  // Temporary storage to store the values computed for each
3084  // expression, if any. (These stay as uninitialized memory if we
3085  // short circuit around the corresponding expression.)
3086  llvm::Type *exprType = expr1->GetType()->LLVMType(g->ctx);
3087  llvm::Value *expr1Ptr = ctx->AllocaInst(exprType);
3088  llvm::Value *expr2Ptr = ctx->AllocaInst(exprType);
3089 
3090  if (shortCircuit1)
3091  lEmitSelectExprCode(ctx, testVal, oldMask, fullMask, expr1, expr1Ptr);
3092  else {
3093  ctx->SetInternalMaskAnd(oldMask, testVal);
3094  llvm::Value *expr1Val = expr1->GetValue(ctx);
3095  ctx->StoreInst(expr1Val, expr1Ptr);
3096  }
3097 
3098  if (shortCircuit2) {
3099  llvm::Value *notTest = ctx->NotOperator(testVal);
3100  lEmitSelectExprCode(ctx, notTest, oldMask, fullMask, expr2, expr2Ptr);
3101  } else {
3102  ctx->SetInternalMaskAndNot(oldMask, testVal);
3103  llvm::Value *expr2Val = expr2->GetValue(ctx);
3104  ctx->StoreInst(expr2Val, expr2Ptr);
3105  }
3106 
3107  ctx->SetInternalMask(oldMask);
3108  llvm::Value *expr1Val = ctx->LoadInst(expr1Ptr);
3109  llvm::Value *expr2Val = ctx->LoadInst(expr2Ptr);
3110  return lEmitVaryingSelect(ctx, testVal, expr1Val, expr2Val, type);
3111  } else {
3112  // FIXME? Short-circuiting doesn't work in the case of
3113  // vector-valued test expressions. (We could also just prohibit
3114  // these and place the issue in the user's hands...)
3115  llvm::Value *testVal = test->GetValue(ctx);
3116  llvm::Value *expr1Val = expr1->GetValue(ctx);
3117  llvm::Value *expr2Val = expr2->GetValue(ctx);
3118 
3119  ctx->SetDebugPos(pos);
3120  const VectorType *vt = CastType<VectorType>(type);
3121  // Things that typechecking should have caught
3122  AssertPos(pos, vt != NULL);
3123  AssertPos(pos, CastType<VectorType>(testType) != NULL &&
3124  (CastType<VectorType>(testType)->GetElementCount() == vt->GetElementCount()));
3125 
3126  // Do an element-wise select
3127  llvm::Value *result = llvm::UndefValue::get(type->LLVMType(g->ctx));
3128  for (int i = 0; i < vt->GetElementCount(); ++i) {
3129  llvm::Value *ti = ctx->ExtractInst(testVal, i);
3130  llvm::Value *e1i = ctx->ExtractInst(expr1Val, i);
3131  llvm::Value *e2i = ctx->ExtractInst(expr2Val, i);
3132  llvm::Value *sel = NULL;
3133  if (testType->IsUniformType())
3134  sel = ctx->SelectInst(ti, e1i, e2i);
3135  else
3136  sel = lEmitVaryingSelect(ctx, ti, e1i, e2i, vt->GetElementType());
3137  result = ctx->InsertInst(result, sel, i);
3138  }
3139  return result;
3140  }
3141 }
3142 
3143 const Type *SelectExpr::GetType() const {
3144  if (!test || !expr1 || !expr2)
3145  return NULL;
3146 
3147  const Type *testType = test->GetType();
3148  const Type *expr1Type = expr1->GetType();
3149  const Type *expr2Type = expr2->GetType();
3150 
3151  if (!testType || !expr1Type || !expr2Type)
3152  return NULL;
3153 
3154  bool becomesVarying = (testType->IsVaryingType() || expr1Type->IsVaryingType() || expr2Type->IsVaryingType());
3155  // if expr1 and expr2 have different vector sizes, typechecking should fail...
3156  int testVecSize = CastType<VectorType>(testType) != NULL ? CastType<VectorType>(testType)->GetElementCount() : 0;
3157  int expr1VecSize = CastType<VectorType>(expr1Type) != NULL ? CastType<VectorType>(expr1Type)->GetElementCount() : 0;
3158  AssertPos(pos, !(testVecSize != 0 && expr1VecSize != 0 && testVecSize != expr1VecSize));
3159 
3160  int vectorSize = std::max(testVecSize, expr1VecSize);
3161  return Type::MoreGeneralType(expr1Type, expr2Type, Union(expr1->pos, expr2->pos), "select expression",
3162  becomesVarying, vectorSize);
3163 }
3164 
3165 template <typename T>
3166 Expr *lConstFoldSelect(const bool bv[], ConstExpr *constExpr1, ConstExpr *constExpr2, const Type *exprType,
3167  SourcePos pos) {
3168  T v1[ISPC_MAX_NVEC], v2[ISPC_MAX_NVEC];
3169  T result[ISPC_MAX_NVEC];
3170  int count = constExpr1->GetValues(v1);
3171  constExpr2->GetValues(v2);
3172  for (int i = 0; i < count; ++i)
3173  result[i] = bv[i] ? v1[i] : v2[i];
3174  return new ConstExpr(exprType, result, pos);
3175 }
3176 
3178  if (test == NULL || expr1 == NULL || expr2 == NULL)
3179  return NULL;
3180 
3181  ConstExpr *constTest = llvm::dyn_cast<ConstExpr>(test);
3182  if (constTest == NULL)
3183  return this;
3184 
3185  // The test is a constant; see if we can resolve to one of the
3186  // expressions..
3187  bool bv[ISPC_MAX_NVEC];
3188  int count = constTest->GetValues(bv);
3189  if (count == 1)
3190  // Uniform test value; return the corresponding expression
3191  return (bv[0] == true) ? expr1 : expr2;
3192  else {
3193  // Varying test: see if all of the values are the same; if so, then
3194  // return the corresponding expression
3195  bool first = bv[0];
3196  bool mismatch = false;
3197  for (int i = 0; i < count; ++i)
3198  if (bv[i] != first) {
3199  mismatch = true;
3200  break;
3201  }
3202  if (mismatch == false)
3203  return (bv[0] == true) ? expr1 : expr2;
3204 
3205  // Last chance: see if the two expressions are constants; if so,
3206  // then we can do an element-wise selection based on the constant
3207  // condition..
3208  ConstExpr *constExpr1 = llvm::dyn_cast<ConstExpr>(expr1);
3209  ConstExpr *constExpr2 = llvm::dyn_cast<ConstExpr>(expr2);
3210  if (constExpr1 == NULL || constExpr2 == NULL)
3211  return this;
3212 
3213  AssertPos(pos, Type::Equal(constExpr1->GetType(), constExpr2->GetType()));
3214  const Type *exprType = constExpr1->GetType()->GetAsNonConstType();
3215  AssertPos(pos, exprType->IsVaryingType());
3216 
3217  if (Type::Equal(exprType, AtomicType::VaryingInt8)) {
3218  return lConstFoldSelect<int8_t>(bv, constExpr1, constExpr2, exprType, pos);
3219  } else if (Type::Equal(exprType, AtomicType::VaryingUInt8)) {
3220  return lConstFoldSelect<uint8_t>(bv, constExpr1, constExpr2, exprType, pos);
3221  } else if (Type::Equal(exprType, AtomicType::VaryingInt16)) {
3222  return lConstFoldSelect<int16_t>(bv, constExpr1, constExpr2, exprType, pos);
3223  } else if (Type::Equal(exprType, AtomicType::VaryingUInt16)) {
3224  return lConstFoldSelect<uint16_t>(bv, constExpr1, constExpr2, exprType, pos);
3225  } else if (Type::Equal(exprType, AtomicType::VaryingInt32)) {
3226  return lConstFoldSelect<int32_t>(bv, constExpr1, constExpr2, exprType, pos);
3227  } else if (Type::Equal(exprType, AtomicType::VaryingUInt32)) {
3228  return lConstFoldSelect<uint32_t>(bv, constExpr1, constExpr2, exprType, pos);
3229  } else if (Type::Equal(exprType, AtomicType::VaryingInt64)) {
3230  return lConstFoldSelect<int64_t>(bv, constExpr1, constExpr2, exprType, pos);
3231  } else if (Type::Equal(exprType, AtomicType::VaryingUInt64)) {
3232  return lConstFoldSelect<uint64_t>(bv, constExpr1, constExpr2, exprType, pos);
3233  } else if (Type::Equal(exprType, AtomicType::VaryingFloat)) {
3234  return lConstFoldSelect<float>(bv, constExpr1, constExpr2, exprType, pos);
3235  } else if (Type::Equal(exprType, AtomicType::VaryingDouble)) {
3236  return lConstFoldSelect<bool>(bv, constExpr1, constExpr2, exprType, pos);
3237  } else if (Type::Equal(exprType, AtomicType::VaryingBool)) {
3238  return lConstFoldSelect<double>(bv, constExpr1, constExpr2, exprType, pos);
3239  }
3240 
3241  return this;
3242  }
3243 }
3244 
3246  if (test == NULL || expr1 == NULL || expr2 == NULL)
3247  return NULL;
3248 
3249  const Type *type1 = expr1->GetType(), *type2 = expr2->GetType();
3250  if (!type1 || !type2)
3251  return NULL;
3252 
3253  if (const ArrayType *at1 = CastType<ArrayType>(type1)) {
3254  expr1 = TypeConvertExpr(expr1, PointerType::GetUniform(at1->GetBaseType()), "select");
3255  if (expr1 == NULL)
3256  return NULL;
3257  type1 = expr1->GetType();
3258  }
3259  if (const ArrayType *at2 = CastType<ArrayType>(type2)) {
3260  expr2 = TypeConvertExpr(expr2, PointerType::GetUniform(at2->GetBaseType()), "select");
3261  if (expr2 == NULL)
3262  return NULL;
3263  type2 = expr2->GetType();
3264  }
3265 
3266  const Type *testType = test->GetType();
3267  if (testType == NULL)
3268  return NULL;
3269  test = TypeConvertExpr(test, lMatchingBoolType(testType), "select");
3270  if (test == NULL)
3271  return NULL;
3272  testType = test->GetType();
3273 
3274  int testVecSize = CastType<VectorType>(testType) ? CastType<VectorType>(testType)->GetElementCount() : 0;
3275  const Type *promotedType = Type::MoreGeneralType(type1, type2, Union(expr1->pos, expr2->pos), "select expression",
3276  testType->IsVaryingType(), testVecSize);
3277  if (promotedType == NULL)
3278  return NULL;
3279 
3280  expr1 = TypeConvertExpr(expr1, promotedType, "select");
3281  expr2 = TypeConvertExpr(expr2, promotedType, "select");
3282  if (expr1 == NULL || expr2 == NULL)
3283  return NULL;
3284 
3285  return this;
3286 }
3287 
3288 int SelectExpr::EstimateCost() const { return COST_SELECT; }
3289 
3290 void SelectExpr::Print() const {
3291  if (!test || !expr1 || !expr2 || !GetType())
3292  return;
3293 
3294  printf("[%s] (", GetType()->GetString().c_str());
3295  test->Print();
3296  printf(" ? ");
3297  expr1->Print();
3298  printf(" : ");
3299  expr2->Print();
3300  printf(")");
3301  pos.Print();
3302 }
3303 
3304 ///////////////////////////////////////////////////////////////////////////
3305 // FunctionCallExpr
3306 
3308  : Expr(p, FunctionCallExprID), isLaunch(il) {
3309  func = f;
3310  args = a;
3311  if (lce != NULL) {
3312  launchCountExpr[0] = lce[0];
3313  launchCountExpr[1] = lce[1];
3314  launchCountExpr[2] = lce[2];
3315  } else
3316  launchCountExpr[0] = launchCountExpr[1] = launchCountExpr[2] = NULL;
3317 }
3318 
3319 static const FunctionType *lGetFunctionType(Expr *func) {
3320  if (func == NULL)
3321  return NULL;
3322 
3323  const Type *type = func->GetType();
3324  if (type == NULL)
3325  return NULL;
3326 
3327  const FunctionType *ftype = CastType<FunctionType>(type);
3328  if (ftype == NULL) {
3329  // Not a regular function symbol--is it a function pointer?
3330  if (CastType<PointerType>(type) != NULL)
3331  ftype = CastType<FunctionType>(type->GetBaseType());
3332  }
3333  return ftype;
3334 }
3335 
3337  if (func == NULL || args == NULL)
3338  return NULL;
3339 
3340  ctx->SetDebugPos(pos);
3341 
3342  llvm::Value *callee = func->GetValue(ctx);
3343 
3344  if (callee == NULL) {
3345  AssertPos(pos, m->errorCount > 0);
3346  return NULL;
3347  }
3348 
3349  const FunctionType *ft = lGetFunctionType(func);
3350  AssertPos(pos, ft != NULL);
3351  bool isVoidFunc = ft->GetReturnType()->IsVoidType();
3352 
3353  // Automatically convert function call args to references if needed.
3354  // FIXME: this should move to the TypeCheck() method... (but the
3355  // GetLValue call below needs a FunctionEmitContext, which is
3356  // problematic...)
3357  std::vector<Expr *> callargs = args->exprs;
3358  bool err = false;
3359 
3360  // Specifically, this can happen if there's an error earlier during
3361  // overload resolution.
3362  if ((int)callargs.size() > ft->GetNumParameters()) {
3363  AssertPos(pos, m->errorCount > 0);
3364  return NULL;
3365  }
3366 
3367  for (unsigned int i = 0; i < callargs.size(); ++i) {
3368  Expr *argExpr = callargs[i];
3369  if (argExpr == NULL)
3370  continue;
3371 
3372  const Type *paramType = ft->GetParameterType(i);
3373 
3374  const Type *argLValueType = argExpr->GetLValueType();
3375  if (argLValueType != NULL && CastType<PointerType>(argLValueType) != NULL && argLValueType->IsVaryingType() &&
3376  CastType<ReferenceType>(paramType) != NULL) {
3377  Error(argExpr->pos,
3378  "Illegal to pass a \"varying\" lvalue to a "
3379  "reference parameter of type \"%s\".",
3380  paramType->GetString().c_str());
3381  return NULL;
3382  }
3383 
3384  // Do whatever type conversion is needed
3385  argExpr = TypeConvertExpr(argExpr, paramType, "function call argument");
3386  if (argExpr == NULL)
3387  return NULL;
3388  callargs[i] = argExpr;
3389  }
3390  if (err)
3391  return NULL;
3392 
3393  // Fill in any default argument values needed.
3394  // FIXME: should we do this during type checking?
3395  for (int i = callargs.size(); i < ft->GetNumParameters(); ++i) {
3396  Expr *paramDefault = ft->GetParameterDefault(i);
3397  const Type *paramType = ft->GetParameterType(i);
3398  // FIXME: this type conv should happen when we create the function
3399  // type!
3400  Expr *d = TypeConvertExpr(paramDefault, paramType, "function call default argument");
3401  if (d == NULL)
3402  return NULL;
3403  callargs.push_back(d);
3404  }
3405 
3406  // Now evaluate the values of all of the parameters being passed.
3407  std::vector<llvm::Value *> argVals;
3408  for (unsigned int i = 0; i < callargs.size(); ++i) {
3409  Expr *argExpr = callargs[i];
3410  if (argExpr == NULL)
3411  // give up; we hit an error earlier
3412  return NULL;
3413 
3414  llvm::Value *argValue = argExpr->GetValue(ctx);
3415  if (argValue == NULL)
3416  // something went wrong in evaluating the argument's
3417  // expression, so give up on this
3418  return NULL;
3419 
3420  argVals.push_back(argValue);
3421  }
3422 
3423  llvm::Value *retVal = NULL;
3424  ctx->SetDebugPos(pos);
3425  if (ft->isTask) {
3426  AssertPos(pos, launchCountExpr[0] != NULL);
3427  llvm::Value *launchCount[3] = {launchCountExpr[0]->GetValue(ctx), launchCountExpr[1]->GetValue(ctx),
3428  launchCountExpr[2]->GetValue(ctx)};
3429 
3430  if (launchCount[0] != NULL)
3431  ctx->LaunchInst(callee, argVals, launchCount);
3432  } else
3433  retVal = ctx->CallInst(callee, ft, argVals, isVoidFunc ? "" : "calltmp");
3434 
3435  if (isVoidFunc)
3436  return NULL;
3437  else
3438  return retVal;
3439 }
3440 
3442  if (GetLValueType() != NULL) {
3443  return GetValue(ctx);
3444  } else {
3445  // Only be a valid LValue type if the function
3446  // returns a pointer or reference.
3447  return NULL;
3448  }
3449 }
3450 
3451 bool FullResolveOverloads(Expr *func, ExprList *args, std::vector<const Type *> *argTypes,
3452  std::vector<bool> *argCouldBeNULL, std::vector<bool> *argIsConstant) {
3453  for (unsigned int i = 0; i < args->exprs.size(); ++i) {
3454  Expr *expr = args->exprs[i];
3455  if (expr == NULL)
3456  return false;
3457  const Type *t = expr->GetType();
3458  if (t == NULL)
3459  return false;
3460  argTypes->push_back(t);
3461  argCouldBeNULL->push_back(lIsAllIntZeros(expr) || llvm::dyn_cast<NullPointerExpr>(expr));
3462  argIsConstant->push_back(llvm::dyn_cast<ConstExpr>(expr) || llvm::dyn_cast<NullPointerExpr>(expr));
3463  }
3464  return true;
3465 }
3466 
3468  std::vector<const Type *> argTypes;
3469  std::vector<bool> argCouldBeNULL, argIsConstant;
3470  if (FullResolveOverloads(func, args, &argTypes, &argCouldBeNULL, &argIsConstant) == true) {
3471  FunctionSymbolExpr *fse = llvm::dyn_cast<FunctionSymbolExpr>(func);
3472  if (fse != NULL) {
3473  fse->ResolveOverloads(args->pos, argTypes, &argCouldBeNULL, &argIsConstant);
3474  }
3475  }
3476  const FunctionType *ftype = lGetFunctionType(func);
3477  return ftype ? ftype->GetReturnType() : NULL;
3478 }
3479 
3481  const FunctionType *ftype = lGetFunctionType(func);
3482  if (ftype && (ftype->GetReturnType()->IsPointerType() || ftype->GetReturnType()->IsReferenceType())) {
3483  return ftype->GetReturnType();
3484  } else {
3485  // Only be a valid LValue type if the function
3486  // returns a pointer or reference.
3487  return NULL;
3488  }
3489 }
3490 
3492  if (func == NULL || args == NULL)
3493  return NULL;
3494  return this;
3495 }
3496 
3498  if (func == NULL || args == NULL)
3499  return NULL;
3500 
3501  std::vector<const Type *> argTypes;
3502  std::vector<bool> argCouldBeNULL, argIsConstant;
3503 
3504  if (FullResolveOverloads(func, args, &argTypes, &argCouldBeNULL, &argIsConstant) == false) {
3505  return NULL;
3506  }
3507 
3508  FunctionSymbolExpr *fse = llvm::dyn_cast<FunctionSymbolExpr>(func);
3509  if (fse != NULL) {
3510  // Regular function call
3511  if (fse->ResolveOverloads(args->pos, argTypes, &argCouldBeNULL, &argIsConstant) == false)
3512  return NULL;
3513 
3514  func = ::TypeCheck(fse);
3515  if (func == NULL)
3516  return NULL;
3517 
3518  const FunctionType *ft = CastType<FunctionType>(func->GetType());
3519  if (ft == NULL) {
3520  const PointerType *pt = CastType<PointerType>(func->GetType());
3521  ft = (pt == NULL) ? NULL : CastType<FunctionType>(pt->GetBaseType());
3522  }
3523 
3524  if (ft == NULL) {
3525  Error(pos, "Valid function name must be used for function call.");
3526  return NULL;
3527  }
3528 
3529  if (ft->isTask) {
3530  if (!isLaunch)
3531  Error(pos, "\"launch\" expression needed to call function "
3532  "with \"task\" qualifier.");
3533  for (int k = 0; k < 3; k++) {
3534  if (!launchCountExpr[k])
3535  return NULL;
3536 
3538  if (launchCountExpr[k] == NULL)
3539  return NULL;
3540  }
3541  } else {
3542  if (isLaunch) {
3543  Error(pos, "\"launch\" expression illegal with non-\"task\"-"
3544  "qualified function.");
3545  return NULL;
3546  }
3547  AssertPos(pos, launchCountExpr[0] == NULL);
3548  }
3549  } else {
3550  // Call through a function pointer
3551  const Type *fptrType = func->GetType();
3552  if (fptrType == NULL)
3553  return NULL;
3554 
3555  // Make sure we do in fact have a function to call
3556  const FunctionType *funcType;
3557  if (CastType<PointerType>(fptrType) == NULL ||
3558  (funcType = CastType<FunctionType>(fptrType->GetBaseType())) == NULL) {
3559  Error(func->pos, "Must provide function name or function pointer for "
3560  "function call expression.");
3561  return NULL;
3562  }
3563 
3564  // Make sure we don't have too many arguments for the function
3565  if ((int)argTypes.size() > funcType->GetNumParameters()) {
3566  Error(args->pos,
3567  "Too many parameter values provided in "
3568  "function call (%d provided, %d expected).",
3569  (int)argTypes.size(), funcType->GetNumParameters());
3570  return NULL;
3571  }
3572  // It's ok to have too few arguments, as long as the function's
3573  // default parameter values have started by the time we run out
3574  // of arguments
3575  if ((int)argTypes.size() < funcType->GetNumParameters() &&
3576  funcType->GetParameterDefault(argTypes.size()) == NULL) {
3577  Error(args->pos,
3578  "Too few parameter values provided in "
3579  "function call (%d provided, %d expected).",
3580  (int)argTypes.size(), funcType->GetNumParameters());
3581  return NULL;
3582  }
3583 
3584  // Now make sure they can all type convert to the corresponding
3585  // parameter types..
3586  for (int i = 0; i < (int)argTypes.size(); ++i) {
3587  if (i < funcType->GetNumParameters()) {
3588  // make sure it can type convert
3589  const Type *paramType = funcType->GetParameterType(i);
3590  if (CanConvertTypes(argTypes[i], paramType) == false &&
3591  !(argCouldBeNULL[i] == true && CastType<PointerType>(paramType) != NULL)) {
3592  Error(args->exprs[i]->pos,
3593  "Can't convert argument of "
3594  "type \"%s\" to type \"%s\" for function call "
3595  "argument.",
3596  argTypes[i]->GetString().c_str(), paramType->GetString().c_str());
3597  return NULL;
3598  }
3599  } else
3600  // Otherwise the parameter default saves us. It should
3601  // be there for sure, given the check right above the
3602  // for loop.
3603  AssertPos(pos, funcType->GetParameterDefault(i) != NULL);
3604  }
3605 
3606  if (fptrType->IsVaryingType()) {
3607  const Type *retType = funcType->GetReturnType();
3608  if (retType->IsVoidType() == false && retType->IsUniformType()) {
3609  Error(pos,
3610  "Illegal to call a varying function pointer that "
3611  "points to a function with a uniform return type \"%s\".",
3612  funcType->GetReturnType()->GetString().c_str());
3613  return NULL;
3614  }
3615  }
3616  }
3617 
3618  if (func == NULL || args == NULL)
3619  return NULL;
3620  return this;
3621 }
3622 
3624  if (isLaunch)
3625  return COST_TASK_LAUNCH;
3626 
3627  const Type *type = func->GetType();
3628  if (type == NULL)
3629  return 0;
3630 
3631  const PointerType *pt = CastType<PointerType>(type);
3632  if (pt != NULL)
3633  type = type->GetBaseType();
3634  const FunctionType *ftype = CastType<FunctionType>(type);
3635 
3636  if (ftype->costOverride > -1)
3637  return ftype->costOverride;
3638 
3639  if (pt != NULL)
3641  else
3642  return COST_FUNCALL;
3643 }
3644 
3646  if (!func || !args || !GetType())
3647  return;
3648 
3649  printf("[%s] funcall %s ", GetType()->GetString().c_str(), isLaunch ? "launch" : "");
3650  func->Print();
3651  printf(" args (");
3652  args->Print();
3653  printf(")");
3654  pos.Print();
3655 }
3656 
3657 ///////////////////////////////////////////////////////////////////////////
3658 // ExprList
3659 
3660 llvm::Value *ExprList::GetValue(FunctionEmitContext *ctx) const {
3661  FATAL("ExprList::GetValue() should never be called");
3662  return NULL;
3663 }
3664 
3665 const Type *ExprList::GetType() const {
3666  FATAL("ExprList::GetType() should never be called");
3667  return NULL;
3668 }
3669 
3670 ExprList *ExprList::Optimize() { return this; }
3671 
3672 ExprList *ExprList::TypeCheck() { return this; }
3673 
3674 llvm::Constant *ExprList::GetConstant(const Type *type) const {
3675  if (exprs.size() == 1 &&
3676  (CastType<AtomicType>(type) != NULL || CastType<EnumType>(type) != NULL || CastType<PointerType>(type) != NULL))
3677  return exprs[0]->GetConstant(type);
3678 
3679  const CollectionType *collectionType = CastType<CollectionType>(type);
3680  if (collectionType == NULL)
3681  return NULL;
3682 
3683  std::string name;
3684  if (CastType<StructType>(type) != NULL)
3685  name = "struct";
3686  else if (CastType<ArrayType>(type) != NULL)
3687  name = "array";
3688  else if (CastType<VectorType>(type) != NULL)
3689  name = "vector";
3690  else
3691  FATAL("Unexpected CollectionType in ExprList::GetConstant()");
3692 
3693  if ((int)exprs.size() > collectionType->GetElementCount()) {
3694  Error(pos,
3695  "Initializer list for %s \"%s\" must have no more than %d "
3696  "elements (has %d).",
3697  name.c_str(), collectionType->GetString().c_str(), collectionType->GetElementCount(), (int)exprs.size());
3698  return NULL;
3699  }
3700 
3701  std::vector<llvm::Constant *> cv;
3702  for (unsigned int i = 0; i < exprs.size(); ++i) {
3703  if (exprs[i] == NULL)
3704  return NULL;
3705  const Type *elementType = collectionType->GetElementType(i);
3706 
3707  Expr *expr = exprs[i];
3708  if (llvm::dyn_cast<ExprList>(expr) == NULL) {
3709  // If there's a simple type conversion from the type of this
3710  // expression to the type we need, then let the regular type
3711  // conversion machinery handle it.
3712  expr = TypeConvertExpr(exprs[i], elementType, "initializer list");
3713  if (expr == NULL) {
3714  AssertPos(pos, m->errorCount > 0);
3715  return NULL;
3716  }
3717  // Re-establish const-ness if possible
3718  expr = ::Optimize(expr);
3719  }
3720 
3721  llvm::Constant *c = expr->GetConstant(elementType);
3722  if (c == NULL)
3723  // If this list element couldn't convert to the right constant
3724  // type for the corresponding collection member, then give up.
3725  return NULL;
3726  cv.push_back(c);
3727  }
3728 
3729  // If there are too few, then treat missing ones as if they were zero
3730  for (int i = (int)exprs.size(); i < collectionType->GetElementCount(); ++i) {
3731  const Type *elementType = collectionType->GetElementType(i);
3732  if (elementType == NULL) {
3733  AssertPos(pos, m->errorCount > 0);
3734  return NULL;
3735  }
3736 
3737  llvm::Type *llvmType = elementType->LLVMType(g->ctx);
3738  if (llvmType == NULL) {
3739  AssertPos(pos, m->errorCount > 0);
3740  return NULL;
3741  }
3742 
3743  llvm::Constant *c = llvm::Constant::getNullValue(llvmType);
3744  cv.push_back(c);
3745  }
3746 
3747  if (CastType<StructType>(type) != NULL) {
3748  llvm::StructType *llvmStructType = llvm::dyn_cast<llvm::StructType>(collectionType->LLVMType(g->ctx));
3749  AssertPos(pos, llvmStructType != NULL);
3750  return llvm::ConstantStruct::get(llvmStructType, cv);
3751  } else {
3752  llvm::Type *lt = type->LLVMType(g->ctx);
3753  llvm::ArrayType *lat = llvm::dyn_cast<llvm::ArrayType>(lt);
3754  if (lat != NULL)
3755  return llvm::ConstantArray::get(lat, cv);
3756  else {
3757  // uniform short vector type
3758  AssertPos(pos, type->IsUniformType() && CastType<VectorType>(type) != NULL);
3759 
3760  llvm::VectorType *lvt = llvm::dyn_cast<llvm::VectorType>(lt);
3761  AssertPos(pos, lvt != NULL);
3762 
3763  // Uniform short vectors are stored as vectors of length
3764  // rounded up to a power of 2 bits in size but not less then 128 bit.
3765  // So we add additional undef values here until we get the right size.
3766  const VectorType *vt = CastType<VectorType>(type);
3767  int vectorWidth = vt->getVectorMemoryCount();
3768 
3769  while ((cv.size() % vectorWidth) != 0) {
3770  cv.push_back(llvm::UndefValue::get(lvt->getElementType()));
3771  }
3772 
3773  return llvm::ConstantVector::get(cv);
3774  }
3775  }
3776  return NULL;
3777 }
3778 
3779 int ExprList::EstimateCost() const { return 0; }
3780 
3781 void ExprList::Print() const {
3782  printf("expr list (");
3783  for (unsigned int i = 0; i < exprs.size(); ++i) {
3784  if (exprs[i] != NULL)
3785  exprs[i]->Print();
3786  printf("%s", (i == exprs.size() - 1) ? ")" : ", ");
3787  }
3788  pos.Print();
3789 }
3790 
3791 ///////////////////////////////////////////////////////////////////////////
3792 // IndexExpr
3793 
3794 IndexExpr::IndexExpr(Expr *a, Expr *i, SourcePos p) : Expr(p, IndexExprID) {
3795  baseExpr = a;
3796  index = i;
3797  type = lvalueType = NULL;
3798 }
3799 
3800 /** When computing pointer values, we need to apply a per-lane offset when
3801  we have a varying pointer that is itself indexing into varying data.
3802  Consdier the following ispc code:
3803 
3804  uniform float u[] = ...;
3805  float v[] = ...;
3806  int index = ...;
3807  float a = u[index];
3808  float b = v[index];
3809 
3810  To compute the varying pointer that holds the addresses to load from
3811  for u[index], we basically just need to multiply index element-wise by
3812  sizeof(float) before doing the memory load. For v[index], we need to
3813  do the same scaling but also need to add per-lane offsets <0,
3814  sizeof(float), 2*sizeof(float), ...> so that the i'th lane loads the
3815  i'th of the varying values at its index value.
3816 
3817  This function handles figuring out when this additional offset is
3818  needed and then incorporates it in the varying pointer value.
3819  */
3820 static llvm::Value *lAddVaryingOffsetsIfNeeded(FunctionEmitContext *ctx, llvm::Value *ptr, const Type *ptrRefType) {
3821  if (CastType<ReferenceType>(ptrRefType) != NULL)
3822  // References are uniform pointers, so no offsetting is needed
3823  return ptr;
3824 
3825  const PointerType *ptrType = CastType<PointerType>(ptrRefType);
3826  Assert(ptrType != NULL);
3827  if (ptrType->IsUniformType() || ptrType->IsSlice())
3828  return ptr;
3829 
3830  const Type *baseType = ptrType->GetBaseType();
3831  if (baseType->IsVaryingType() == false)
3832  return ptr;
3833 
3834  // must be indexing into varying atomic, enum, or pointer types
3835  if (Type::IsBasicType(baseType) == false)
3836  return ptr;
3837 
3838  // Onward: compute the per lane offsets.
3839  llvm::Value *varyingOffsets = ctx->ProgramIndexVector();
3840 
3841  // And finally add the per-lane offsets. Note that we lie to the GEP
3842  // call and tell it that the pointers are to uniform elements and not
3843  // varying elements, so that the offsets in terms of (0,1,2,...) will
3844  // end up turning into the correct step in bytes...
3845  const Type *uniformElementType = baseType->GetAsUniformType();
3846  const Type *ptrUnifType = PointerType::GetVarying(uniformElementType);
3847  return ctx->GetElementPtrInst(ptr, varyingOffsets, ptrUnifType);
3848 }
3849 
3850 /** Check to see if the given type is an array of or pointer to a varying
3851  struct type that in turn has a member with bound 'uniform' variability.
3852  Issue an error and return true if such a member is found.
3853  */
3854 static bool lVaryingStructHasUniformMember(const Type *type, SourcePos pos) {
3855  if (CastType<VectorType>(type) != NULL || CastType<ReferenceType>(type) != NULL)
3856  return false;
3857 
3858  const StructType *st = CastType<StructType>(type);
3859  if (st == NULL) {
3860  const ArrayType *at = CastType<ArrayType>(type);
3861  if (at != NULL)
3862  st = CastType<StructType>(at->GetElementType());
3863  else {
3864  const PointerType *pt = CastType<PointerType>(type);
3865  if (pt == NULL)
3866  return false;
3867 
3868  st = CastType<StructType>(pt->GetBaseType());
3869  }
3870 
3871  if (st == NULL)
3872  return false;
3873  }
3874 
3875  if (st->IsVaryingType() == false)
3876  return false;
3877 
3878  for (int i = 0; i < st->GetElementCount(); ++i) {
3879  const Type *eltType = st->GetElementType(i);
3880  if (eltType == NULL) {
3881  AssertPos(pos, m->errorCount > 0);
3882  continue;
3883  }
3884 
3885  if (CastType<StructType>(eltType) != NULL) {
3886  // We know that the enclosing struct is varying at this point,
3887  // so push that down to the enclosed struct before makign the
3888  // recursive call.
3889  eltType = eltType->GetAsVaryingType();
3890  if (lVaryingStructHasUniformMember(eltType, pos))
3891  return true;
3892  } else if (eltType->IsUniformType()) {
3893  Error(pos,
3894  "Gather operation is impossible due to the presence of "
3895  "struct member \"%s\" with uniform type \"%s\" in the "
3896  "varying struct type \"%s\".",
3897  st->GetElementName(i).c_str(), eltType->GetString().c_str(), st->GetString().c_str());
3898  return true;
3899  }
3900  }
3901 
3902  return false;
3903 }
3904 
3905 llvm::Value *IndexExpr::GetValue(FunctionEmitContext *ctx) const {
3906  const Type *indexType, *returnType;
3907  if (baseExpr == NULL || index == NULL || ((indexType = index->GetType()) == NULL) ||
3908  ((returnType = GetType()) == NULL)) {
3909  AssertPos(pos, m->errorCount > 0);
3910  return NULL;
3911  }
3912 
3913  // If this is going to be a gather, make sure that the varying return
3914  // type can represent the result (i.e. that we don't have a bound
3915  // 'uniform' member in a varying struct...)
3916  if (indexType->IsVaryingType() && lVaryingStructHasUniformMember(returnType, pos))
3917  return NULL;
3918 
3919  ctx->SetDebugPos(pos);
3920 
3921  llvm::Value *ptr = GetLValue(ctx);
3922  llvm::Value *mask = NULL;
3923  const Type *lvType = GetLValueType();
3924  if (ptr == NULL) {
3925  // We may be indexing into a temporary that hasn't hit memory, so
3926  // get the full value and stuff it into temporary alloca'd space so
3927  // that we can index from there...
3928  const Type *baseExprType = baseExpr->GetType();
3929  llvm::Value *val = baseExpr->GetValue(ctx);
3930  if (baseExprType == NULL || val == NULL) {
3931  AssertPos(pos, m->errorCount > 0);
3932  return NULL;
3933  }
3934  ctx->SetDebugPos(pos);
3935  llvm::Value *tmpPtr = ctx->AllocaInst(baseExprType->LLVMType(g->ctx), "array_tmp");
3936  ctx->StoreInst(val, tmpPtr);
3937 
3938  // Get a pointer type to the underlying elements
3939  const SequentialType *st = CastType<SequentialType>(baseExprType);
3940  if (st == NULL) {
3941  Assert(m->errorCount > 0);
3942  return NULL;
3943  }
3944  lvType = PointerType::GetUniform(st->GetElementType());
3945 
3946  // And do the indexing calculation into the temporary array in memory
3947  ptr = ctx->GetElementPtrInst(tmpPtr, LLVMInt32(0), index->GetValue(ctx), PointerType::GetUniform(baseExprType));
3948  ptr = lAddVaryingOffsetsIfNeeded(ctx, ptr, lvType);
3949 
3950  mask = LLVMMaskAllOn;
3951  } else {
3952  Symbol *baseSym = GetBaseSymbol();
3953  if (llvm::dyn_cast<FunctionCallExpr>(baseExpr) == NULL && llvm::dyn_cast<BinaryExpr>(baseExpr) == NULL) {
3954  // Don't check if we're doing a function call or pointer arith
3955  AssertPos(pos, baseSym != NULL);
3956  }
3957  mask = lMaskForSymbol(baseSym, ctx);
3958  }
3959 
3960  ctx->SetDebugPos(pos);
3961  return ctx->LoadInst(ptr, mask, lvType);
3962 }
3963 
3964 const Type *IndexExpr::GetType() const {
3965  if (type != NULL)
3966  return type;
3967 
3968  const Type *baseExprType, *indexType;
3969  if (!baseExpr || !index || ((baseExprType = baseExpr->GetType()) == NULL) ||
3970  ((indexType = index->GetType()) == NULL))
3971  return NULL;
3972 
3973  const Type *elementType = NULL;
3974  const PointerType *pointerType = CastType<PointerType>(baseExprType);
3975  if (pointerType != NULL)
3976  // ptr[index] -> type that the pointer points to
3977  elementType = pointerType->GetBaseType();
3978  else {
3979  // sequential type[index] -> element type of the sequential type
3980  const SequentialType *sequentialType = CastType<SequentialType>(baseExprType->GetReferenceTarget());
3981  // Typechecking should have caught this...
3982  AssertPos(pos, sequentialType != NULL);
3983  elementType = sequentialType->GetElementType();
3984  }
3985 
3986  // If we're indexing into a sequence of SOA types, the result type is
3987  // actually the underlying type, as a uniform or varying. Get the
3988  // uniform variant of it for starters, then below we'll make it varying
3989  // if the index is varying.
3990  // (If we ever provide a way to index into SOA types and get an entire
3991  // SOA'd struct out of the array, then we won't want to do this in that
3992  // case..)
3993  if (elementType->IsSOAType())
3994  elementType = elementType->GetAsUniformType();
3995 
3996  // If either the index is varying or we're indexing into a varying
3997  // pointer, then the result type is the varying variant of the indexed
3998  // type.
3999  if (indexType->IsUniformType() && (pointerType == NULL || pointerType->IsUniformType()))
4000  type = elementType;
4001  else
4002  type = elementType->GetAsVaryingType();
4003 
4004  return type;
4005 }
4006 
4008 
4009 /** Utility routine that takes a regualr pointer (either uniform or
4010  varying) and returns a slice pointer with zero offsets.
4011  */
4012 static llvm::Value *lConvertToSlicePointer(FunctionEmitContext *ctx, llvm::Value *ptr,
4013  const PointerType *slicePtrType) {
4014  llvm::Type *llvmSlicePtrType = slicePtrType->LLVMType(g->ctx);
4015  llvm::StructType *sliceStructType = llvm::dyn_cast<llvm::StructType>(llvmSlicePtrType);
4016  Assert(sliceStructType != NULL && sliceStructType->getElementType(0) == ptr->getType());
4017 
4018  // Get a null-initialized struct to take care of having zeros for the
4019  // offsets
4020  llvm::Value *result = llvm::Constant::getNullValue(sliceStructType);
4021  // And replace the pointer in the struct with the given pointer
4022  return ctx->InsertInst(result, ptr, 0, LLVMGetName(ptr, "_slice"));
4023 }
4024 
4025 /** If the given array index is a compile time constant, check to see if it
4026  value/values don't go past the end of the array; issue a warning if
4027  so.
4028 */
4029 static void lCheckIndicesVersusBounds(const Type *baseExprType, Expr *index) {
4030  const SequentialType *seqType = CastType<SequentialType>(baseExprType);
4031  if (seqType == NULL)
4032  return;
4033 
4034  int nElements = seqType->GetElementCount();
4035  if (nElements == 0)
4036  // Unsized array...
4037  return;
4038 
4039  // If it's an array of soa<> items, then the number of elements to
4040  // worry about w.r.t. index values is the product of the array size and
4041  // the soa width.
4042  int soaWidth = seqType->GetElementType()->GetSOAWidth();
4043  if (soaWidth > 0)
4044  nElements *= soaWidth;
4045 
4046  ConstExpr *ce = llvm::dyn_cast<ConstExpr>(index);
4047  if (ce == NULL)
4048  return;
4049 
4050  int32_t indices[ISPC_MAX_NVEC];
4051  int count = ce->GetValues(indices);
4052  for (int i = 0; i < count; ++i) {
4053  if (indices[i] < 0 || indices[i] >= nElements)
4054  Warning(index->pos,
4055  "Array index \"%d\" may be out of bounds for %d "
4056  "element array.",
4057  indices[i], nElements);
4058  }
4059 }
4060 
4061 /** Converts the given pointer value to a slice pointer if the pointer
4062  points to SOA'ed data.
4063 */
4064 static llvm::Value *lConvertPtrToSliceIfNeeded(FunctionEmitContext *ctx, llvm::Value *ptr, const Type **type) {
4065  Assert(*type != NULL);
4066  const PointerType *ptrType = CastType<PointerType>(*type);
4067  bool convertToSlice = (ptrType->GetBaseType()->IsSOAType() && ptrType->IsSlice() == false);
4068  if (convertToSlice == false)
4069  return ptr;
4070 
4071  *type = ptrType->GetAsSlice();
4072  return lConvertToSlicePointer(ctx, ptr, ptrType->GetAsSlice());
4073 }
4074 
4075 llvm::Value *IndexExpr::GetLValue(FunctionEmitContext *ctx) const {
4076  const Type *baseExprType;
4077  if (baseExpr == NULL || index == NULL || ((baseExprType = baseExpr->GetType()) == NULL)) {
4078  AssertPos(pos, m->errorCount > 0);
4079  return NULL;
4080  }
4081 
4082  ctx->SetDebugPos(pos);
4083  llvm::Value *indexValue = index->GetValue(ctx);
4084  if (indexValue == NULL) {
4085  AssertPos(pos, m->errorCount > 0);
4086  return NULL;
4087  }
4088 
4089  ctx->SetDebugPos(pos);
4090  if (CastType<PointerType>(baseExprType) != NULL) {
4091  // We're indexing off of a pointer
4092  llvm::Value *basePtrValue = baseExpr->GetValue(ctx);
4093  if (basePtrValue == NULL) {
4094  AssertPos(pos, m->errorCount > 0);
4095  return NULL;
4096  }
4097  ctx->SetDebugPos(pos);
4098 
4099  // Convert to a slice pointer if we're indexing into SOA data
4100  basePtrValue = lConvertPtrToSliceIfNeeded(ctx, basePtrValue, &baseExprType);
4101 
4102  llvm::Value *ptr =
4103  ctx->GetElementPtrInst(basePtrValue, indexValue, baseExprType, LLVMGetName(basePtrValue, "_offset"));
4104  return lAddVaryingOffsetsIfNeeded(ctx, ptr, GetLValueType());
4105  }
4106 
4107  // Not a pointer: we must be indexing an array or vector (and possibly
4108  // a reference thereuponfore.)
4109  llvm::Value *basePtr = NULL;
4110  const PointerType *basePtrType = NULL;
4111  if (CastType<ArrayType>(baseExprType) || CastType<VectorType>(baseExprType)) {
4112  basePtr = baseExpr->GetLValue(ctx);
4113  basePtrType = CastType<PointerType>(baseExpr->GetLValueType());
4114  if (baseExpr->GetLValueType())
4115  AssertPos(pos, basePtrType != NULL);
4116  } else {
4117  baseExprType = baseExprType->GetReferenceTarget();
4118  AssertPos(pos, CastType<ArrayType>(baseExprType) || CastType<VectorType>(baseExprType));
4119  basePtr = baseExpr->GetValue(ctx);
4120  basePtrType = PointerType::GetUniform(baseExprType);
4121  }
4122  if (!basePtr)
4123  return NULL;
4124 
4125  // If possible, check the index value(s) against the size of the array
4126  lCheckIndicesVersusBounds(baseExprType, index);
4127 
4128  // Convert to a slice pointer if indexing into SOA data
4129  basePtr = lConvertPtrToSliceIfNeeded(ctx, basePtr, (const Type **)&basePtrType);
4130 
4131  ctx->SetDebugPos(pos);
4132 
4133  // And do the actual indexing calculation..
4134  llvm::Value *ptr =
4135  ctx->GetElementPtrInst(basePtr, LLVMInt32(0), indexValue, basePtrType, LLVMGetName(basePtr, "_offset"));
4136  return lAddVaryingOffsetsIfNeeded(ctx, ptr, GetLValueType());
4137 }
4138 
4140  if (lvalueType != NULL)
4141  return lvalueType;
4142 
4143  const Type *baseExprType, *baseExprLValueType, *indexType;
4144  if (baseExpr == NULL || index == NULL || ((baseExprType = baseExpr->GetType()) == NULL) ||
4145  ((baseExprLValueType = baseExpr->GetLValueType()) == NULL) || ((indexType = index->GetType()) == NULL))
4146  return NULL;
4147 
4148  // regularize to a PointerType
4149  if (CastType<ReferenceType>(baseExprLValueType) != NULL) {
4150  const Type *refTarget = baseExprLValueType->GetReferenceTarget();
4151  baseExprLValueType = PointerType::GetUniform(refTarget);
4152  }
4153  AssertPos(pos, CastType<PointerType>(baseExprLValueType) != NULL);
4154 
4155  // Find the type of thing that we're indexing into
4156  const Type *elementType;
4157  const SequentialType *st = CastType<SequentialType>(baseExprLValueType->GetBaseType());
4158  if (st != NULL)
4159  elementType = st->GetElementType();
4160  else {
4161  const PointerType *pt = CastType<PointerType>(baseExprLValueType->GetBaseType());
4162  // This assertion seems overly strict.
4163  // Why does it need to be a pointer to a pointer?
4164  // AssertPos(pos, pt != NULL);
4165 
4166  if (pt != NULL) {
4167  elementType = pt->GetBaseType();
4168  } else {
4169  elementType = baseExprLValueType->GetBaseType();
4170  }
4171  }
4172 
4173  // Are we indexing into a varying type, or are we indexing with a
4174  // varying pointer?
4175  bool baseVarying;
4176  if (CastType<PointerType>(baseExprType) != NULL)
4177  baseVarying = baseExprType->IsVaryingType();
4178  else
4179  baseVarying = baseExprLValueType->IsVaryingType();
4180 
4181  // The return type is uniform iff. the base is a uniform pointer / a
4182  // collection of uniform typed elements and the index is uniform.
4183  if (baseVarying == false && indexType->IsUniformType())
4184  lvalueType = PointerType::GetUniform(elementType);
4185  else
4186  lvalueType = PointerType::GetVarying(elementType);
4187 
4188  // Finally, if we're indexing into an SOA type, then the resulting
4189  // pointer must (currently) be a slice pointer; we don't allow indexing
4190  // the soa-width-wide structs directly.
4191  if (elementType->IsSOAType())
4193 
4194  return lvalueType;
4195 }
4196 
4198  if (baseExpr == NULL || index == NULL)
4199  return NULL;
4200  return this;
4201 }
4202 
4204  const Type *indexType;
4205  if (baseExpr == NULL || index == NULL || ((indexType = index->GetType()) == NULL)) {
4206  AssertPos(pos, m->errorCount > 0);
4207  return NULL;
4208  }
4209 
4210  const Type *baseExprType = baseExpr->GetType();
4211  if (baseExprType == NULL) {
4212  AssertPos(pos, m->errorCount > 0);
4213  return NULL;
4214  }
4215 
4216  if (!CastType<SequentialType>(baseExprType->GetReferenceTarget())) {
4217  if (const PointerType *pt = CastType<PointerType>(baseExprType)) {
4218  if (pt->GetBaseType()->IsVoidType()) {
4219  Error(pos, "Illegal to dereference void pointer type \"%s\".", baseExprType->GetString().c_str());
4220  return NULL;
4221  }
4222  } else {
4223  Error(pos,
4224  "Trying to index into non-array, vector, or pointer "
4225  "type \"%s\".",
4226  baseExprType->GetString().c_str());
4227  return NULL;
4228  }
4229  }
4230 
4231  bool isUniform = (index->GetType()->IsUniformType() && !g->opt.disableUniformMemoryOptimizations);
4232 
4233  if (!isUniform) {
4234  // Unless we have an explicit 64-bit index and are compiling to a
4235  // 64-bit target with 64-bit addressing, convert the index to an int32
4236  // type.
4237  // The range of varying index is limited to [0,2^31) as a result.
4241  const Type *indexType = AtomicType::VaryingInt32;
4242  index = TypeConvertExpr(index, indexType, "array index");
4243  if (index == NULL)
4244  return NULL;
4245  }
4246  } else { // isUniform
4247  // For 32-bit target:
4248  // force the index to 32 bit.
4249  // For 64-bit target:
4250  // We don't want to limit the index range.
4251  // We sxt/zxt the index to 64 bit right here because
4252  // LLVM doesn't distinguish unsigned from signed (both are i32)
4253  //
4254  // However, the index can be still truncated to signed int32 if
4255  // the index type is 64 bit and --addressing=32.
4256  bool force_32bit =
4259  const Type *indexType = force_32bit ? AtomicType::UniformInt32 : AtomicType::UniformInt64;
4260  index = TypeConvertExpr(index, indexType, "array index");
4261  if (index == NULL)
4262  return NULL;
4263  }
4264 
4265  return this;
4266 }
4267 
4269  if (index == NULL || baseExpr == NULL)
4270  return 0;
4271 
4272  const Type *indexType = index->GetType();
4273  const Type *baseExprType = baseExpr->GetType();
4274 
4275  if ((indexType != NULL && indexType->IsVaryingType()) ||
4276  (CastType<PointerType>(baseExprType) != NULL && baseExprType->IsVaryingType()))
4277  // be pessimistic; some of these will later turn out to be vector
4278  // loads/stores, but it's too early for us to know that here.
4279  return COST_GATHER;
4280  else
4281  return COST_LOAD;
4282 }
4283 
4284 void IndexExpr::Print() const {
4285  if (!baseExpr || !index || !GetType())
4286  return;
4287 
4288  printf("[%s] index ", GetType()->GetString().c_str());
4289  baseExpr->Print();
4290  printf("[");
4291  index->Print();
4292  printf("]");
4293  pos.Print();
4294 }
4295 
4296 ///////////////////////////////////////////////////////////////////////////
4297 // MemberExpr
4298 
4299 /** Map one character ids to vector element numbers. Allow a few different
4300  conventions--xyzw, rgba, uv.
4301 */
4302 static int lIdentifierToVectorElement(char id) {
4303  switch (id) {
4304  case 'x':
4305  case 'r':
4306  case 'u':
4307  return 0;
4308  case 'y':
4309  case 'g':
4310  case 'v':
4311  return 1;
4312  case 'z':
4313  case 'b':
4314  return 2;
4315  case 'w':
4316  case 'a':
4317  return 3;
4318  default:
4319  return -1;
4320  }
4321 }
4322 
4323 //////////////////////////////////////////////////
4324 // StructMemberExpr
4325 
4327  public:
4328  StructMemberExpr(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue);
4329 
4330  static inline bool classof(StructMemberExpr const *) { return true; }
4331  static inline bool classof(ASTNode const *N) { return N->getValueID() == StructMemberExprID; }
4332 
4333  const Type *GetType() const;
4334  const Type *GetLValueType() const;
4335  int getElementNumber() const;
4336  const Type *getElementType() const;
4337 
4338  private:
4339  const StructType *getStructType() const;
4340 };
4341 
4342 StructMemberExpr::StructMemberExpr(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue)
4343  : MemberExpr(e, id, p, idpos, derefLValue, StructMemberExprID) {}
4344 
4346  if (type != NULL)
4347  return type;
4348 
4349  // It's a struct, and the result type is the element type, possibly
4350  // promoted to varying if the struct type / lvalue is varying.
4351  const Type *exprType, *lvalueType;
4352  const StructType *structType;
4353  if (expr == NULL || ((exprType = expr->GetType()) == NULL) || ((structType = getStructType()) == NULL) ||
4354  ((lvalueType = GetLValueType()) == NULL)) {
4355  AssertPos(pos, m->errorCount > 0);
4356  return NULL;
4357  }
4358 
4359  const Type *elementType = structType->GetElementType(identifier);
4360  if (elementType == NULL) {
4361  Error(identifierPos, "Element name \"%s\" not present in struct type \"%s\".%s", identifier.c_str(),
4362  structType->GetString().c_str(), getCandidateNearMatches().c_str());
4363  return NULL;
4364  }
4365  AssertPos(pos, Type::Equal(lvalueType->GetBaseType(), elementType));
4366 
4367  bool isSlice = (CastType<PointerType>(lvalueType) && CastType<PointerType>(lvalueType)->IsSlice());
4368  if (isSlice) {
4369  // FIXME: not true if we allow bound unif/varying for soa<>
4370  // structs?...
4371  AssertPos(pos, elementType->IsSOAType());
4372 
4373  // If we're accessing a member of an soa structure via a uniform
4374  // slice pointer, then the result type is the uniform variant of
4375  // the element type.
4376  if (lvalueType->IsUniformType())
4377  elementType = elementType->GetAsUniformType();
4378  }
4379 
4380  if (lvalueType->IsVaryingType())
4381  // If the expression we're getting the member of has an lvalue that
4382  // is a varying pointer type (be it slice or non-slice), then the
4383  // result type must be the varying version of the element type.
4384  elementType = elementType->GetAsVaryingType();
4385 
4386  type = elementType;
4387  return type;
4388 }
4389 
4391  if (lvalueType != NULL)
4392  return lvalueType;
4393 
4394  if (expr == NULL) {
4395  AssertPos(pos, m->errorCount > 0);
4396  return NULL;
4397  }
4398 
4399  const Type *exprLValueType = dereferenceExpr ? expr->GetType() : expr->GetLValueType();
4400  if (exprLValueType == NULL) {
4401  AssertPos(pos, m->errorCount > 0);
4402  return NULL;
4403  }
4404 
4405  // The pointer type is varying if the lvalue type of the expression is
4406  // varying (and otherwise uniform)
4407  const PointerType *ptrType = (exprLValueType->IsUniformType() || CastType<ReferenceType>(exprLValueType) != NULL)
4410 
4411  // If struct pointer is a slice pointer, the resulting member pointer
4412  // needs to be a frozen slice pointer--i.e. any further indexing with
4413  // the result shouldn't modify the minor slice offset, but it should be
4414  // left unchanged until we get to a leaf SOA value.
4415  if (CastType<PointerType>(exprLValueType) && CastType<PointerType>(exprLValueType)->IsSlice())
4416  ptrType = ptrType->GetAsFrozenSlice();
4417 
4418  lvalueType = ptrType;
4419  return lvalueType;
4420 }
4421 
4423  const StructType *structType = getStructType();
4424  if (structType == NULL)
4425  return -1;
4426 
4427  int elementNumber = structType->GetElementNumber(identifier);
4428  if (elementNumber == -1)
4429  Error(identifierPos, "Element name \"%s\" not present in struct type \"%s\".%s", identifier.c_str(),
4430  structType->GetString().c_str(), getCandidateNearMatches().c_str());
4431 
4432  return elementNumber;
4433 }
4434 
4436  const StructType *structType = getStructType();
4437  if (structType == NULL)
4438  return NULL;
4439 
4440  return structType->GetElementType(identifier);
4441 }
4442 
4443 /** Returns the type of the underlying struct that we're returning a member
4444  of. */
4446  const Type *type = dereferenceExpr ? expr->GetType() : expr->GetLValueType();
4447  if (type == NULL)
4448  return NULL;
4449 
4450  const Type *structType;
4451  const ReferenceType *rt = CastType<ReferenceType>(type);
4452  if (rt != NULL)
4453  structType = rt->GetReferenceTarget();
4454  else {
4455  const PointerType *pt = CastType<PointerType>(type);
4456  AssertPos(pos, pt != NULL);
4457  structType = pt->GetBaseType();
4458  }
4459 
4460  const StructType *ret = CastType<StructType>(structType);
4461  AssertPos(pos, ret != NULL);
4462  return ret;
4463 }
4464 
4465 //////////////////////////////////////////////////
4466 // VectorMemberExpr
4467 
4469  public:
4470  VectorMemberExpr(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue);
4471 
4472  static inline bool classof(VectorMemberExpr const *) { return true; }
4473  static inline bool classof(ASTNode const *N) { return N->getValueID() == VectorMemberExprID; }
4474 
4475  llvm::Value *GetValue(FunctionEmitContext *ctx) const;
4476  llvm::Value *GetLValue(FunctionEmitContext *ctx) const;
4477  const Type *GetType() const;
4478  const Type *GetLValueType() const;
4479 
4480  int getElementNumber() const;
4481  const Type *getElementType() const;
4482 
4483  private:
4486 };
4487 
4488 VectorMemberExpr::VectorMemberExpr(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue)
4489  : MemberExpr(e, id, p, idpos, derefLValue, VectorMemberExprID) {
4490  const Type *exprType = e->GetType();
4491  exprVectorType = CastType<VectorType>(exprType);
4492  if (exprVectorType == NULL) {
4493  const PointerType *pt = CastType<PointerType>(exprType);
4494  if (pt != NULL)
4495  exprVectorType = CastType<VectorType>(pt->GetBaseType());
4496  else {
4497  AssertPos(pos, CastType<ReferenceType>(exprType) != NULL);
4498  exprVectorType = CastType<VectorType>(exprType->GetReferenceTarget());
4499  }
4500  AssertPos(pos, exprVectorType != NULL);
4501  }
4503 }
4504 
4506  if (type != NULL)
4507  return type;
4508 
4509  // For 1-element expressions, we have the base vector element
4510  // type. For n-element expressions, we have a shortvec type
4511  // with n > 1 elements. This can be changed when we get
4512  // type<1> -> type conversions.
4513  type = (identifier.length() == 1) ? (const Type *)exprVectorType->GetElementType() : (const Type *)memberType;
4514 
4515  const Type *lvType = GetLValueType();
4516  if (lvType != NULL) {
4517  bool isSlice = (CastType<PointerType>(lvType) && CastType<PointerType>(lvType)->IsSlice());
4518  if (isSlice) {
4519  // CO AssertPos(pos, type->IsSOAType());
4520  if (lvType->IsUniformType())
4521  type = type->GetAsUniformType();
4522  }
4523 
4524  if (lvType->IsVaryingType())
4525  type = type->GetAsVaryingType();
4526  }
4527 
4528  return type;
4529 }
4530 
4532  if (identifier.length() == 1) {
4533  return MemberExpr::GetLValue(ctx);
4534  } else {
4535  return NULL;
4536  }
4537 }
4538 
4540  if (lvalueType != NULL)
4541  return lvalueType;
4542 
4543  if (identifier.length() == 1) {
4544  if (expr == NULL) {
4545  AssertPos(pos, m->errorCount > 0);
4546  return NULL;
4547  }
4548 
4549  const Type *exprLValueType = dereferenceExpr ? expr->GetType() : expr->GetLValueType();
4550  if (exprLValueType == NULL)
4551  return NULL;
4552 
4553  const VectorType *vt = NULL;
4554  if (CastType<ReferenceType>(exprLValueType) != NULL)
4555  vt = CastType<VectorType>(exprLValueType->GetReferenceTarget());
4556  else
4557  vt = CastType<VectorType>(exprLValueType->GetBaseType());
4558  AssertPos(pos, vt != NULL);
4559 
4560  // we don't want to report that it's e.g. a pointer to a float<1>,
4561  // but a pointer to a float, etc.
4562  const Type *elementType = vt->GetElementType();
4563  if (CastType<ReferenceType>(exprLValueType) != NULL)
4564  lvalueType = new ReferenceType(elementType);
4565  else {
4566  const PointerType *ptrType = exprLValueType->IsUniformType() ? PointerType::GetUniform(elementType)
4567  : PointerType::GetVarying(elementType);
4568  // FIXME: replicated logic with structmemberexpr....
4569  if (CastType<PointerType>(exprLValueType) && CastType<PointerType>(exprLValueType)->IsSlice())
4570  ptrType = ptrType->GetAsFrozenSlice();
4571  lvalueType = ptrType;
4572  }
4573  }
4574 
4575  return lvalueType;
4576 }
4577 
4579  if (identifier.length() == 1) {
4580  return MemberExpr::GetValue(ctx);
4581  } else {
4582  std::vector<int> indices;
4583 
4584  for (size_t i = 0; i < identifier.size(); ++i) {
4585  int idx = lIdentifierToVectorElement(identifier[i]);
4586  if (idx == -1)
4587  Error(pos, "Invalid swizzle charcter '%c' in swizzle \"%s\".", identifier[i], identifier.c_str());
4588 
4589  indices.push_back(idx);
4590  }
4591 
4592  llvm::Value *basePtr = NULL;
4593  const Type *basePtrType = NULL;
4594  if (dereferenceExpr) {
4595  basePtr = expr->GetValue(ctx);
4596  basePtrType = expr->GetType();
4597  } else {
4598  basePtr = expr->GetLValue(ctx);
4599  basePtrType = expr->GetLValueType();
4600  }
4601 
4602  if (basePtr == NULL || basePtrType == NULL) {
4603  AssertPos(pos, m->errorCount > 0);
4604  return NULL;
4605  }
4606 
4607  // Allocate temporary memory to tore the result
4608  llvm::Value *resultPtr = ctx->AllocaInst(memberType->LLVMType(g->ctx), "vector_tmp");
4609 
4610  // FIXME: we should be able to use the internal mask here according
4611  // to the same logic where it's used elsewhere
4612  llvm::Value *elementMask = ctx->GetFullMask();
4613 
4614  const Type *elementPtrType = NULL;
4615  if (CastType<ReferenceType>(basePtrType) != NULL)
4616  elementPtrType = PointerType::GetUniform(basePtrType->GetReferenceTarget());
4617  else
4618  elementPtrType = basePtrType->IsUniformType() ? PointerType::GetUniform(exprVectorType->GetElementType())
4620 
4621  ctx->SetDebugPos(pos);
4622  for (size_t i = 0; i < identifier.size(); ++i) {
4623  char idStr[2] = {identifier[i], '\0'};
4624  llvm::Value *elementPtr =
4625  ctx->AddElementOffset(basePtr, indices[i], basePtrType, LLVMGetName(basePtr, idStr));
4626  llvm::Value *elementValue = ctx->LoadInst(elementPtr, elementMask, elementPtrType);
4627 
4628  const char *resultName = LLVMGetName(resultPtr, idStr);
4629  llvm::Value *ptmp = ctx->AddElementOffset(resultPtr, i, NULL, resultName);
4630  ctx->StoreInst(elementValue, ptmp);
4631  }
4632 
4633  return ctx->LoadInst(resultPtr, LLVMGetName(basePtr, "_swizzle"));
4634  }
4635 }
4636 
4638  int elementNumber = lIdentifierToVectorElement(identifier[0]);
4639  if (elementNumber == -1)
4640  Error(pos, "Vector element identifier \"%s\" unknown.", identifier.c_str());
4641  return elementNumber;
4642 }
4643 
4645 
4646 MemberExpr *MemberExpr::create(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue) {
4647  // FIXME: we need to call TypeCheck() here so that we can call
4648  // e->GetType() in the following. But really we just shouldn't try to
4649  // resolve this now but just have a generic MemberExpr type that
4650  // handles all cases so that this is unnecessary.
4651  e = ::TypeCheck(e);
4652 
4653  const Type *exprType;
4654  if (e == NULL || (exprType = e->GetType()) == NULL)
4655  return NULL;
4656 
4657  const ReferenceType *referenceType = CastType<ReferenceType>(exprType);
4658  if (referenceType != NULL) {
4659  e = new RefDerefExpr(e, e->pos);
4660  exprType = e->GetType();
4661  Assert(exprType != NULL);
4662  }
4663 
4664  const PointerType *pointerType = CastType<PointerType>(exprType);
4665  if (pointerType != NULL)
4666  exprType = pointerType->GetBaseType();
4667 
4668  if (derefLValue == true && pointerType == NULL) {
4669  const Type *targetType = exprType->GetReferenceTarget();
4670  if (CastType<StructType>(targetType) != NULL)
4671  Error(p,
4672  "Member operator \"->\" can't be applied to non-pointer "
4673  "type \"%s\". Did you mean to use \".\"?",
4674  exprType->GetString().c_str());
4675  else
4676  Error(p,
4677  "Member operator \"->\" can't be applied to non-struct "
4678  "pointer type \"%s\".",
4679  exprType->GetString().c_str());
4680  return NULL;
4681  }
4682  if (derefLValue == false && pointerType != NULL && CastType<StructType>(pointerType->GetBaseType()) != NULL) {
4683  Error(p,
4684  "Member operator \".\" can't be applied to pointer "
4685  "type \"%s\". Did you mean to use \"->\"?",
4686  exprType->GetString().c_str());
4687  return NULL;
4688  }
4689  if (CastType<StructType>(exprType) != NULL) {
4690  const StructType *st = CastType<StructType>(exprType);
4691  if (st->IsDefined()) {
4692  return new StructMemberExpr(e, id, p, idpos, derefLValue);
4693  } else {
4694  Error(p,
4695  "Member operator \"%s\" can't be applied to declared "
4696  "struct \"%s\" containing an undefined struct type.",
4697  derefLValue ? "->" : ".", exprType->GetString().c_str());
4698  return NULL;
4699  }
4700  } else if (CastType<VectorType>(exprType) != NULL)
4701  return new VectorMemberExpr(e, id, p, idpos, derefLValue);
4702  else if (CastType<UndefinedStructType>(exprType)) {
4703  Error(p,
4704  "Member operator \"%s\" can't be applied to declared "
4705  "but not defined struct type \"%s\".",
4706  derefLValue ? "->" : ".", exprType->GetString().c_str());
4707  return NULL;
4708  } else {
4709  Error(p,
4710  "Member operator \"%s\" can't be used with expression of "
4711  "\"%s\" type.",
4712  derefLValue ? "->" : ".", exprType->GetString().c_str());
4713  return NULL;
4714  }
4715 }
4716 
4717 MemberExpr::MemberExpr(Expr *e, const char *id, SourcePos p, SourcePos idpos, bool derefLValue, unsigned scid)
4718  : Expr(p, scid), identifierPos(idpos) {
4719  expr = e;
4720  identifier = id;
4721  dereferenceExpr = derefLValue;
4722  type = lvalueType = NULL;
4723 }
4724 
4725 llvm::Value *MemberExpr::GetValue(FunctionEmitContext *ctx) const {
4726  if (!expr)
4727  return NULL;
4728 
4729  llvm::Value *lvalue = GetLValue(ctx);
4730  const Type *lvalueType = GetLValueType();
4731 
4732  llvm::Value *mask = NULL;
4733  if (lvalue == NULL) {
4734  if (m->errorCount > 0)
4735  return NULL;
4736 
4737  // As in the array case, this may be a temporary that hasn't hit
4738  // memory; get the full value and stuff it into a temporary array
4739  // so that we can index from there...
4740  llvm::Value *val = expr->GetValue(ctx);
4741  if (!val) {
4742  AssertPos(pos, m->errorCount > 0);
4743  return NULL;
4744  }
4745  ctx->SetDebugPos(pos);
4746  const Type *exprType = expr->GetType();
4747  llvm::Value *ptr = ctx->AllocaInst(exprType->LLVMType(g->ctx), "struct_tmp");
4748  ctx->StoreInst(val, ptr);
4749 
4750  int elementNumber = getElementNumber();
4751  if (elementNumber == -1)
4752  return NULL;
4753 
4754  lvalue = ctx->AddElementOffset(ptr, elementNumber, PointerType::GetUniform(exprType));
4756  mask = LLVMMaskAllOn;
4757  } else {
4758  Symbol *baseSym = GetBaseSymbol();
4759  AssertPos(pos, baseSym != NULL);
4760  mask = lMaskForSymbol(baseSym, ctx);
4761  }
4762 
4763  ctx->SetDebugPos(pos);
4764  std::string suffix = std::string("_") + identifier;
4765  return ctx->LoadInst(lvalue, mask, lvalueType, LLVMGetName(lvalue, suffix.c_str()));
4766 }
4767 
4768 const Type *MemberExpr::GetType() const { return NULL; }
4769 
4770 Symbol *MemberExpr::GetBaseSymbol() const { return expr ? expr->GetBaseSymbol() : NULL; }
4771 
4772 int MemberExpr::getElementNumber() const { return -1; }
4773 
4774 llvm::Value *MemberExpr::GetLValue(FunctionEmitContext *ctx) const {
4775  const Type *exprType;
4776  if (!expr || ((exprType = expr->GetType()) == NULL))
4777  return NULL;
4778 
4779  ctx->SetDebugPos(pos);
4780  llvm::Value *basePtr = dereferenceExpr ? expr->GetValue(ctx) : expr->GetLValue(ctx);
4781  if (!basePtr)
4782  return NULL;
4783 
4784  int elementNumber = getElementNumber();
4785  if (elementNumber == -1)
4786  return NULL;
4787 
4788  const Type *exprLValueType = dereferenceExpr ? expr->GetType() : expr->GetLValueType();
4789  ctx->SetDebugPos(pos);
4790  llvm::Value *ptr = ctx->AddElementOffset(basePtr, elementNumber, exprLValueType, basePtr->getName().str().c_str());
4791  if (ptr == NULL) {
4792  AssertPos(pos, m->errorCount > 0);
4793  return NULL;
4794  }
4795 
4796  ptr = lAddVaryingOffsetsIfNeeded(ctx, ptr, GetLValueType());
4797 
4798  return ptr;
4799 }
4800 
4801 Expr *MemberExpr::TypeCheck() { return expr ? this : NULL; }
4802 
4803 Expr *MemberExpr::Optimize() { return expr ? this : NULL; }
4804 
4806  const Type *lvalueType = GetLValueType();
4807  if (lvalueType != NULL && lvalueType->IsVaryingType())
4809  else
4811 }
4812 
4813 void MemberExpr::Print() const {
4814  if (!expr || !GetType())
4815  return;
4816 
4817  printf("[%s] member (", GetType()->GetString().c_str());
4818  expr->Print();
4819  printf(" . %s)", identifier.c_str());
4820  pos.Print();
4821 }
4822 
4823 /** There is no structure member with the name we've got in "identifier".
4824  Use the approximate string matching routine to see if the identifier is
4825  a minor misspelling of one of the ones that is there.
4826  */
4828  const StructType *structType = CastType<StructType>(expr->GetType());
4829  if (!structType)
4830  return "";
4831 
4832  std::vector<std::string> elementNames;
4833  for (int i = 0; i < structType->GetElementCount(); ++i)
4834  elementNames.push_back(structType->GetElementName(i));
4835  std::vector<std::string> alternates = MatchStrings(identifier, elementNames);
4836  if (!alternates.size())
4837  return "";
4838 
4839  std::string ret = " Did you mean ";
4840  for (unsigned int i = 0; i < alternates.size(); ++i) {
4841  ret += std::string("\"") + alternates[i] + std::string("\"");
4842  if (i < alternates.size() - 1)
4843  ret += ", or ";
4844  }
4845  ret += "?";
4846  return ret;
4847 }
4848 
4849 ///////////////////////////////////////////////////////////////////////////
4850 // ConstExpr
4851 
4852 ConstExpr::ConstExpr(const Type *t, int8_t i, SourcePos p) : Expr(p, ConstExprID) {
4853  type = t;
4854  type = type->GetAsConstType();
4855  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt8->GetAsConstType()));
4856  int8Val[0] = i;
4857 }
4858 
4859 ConstExpr::ConstExpr(const Type *t, int8_t *i, SourcePos p) : Expr(p, ConstExprID) {
4860  type = t;
4861  type = type->GetAsConstType();
4862  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt8->GetAsConstType()) ||
4863  Type::Equal(type, AtomicType::VaryingInt8->GetAsConstType()));
4864  for (int j = 0; j < Count(); ++j)
4865  int8Val[j] = i[j];
4866 }
4867 
4868 ConstExpr::ConstExpr(const Type *t, uint8_t u, SourcePos p) : Expr(p, ConstExprID) {
4869  type = t;
4870  type = type->GetAsConstType();
4871  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt8->GetAsConstType()));
4872  uint8Val[0] = u;
4873 }
4874 
4875 ConstExpr::ConstExpr(const Type *t, uint8_t *u, SourcePos p) : Expr(p, ConstExprID) {
4876  type = t;
4877  type = type->GetAsConstType();
4878  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt8->GetAsConstType()) ||
4879  Type::Equal(type, AtomicType::VaryingUInt8->GetAsConstType()));
4880  for (int j = 0; j < Count(); ++j)
4881  uint8Val[j] = u[j];
4882 }
4883 
4884 ConstExpr::ConstExpr(const Type *t, int16_t i, SourcePos p) : Expr(p, ConstExprID) {
4885  type = t;
4886  type = type->GetAsConstType();
4887  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt16->GetAsConstType()));
4888  int16Val[0] = i;
4889 }
4890 
4891 ConstExpr::ConstExpr(const Type *t, int16_t *i, SourcePos p) : Expr(p, ConstExprID) {
4892  type = t;
4893  type = type->GetAsConstType();
4894  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt16->GetAsConstType()) ||
4895  Type::Equal(type, AtomicType::VaryingInt16->GetAsConstType()));
4896  for (int j = 0; j < Count(); ++j)
4897  int16Val[j] = i[j];
4898 }
4899 
4900 ConstExpr::ConstExpr(const Type *t, uint16_t u, SourcePos p) : Expr(p, ConstExprID) {
4901  type = t;
4902  type = type->GetAsConstType();
4903  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt16->GetAsConstType()));
4904  uint16Val[0] = u;
4905 }
4906 
4907 ConstExpr::ConstExpr(const Type *t, uint16_t *u, SourcePos p) : Expr(p, ConstExprID) {
4908  type = t;
4909  type = type->GetAsConstType();
4910  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt16->GetAsConstType()) ||
4911  Type::Equal(type, AtomicType::VaryingUInt16->GetAsConstType()));
4912  for (int j = 0; j < Count(); ++j)
4913  uint16Val[j] = u[j];
4914 }
4915 
4916 ConstExpr::ConstExpr(const Type *t, int32_t i, SourcePos p) : Expr(p, ConstExprID) {
4917  type = t;
4918  type = type->GetAsConstType();
4919  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt32->GetAsConstType()));
4920  int32Val[0] = i;
4921 }
4922 
4923 ConstExpr::ConstExpr(const Type *t, int32_t *i, SourcePos p) : Expr(p, ConstExprID) {
4924  type = t;
4925  type = type->GetAsConstType();
4926  AssertPos(pos, Type::Equal(type, AtomicType::UniformInt32->GetAsConstType()) ||
4927  Type::Equal(type, AtomicType::VaryingInt32->GetAsConstType()));
4928  for (int j = 0; j < Count(); ++j)
4929  int32Val[j] = i[j];
4930 }
4931 
4932 ConstExpr::ConstExpr(const Type *t, uint32_t u, SourcePos p) : Expr(p, ConstExprID) {
4933  type = t;
4934  type = type->GetAsConstType();
4935  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt32->GetAsConstType()) ||
4936  (CastType<EnumType>(type) != NULL && type->IsUniformType()));
4937  uint32Val[0] = u;
4938 }
4939 
4940 ConstExpr::ConstExpr(const Type *t, uint32_t *u, SourcePos p) : Expr(p, ConstExprID) {
4941  type = t;
4942  type = type->GetAsConstType();
4943  AssertPos(pos, Type::Equal(type, AtomicType::UniformUInt32->GetAsConstType()) ||
4944  Type::Equal(type, AtomicType::VaryingUInt32->GetAsConstType()) ||
4945  (CastType<EnumType>(type) != NULL));
4946  for (int j = 0; j < Count(); ++j)
4947  uint32Val[j] = u[j];
4948 }
4949 
4950 ConstExpr::ConstExpr(const Type *t, float f, SourcePos p) : Expr(p, ConstExprID) {
4951  type = t;
4952  type = type->GetAsConstType();
4953  AssertPos(pos, Type::Equal(type, AtomicType::UniformFloat->GetAsConstType()));
4954  floatVal[0] = f;
4955 }
4956 
4957 ConstExpr::ConstExpr(const Type *t, float *f, SourcePos p) : Expr(p, ConstExprID) {
4958  type = t;
4959  type = type->GetAsConstType();
4960  AssertPos(pos, Type::Equal(type, AtomicType::UniformFloat->GetAsConstType()) ||
4961  Type::Equal(type, AtomicType::VaryingFloat->GetAsConstType()));