CGExprScalar.cpp revision 7e203b7f5d894b1969765332e35c6a8b4d45556f
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/DeclObjC.h"
18#include "clang/AST/RecordLayout.h"
19#include "clang/AST/StmtVisitor.h"
20#include "clang/Basic/TargetInfo.h"
21#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Target/TargetData.h"
28#include <cstdarg>
29
30using namespace clang;
31using namespace CodeGen;
32using llvm::Value;
33
34//===----------------------------------------------------------------------===//
35//                         Scalar Expression Emitter
36//===----------------------------------------------------------------------===//
37
38struct BinOpInfo {
39  Value *LHS;
40  Value *RHS;
41  QualType Ty;  // Computation Type.
42  const BinaryOperator *E;
43};
44
45namespace {
46class VISIBILITY_HIDDEN ScalarExprEmitter
47  : public StmtVisitor<ScalarExprEmitter, Value*> {
48  CodeGenFunction &CGF;
49  CGBuilderTy &Builder;
50
51public:
52
53  ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
54    Builder(CGF.Builder) {
55  }
56
57  //===--------------------------------------------------------------------===//
58  //                               Utilities
59  //===--------------------------------------------------------------------===//
60
61  const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
62  LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
63
64  Value *EmitLoadOfLValue(LValue LV, QualType T) {
65    return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
66  }
67
68  /// EmitLoadOfLValue - Given an expression with complex type that represents a
69  /// value l-value, this method emits the address of the l-value, then loads
70  /// and returns the result.
71  Value *EmitLoadOfLValue(const Expr *E) {
72    return EmitLoadOfLValue(EmitLValue(E), E->getType());
73  }
74
75  /// EmitConversionToBool - Convert the specified expression value to a
76  /// boolean (i1) truth value.  This is equivalent to "Val != 0".
77  Value *EmitConversionToBool(Value *Src, QualType DstTy);
78
79  /// EmitScalarConversion - Emit a conversion from the specified type to the
80  /// specified destination type, both of which are LLVM scalar types.
81  Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
82
83  /// EmitComplexToScalarConversion - Emit a conversion from the specified
84  /// complex type to the specified destination type, where the destination
85  /// type is an LLVM scalar type.
86  Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
87                                       QualType SrcTy, QualType DstTy);
88
89  //===--------------------------------------------------------------------===//
90  //                            Visitor Methods
91  //===--------------------------------------------------------------------===//
92
93  Value *VisitStmt(Stmt *S) {
94    S->dump(CGF.getContext().getSourceManager());
95    assert(0 && "Stmt can't have complex result type!");
96    return 0;
97  }
98  Value *VisitExpr(Expr *S);
99  Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
100
101  // Leaves.
102  Value *VisitIntegerLiteral(const IntegerLiteral *E) {
103    return llvm::ConstantInt::get(E->getValue());
104  }
105  Value *VisitFloatingLiteral(const FloatingLiteral *E) {
106    return llvm::ConstantFP::get(E->getValue());
107  }
108  Value *VisitCharacterLiteral(const CharacterLiteral *E) {
109    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
110  }
111  Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
112    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
113  }
114  Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
115    return llvm::Constant::getNullValue(ConvertType(E->getType()));
116  }
117  Value *VisitGNUNullExpr(const GNUNullExpr *E) {
118    return llvm::Constant::getNullValue(ConvertType(E->getType()));
119  }
120  Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
121    return llvm::ConstantInt::get(ConvertType(E->getType()),
122                                  CGF.getContext().typesAreCompatible(
123                                    E->getArgType1(), E->getArgType2()));
124  }
125  Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
126  Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
127    llvm::Value *V =
128      llvm::ConstantInt::get(llvm::Type::Int32Ty,
129                             CGF.GetIDForAddrOfLabel(E->getLabel()));
130
131    return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
132  }
133
134  // l-values.
135  Value *VisitDeclRefExpr(DeclRefExpr *E) {
136    if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
137      return llvm::ConstantInt::get(EC->getInitVal());
138    return EmitLoadOfLValue(E);
139  }
140  Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
141    return CGF.EmitObjCSelectorExpr(E);
142  }
143  Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
144    return CGF.EmitObjCProtocolExpr(E);
145  }
146  Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
147    return EmitLoadOfLValue(E);
148  }
149  Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
150    return EmitLoadOfLValue(E);
151  }
152  Value *VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
153    return EmitLoadOfLValue(E);
154  }
155  Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
156    return CGF.EmitObjCMessageExpr(E).getScalarVal();
157  }
158
159  Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
160  Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
161  Value *VisitMemberExpr(Expr *E)           { return EmitLoadOfLValue(E); }
162  Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
163  Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
164    return EmitLoadOfLValue(E);
165  }
166  Value *VisitStringLiteral(Expr *E)  { return EmitLValue(E).getAddress(); }
167  Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
168     return EmitLValue(E).getAddress();
169  }
170
171  Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
172
173  Value *VisitInitListExpr(InitListExpr *E) {
174    unsigned NumInitElements = E->getNumInits();
175
176    if (E->hadArrayRangeDesignator()) {
177      CGF.ErrorUnsupported(E, "GNU array range designator extension");
178    }
179
180    const llvm::VectorType *VType =
181      dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
182
183    // We have a scalar in braces. Just use the first element.
184    if (!VType)
185      return Visit(E->getInit(0));
186
187    unsigned NumVectorElements = VType->getNumElements();
188    const llvm::Type *ElementType = VType->getElementType();
189
190    // Emit individual vector element stores.
191    llvm::Value *V = llvm::UndefValue::get(VType);
192
193    // Emit initializers
194    unsigned i;
195    for (i = 0; i < NumInitElements; ++i) {
196      Value *NewV = Visit(E->getInit(i));
197      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
198      V = Builder.CreateInsertElement(V, NewV, Idx);
199    }
200
201    // Emit remaining default initializers
202    for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
203      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
204      llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
205      V = Builder.CreateInsertElement(V, NewV, Idx);
206    }
207
208    return V;
209  }
210
211  Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
212    return llvm::Constant::getNullValue(ConvertType(E->getType()));
213  }
214  Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
215  Value *VisitCastExpr(const CastExpr *E) {
216    return EmitCastExpr(E->getSubExpr(), E->getType());
217  }
218  Value *EmitCastExpr(const Expr *E, QualType T);
219
220  Value *VisitCallExpr(const CallExpr *E) {
221    return CGF.EmitCallExpr(E).getScalarVal();
222  }
223
224  Value *VisitStmtExpr(const StmtExpr *E);
225
226  Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
227
228  // Unary Operators.
229  Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
230  Value *VisitUnaryPostDec(const UnaryOperator *E) {
231    return VisitPrePostIncDec(E, false, false);
232  }
233  Value *VisitUnaryPostInc(const UnaryOperator *E) {
234    return VisitPrePostIncDec(E, true, false);
235  }
236  Value *VisitUnaryPreDec(const UnaryOperator *E) {
237    return VisitPrePostIncDec(E, false, true);
238  }
239  Value *VisitUnaryPreInc(const UnaryOperator *E) {
240    return VisitPrePostIncDec(E, true, true);
241  }
242  Value *VisitUnaryAddrOf(const UnaryOperator *E) {
243    return EmitLValue(E->getSubExpr()).getAddress();
244  }
245  Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
246  Value *VisitUnaryPlus(const UnaryOperator *E) {
247    return Visit(E->getSubExpr());
248  }
249  Value *VisitUnaryMinus    (const UnaryOperator *E);
250  Value *VisitUnaryNot      (const UnaryOperator *E);
251  Value *VisitUnaryLNot     (const UnaryOperator *E);
252  Value *VisitUnaryReal     (const UnaryOperator *E);
253  Value *VisitUnaryImag     (const UnaryOperator *E);
254  Value *VisitUnaryExtension(const UnaryOperator *E) {
255    return Visit(E->getSubExpr());
256  }
257  Value *VisitUnaryOffsetOf(const UnaryOperator *E);
258  Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
259    return Visit(DAE->getExpr());
260  }
261
262  // Binary Operators.
263  Value *EmitMul(const BinOpInfo &Ops) {
264    return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
265  }
266  Value *EmitDiv(const BinOpInfo &Ops);
267  Value *EmitRem(const BinOpInfo &Ops);
268  Value *EmitAdd(const BinOpInfo &Ops);
269  Value *EmitSub(const BinOpInfo &Ops);
270  Value *EmitShl(const BinOpInfo &Ops);
271  Value *EmitShr(const BinOpInfo &Ops);
272  Value *EmitAnd(const BinOpInfo &Ops) {
273    return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
274  }
275  Value *EmitXor(const BinOpInfo &Ops) {
276    return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
277  }
278  Value *EmitOr (const BinOpInfo &Ops) {
279    return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
280  }
281
282  BinOpInfo EmitBinOps(const BinaryOperator *E);
283  Value *EmitCompoundAssign(const CompoundAssignOperator *E,
284                            Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
285
286  // Binary operators and binary compound assignment operators.
287#define HANDLEBINOP(OP) \
288  Value *VisitBin ## OP(const BinaryOperator *E) {                         \
289    return Emit ## OP(EmitBinOps(E));                                      \
290  }                                                                        \
291  Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) {       \
292    return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP);          \
293  }
294  HANDLEBINOP(Mul);
295  HANDLEBINOP(Div);
296  HANDLEBINOP(Rem);
297  HANDLEBINOP(Add);
298  HANDLEBINOP(Sub);
299  HANDLEBINOP(Shl);
300  HANDLEBINOP(Shr);
301  HANDLEBINOP(And);
302  HANDLEBINOP(Xor);
303  HANDLEBINOP(Or);
304#undef HANDLEBINOP
305
306  // Comparisons.
307  Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
308                     unsigned SICmpOpc, unsigned FCmpOpc);
309#define VISITCOMP(CODE, UI, SI, FP) \
310    Value *VisitBin##CODE(const BinaryOperator *E) { \
311      return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
312                         llvm::FCmpInst::FP); }
313  VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
314  VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
315  VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
316  VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
317  VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
318  VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
319#undef VISITCOMP
320
321  Value *VisitBinAssign     (const BinaryOperator *E);
322
323  Value *VisitBinLAnd       (const BinaryOperator *E);
324  Value *VisitBinLOr        (const BinaryOperator *E);
325  Value *VisitBinComma      (const BinaryOperator *E);
326
327  // Other Operators.
328  Value *VisitBlockExpr(const BlockExpr *BE);
329  Value *VisitConditionalOperator(const ConditionalOperator *CO);
330  Value *VisitChooseExpr(ChooseExpr *CE);
331  Value *VisitVAArgExpr(VAArgExpr *VE);
332  Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
333    return CGF.EmitObjCStringLiteral(E);
334  }
335};
336}  // end anonymous namespace.
337
338//===----------------------------------------------------------------------===//
339//                                Utilities
340//===----------------------------------------------------------------------===//
341
342/// EmitConversionToBool - Convert the specified expression value to a
343/// boolean (i1) truth value.  This is equivalent to "Val != 0".
344Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
345  assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
346
347  if (SrcType->isRealFloatingType()) {
348    // Compare against 0.0 for fp scalars.
349    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
350    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
351  }
352
353  assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
354         "Unknown scalar type to convert");
355
356  // Because of the type rules of C, we often end up computing a logical value,
357  // then zero extending it to int, then wanting it as a logical value again.
358  // Optimize this common case.
359  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
360    if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
361      Value *Result = ZI->getOperand(0);
362      // If there aren't any more uses, zap the instruction to save space.
363      // Note that there can be more uses, for example if this
364      // is the result of an assignment.
365      if (ZI->use_empty())
366        ZI->eraseFromParent();
367      return Result;
368    }
369  }
370
371  // Compare against an integer or pointer null.
372  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
373  return Builder.CreateICmpNE(Src, Zero, "tobool");
374}
375
376/// EmitScalarConversion - Emit a conversion from the specified type to the
377/// specified destination type, both of which are LLVM scalar types.
378Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
379                                               QualType DstType) {
380  SrcType = CGF.getContext().getCanonicalType(SrcType);
381  DstType = CGF.getContext().getCanonicalType(DstType);
382  if (SrcType == DstType) return Src;
383
384  if (DstType->isVoidType()) return 0;
385
386  // Handle conversions to bool first, they are special: comparisons against 0.
387  if (DstType->isBooleanType())
388    return EmitConversionToBool(Src, SrcType);
389
390  const llvm::Type *DstTy = ConvertType(DstType);
391
392  // Ignore conversions like int -> uint.
393  if (Src->getType() == DstTy)
394    return Src;
395
396  // Handle pointer conversions next: pointers can only be converted
397  // to/from other pointers and integers. Check for pointer types in
398  // terms of LLVM, as some native types (like Obj-C id) may map to a
399  // pointer type.
400  if (isa<llvm::PointerType>(DstTy)) {
401    // The source value may be an integer, or a pointer.
402    if (isa<llvm::PointerType>(Src->getType()))
403      return Builder.CreateBitCast(Src, DstTy, "conv");
404    assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
405    // First, convert to the correct width so that we control the kind of
406    // extension.
407    const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth);
408    bool InputSigned = SrcType->isSignedIntegerType();
409    llvm::Value* IntResult =
410        Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
411    // Then, cast to pointer.
412    return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
413  }
414
415  if (isa<llvm::PointerType>(Src->getType())) {
416    // Must be an ptr to int cast.
417    assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
418    return Builder.CreatePtrToInt(Src, DstTy, "conv");
419  }
420
421  // A scalar can be splatted to an extended vector of the same element type
422  if (DstType->isExtVectorType() && !isa<VectorType>(SrcType)) {
423    // Cast the scalar to element type
424    QualType EltTy = DstType->getAsExtVectorType()->getElementType();
425    llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
426
427    // Insert the element in element zero of an undef vector
428    llvm::Value *UnV = llvm::UndefValue::get(DstTy);
429    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
430    UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
431
432    // Splat the element across to all elements
433    llvm::SmallVector<llvm::Constant*, 16> Args;
434    unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
435    for (unsigned i = 0; i < NumElements; i++)
436      Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
437
438    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
439    llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
440    return Yay;
441  }
442
443  // Allow bitcast from vector to integer/fp of the same size.
444  if (isa<llvm::VectorType>(Src->getType()) ||
445      isa<llvm::VectorType>(DstTy))
446    return Builder.CreateBitCast(Src, DstTy, "conv");
447
448  // Finally, we have the arithmetic types: real int/float.
449  if (isa<llvm::IntegerType>(Src->getType())) {
450    bool InputSigned = SrcType->isSignedIntegerType();
451    if (isa<llvm::IntegerType>(DstTy))
452      return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
453    else if (InputSigned)
454      return Builder.CreateSIToFP(Src, DstTy, "conv");
455    else
456      return Builder.CreateUIToFP(Src, DstTy, "conv");
457  }
458
459  assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
460  if (isa<llvm::IntegerType>(DstTy)) {
461    if (DstType->isSignedIntegerType())
462      return Builder.CreateFPToSI(Src, DstTy, "conv");
463    else
464      return Builder.CreateFPToUI(Src, DstTy, "conv");
465  }
466
467  assert(DstTy->isFloatingPoint() && "Unknown real conversion");
468  if (DstTy->getTypeID() < Src->getType()->getTypeID())
469    return Builder.CreateFPTrunc(Src, DstTy, "conv");
470  else
471    return Builder.CreateFPExt(Src, DstTy, "conv");
472}
473
474/// EmitComplexToScalarConversion - Emit a conversion from the specified
475/// complex type to the specified destination type, where the destination
476/// type is an LLVM scalar type.
477Value *ScalarExprEmitter::
478EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
479                              QualType SrcTy, QualType DstTy) {
480  // Get the source element type.
481  SrcTy = SrcTy->getAsComplexType()->getElementType();
482
483  // Handle conversions to bool first, they are special: comparisons against 0.
484  if (DstTy->isBooleanType()) {
485    //  Complex != 0  -> (Real != 0) | (Imag != 0)
486    Src.first  = EmitScalarConversion(Src.first, SrcTy, DstTy);
487    Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
488    return Builder.CreateOr(Src.first, Src.second, "tobool");
489  }
490
491  // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
492  // the imaginary part of the complex value is discarded and the value of the
493  // real part is converted according to the conversion rules for the
494  // corresponding real type.
495  return EmitScalarConversion(Src.first, SrcTy, DstTy);
496}
497
498
499//===----------------------------------------------------------------------===//
500//                            Visitor Methods
501//===----------------------------------------------------------------------===//
502
503Value *ScalarExprEmitter::VisitExpr(Expr *E) {
504  CGF.ErrorUnsupported(E, "scalar expression");
505  if (E->getType()->isVoidType())
506    return 0;
507  return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
508}
509
510Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
511  llvm::SmallVector<llvm::Constant*, 32> indices;
512  for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
513    indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
514  }
515  Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
516  Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
517  Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
518  return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
519}
520
521Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
522  // Emit subscript expressions in rvalue context's.  For most cases, this just
523  // loads the lvalue formed by the subscript expr.  However, we have to be
524  // careful, because the base of a vector subscript is occasionally an rvalue,
525  // so we can't get it as an lvalue.
526  if (!E->getBase()->getType()->isVectorType())
527    return EmitLoadOfLValue(E);
528
529  // Handle the vector case.  The base must be a vector, the index must be an
530  // integer value.
531  Value *Base = Visit(E->getBase());
532  Value *Idx  = Visit(E->getIdx());
533  bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
534  Idx = Builder.CreateIntCast(Idx, llvm::Type::Int32Ty, IdxSigned,
535                              "vecidxcast");
536  return Builder.CreateExtractElement(Base, Idx, "vecext");
537}
538
539/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
540/// also handle things like function to pointer-to-function decay, and array to
541/// pointer decay.
542Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
543  const Expr *Op = E->getSubExpr();
544
545  // If this is due to array->pointer conversion, emit the array expression as
546  // an l-value.
547  if (Op->getType()->isArrayType()) {
548    Value *V = EmitLValue(Op).getAddress();  // Bitfields can't be arrays.
549
550    // Note that VLA pointers are always decayed, so we don't need to do
551    // anything here.
552    if (!Op->getType()->isVariableArrayType()) {
553      assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
554      assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
555                                 ->getElementType()) &&
556             "Expected pointer to array");
557      V = Builder.CreateStructGEP(V, 0, "arraydecay");
558    }
559
560    // The resultant pointer type can be implicitly casted to other pointer
561    // types as well (e.g. void*) and can be implicitly converted to integer.
562    const llvm::Type *DestTy = ConvertType(E->getType());
563    if (V->getType() != DestTy) {
564      if (isa<llvm::PointerType>(DestTy))
565        V = Builder.CreateBitCast(V, DestTy, "ptrconv");
566      else {
567        assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
568        V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
569      }
570    }
571    return V;
572  }
573
574  return EmitCastExpr(Op, E->getType());
575}
576
577
578// VisitCastExpr - Emit code for an explicit or implicit cast.  Implicit casts
579// have to handle a more broad range of conversions than explicit casts, as they
580// handle things like function to ptr-to-function decay etc.
581Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
582  // Handle cases where the source is an non-complex type.
583
584  if (!CGF.hasAggregateLLVMType(E->getType())) {
585    Value *Src = Visit(const_cast<Expr*>(E));
586
587    // Use EmitScalarConversion to perform the conversion.
588    return EmitScalarConversion(Src, E->getType(), DestTy);
589  }
590
591  if (E->getType()->isAnyComplexType()) {
592    // Handle cases where the source is a complex type.
593    return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
594                                         DestTy);
595  }
596
597  // Okay, this is a cast from an aggregate.  It must be a cast to void.  Just
598  // evaluate the result and return.
599  CGF.EmitAggExpr(E, 0, false);
600  return 0;
601}
602
603Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
604  return CGF.EmitCompoundStmt(*E->getSubStmt(),
605                              !E->getType()->isVoidType()).getScalarVal();
606}
607
608Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
609  return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
610}
611
612//===----------------------------------------------------------------------===//
613//                             Unary Operators
614//===----------------------------------------------------------------------===//
615
616Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
617                                             bool isInc, bool isPre) {
618  LValue LV = EmitLValue(E->getSubExpr());
619  QualType ValTy = E->getSubExpr()->getType();
620  Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
621
622  int AmountVal = isInc ? 1 : -1;
623
624  if (ValTy->isPointerType() &&
625      ValTy->getAsPointerType()->isVariableArrayType()) {
626    // The amount of the addition/subtraction needs to account for the VLA size
627    CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
628  }
629
630  Value *NextVal;
631  if (const llvm::PointerType *PT =
632         dyn_cast<llvm::PointerType>(InVal->getType())) {
633    llvm::Constant *Inc =llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
634    if (!isa<llvm::FunctionType>(PT->getElementType())) {
635      NextVal = Builder.CreateGEP(InVal, Inc, "ptrincdec");
636    } else {
637      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
638      NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
639      NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
640      NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
641    }
642  } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) {
643    // Bool++ is an interesting case, due to promotion rules, we get:
644    // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
645    // Bool = ((int)Bool+1) != 0
646    // An interesting aspect of this is that increment is always true.
647    // Decrement does not have this property.
648    NextVal = llvm::ConstantInt::getTrue();
649  } else {
650    // Add the inc/dec to the real part.
651    if (isa<llvm::IntegerType>(InVal->getType()))
652      NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
653    else if (InVal->getType() == llvm::Type::FloatTy)
654      NextVal =
655        llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
656    else if (InVal->getType() == llvm::Type::DoubleTy)
657      NextVal =
658        llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
659    else {
660      llvm::APFloat F(static_cast<float>(AmountVal));
661      bool ignored;
662      F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
663                &ignored);
664      NextVal = llvm::ConstantFP::get(F);
665    }
666    NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
667  }
668
669  // Store the updated result through the lvalue.
670  if (LV.isBitfield())
671    CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
672                                       &NextVal);
673  else
674    CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
675
676  // If this is a postinc, return the value read from memory, otherwise use the
677  // updated value.
678  return isPre ? NextVal : InVal;
679}
680
681
682Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
683  Value *Op = Visit(E->getSubExpr());
684  return Builder.CreateNeg(Op, "neg");
685}
686
687Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
688  Value *Op = Visit(E->getSubExpr());
689  return Builder.CreateNot(Op, "neg");
690}
691
692Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
693  // Compare operand to zero.
694  Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
695
696  // Invert value.
697  // TODO: Could dynamically modify easy computations here.  For example, if
698  // the operand is an icmp ne, turn into icmp eq.
699  BoolVal = Builder.CreateNot(BoolVal, "lnot");
700
701  // ZExt result to int.
702  return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
703}
704
705/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
706/// argument of the sizeof expression as an integer.
707Value *
708ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
709  QualType TypeToSize = E->getTypeOfArgument();
710  if (E->isSizeOf()) {
711    if (const VariableArrayType *VAT =
712          CGF.getContext().getAsVariableArrayType(TypeToSize)) {
713      if (E->isArgumentType()) {
714        // sizeof(type) - make sure to emit the VLA size.
715        CGF.EmitVLASize(TypeToSize);
716      }
717
718      return CGF.GetVLASize(VAT);
719    }
720  }
721
722  // If this isn't sizeof(vla), the result must be constant; use the
723  // constant folding logic so we don't have to duplicate it here.
724  Expr::EvalResult Result;
725  E->Evaluate(Result, CGF.getContext());
726  return llvm::ConstantInt::get(Result.Val.getInt());
727}
728
729Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
730  Expr *Op = E->getSubExpr();
731  if (Op->getType()->isAnyComplexType())
732    return CGF.EmitComplexExpr(Op).first;
733  return Visit(Op);
734}
735Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
736  Expr *Op = E->getSubExpr();
737  if (Op->getType()->isAnyComplexType())
738    return CGF.EmitComplexExpr(Op).second;
739
740  // __imag on a scalar returns zero.  Emit it the subexpr to ensure side
741  // effects are evaluated.
742  CGF.EmitScalarExpr(Op);
743  return llvm::Constant::getNullValue(ConvertType(E->getType()));
744}
745
746Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
747{
748  Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
749  const llvm::Type* ResultType = ConvertType(E->getType());
750  return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
751}
752
753//===----------------------------------------------------------------------===//
754//                           Binary Operators
755//===----------------------------------------------------------------------===//
756
757BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
758  BinOpInfo Result;
759  Result.LHS = Visit(E->getLHS());
760  Result.RHS = Visit(E->getRHS());
761  Result.Ty  = E->getType();
762  Result.E = E;
763  return Result;
764}
765
766Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
767                      Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
768  QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
769
770  BinOpInfo OpInfo;
771
772  if (E->getComputationResultType()->isAnyComplexType()) {
773    // This needs to go through the complex expression emitter, but
774    // it's a tad complicated to do that... I'm leaving it out for now.
775    // (Note that we do actually need the imaginary part of the RHS for
776    // multiplication and division.)
777    CGF.ErrorUnsupported(E, "complex compound assignment");
778    return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
779  }
780
781  // Load/convert the LHS.
782  LValue LHSLV = EmitLValue(E->getLHS());
783  OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
784  OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
785                                    E->getComputationLHSType());
786  // Emit the RHS.
787  OpInfo.RHS = Visit(E->getRHS());
788  OpInfo.Ty = E->getComputationResultType();
789  OpInfo.E = E;
790
791  // Expand the binary operator.
792  Value *Result = (this->*Func)(OpInfo);
793
794  // Convert the result back to the LHS type.
795  Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
796
797  // Store the result value into the LHS lvalue. Bit-fields are
798  // handled specially because the result is altered by the store,
799  // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
800  // the left operand after the assignment...'.
801  if (LHSLV.isBitfield())
802    CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
803                                       &Result);
804  else
805    CGF.EmitStoreThroughLValue(RValue::get(Result), 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  if (Ops.Ty->getAsPointerType()->isVariableArrayType()) {
834    // The amount of the addition needs to account for the VLA size
835    CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
836  }
837  Value *Ptr, *Idx;
838  Expr *IdxExp;
839  const PointerType *PT;
840  if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
841    Ptr = Ops.LHS;
842    Idx = Ops.RHS;
843    IdxExp = Ops.E->getRHS();
844  } else {                                           // int + pointer
845    PT = Ops.E->getRHS()->getType()->getAsPointerType();
846    assert(PT && "Invalid add expr");
847    Ptr = Ops.RHS;
848    Idx = Ops.LHS;
849    IdxExp = Ops.E->getLHS();
850  }
851
852  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
853  if (Width < CGF.LLVMPointerWidth) {
854    // Zero or sign extend the pointer value based on whether the index is
855    // signed or not.
856    const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
857    if (IdxExp->getType()->isSignedIntegerType())
858      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
859    else
860      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
861  }
862
863  // Explicitly handle GNU void* and function pointer arithmetic
864  // extensions. The GNU void* casts amount to no-ops since our void*
865  // type is i8*, but this is future proof.
866  const QualType ElementType = PT->getPointeeType();
867  if (ElementType->isVoidType() || ElementType->isFunctionType()) {
868    const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
869    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
870    Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
871    return Builder.CreateBitCast(Res, Ptr->getType());
872  }
873
874  return Builder.CreateGEP(Ptr, Idx, "add.ptr");
875}
876
877Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
878  if (!isa<llvm::PointerType>(Ops.LHS->getType()))
879    return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
880
881  if (Ops.E->getLHS()->getType()->getAsPointerType()->isVariableArrayType()) {
882    // The amount of the addition needs to account for the VLA size for
883    // ptr-int
884    // The amount of the division needs to account for the VLA size for
885    // ptr-ptr.
886    CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
887  }
888
889  const QualType LHSType = Ops.E->getLHS()->getType();
890  const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
891  if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
892    // pointer - int
893    Value *Idx = Ops.RHS;
894    unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
895    if (Width < CGF.LLVMPointerWidth) {
896      // Zero or sign extend the pointer value based on whether the index is
897      // signed or not.
898      const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
899      if (Ops.E->getRHS()->getType()->isSignedIntegerType())
900        Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
901      else
902        Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
903    }
904    Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
905
906    // Explicitly handle GNU void* and function pointer arithmetic
907    // extensions. The GNU void* casts amount to no-ops since our
908    // void* type is i8*, but this is future proof.
909    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
910      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
911      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
912      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
913      return Builder.CreateBitCast(Res, Ops.LHS->getType());
914    }
915
916    return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
917  } else {
918    // pointer - pointer
919    Value *LHS = Ops.LHS;
920    Value *RHS = Ops.RHS;
921
922    uint64_t ElementSize;
923
924    // Handle GCC extension for pointer arithmetic on void* and function pointer
925    // types.
926    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
927      ElementSize = 1;
928    } else {
929      ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
930    }
931
932    const llvm::Type *ResultType = ConvertType(Ops.Ty);
933    LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
934    RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
935    Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
936
937    // Optimize out the shift for element size of 1.
938    if (ElementSize == 1)
939      return BytesBetween;
940
941    // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
942    // remainder.  As such, we handle common power-of-two cases here to generate
943    // better code. See PR2247.
944    if (llvm::isPowerOf2_64(ElementSize)) {
945      Value *ShAmt =
946        llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
947      return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
948    }
949
950    // Otherwise, do a full sdiv.
951    Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
952    return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
953  }
954}
955
956Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
957  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
958  // RHS to the same size as the LHS.
959  Value *RHS = Ops.RHS;
960  if (Ops.LHS->getType() != RHS->getType())
961    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
962
963  return Builder.CreateShl(Ops.LHS, RHS, "shl");
964}
965
966Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
967  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
968  // RHS to the same size as the LHS.
969  Value *RHS = Ops.RHS;
970  if (Ops.LHS->getType() != RHS->getType())
971    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
972
973  if (Ops.Ty->isUnsignedIntegerType())
974    return Builder.CreateLShr(Ops.LHS, RHS, "shr");
975  return Builder.CreateAShr(Ops.LHS, RHS, "shr");
976}
977
978Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
979                                      unsigned SICmpOpc, unsigned FCmpOpc) {
980  Value *Result;
981  QualType LHSTy = E->getLHS()->getType();
982  if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
983    Value *LHS = Visit(E->getLHS());
984    Value *RHS = Visit(E->getRHS());
985
986    if (LHS->getType()->isFloatingPoint()) {
987      Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
988                                  LHS, RHS, "cmp");
989    } else if (LHSTy->isSignedIntegerType()) {
990      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
991                                  LHS, RHS, "cmp");
992    } else {
993      // Unsigned integers and pointers.
994      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
995                                  LHS, RHS, "cmp");
996    }
997  } else if (LHSTy->isVectorType()) {
998    Value *LHS = Visit(E->getLHS());
999    Value *RHS = Visit(E->getRHS());
1000
1001    if (LHS->getType()->isFPOrFPVector()) {
1002      Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1003                                  LHS, RHS, "cmp");
1004    } else if (LHSTy->isUnsignedIntegerType()) {
1005      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
1006                                  LHS, RHS, "cmp");
1007    } else {
1008      // Signed integers and pointers.
1009      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
1010                                  LHS, RHS, "cmp");
1011    }
1012    return Result;
1013  } else {
1014    // Complex Comparison: can only be an equality comparison.
1015    CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1016    CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1017
1018    QualType CETy = LHSTy->getAsComplexType()->getElementType();
1019
1020    Value *ResultR, *ResultI;
1021    if (CETy->isRealFloatingType()) {
1022      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1023                                   LHS.first, RHS.first, "cmp.r");
1024      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1025                                   LHS.second, RHS.second, "cmp.i");
1026    } else {
1027      // Complex comparisons can only be equality comparisons.  As such, signed
1028      // and unsigned opcodes are the same.
1029      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1030                                   LHS.first, RHS.first, "cmp.r");
1031      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1032                                   LHS.second, RHS.second, "cmp.i");
1033    }
1034
1035    if (E->getOpcode() == BinaryOperator::EQ) {
1036      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1037    } else {
1038      assert(E->getOpcode() == BinaryOperator::NE &&
1039             "Complex comparison other than == or != ?");
1040      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1041    }
1042  }
1043
1044  return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
1045}
1046
1047Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1048  LValue LHS = EmitLValue(E->getLHS());
1049  Value *RHS = Visit(E->getRHS());
1050
1051  // Store the value into the LHS.  Bit-fields are handled specially
1052  // because the result is altered by the store, i.e., [C99 6.5.16p1]
1053  // 'An assignment expression has the value of the left operand after
1054  // the assignment...'.
1055  if (LHS.isBitfield())
1056    CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1057                                       &RHS);
1058  else
1059    CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
1060
1061  // Return the RHS.
1062  return RHS;
1063}
1064
1065Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1066  // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1067  // If we have 1 && X, just emit X without inserting the control flow.
1068  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1069    if (Cond == 1) { // If we have 1 && X, just emit X.
1070      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1071      // ZExt result to int.
1072      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1073    }
1074
1075    // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1076    if (!CGF.ContainsLabel(E->getRHS()))
1077      return llvm::Constant::getNullValue(CGF.LLVMIntTy);
1078  }
1079
1080  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1081  llvm::BasicBlock *RHSBlock  = CGF.createBasicBlock("land.rhs");
1082
1083  // Branch on the LHS first.  If it is false, go to the failure (cont) block.
1084  CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1085
1086  // Any edges into the ContBlock are now from an (indeterminate number of)
1087  // edges from this first condition.  All of these values will be false.  Start
1088  // setting up the PHI node in the Cont Block for this.
1089  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1090  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1091  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1092       PI != PE; ++PI)
1093    PN->addIncoming(llvm::ConstantInt::getFalse(), *PI);
1094
1095  CGF.EmitBlock(RHSBlock);
1096  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1097
1098  // Reaquire the RHS block, as there may be subblocks inserted.
1099  RHSBlock = Builder.GetInsertBlock();
1100
1101  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1102  // into the phi node for the edge with the value of RHSCond.
1103  CGF.EmitBlock(ContBlock);
1104  PN->addIncoming(RHSCond, RHSBlock);
1105
1106  // ZExt result to int.
1107  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1108}
1109
1110Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1111  // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1112  // If we have 0 || X, just emit X without inserting the control flow.
1113  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1114    if (Cond == -1) { // If we have 0 || X, just emit X.
1115      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1116      // ZExt result to int.
1117      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1118    }
1119
1120    // 1 || RHS: If it is safe, just elide the RHS, and return 1.
1121    if (!CGF.ContainsLabel(E->getRHS()))
1122      return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
1123  }
1124
1125  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1126  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
1127
1128  // Branch on the LHS first.  If it is true, go to the success (cont) block.
1129  CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1130
1131  // Any edges into the ContBlock are now from an (indeterminate number of)
1132  // edges from this first condition.  All of these values will be true.  Start
1133  // setting up the PHI node in the Cont Block for this.
1134  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1135  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1136  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1137       PI != PE; ++PI)
1138    PN->addIncoming(llvm::ConstantInt::getTrue(), *PI);
1139
1140  // Emit the RHS condition as a bool value.
1141  CGF.EmitBlock(RHSBlock);
1142  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1143
1144  // Reaquire the RHS block, as there may be subblocks inserted.
1145  RHSBlock = Builder.GetInsertBlock();
1146
1147  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1148  // into the phi node for the edge with the value of RHSCond.
1149  CGF.EmitBlock(ContBlock);
1150  PN->addIncoming(RHSCond, RHSBlock);
1151
1152  // ZExt result to int.
1153  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1154}
1155
1156Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1157  CGF.EmitStmt(E->getLHS());
1158  CGF.EnsureInsertPoint();
1159  return Visit(E->getRHS());
1160}
1161
1162//===----------------------------------------------------------------------===//
1163//                             Other Operators
1164//===----------------------------------------------------------------------===//
1165
1166/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1167/// expression is cheap enough and side-effect-free enough to evaluate
1168/// unconditionally instead of conditionally.  This is used to convert control
1169/// flow into selects in some cases.
1170static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1171  if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1172    return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1173
1174  // TODO: Allow anything we can constant fold to an integer or fp constant.
1175  if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1176      isa<FloatingLiteral>(E))
1177    return true;
1178
1179  // Non-volatile automatic variables too, to get "cond ? X : Y" where
1180  // X and Y are local variables.
1181  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1182    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1183      if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1184        return true;
1185
1186  return false;
1187}
1188
1189
1190Value *ScalarExprEmitter::
1191VisitConditionalOperator(const ConditionalOperator *E) {
1192  // If the condition constant folds and can be elided, try to avoid emitting
1193  // the condition and the dead arm.
1194  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
1195    Expr *Live = E->getLHS(), *Dead = E->getRHS();
1196    if (Cond == -1)
1197      std::swap(Live, Dead);
1198
1199    // If the dead side doesn't have labels we need, and if the Live side isn't
1200    // the gnu missing ?: extension (which we could handle, but don't bother
1201    // to), just emit the Live part.
1202    if ((!Dead || !CGF.ContainsLabel(Dead)) &&  // No labels in dead part
1203        Live)                                   // Live part isn't missing.
1204      return Visit(Live);
1205  }
1206
1207
1208  // If this is a really simple expression (like x ? 4 : 5), emit this as a
1209  // select instead of as control flow.  We can only do this if it is cheap and
1210  // safe to evaluate the LHS and RHS unconditionally.
1211  if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1212      isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1213    llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1214    llvm::Value *LHS = Visit(E->getLHS());
1215    llvm::Value *RHS = Visit(E->getRHS());
1216    return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1217  }
1218
1219
1220  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1221  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1222  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1223  Value *CondVal = 0;
1224
1225  // If we don't have the GNU missing condition extension, emit a branch on
1226  // bool the normal way.
1227  if (E->getLHS()) {
1228    // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1229    // the branch on bool.
1230    CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1231  } else {
1232    // Otherwise, for the ?: extension, evaluate the conditional and then
1233    // convert it to bool the hard way.  We do this explicitly because we need
1234    // the unconverted value for the missing middle value of the ?:.
1235    CondVal = CGF.EmitScalarExpr(E->getCond());
1236
1237    // In some cases, EmitScalarConversion will delete the "CondVal" expression
1238    // if there are no extra uses (an optimization).  Inhibit this by making an
1239    // extra dead use, because we're going to add a use of CondVal later.  We
1240    // don't use the builder for this, because we don't want it to get optimized
1241    // away.  This leaves dead code, but the ?: extension isn't common.
1242    new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1243                          Builder.GetInsertBlock());
1244
1245    Value *CondBoolVal =
1246      CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1247                               CGF.getContext().BoolTy);
1248    Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
1249  }
1250
1251  CGF.EmitBlock(LHSBlock);
1252
1253  // Handle the GNU extension for missing LHS.
1254  Value *LHS;
1255  if (E->getLHS())
1256    LHS = Visit(E->getLHS());
1257  else    // Perform promotions, to handle cases like "short ?: int"
1258    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1259
1260  LHSBlock = Builder.GetInsertBlock();
1261  CGF.EmitBranch(ContBlock);
1262
1263  CGF.EmitBlock(RHSBlock);
1264
1265  Value *RHS = Visit(E->getRHS());
1266  RHSBlock = Builder.GetInsertBlock();
1267  CGF.EmitBranch(ContBlock);
1268
1269  CGF.EmitBlock(ContBlock);
1270
1271  if (!LHS || !RHS) {
1272    assert(E->getType()->isVoidType() && "Non-void value should have a value");
1273    return 0;
1274  }
1275
1276  // Create a PHI node for the real part.
1277  llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1278  PN->reserveOperandSpace(2);
1279  PN->addIncoming(LHS, LHSBlock);
1280  PN->addIncoming(RHS, RHSBlock);
1281  return PN;
1282}
1283
1284Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1285  return Visit(E->getChosenSubExpr(CGF.getContext()));
1286}
1287
1288Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1289  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
1290  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1291
1292  // If EmitVAArg fails, we fall back to the LLVM instruction.
1293  if (!ArgPtr)
1294    return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1295
1296  return Builder.CreateLoad(ArgPtr);
1297}
1298
1299Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
1300  return CGF.BuildBlockLiteralTmp(BE);
1301}
1302
1303//===----------------------------------------------------------------------===//
1304//                         Entry Point into this File
1305//===----------------------------------------------------------------------===//
1306
1307/// EmitComplexExpr - Emit the computation of the specified expression of
1308/// complex type, ignoring the result.
1309Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1310  assert(E && !hasAggregateLLVMType(E->getType()) &&
1311         "Invalid scalar expression to emit");
1312
1313  return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1314}
1315
1316/// EmitScalarConversion - Emit a conversion from the specified type to the
1317/// specified destination type, both of which are LLVM scalar types.
1318Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1319                                             QualType DstTy) {
1320  assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1321         "Invalid scalar expression to emit");
1322  return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1323}
1324
1325/// EmitComplexToScalarConversion - Emit a conversion from the specified
1326/// complex type to the specified destination type, where the destination
1327/// type is an LLVM scalar type.
1328Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1329                                                      QualType SrcTy,
1330                                                      QualType DstTy) {
1331  assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
1332         "Invalid complex -> scalar conversion");
1333  return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1334                                                                DstTy);
1335}
1336
1337Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1338  assert(V1->getType() == V2->getType() &&
1339         "Vector operands must be of the same type");
1340  unsigned NumElements =
1341    cast<llvm::VectorType>(V1->getType())->getNumElements();
1342
1343  va_list va;
1344  va_start(va, V2);
1345
1346  llvm::SmallVector<llvm::Constant*, 16> Args;
1347  for (unsigned i = 0; i < NumElements; i++) {
1348    int n = va_arg(va, int);
1349    assert(n >= 0 && n < (int)NumElements * 2 &&
1350           "Vector shuffle index out of bounds!");
1351    Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1352  }
1353
1354  const char *Name = va_arg(va, const char *);
1355  va_end(va);
1356
1357  llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1358
1359  return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1360}
1361
1362llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
1363                                         unsigned NumVals, bool isSplat) {
1364  llvm::Value *Vec
1365    = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1366
1367  for (unsigned i = 0, e = NumVals; i != e; ++i) {
1368    llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
1369    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
1370    Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
1371  }
1372
1373  return Vec;
1374}
1375