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