CGExprScalar.cpp revision de7fb8413b13651fd85b7125d08b3c9ac2816d9d
1//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
18#include "clang/Basic/TargetInfo.h"
19#include "llvm/Constants.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Support/Compiler.h"
24#include <cstdarg>
25
26using namespace clang;
27using namespace CodeGen;
28using llvm::Value;
29
30//===----------------------------------------------------------------------===//
31//                         Scalar Expression Emitter
32//===----------------------------------------------------------------------===//
33
34struct BinOpInfo {
35  Value *LHS;
36  Value *RHS;
37  QualType Ty;  // Computation Type.
38  const BinaryOperator *E;
39};
40
41namespace {
42class VISIBILITY_HIDDEN ScalarExprEmitter
43  : public StmtVisitor<ScalarExprEmitter, Value*> {
44  CodeGenFunction &CGF;
45  llvm::IRBuilder<> &Builder;
46  CGObjCRuntime *Runtime;
47
48public:
49
50  ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
51    Builder(CGF.Builder),
52    Runtime(CGF.CGM.getObjCRuntime()) {
53  }
54
55  //===--------------------------------------------------------------------===//
56  //                               Utilities
57  //===--------------------------------------------------------------------===//
58
59  const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
60  LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
61
62  Value *EmitLoadOfLValue(LValue LV, QualType T) {
63    return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
64  }
65
66  /// EmitLoadOfLValue - Given an expression with complex type that represents a
67  /// value l-value, this method emits the address of the l-value, then loads
68  /// and returns the result.
69  Value *EmitLoadOfLValue(const Expr *E) {
70    // FIXME: Volatile
71    return EmitLoadOfLValue(EmitLValue(E), E->getType());
72  }
73
74  /// EmitConversionToBool - Convert the specified expression value to a
75  /// boolean (i1) truth value.  This is equivalent to "Val != 0".
76  Value *EmitConversionToBool(Value *Src, QualType DstTy);
77
78  /// EmitScalarConversion - Emit a conversion from the specified type to the
79  /// specified destination type, both of which are LLVM scalar types.
80  Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
81
82  /// EmitComplexToScalarConversion - Emit a conversion from the specified
83  /// complex type to the specified destination type, where the destination
84  /// type is an LLVM scalar type.
85  Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
86                                       QualType SrcTy, QualType DstTy);
87
88  //===--------------------------------------------------------------------===//
89  //                            Visitor Methods
90  //===--------------------------------------------------------------------===//
91
92  Value *VisitStmt(Stmt *S) {
93    S->dump(CGF.getContext().getSourceManager());
94    assert(0 && "Stmt can't have complex result type!");
95    return 0;
96  }
97  Value *VisitExpr(Expr *S);
98  Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
99
100  // Leaves.
101  Value *VisitIntegerLiteral(const IntegerLiteral *E) {
102    return llvm::ConstantInt::get(E->getValue());
103  }
104  Value *VisitFloatingLiteral(const FloatingLiteral *E) {
105    return llvm::ConstantFP::get(E->getValue());
106  }
107  Value *VisitCharacterLiteral(const CharacterLiteral *E) {
108    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
109  }
110  Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
111    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
112  }
113  Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
114    return llvm::ConstantInt::get(ConvertType(E->getType()),
115                                  CGF.getContext().typesAreCompatible(
116                                    E->getArgType1(), E->getArgType2()));
117  }
118  Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
119    return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
120  }
121  Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
122    Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty,
123                                      CGF.GetIDForAddrOfLabel(E->getLabel()));
124    return Builder.CreateIntToPtr(V,
125                                  llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
126  }
127
128  // l-values.
129  Value *VisitDeclRefExpr(DeclRefExpr *E) {
130    if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
131      return llvm::ConstantInt::get(EC->getInitVal());
132    return EmitLoadOfLValue(E);
133  }
134  Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
135  Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
136  Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
137  Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
138  Value *VisitMemberExpr(Expr *E)           { return EmitLoadOfLValue(E); }
139  Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
140  Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
141  Value *VisitStringLiteral(Expr *E)  { return EmitLValue(E).getAddress(); }
142  Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
143
144  Value *VisitInitListExpr(InitListExpr *E) {
145    unsigned NumInitElements = E->getNumInits();
146
147    const llvm::VectorType *VType =
148      dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
149
150    // We have a scalar in braces. Just use the first element.
151    if (!VType)
152      return Visit(E->getInit(0));
153
154    unsigned NumVectorElements = VType->getNumElements();
155    const llvm::Type *ElementType = VType->getElementType();
156
157    // Emit individual vector element stores.
158    llvm::Value *V = llvm::UndefValue::get(VType);
159
160    // Emit initializers
161    unsigned i;
162    for (i = 0; i < NumInitElements; ++i) {
163      Value *NewV = Visit(E->getInit(i));
164      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
165      V = Builder.CreateInsertElement(V, NewV, Idx);
166    }
167
168    // Emit remaining default initializers
169    for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
170      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
171      llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
172      V = Builder.CreateInsertElement(V, NewV, Idx);
173    }
174
175    return V;
176  }
177
178  Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
179  Value *VisitCastExpr(const CastExpr *E) {
180    return EmitCastExpr(E->getSubExpr(), E->getType());
181  }
182  Value *EmitCastExpr(const Expr *E, QualType T);
183
184  Value *VisitCallExpr(const CallExpr *E) {
185    return CGF.EmitCallExpr(E).getScalarVal();
186  }
187
188  Value *VisitStmtExpr(const StmtExpr *E);
189
190  // Unary Operators.
191  Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
192  Value *VisitUnaryPostDec(const UnaryOperator *E) {
193    return VisitPrePostIncDec(E, false, false);
194  }
195  Value *VisitUnaryPostInc(const UnaryOperator *E) {
196    return VisitPrePostIncDec(E, true, false);
197  }
198  Value *VisitUnaryPreDec(const UnaryOperator *E) {
199    return VisitPrePostIncDec(E, false, true);
200  }
201  Value *VisitUnaryPreInc(const UnaryOperator *E) {
202    return VisitPrePostIncDec(E, true, true);
203  }
204  Value *VisitUnaryAddrOf(const UnaryOperator *E) {
205    return EmitLValue(E->getSubExpr()).getAddress();
206  }
207  Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
208  Value *VisitUnaryPlus(const UnaryOperator *E) {
209    return Visit(E->getSubExpr());
210  }
211  Value *VisitUnaryMinus    (const UnaryOperator *E);
212  Value *VisitUnaryNot      (const UnaryOperator *E);
213  Value *VisitUnaryLNot     (const UnaryOperator *E);
214  Value *VisitUnarySizeOf   (const UnaryOperator *E) {
215    return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
216  }
217  Value *VisitUnaryAlignOf  (const UnaryOperator *E) {
218    return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
219  }
220  Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
221                         bool isSizeOf);
222  Value *VisitUnaryReal     (const UnaryOperator *E);
223  Value *VisitUnaryImag     (const UnaryOperator *E);
224  Value *VisitUnaryExtension(const UnaryOperator *E) {
225    return Visit(E->getSubExpr());
226  }
227  Value *VisitUnaryOffsetOf(const UnaryOperator *E);
228  Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
229    return Visit(DAE->getExpr());
230  }
231
232  // Binary Operators.
233  Value *EmitMul(const BinOpInfo &Ops) {
234    return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
235  }
236  Value *EmitDiv(const BinOpInfo &Ops);
237  Value *EmitRem(const BinOpInfo &Ops);
238  Value *EmitAdd(const BinOpInfo &Ops);
239  Value *EmitSub(const BinOpInfo &Ops);
240  Value *EmitShl(const BinOpInfo &Ops);
241  Value *EmitShr(const BinOpInfo &Ops);
242  Value *EmitAnd(const BinOpInfo &Ops) {
243    return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
244  }
245  Value *EmitXor(const BinOpInfo &Ops) {
246    return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
247  }
248  Value *EmitOr (const BinOpInfo &Ops) {
249    return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
250  }
251
252  BinOpInfo EmitBinOps(const BinaryOperator *E);
253  Value *EmitCompoundAssign(const CompoundAssignOperator *E,
254                            Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
255
256  // Binary operators and binary compound assignment operators.
257#define HANDLEBINOP(OP) \
258  Value *VisitBin ## OP(const BinaryOperator *E) {                         \
259    return Emit ## OP(EmitBinOps(E));                                      \
260  }                                                                        \
261  Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) {       \
262    return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP);          \
263  }
264  HANDLEBINOP(Mul);
265  HANDLEBINOP(Div);
266  HANDLEBINOP(Rem);
267  HANDLEBINOP(Add);
268  HANDLEBINOP(Sub);
269  HANDLEBINOP(Shl);
270  HANDLEBINOP(Shr);
271  HANDLEBINOP(And);
272  HANDLEBINOP(Xor);
273  HANDLEBINOP(Or);
274#undef HANDLEBINOP
275
276  // Comparisons.
277  Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
278                     unsigned SICmpOpc, unsigned FCmpOpc);
279#define VISITCOMP(CODE, UI, SI, FP) \
280    Value *VisitBin##CODE(const BinaryOperator *E) { \
281      return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
282                         llvm::FCmpInst::FP); }
283  VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
284  VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
285  VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
286  VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
287  VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
288  VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
289#undef VISITCOMP
290
291  Value *VisitBinAssign     (const BinaryOperator *E);
292
293  Value *VisitBinLAnd       (const BinaryOperator *E);
294  Value *VisitBinLOr        (const BinaryOperator *E);
295  Value *VisitBinComma      (const BinaryOperator *E);
296
297  // Other Operators.
298  Value *VisitConditionalOperator(const ConditionalOperator *CO);
299  Value *VisitChooseExpr(ChooseExpr *CE);
300  Value *VisitOverloadExpr(OverloadExpr *OE);
301  Value *VisitVAArgExpr(VAArgExpr *VE);
302  Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
303    return CGF.EmitObjCStringLiteral(E);
304  }
305  Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
306};
307}  // end anonymous namespace.
308
309//===----------------------------------------------------------------------===//
310//                                Utilities
311//===----------------------------------------------------------------------===//
312
313/// EmitConversionToBool - Convert the specified expression value to a
314/// boolean (i1) truth value.  This is equivalent to "Val != 0".
315Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
316  assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
317
318  if (SrcType->isRealFloatingType()) {
319    // Compare against 0.0 for fp scalars.
320    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
321    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
322  }
323
324  assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
325         "Unknown scalar type to convert");
326
327  // Because of the type rules of C, we often end up computing a logical value,
328  // then zero extending it to int, then wanting it as a logical value again.
329  // Optimize this common case.
330  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
331    if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
332      Value *Result = ZI->getOperand(0);
333      // If there aren't any more uses, zap the instruction to save space.
334      // Note that there can be more uses, for example if this
335      // is the result of an assignment.
336      if (ZI->use_empty())
337        ZI->eraseFromParent();
338      return Result;
339    }
340  }
341
342  // Compare against an integer or pointer null.
343  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
344  return Builder.CreateICmpNE(Src, Zero, "tobool");
345}
346
347/// EmitScalarConversion - Emit a conversion from the specified type to the
348/// specified destination type, both of which are LLVM scalar types.
349Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
350                                               QualType DstType) {
351  SrcType = CGF.getContext().getCanonicalType(SrcType);
352  DstType = CGF.getContext().getCanonicalType(DstType);
353  if (SrcType == DstType) return Src;
354
355  if (DstType->isVoidType()) return 0;
356
357  // Handle conversions to bool first, they are special: comparisons against 0.
358  if (DstType->isBooleanType())
359    return EmitConversionToBool(Src, SrcType);
360
361  const llvm::Type *DstTy = ConvertType(DstType);
362
363  // Ignore conversions like int -> uint.
364  if (Src->getType() == DstTy)
365    return Src;
366
367  // Handle pointer conversions next: pointers can only be converted to/from
368  // other pointers and integers.
369  if (isa<PointerType>(DstType)) {
370    // The source value may be an integer, or a pointer.
371    if (isa<llvm::PointerType>(Src->getType()))
372      return Builder.CreateBitCast(Src, DstTy, "conv");
373    assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
374    return Builder.CreateIntToPtr(Src, DstTy, "conv");
375  }
376
377  if (isa<PointerType>(SrcType)) {
378    // Must be an ptr to int cast.
379    assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
380    return Builder.CreatePtrToInt(Src, DstTy, "conv");
381  }
382
383  // A scalar can be splatted to an extended vector of the same element type
384  if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
385      cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
386    return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
387                          true);
388
389  // Allow bitcast from vector to integer/fp of the same size.
390  if (isa<llvm::VectorType>(Src->getType()) ||
391      isa<llvm::VectorType>(DstTy))
392    return Builder.CreateBitCast(Src, DstTy, "conv");
393
394  // Finally, we have the arithmetic types: real int/float.
395  if (isa<llvm::IntegerType>(Src->getType())) {
396    bool InputSigned = SrcType->isSignedIntegerType();
397    if (isa<llvm::IntegerType>(DstTy))
398      return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
399    else if (InputSigned)
400      return Builder.CreateSIToFP(Src, DstTy, "conv");
401    else
402      return Builder.CreateUIToFP(Src, DstTy, "conv");
403  }
404
405  assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
406  if (isa<llvm::IntegerType>(DstTy)) {
407    if (DstType->isSignedIntegerType())
408      return Builder.CreateFPToSI(Src, DstTy, "conv");
409    else
410      return Builder.CreateFPToUI(Src, DstTy, "conv");
411  }
412
413  assert(DstTy->isFloatingPoint() && "Unknown real conversion");
414  if (DstTy->getTypeID() < Src->getType()->getTypeID())
415    return Builder.CreateFPTrunc(Src, DstTy, "conv");
416  else
417    return Builder.CreateFPExt(Src, DstTy, "conv");
418}
419
420/// EmitComplexToScalarConversion - Emit a conversion from the specified
421/// complex type to the specified destination type, where the destination
422/// type is an LLVM scalar type.
423Value *ScalarExprEmitter::
424EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
425                              QualType SrcTy, QualType DstTy) {
426  // Get the source element type.
427  SrcTy = SrcTy->getAsComplexType()->getElementType();
428
429  // Handle conversions to bool first, they are special: comparisons against 0.
430  if (DstTy->isBooleanType()) {
431    //  Complex != 0  -> (Real != 0) | (Imag != 0)
432    Src.first  = EmitScalarConversion(Src.first, SrcTy, DstTy);
433    Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
434    return Builder.CreateOr(Src.first, Src.second, "tobool");
435  }
436
437  // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
438  // the imaginary part of the complex value is discarded and the value of the
439  // real part is converted according to the conversion rules for the
440  // corresponding real type.
441  return EmitScalarConversion(Src.first, SrcTy, DstTy);
442}
443
444
445//===----------------------------------------------------------------------===//
446//                            Visitor Methods
447//===----------------------------------------------------------------------===//
448
449Value *ScalarExprEmitter::VisitExpr(Expr *E) {
450  CGF.WarnUnsupported(E, "scalar expression");
451  if (E->getType()->isVoidType())
452    return 0;
453  return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
454}
455
456Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
457  llvm::SmallVector<llvm::Constant*, 32> indices;
458  for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
459    indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
460  }
461  Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
462  Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
463  Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
464  return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
465}
466
467Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
468  // Only the lookup mechanism and first two arguments of the method
469  // implementation vary between runtimes.  We can get the receiver and
470  // arguments in generic code.
471
472  // Find the receiver
473  llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
474
475  // Process the arguments
476  unsigned ArgC = E->getNumArgs();
477  llvm::SmallVector<llvm::Value*, 16> Args;
478  for (unsigned i = 0; i != ArgC; ++i) {
479    Expr *ArgExpr = E->getArg(i);
480    QualType ArgTy = ArgExpr->getType();
481    if (!CGF.hasAggregateLLVMType(ArgTy)) {
482      // Scalar argument is passed by-value.
483      Args.push_back(CGF.EmitScalarExpr(ArgExpr));
484    } else if (ArgTy->isAnyComplexType()) {
485      // Make a temporary alloca to pass the argument.
486      llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
487      CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
488      Args.push_back(DestMem);
489    } else {
490      llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
491      CGF.EmitAggExpr(ArgExpr, DestMem, false);
492      Args.push_back(DestMem);
493    }
494  }
495
496  return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
497                                      CGF.LoadObjCSelf(),
498                                      Receiver, E->getSelector(),
499                                      &Args[0], Args.size());
500}
501
502Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
503  // Emit subscript expressions in rvalue context's.  For most cases, this just
504  // loads the lvalue formed by the subscript expr.  However, we have to be
505  // careful, because the base of a vector subscript is occasionally an rvalue,
506  // so we can't get it as an lvalue.
507  if (!E->getBase()->getType()->isVectorType())
508    return EmitLoadOfLValue(E);
509
510  // Handle the vector case.  The base must be a vector, the index must be an
511  // integer value.
512  Value *Base = Visit(E->getBase());
513  Value *Idx  = Visit(E->getIdx());
514
515  // FIXME: Convert Idx to i32 type.
516  return Builder.CreateExtractElement(Base, Idx, "vecext");
517}
518
519/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
520/// also handle things like function to pointer-to-function decay, and array to
521/// pointer decay.
522Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
523  const Expr *Op = E->getSubExpr();
524
525  // If this is due to array->pointer conversion, emit the array expression as
526  // an l-value.
527  if (Op->getType()->isArrayType()) {
528    // FIXME: For now we assume that all source arrays map to LLVM arrays.  This
529    // will not true when we add support for VLAs.
530    Value *V = EmitLValue(Op).getAddress();  // Bitfields can't be arrays.
531
532    assert(isa<llvm::PointerType>(V->getType()) &&
533           isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
534                                ->getElementType()) &&
535           "Doesn't support VLAs yet!");
536    V = Builder.CreateStructGEP(V, 0, "arraydecay");
537
538    // The resultant pointer type can be implicitly casted to other pointer
539    // types as well (e.g. void*) and can be implicitly converted to integer.
540    const llvm::Type *DestTy = ConvertType(E->getType());
541    if (V->getType() != DestTy) {
542      if (isa<llvm::PointerType>(DestTy))
543        V = Builder.CreateBitCast(V, DestTy, "ptrconv");
544      else {
545        assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
546        V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
547      }
548    }
549    return V;
550
551  } else if (E->getType()->isReferenceType()) {
552    return EmitLValue(Op).getAddress();
553  }
554
555  return EmitCastExpr(Op, E->getType());
556}
557
558
559// VisitCastExpr - Emit code for an explicit or implicit cast.  Implicit casts
560// have to handle a more broad range of conversions than explicit casts, as they
561// handle things like function to ptr-to-function decay etc.
562Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
563  // Handle cases where the source is an non-complex type.
564
565  if (!CGF.hasAggregateLLVMType(E->getType())) {
566    Value *Src = Visit(const_cast<Expr*>(E));
567
568    // Use EmitScalarConversion to perform the conversion.
569    return EmitScalarConversion(Src, E->getType(), DestTy);
570  }
571
572  if (E->getType()->isAnyComplexType()) {
573    // Handle cases where the source is a complex type.
574    return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
575                                         DestTy);
576  }
577
578  // Okay, this is a cast from an aggregate.  It must be a cast to void.  Just
579  // evaluate the result and return.
580  CGF.EmitAggExpr(E, 0, false);
581  return 0;
582}
583
584Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
585  return CGF.EmitCompoundStmt(*E->getSubStmt(),
586                              !E->getType()->isVoidType()).getScalarVal();
587}
588
589
590//===----------------------------------------------------------------------===//
591//                             Unary Operators
592//===----------------------------------------------------------------------===//
593
594Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
595                                             bool isInc, bool isPre) {
596  LValue LV = EmitLValue(E->getSubExpr());
597  // FIXME: Handle volatile!
598  Value *InVal = CGF.EmitLoadOfLValue(LV, // false
599                                     E->getSubExpr()->getType()).getScalarVal();
600
601  int AmountVal = isInc ? 1 : -1;
602
603  Value *NextVal;
604  if (isa<llvm::PointerType>(InVal->getType())) {
605    // FIXME: This isn't right for VLAs.
606    NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
607    NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
608  } else {
609    // Add the inc/dec to the real part.
610    if (isa<llvm::IntegerType>(InVal->getType()))
611      NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
612    else if (InVal->getType() == llvm::Type::FloatTy)
613      NextVal =
614        llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
615    else if (InVal->getType() == llvm::Type::DoubleTy)
616      NextVal =
617        llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
618    else {
619      llvm::APFloat F(static_cast<float>(AmountVal));
620      F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
621      NextVal = llvm::ConstantFP::get(F);
622    }
623    NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
624  }
625
626  // Store the updated result through the lvalue.
627  CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
628                             E->getSubExpr()->getType());
629
630  // If this is a postinc, return the value read from memory, otherwise use the
631  // updated value.
632  return isPre ? NextVal : InVal;
633}
634
635
636Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
637  Value *Op = Visit(E->getSubExpr());
638  return Builder.CreateNeg(Op, "neg");
639}
640
641Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
642  Value *Op = Visit(E->getSubExpr());
643  return Builder.CreateNot(Op, "neg");
644}
645
646Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
647  // Compare operand to zero.
648  Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
649
650  // Invert value.
651  // TODO: Could dynamically modify easy computations here.  For example, if
652  // the operand is an icmp ne, turn into icmp eq.
653  BoolVal = Builder.CreateNot(BoolVal, "lnot");
654
655  // ZExt result to int.
656  return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
657}
658
659/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
660/// an integer (RetType).
661Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
662                                          QualType RetType,bool isSizeOf){
663  assert(RetType->isIntegerType() && "Result type must be an integer!");
664  uint32_t ResultWidth =
665    static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
666
667  // sizeof(void) and __alignof__(void) = 1 as a gcc extension. Also
668  // for function types.
669  // FIXME: what is alignof a function type in gcc?
670  if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
671    return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
672
673  /// FIXME: This doesn't handle VLAs yet!
674  std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
675
676  uint64_t Val = isSizeOf ? Info.first : Info.second;
677  Val /= 8;  // Return size in bytes, not bits.
678
679  return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
680}
681
682Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
683  Expr *Op = E->getSubExpr();
684  if (Op->getType()->isAnyComplexType())
685    return CGF.EmitComplexExpr(Op).first;
686  return Visit(Op);
687}
688Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
689  Expr *Op = E->getSubExpr();
690  if (Op->getType()->isAnyComplexType())
691    return CGF.EmitComplexExpr(Op).second;
692
693  // __imag on a scalar returns zero.  Emit it the subexpr to ensure side
694  // effects are evaluated.
695  CGF.EmitScalarExpr(Op);
696  return llvm::Constant::getNullValue(ConvertType(E->getType()));
697}
698
699Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
700{
701  int64_t Val = E->evaluateOffsetOf(CGF.getContext());
702
703  assert(E->getType()->isIntegerType() && "Result type must be an integer!");
704
705  uint32_t ResultWidth =
706    static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
707  return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
708}
709
710//===----------------------------------------------------------------------===//
711//                           Binary Operators
712//===----------------------------------------------------------------------===//
713
714BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
715  BinOpInfo Result;
716  Result.LHS = Visit(E->getLHS());
717  Result.RHS = Visit(E->getRHS());
718  Result.Ty  = E->getType();
719  Result.E = E;
720  return Result;
721}
722
723Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
724                      Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
725  QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
726
727  BinOpInfo OpInfo;
728
729  // Load the LHS and RHS operands.
730  LValue LHSLV = EmitLValue(E->getLHS());
731  OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
732
733  // Determine the computation type.  If the RHS is complex, then this is one of
734  // the add/sub/mul/div operators.  All of these operators can be computed in
735  // with just their real component even though the computation domain really is
736  // complex.
737  QualType ComputeType = E->getComputationType();
738
739  // If the computation type is complex, then the RHS is complex.  Emit the RHS.
740  if (const ComplexType *CT = ComputeType->getAsComplexType()) {
741    ComputeType = CT->getElementType();
742
743    // Emit the RHS, only keeping the real component.
744    OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
745    RHSTy = RHSTy->getAsComplexType()->getElementType();
746  } else {
747    // Otherwise the RHS is a simple scalar value.
748    OpInfo.RHS = Visit(E->getRHS());
749  }
750
751  QualType LComputeTy, RComputeTy, ResultTy;
752
753  // Compound assignment does not contain enough information about all
754  // the types involved for pointer arithmetic cases. Figure it out
755  // here for now.
756  if (E->getLHS()->getType()->isPointerType()) {
757    // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr,
758    assert((E->getOpcode() == BinaryOperator::AddAssign ||
759            E->getOpcode() == BinaryOperator::SubAssign) &&
760           "Invalid compound assignment operator on pointer type.");
761    LComputeTy = E->getLHS()->getType();
762
763    if (E->getRHS()->getType()->isPointerType()) {
764      // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast
765      // extension, the conversion from the pointer difference back to
766      // the LHS type is handled at the end.
767      assert(E->getOpcode() == BinaryOperator::SubAssign &&
768             "Invalid compound assignment operator on pointer type.");
769      RComputeTy = E->getLHS()->getType();
770      ResultTy = CGF.getContext().getPointerDiffType();
771    } else {
772      RComputeTy = E->getRHS()->getType();
773      ResultTy = LComputeTy;
774    }
775  } else if (E->getRHS()->getType()->isPointerType()) {
776    // Degenerate case of (int += ptr) allowed by GCC implicit cast
777    // extension.
778    assert(E->getOpcode() == BinaryOperator::AddAssign &&
779           "Invalid compound assignment operator on pointer type.");
780    LComputeTy = E->getLHS()->getType();
781    RComputeTy = E->getRHS()->getType();
782    ResultTy = RComputeTy;
783  } else {
784    LComputeTy = RComputeTy = ResultTy = ComputeType;
785  }
786
787  // Convert the LHS/RHS values to the computation type.
788  OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy);
789  OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy);
790  OpInfo.Ty = ResultTy;
791  OpInfo.E = E;
792
793  // Expand the binary operator.
794  Value *Result = (this->*Func)(OpInfo);
795
796  // Convert the result back to the LHS type.
797  Result = EmitScalarConversion(Result, ResultTy, LHSTy);
798
799  // Store the result value into the LHS lvalue.
800  CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
801
802  // For bitfields, we need the value in the bitfield
803  // FIXME: This adds an extra bitfield load
804  if (LHSLV.isBitfield())
805    Result = EmitLoadOfLValue(LHSLV, LHSTy);
806
807  return Result;
808}
809
810
811Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
812  if (Ops.LHS->getType()->isFPOrFPVector())
813    return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
814  else if (Ops.Ty->isUnsignedIntegerType())
815    return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
816  else
817    return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
818}
819
820Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
821  // Rem in C can't be a floating point type: C99 6.5.5p2.
822  if (Ops.Ty->isUnsignedIntegerType())
823    return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
824  else
825    return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
826}
827
828
829Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
830  if (!Ops.Ty->isPointerType())
831    return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
832
833  // FIXME: What about a pointer to a VLA?
834  Value *Ptr, *Idx;
835  Expr *IdxExp;
836  if (isa<llvm::PointerType>(Ops.LHS->getType())) {  // pointer + int
837    Ptr = Ops.LHS;
838    Idx = Ops.RHS;
839    IdxExp = Ops.E->getRHS();
840  } else {                                           // int + pointer
841    Ptr = Ops.RHS;
842    Idx = Ops.LHS;
843    IdxExp = Ops.E->getLHS();
844  }
845
846  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
847  if (Width < CGF.LLVMPointerWidth) {
848    // Zero or sign extend the pointer value based on whether the index is
849    // signed or not.
850    const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
851    if (IdxExp->getType()->isSignedIntegerType())
852      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
853    else
854      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
855  }
856
857  return Builder.CreateGEP(Ptr, Idx, "add.ptr");
858}
859
860Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
861  if (!isa<llvm::PointerType>(Ops.LHS->getType()))
862    return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
863
864  if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
865    // pointer - int
866    Value *Idx = Ops.RHS;
867    unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
868    if (Width < CGF.LLVMPointerWidth) {
869      // Zero or sign extend the pointer value based on whether the index is
870      // signed or not.
871      const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
872      if (Ops.E->getRHS()->getType()->isSignedIntegerType())
873        Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
874      else
875        Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
876    }
877    Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
878
879    // FIXME: The pointer could point to a VLA.
880    // The GNU void* - int case is automatically handled here because
881    // our LLVM type for void* is i8*.
882    return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
883  } else {
884    // pointer - pointer
885    Value *LHS = Ops.LHS;
886    Value *RHS = Ops.RHS;
887
888    const QualType LHSType = Ops.E->getLHS()->getType();
889    const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
890    uint64_t ElementSize;
891
892    // Handle GCC extension for pointer arithmetic on void* types.
893    if (LHSElementType->isVoidType()) {
894      ElementSize = 1;
895    } else {
896      ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
897    }
898
899    const llvm::Type *ResultType = ConvertType(Ops.Ty);
900    LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
901    RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
902    Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
903
904    // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
905    // remainder.  As such, we handle common power-of-two cases here to generate
906    // better code. See PR2247.
907    if (llvm::isPowerOf2_64(ElementSize)) {
908      Value *ShAmt =
909        llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
910      return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
911    }
912
913    // Otherwise, do a full sdiv.
914    Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
915    return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
916  }
917}
918
919Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
920  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
921  // RHS to the same size as the LHS.
922  Value *RHS = Ops.RHS;
923  if (Ops.LHS->getType() != RHS->getType())
924    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
925
926  return Builder.CreateShl(Ops.LHS, RHS, "shl");
927}
928
929Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
930  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
931  // RHS to the same size as the LHS.
932  Value *RHS = Ops.RHS;
933  if (Ops.LHS->getType() != RHS->getType())
934    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
935
936  if (Ops.Ty->isUnsignedIntegerType())
937    return Builder.CreateLShr(Ops.LHS, RHS, "shr");
938  return Builder.CreateAShr(Ops.LHS, RHS, "shr");
939}
940
941Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
942                                      unsigned SICmpOpc, unsigned FCmpOpc) {
943  Value *Result;
944  QualType LHSTy = E->getLHS()->getType();
945  if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
946    Value *LHS = Visit(E->getLHS());
947    Value *RHS = Visit(E->getRHS());
948
949    if (LHS->getType()->isFloatingPoint()) {
950      Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
951                                  LHS, RHS, "cmp");
952    } else if (LHSTy->isSignedIntegerType()) {
953      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
954                                  LHS, RHS, "cmp");
955    } else {
956      // Unsigned integers and pointers.
957      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
958                                  LHS, RHS, "cmp");
959    }
960  } else if (LHSTy->isVectorType()) {
961    Value *LHS = Visit(E->getLHS());
962    Value *RHS = Visit(E->getRHS());
963
964    if (LHS->getType()->isFPOrFPVector()) {
965      Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
966                                  LHS, RHS, "cmp");
967    } else if (LHSTy->isUnsignedIntegerType()) {
968      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
969                                  LHS, RHS, "cmp");
970    } else {
971      // Signed integers and pointers.
972      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
973                                  LHS, RHS, "cmp");
974    }
975    return Result;
976  } else {
977    // Complex Comparison: can only be an equality comparison.
978    CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
979    CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
980
981    QualType CETy = LHSTy->getAsComplexType()->getElementType();
982
983    Value *ResultR, *ResultI;
984    if (CETy->isRealFloatingType()) {
985      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
986                                   LHS.first, RHS.first, "cmp.r");
987      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
988                                   LHS.second, RHS.second, "cmp.i");
989    } else {
990      // Complex comparisons can only be equality comparisons.  As such, signed
991      // and unsigned opcodes are the same.
992      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
993                                   LHS.first, RHS.first, "cmp.r");
994      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
995                                   LHS.second, RHS.second, "cmp.i");
996    }
997
998    if (E->getOpcode() == BinaryOperator::EQ) {
999      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1000    } else {
1001      assert(E->getOpcode() == BinaryOperator::NE &&
1002             "Complex comparison other than == or != ?");
1003      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1004    }
1005  }
1006
1007  // ZExt result to int.
1008  return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
1009}
1010
1011Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1012  LValue LHS = EmitLValue(E->getLHS());
1013  Value *RHS = Visit(E->getRHS());
1014
1015  // Store the value into the LHS.
1016  // FIXME: Volatility!
1017  CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
1018
1019  // For bitfields, we need the value in the bitfield
1020  // FIXME: This adds an extra bitfield load
1021  if (LHS.isBitfield())
1022    return EmitLoadOfLValue(LHS, E->getLHS()->getType());
1023  // Return the RHS.
1024  return RHS;
1025}
1026
1027Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1028  Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1029
1030  llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
1031  llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
1032
1033  llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1034  Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
1035
1036  CGF.EmitBlock(RHSBlock);
1037  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1038
1039  // Reaquire the RHS block, as there may be subblocks inserted.
1040  RHSBlock = Builder.GetInsertBlock();
1041  CGF.EmitBlock(ContBlock);
1042
1043  // Create a PHI node.  If we just evaluted the LHS condition, the result is
1044  // false.  If we evaluated both, the result is the RHS condition.
1045  llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
1046  PN->reserveOperandSpace(2);
1047  PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
1048  PN->addIncoming(RHSCond, RHSBlock);
1049
1050  // ZExt result to int.
1051  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1052}
1053
1054Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1055  Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1056
1057  llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
1058  llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
1059
1060  llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1061  Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
1062
1063  CGF.EmitBlock(RHSBlock);
1064  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1065
1066  // Reaquire the RHS block, as there may be subblocks inserted.
1067  RHSBlock = Builder.GetInsertBlock();
1068  CGF.EmitBlock(ContBlock);
1069
1070  // Create a PHI node.  If we just evaluted the LHS condition, the result is
1071  // true.  If we evaluated both, the result is the RHS condition.
1072  llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1073  PN->reserveOperandSpace(2);
1074  PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1075  PN->addIncoming(RHSCond, RHSBlock);
1076
1077  // ZExt result to int.
1078  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1079}
1080
1081Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1082  CGF.EmitStmt(E->getLHS());
1083  return Visit(E->getRHS());
1084}
1085
1086//===----------------------------------------------------------------------===//
1087//                             Other Operators
1088//===----------------------------------------------------------------------===//
1089
1090Value *ScalarExprEmitter::
1091VisitConditionalOperator(const ConditionalOperator *E) {
1092  llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1093  llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1094  llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
1095
1096  // Evaluate the conditional, then convert it to bool.  We do this explicitly
1097  // because we need the unconverted value if this is a GNU ?: expression with
1098  // missing middle value.
1099  Value *CondVal = CGF.EmitScalarExpr(E->getCond());
1100  Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1101                                               CGF.getContext().BoolTy);
1102  Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
1103
1104  CGF.EmitBlock(LHSBlock);
1105
1106  // Handle the GNU extension for missing LHS.
1107  Value *LHS;
1108  if (E->getLHS())
1109    LHS = Visit(E->getLHS());
1110  else    // Perform promotions, to handle cases like "short ?: int"
1111    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1112
1113  Builder.CreateBr(ContBlock);
1114  LHSBlock = Builder.GetInsertBlock();
1115
1116  CGF.EmitBlock(RHSBlock);
1117
1118  Value *RHS = Visit(E->getRHS());
1119  Builder.CreateBr(ContBlock);
1120  RHSBlock = Builder.GetInsertBlock();
1121
1122  CGF.EmitBlock(ContBlock);
1123
1124  if (!LHS || !RHS) {
1125    assert(E->getType()->isVoidType() && "Non-void value should have a value");
1126    return 0;
1127  }
1128
1129  // Create a PHI node for the real part.
1130  llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1131  PN->reserveOperandSpace(2);
1132  PN->addIncoming(LHS, LHSBlock);
1133  PN->addIncoming(RHS, RHSBlock);
1134  return PN;
1135}
1136
1137Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1138  // Emit the LHS or RHS as appropriate.
1139  return
1140    Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
1141}
1142
1143Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
1144  return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
1145                          E->arg_end(CGF.getContext())).getScalarVal();
1146}
1147
1148Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1149  llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1150
1151  llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1152  return V;
1153}
1154
1155Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
1156  std::string str;
1157  llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1158  CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1159                                          EncodingRecordTypes);
1160
1161  llvm::Constant *C = llvm::ConstantArray::get(str);
1162  C = new llvm::GlobalVariable(C->getType(), true,
1163                               llvm::GlobalValue::InternalLinkage,
1164                               C, ".str", &CGF.CGM.getModule());
1165  llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1166  llvm::Constant *Zeros[] = { Zero, Zero };
1167  C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1168
1169  return C;
1170}
1171
1172//===----------------------------------------------------------------------===//
1173//                         Entry Point into this File
1174//===----------------------------------------------------------------------===//
1175
1176/// EmitComplexExpr - Emit the computation of the specified expression of
1177/// complex type, ignoring the result.
1178Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1179  assert(E && !hasAggregateLLVMType(E->getType()) &&
1180         "Invalid scalar expression to emit");
1181
1182  return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1183}
1184
1185/// EmitScalarConversion - Emit a conversion from the specified type to the
1186/// specified destination type, both of which are LLVM scalar types.
1187Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1188                                             QualType DstTy) {
1189  assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1190         "Invalid scalar expression to emit");
1191  return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1192}
1193
1194/// EmitComplexToScalarConversion - Emit a conversion from the specified
1195/// complex type to the specified destination type, where the destination
1196/// type is an LLVM scalar type.
1197Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1198                                                      QualType SrcTy,
1199                                                      QualType DstTy) {
1200  assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
1201         "Invalid complex -> scalar conversion");
1202  return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1203                                                                DstTy);
1204}
1205
1206Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1207  assert(V1->getType() == V2->getType() &&
1208         "Vector operands must be of the same type");
1209  unsigned NumElements =
1210    cast<llvm::VectorType>(V1->getType())->getNumElements();
1211
1212  va_list va;
1213  va_start(va, V2);
1214
1215  llvm::SmallVector<llvm::Constant*, 16> Args;
1216  for (unsigned i = 0; i < NumElements; i++) {
1217    int n = va_arg(va, int);
1218    assert(n >= 0 && n < (int)NumElements * 2 &&
1219           "Vector shuffle index out of bounds!");
1220    Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1221  }
1222
1223  const char *Name = va_arg(va, const char *);
1224  va_end(va);
1225
1226  llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1227
1228  return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1229}
1230
1231llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
1232                                         unsigned NumVals, bool isSplat) {
1233  llvm::Value *Vec
1234    = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1235
1236  for (unsigned i = 0, e = NumVals; i != e; ++i) {
1237    llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
1238    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
1239    Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
1240  }
1241
1242  return Vec;
1243}
1244