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