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