CGExprScalar.cpp revision 85b4521e34dcd4a0a4a1f0819e1123128e5a3125
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 "CGObjCRuntime.h"
16#include "CodeGenModule.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/RecordLayout.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/Basic/TargetInfo.h"
22#include "llvm/Constants.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Module.h"
27#include "llvm/Support/CFG.h"
28#include "llvm/Target/TargetData.h"
29#include <cstdarg>
30
31using namespace clang;
32using namespace CodeGen;
33using llvm::Value;
34
35//===----------------------------------------------------------------------===//
36//                         Scalar Expression Emitter
37//===----------------------------------------------------------------------===//
38
39struct BinOpInfo {
40  Value *LHS;
41  Value *RHS;
42  QualType Ty;  // Computation Type.
43  const BinaryOperator *E;
44};
45
46namespace {
47class ScalarExprEmitter
48  : public StmtVisitor<ScalarExprEmitter, Value*> {
49  CodeGenFunction &CGF;
50  CGBuilderTy &Builder;
51  bool IgnoreResultAssign;
52  llvm::LLVMContext &VMContext;
53public:
54
55  ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
56    : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
57      VMContext(cgf.getLLVMContext()) {
58  }
59
60  //===--------------------------------------------------------------------===//
61  //                               Utilities
62  //===--------------------------------------------------------------------===//
63
64  bool TestAndClearIgnoreResultAssign() {
65    bool I = IgnoreResultAssign;
66    IgnoreResultAssign = false;
67    return I;
68  }
69
70  const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
71  LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
72
73  Value *EmitLoadOfLValue(LValue LV, QualType T) {
74    return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
75  }
76
77  /// EmitLoadOfLValue - Given an expression with complex type that represents a
78  /// value l-value, this method emits the address of the l-value, then loads
79  /// and returns the result.
80  Value *EmitLoadOfLValue(const Expr *E) {
81    return EmitLoadOfLValue(EmitLValue(E), E->getType());
82  }
83
84  /// EmitConversionToBool - Convert the specified expression value to a
85  /// boolean (i1) truth value.  This is equivalent to "Val != 0".
86  Value *EmitConversionToBool(Value *Src, QualType DstTy);
87
88  /// EmitScalarConversion - Emit a conversion from the specified type to the
89  /// specified destination type, both of which are LLVM scalar types.
90  Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
91
92  /// EmitComplexToScalarConversion - Emit a conversion from the specified
93  /// complex type to the specified destination type, where the destination type
94  /// is an LLVM scalar type.
95  Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
96                                       QualType SrcTy, QualType DstTy);
97
98  //===--------------------------------------------------------------------===//
99  //                            Visitor Methods
100  //===--------------------------------------------------------------------===//
101
102  Value *VisitStmt(Stmt *S) {
103    S->dump(CGF.getContext().getSourceManager());
104    assert(0 && "Stmt can't have complex result type!");
105    return 0;
106  }
107  Value *VisitExpr(Expr *S);
108
109  Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
110
111  // Leaves.
112  Value *VisitIntegerLiteral(const IntegerLiteral *E) {
113    return llvm::ConstantInt::get(VMContext, E->getValue());
114  }
115  Value *VisitFloatingLiteral(const FloatingLiteral *E) {
116    return llvm::ConstantFP::get(VMContext, E->getValue());
117  }
118  Value *VisitCharacterLiteral(const CharacterLiteral *E) {
119    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
120  }
121  Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
122    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
123  }
124  Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
125    return llvm::Constant::getNullValue(ConvertType(E->getType()));
126  }
127  Value *VisitGNUNullExpr(const GNUNullExpr *E) {
128    return llvm::Constant::getNullValue(ConvertType(E->getType()));
129  }
130  Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
131    return llvm::ConstantInt::get(ConvertType(E->getType()),
132                                  CGF.getContext().typesAreCompatible(
133                                    E->getArgType1(), E->getArgType2()));
134  }
135  Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
136  Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
137    llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
138    return Builder.CreateBitCast(V, ConvertType(E->getType()));
139  }
140
141  // l-values.
142  Value *VisitDeclRefExpr(DeclRefExpr *E) {
143    Expr::EvalResult Result;
144    if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
145      assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
146      return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
147    }
148    return EmitLoadOfLValue(E);
149  }
150  Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
151    return CGF.EmitObjCSelectorExpr(E);
152  }
153  Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
154    return CGF.EmitObjCProtocolExpr(E);
155  }
156  Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
157    return EmitLoadOfLValue(E);
158  }
159  Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
160    return EmitLoadOfLValue(E);
161  }
162  Value *VisitObjCImplicitSetterGetterRefExpr(
163                        ObjCImplicitSetterGetterRefExpr *E) {
164    return EmitLoadOfLValue(E);
165  }
166  Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
167    return CGF.EmitObjCMessageExpr(E).getScalarVal();
168  }
169
170  Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
171  Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
172  Value *VisitMemberExpr(MemberExpr *E);
173  Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
174  Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
175    return EmitLoadOfLValue(E);
176  }
177  Value *VisitStringLiteral(Expr *E)  { return EmitLValue(E).getAddress(); }
178  Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
179     return EmitLValue(E).getAddress();
180  }
181
182  Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
183
184  Value *VisitInitListExpr(InitListExpr *E);
185
186  Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
187    return llvm::Constant::getNullValue(ConvertType(E->getType()));
188  }
189  Value *VisitCastExpr(CastExpr *E) {
190    // Make sure to evaluate VLA bounds now so that we have them for later.
191    if (E->getType()->isVariablyModifiedType())
192      CGF.EmitVLASize(E->getType());
193
194    return EmitCastExpr(E);
195  }
196  Value *EmitCastExpr(CastExpr *E);
197
198  Value *VisitCallExpr(const CallExpr *E) {
199    if (E->getCallReturnType()->isReferenceType())
200      return EmitLoadOfLValue(E);
201
202    return CGF.EmitCallExpr(E).getScalarVal();
203  }
204
205  Value *VisitStmtExpr(const StmtExpr *E);
206
207  Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
208
209  // Unary Operators.
210  Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
211  Value *VisitUnaryPostDec(const UnaryOperator *E) {
212    return VisitPrePostIncDec(E, false, false);
213  }
214  Value *VisitUnaryPostInc(const UnaryOperator *E) {
215    return VisitPrePostIncDec(E, true, false);
216  }
217  Value *VisitUnaryPreDec(const UnaryOperator *E) {
218    return VisitPrePostIncDec(E, false, true);
219  }
220  Value *VisitUnaryPreInc(const UnaryOperator *E) {
221    return VisitPrePostIncDec(E, true, true);
222  }
223  Value *VisitUnaryAddrOf(const UnaryOperator *E) {
224    return EmitLValue(E->getSubExpr()).getAddress();
225  }
226  Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
227  Value *VisitUnaryPlus(const UnaryOperator *E) {
228    // This differs from gcc, though, most likely due to a bug in gcc.
229    TestAndClearIgnoreResultAssign();
230    return Visit(E->getSubExpr());
231  }
232  Value *VisitUnaryMinus    (const UnaryOperator *E);
233  Value *VisitUnaryNot      (const UnaryOperator *E);
234  Value *VisitUnaryLNot     (const UnaryOperator *E);
235  Value *VisitUnaryReal     (const UnaryOperator *E);
236  Value *VisitUnaryImag     (const UnaryOperator *E);
237  Value *VisitUnaryExtension(const UnaryOperator *E) {
238    return Visit(E->getSubExpr());
239  }
240  Value *VisitUnaryOffsetOf(const UnaryOperator *E);
241
242  // C++
243  Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
244    return Visit(DAE->getExpr());
245  }
246  Value *VisitCXXThisExpr(CXXThisExpr *TE) {
247    return CGF.LoadCXXThis();
248  }
249
250  Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
251    return CGF.EmitCXXExprWithTemporaries(E).getScalarVal();
252  }
253  Value *VisitCXXNewExpr(const CXXNewExpr *E) {
254    return CGF.EmitCXXNewExpr(E);
255  }
256  Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
257    CGF.EmitCXXDeleteExpr(E);
258    return 0;
259  }
260
261  Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
262    // C++ [expr.pseudo]p1:
263    //   The result shall only be used as the operand for the function call
264    //   operator (), and the result of such a call has type void. The only
265    //   effect is the evaluation of the postfix-expression before the dot or
266    //   arrow.
267    CGF.EmitScalarExpr(E->getBase());
268    return 0;
269  }
270
271  Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
272    return llvm::Constant::getNullValue(ConvertType(E->getType()));
273  }
274
275  Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
276    CGF.EmitCXXThrowExpr(E);
277    return 0;
278  }
279
280  // Binary Operators.
281  Value *EmitMul(const BinOpInfo &Ops) {
282    if (CGF.getContext().getLangOptions().OverflowChecking
283        && Ops.Ty->isSignedIntegerType())
284      return EmitOverflowCheckedBinOp(Ops);
285    if (Ops.LHS->getType()->isFPOrFPVector())
286      return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
287    return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
288  }
289  /// Create a binary op that checks for overflow.
290  /// Currently only supports +, - and *.
291  Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
292  Value *EmitDiv(const BinOpInfo &Ops);
293  Value *EmitRem(const BinOpInfo &Ops);
294  Value *EmitAdd(const BinOpInfo &Ops);
295  Value *EmitSub(const BinOpInfo &Ops);
296  Value *EmitShl(const BinOpInfo &Ops);
297  Value *EmitShr(const BinOpInfo &Ops);
298  Value *EmitAnd(const BinOpInfo &Ops) {
299    return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
300  }
301  Value *EmitXor(const BinOpInfo &Ops) {
302    return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
303  }
304  Value *EmitOr (const BinOpInfo &Ops) {
305    return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
306  }
307
308  BinOpInfo EmitBinOps(const BinaryOperator *E);
309  Value *EmitCompoundAssign(const CompoundAssignOperator *E,
310                            Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
311
312  // Binary operators and binary compound assignment operators.
313#define HANDLEBINOP(OP) \
314  Value *VisitBin ## OP(const BinaryOperator *E) {                         \
315    return Emit ## OP(EmitBinOps(E));                                      \
316  }                                                                        \
317  Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) {       \
318    return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP);          \
319  }
320  HANDLEBINOP(Mul);
321  HANDLEBINOP(Div);
322  HANDLEBINOP(Rem);
323  HANDLEBINOP(Add);
324  HANDLEBINOP(Sub);
325  HANDLEBINOP(Shl);
326  HANDLEBINOP(Shr);
327  HANDLEBINOP(And);
328  HANDLEBINOP(Xor);
329  HANDLEBINOP(Or);
330#undef HANDLEBINOP
331
332  // Comparisons.
333  Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
334                     unsigned SICmpOpc, unsigned FCmpOpc);
335#define VISITCOMP(CODE, UI, SI, FP) \
336    Value *VisitBin##CODE(const BinaryOperator *E) { \
337      return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
338                         llvm::FCmpInst::FP); }
339  VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
340  VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
341  VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
342  VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
343  VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
344  VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
345#undef VISITCOMP
346
347  Value *VisitBinAssign     (const BinaryOperator *E);
348
349  Value *VisitBinLAnd       (const BinaryOperator *E);
350  Value *VisitBinLOr        (const BinaryOperator *E);
351  Value *VisitBinComma      (const BinaryOperator *E);
352
353  Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); }
354  Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); }
355
356  // Other Operators.
357  Value *VisitBlockExpr(const BlockExpr *BE);
358  Value *VisitConditionalOperator(const ConditionalOperator *CO);
359  Value *VisitChooseExpr(ChooseExpr *CE);
360  Value *VisitVAArgExpr(VAArgExpr *VE);
361  Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
362    return CGF.EmitObjCStringLiteral(E);
363  }
364};
365}  // end anonymous namespace.
366
367//===----------------------------------------------------------------------===//
368//                                Utilities
369//===----------------------------------------------------------------------===//
370
371/// EmitConversionToBool - Convert the specified expression value to a
372/// boolean (i1) truth value.  This is equivalent to "Val != 0".
373Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
374  assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs");
375
376  if (SrcType->isRealFloatingType()) {
377    // Compare against 0.0 for fp scalars.
378    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
379    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
380  }
381
382  if (SrcType->isMemberPointerType()) {
383    // FIXME: This is ABI specific.
384
385    // Compare against -1.
386    llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType());
387    return Builder.CreateICmpNE(Src, NegativeOne, "tobool");
388  }
389
390  assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
391         "Unknown scalar type to convert");
392
393  // Because of the type rules of C, we often end up computing a logical value,
394  // then zero extending it to int, then wanting it as a logical value again.
395  // Optimize this common case.
396  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
397    if (ZI->getOperand(0)->getType() ==
398        llvm::Type::getInt1Ty(CGF.getLLVMContext())) {
399      Value *Result = ZI->getOperand(0);
400      // If there aren't any more uses, zap the instruction to save space.
401      // Note that there can be more uses, for example if this
402      // is the result of an assignment.
403      if (ZI->use_empty())
404        ZI->eraseFromParent();
405      return Result;
406    }
407  }
408
409  // Compare against an integer or pointer null.
410  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
411  return Builder.CreateICmpNE(Src, Zero, "tobool");
412}
413
414/// EmitScalarConversion - Emit a conversion from the specified type to the
415/// specified destination type, both of which are LLVM scalar types.
416Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
417                                               QualType DstType) {
418  SrcType = CGF.getContext().getCanonicalType(SrcType);
419  DstType = CGF.getContext().getCanonicalType(DstType);
420  if (SrcType == DstType) return Src;
421
422  if (DstType->isVoidType()) return 0;
423
424  llvm::LLVMContext &VMContext = CGF.getLLVMContext();
425
426  // Handle conversions to bool first, they are special: comparisons against 0.
427  if (DstType->isBooleanType())
428    return EmitConversionToBool(Src, SrcType);
429
430  const llvm::Type *DstTy = ConvertType(DstType);
431
432  // Ignore conversions like int -> uint.
433  if (Src->getType() == DstTy)
434    return Src;
435
436  // Handle pointer conversions next: pointers can only be converted to/from
437  // other pointers and integers. Check for pointer types in terms of LLVM, as
438  // some native types (like Obj-C id) may map to a pointer type.
439  if (isa<llvm::PointerType>(DstTy)) {
440    // The source value may be an integer, or a pointer.
441    if (isa<llvm::PointerType>(Src->getType()))
442      return Builder.CreateBitCast(Src, DstTy, "conv");
443
444    assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
445    // First, convert to the correct width so that we control the kind of
446    // extension.
447    const llvm::Type *MiddleTy =
448          llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
449    bool InputSigned = SrcType->isSignedIntegerType();
450    llvm::Value* IntResult =
451        Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
452    // Then, cast to pointer.
453    return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
454  }
455
456  if (isa<llvm::PointerType>(Src->getType())) {
457    // Must be an ptr to int cast.
458    assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
459    return Builder.CreatePtrToInt(Src, DstTy, "conv");
460  }
461
462  // A scalar can be splatted to an extended vector of the same element type
463  if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
464    // Cast the scalar to element type
465    QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType();
466    llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
467
468    // Insert the element in element zero of an undef vector
469    llvm::Value *UnV = llvm::UndefValue::get(DstTy);
470    llvm::Value *Idx =
471        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
472    UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
473
474    // Splat the element across to all elements
475    llvm::SmallVector<llvm::Constant*, 16> Args;
476    unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
477    for (unsigned i = 0; i < NumElements; i++)
478      Args.push_back(llvm::ConstantInt::get(
479                                        llvm::Type::getInt32Ty(VMContext), 0));
480
481    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
482    llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
483    return Yay;
484  }
485
486  // Allow bitcast from vector to integer/fp of the same size.
487  if (isa<llvm::VectorType>(Src->getType()) ||
488      isa<llvm::VectorType>(DstTy))
489    return Builder.CreateBitCast(Src, DstTy, "conv");
490
491  // Finally, we have the arithmetic types: real int/float.
492  if (isa<llvm::IntegerType>(Src->getType())) {
493    bool InputSigned = SrcType->isSignedIntegerType();
494    if (isa<llvm::IntegerType>(DstTy))
495      return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
496    else if (InputSigned)
497      return Builder.CreateSIToFP(Src, DstTy, "conv");
498    else
499      return Builder.CreateUIToFP(Src, DstTy, "conv");
500  }
501
502  assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
503  if (isa<llvm::IntegerType>(DstTy)) {
504    if (DstType->isSignedIntegerType())
505      return Builder.CreateFPToSI(Src, DstTy, "conv");
506    else
507      return Builder.CreateFPToUI(Src, DstTy, "conv");
508  }
509
510  assert(DstTy->isFloatingPoint() && "Unknown real conversion");
511  if (DstTy->getTypeID() < Src->getType()->getTypeID())
512    return Builder.CreateFPTrunc(Src, DstTy, "conv");
513  else
514    return Builder.CreateFPExt(Src, DstTy, "conv");
515}
516
517/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
518/// type to the specified destination type, where the destination type is an
519/// LLVM scalar type.
520Value *ScalarExprEmitter::
521EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
522                              QualType SrcTy, QualType DstTy) {
523  // Get the source element type.
524  SrcTy = SrcTy->getAs<ComplexType>()->getElementType();
525
526  // Handle conversions to bool first, they are special: comparisons against 0.
527  if (DstTy->isBooleanType()) {
528    //  Complex != 0  -> (Real != 0) | (Imag != 0)
529    Src.first  = EmitScalarConversion(Src.first, SrcTy, DstTy);
530    Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
531    return Builder.CreateOr(Src.first, Src.second, "tobool");
532  }
533
534  // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
535  // the imaginary part of the complex value is discarded and the value of the
536  // real part is converted according to the conversion rules for the
537  // corresponding real type.
538  return EmitScalarConversion(Src.first, SrcTy, DstTy);
539}
540
541
542//===----------------------------------------------------------------------===//
543//                            Visitor Methods
544//===----------------------------------------------------------------------===//
545
546Value *ScalarExprEmitter::VisitExpr(Expr *E) {
547  CGF.ErrorUnsupported(E, "scalar expression");
548  if (E->getType()->isVoidType())
549    return 0;
550  return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
551}
552
553Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
554  llvm::SmallVector<llvm::Constant*, 32> indices;
555  for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
556    indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
557  }
558  Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
559  Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
560  Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
561  return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
562}
563Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
564  Expr::EvalResult Result;
565  if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
566    if (E->isArrow())
567      CGF.EmitScalarExpr(E->getBase());
568    else
569      EmitLValue(E->getBase());
570    return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
571  }
572  return EmitLoadOfLValue(E);
573}
574
575Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
576  TestAndClearIgnoreResultAssign();
577
578  // Emit subscript expressions in rvalue context's.  For most cases, this just
579  // loads the lvalue formed by the subscript expr.  However, we have to be
580  // careful, because the base of a vector subscript is occasionally an rvalue,
581  // so we can't get it as an lvalue.
582  if (!E->getBase()->getType()->isVectorType())
583    return EmitLoadOfLValue(E);
584
585  // Handle the vector case.  The base must be a vector, the index must be an
586  // integer value.
587  Value *Base = Visit(E->getBase());
588  Value *Idx  = Visit(E->getIdx());
589  bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
590  Idx = Builder.CreateIntCast(Idx,
591                              llvm::Type::getInt32Ty(CGF.getLLVMContext()),
592                              IdxSigned,
593                              "vecidxcast");
594  return Builder.CreateExtractElement(Base, Idx, "vecext");
595}
596
597static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
598                                  unsigned Off, const llvm::Type *I32Ty) {
599  int MV = SVI->getMaskValue(Idx);
600  if (MV == -1)
601    return llvm::UndefValue::get(I32Ty);
602  return llvm::ConstantInt::get(I32Ty, Off+MV);
603}
604
605Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
606  bool Ignore = TestAndClearIgnoreResultAssign();
607  (void)Ignore;
608  assert (Ignore == false && "init list ignored");
609  unsigned NumInitElements = E->getNumInits();
610
611  if (E->hadArrayRangeDesignator())
612    CGF.ErrorUnsupported(E, "GNU array range designator extension");
613
614  const llvm::VectorType *VType =
615    dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
616
617  // We have a scalar in braces. Just use the first element.
618  if (!VType)
619    return Visit(E->getInit(0));
620
621  unsigned ResElts = VType->getNumElements();
622  const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CGF.getLLVMContext());
623
624  // Loop over initializers collecting the Value for each, and remembering
625  // whether the source was swizzle (ExtVectorElementExpr).  This will allow
626  // us to fold the shuffle for the swizzle into the shuffle for the vector
627  // initializer, since LLVM optimizers generally do not want to touch
628  // shuffles.
629  unsigned CurIdx = 0;
630  bool VIsUndefShuffle = false;
631  llvm::Value *V = llvm::UndefValue::get(VType);
632  for (unsigned i = 0; i != NumInitElements; ++i) {
633    Expr *IE = E->getInit(i);
634    Value *Init = Visit(IE);
635    llvm::SmallVector<llvm::Constant*, 16> Args;
636
637    const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
638
639    // Handle scalar elements.  If the scalar initializer is actually one
640    // element of a different vector of the same width, use shuffle instead of
641    // extract+insert.
642    if (!VVT) {
643      if (isa<ExtVectorElementExpr>(IE)) {
644        llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
645
646        if (EI->getVectorOperandType()->getNumElements() == ResElts) {
647          llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
648          Value *LHS = 0, *RHS = 0;
649          if (CurIdx == 0) {
650            // insert into undef -> shuffle (src, undef)
651            Args.push_back(C);
652            for (unsigned j = 1; j != ResElts; ++j)
653              Args.push_back(llvm::UndefValue::get(I32Ty));
654
655            LHS = EI->getVectorOperand();
656            RHS = V;
657            VIsUndefShuffle = true;
658          } else if (VIsUndefShuffle) {
659            // insert into undefshuffle && size match -> shuffle (v, src)
660            llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
661            for (unsigned j = 0; j != CurIdx; ++j)
662              Args.push_back(getMaskElt(SVV, j, 0, I32Ty));
663            Args.push_back(llvm::ConstantInt::get(I32Ty,
664                                                  ResElts + C->getZExtValue()));
665            for (unsigned j = CurIdx + 1; j != ResElts; ++j)
666              Args.push_back(llvm::UndefValue::get(I32Ty));
667
668            LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
669            RHS = EI->getVectorOperand();
670            VIsUndefShuffle = false;
671          }
672          if (!Args.empty()) {
673            llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
674            V = Builder.CreateShuffleVector(LHS, RHS, Mask);
675            ++CurIdx;
676            continue;
677          }
678        }
679      }
680      Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx);
681      V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
682      VIsUndefShuffle = false;
683      ++CurIdx;
684      continue;
685    }
686
687    unsigned InitElts = VVT->getNumElements();
688
689    // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
690    // input is the same width as the vector being constructed, generate an
691    // optimized shuffle of the swizzle input into the result.
692    unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
693    if (isa<ExtVectorElementExpr>(IE)) {
694      llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
695      Value *SVOp = SVI->getOperand(0);
696      const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
697
698      if (OpTy->getNumElements() == ResElts) {
699        for (unsigned j = 0; j != CurIdx; ++j) {
700          // If the current vector initializer is a shuffle with undef, merge
701          // this shuffle directly into it.
702          if (VIsUndefShuffle) {
703            Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
704                                      I32Ty));
705          } else {
706            Args.push_back(llvm::ConstantInt::get(I32Ty, j));
707          }
708        }
709        for (unsigned j = 0, je = InitElts; j != je; ++j)
710          Args.push_back(getMaskElt(SVI, j, Offset, I32Ty));
711        for (unsigned j = CurIdx + InitElts; j != ResElts; ++j)
712          Args.push_back(llvm::UndefValue::get(I32Ty));
713
714        if (VIsUndefShuffle)
715          V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
716
717        Init = SVOp;
718      }
719    }
720
721    // Extend init to result vector length, and then shuffle its contribution
722    // to the vector initializer into V.
723    if (Args.empty()) {
724      for (unsigned j = 0; j != InitElts; ++j)
725        Args.push_back(llvm::ConstantInt::get(I32Ty, j));
726      for (unsigned j = InitElts; j != ResElts; ++j)
727        Args.push_back(llvm::UndefValue::get(I32Ty));
728      llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
729      Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
730                                         Mask, "vext");
731
732      Args.clear();
733      for (unsigned j = 0; j != CurIdx; ++j)
734        Args.push_back(llvm::ConstantInt::get(I32Ty, j));
735      for (unsigned j = 0; j != InitElts; ++j)
736        Args.push_back(llvm::ConstantInt::get(I32Ty, j+Offset));
737      for (unsigned j = CurIdx + InitElts; j != ResElts; ++j)
738        Args.push_back(llvm::UndefValue::get(I32Ty));
739    }
740
741    // If V is undef, make sure it ends up on the RHS of the shuffle to aid
742    // merging subsequent shuffles into this one.
743    if (CurIdx == 0)
744      std::swap(V, Init);
745    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
746    V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
747    VIsUndefShuffle = isa<llvm::UndefValue>(Init);
748    CurIdx += InitElts;
749  }
750
751  // FIXME: evaluate codegen vs. shuffling against constant null vector.
752  // Emit remaining default initializers.
753  const llvm::Type *EltTy = VType->getElementType();
754
755  // Emit remaining default initializers
756  for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
757    Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx);
758    llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
759    V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
760  }
761  return V;
762}
763
764static bool ShouldNullCheckClassCastValue(const CastExpr *CE) {
765  const Expr *E = CE->getSubExpr();
766
767  if (isa<CXXThisExpr>(E)) {
768    // We always assume that 'this' is never null.
769    return false;
770  }
771
772  if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
773    // And that lvalue casts are never null.
774    if (ICE->isLvalueCast())
775      return false;
776  }
777
778  return true;
779}
780
781// VisitCastExpr - Emit code for an explicit or implicit cast.  Implicit casts
782// have to handle a more broad range of conversions than explicit casts, as they
783// handle things like function to ptr-to-function decay etc.
784Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) {
785  Expr *E = CE->getSubExpr();
786  QualType DestTy = CE->getType();
787  CastExpr::CastKind Kind = CE->getCastKind();
788
789  if (!DestTy->isVoidType())
790    TestAndClearIgnoreResultAssign();
791
792  // Since almost all cast kinds apply to scalars, this switch doesn't have
793  // a default case, so the compiler will warn on a missing case.  The cases
794  // are in the same order as in the CastKind enum.
795  switch (Kind) {
796  case CastExpr::CK_Unknown:
797    // FIXME: All casts should have a known kind!
798    //assert(0 && "Unknown cast kind!");
799    break;
800
801  case CastExpr::CK_BitCast: {
802    Value *Src = Visit(const_cast<Expr*>(E));
803    return Builder.CreateBitCast(Src, ConvertType(DestTy));
804  }
805  case CastExpr::CK_NoOp:
806    return Visit(const_cast<Expr*>(E));
807
808  case CastExpr::CK_BaseToDerived: {
809    const CXXRecordDecl *BaseClassDecl =
810      E->getType()->getCXXRecordDeclForPointerType();
811    const CXXRecordDecl *DerivedClassDecl =
812      DestTy->getCXXRecordDeclForPointerType();
813
814    Value *Src = Visit(const_cast<Expr*>(E));
815
816    bool NullCheckValue = ShouldNullCheckClassCastValue(CE);
817    return CGF.GetAddressOfDerivedClass(Src, BaseClassDecl, DerivedClassDecl,
818                                        NullCheckValue);
819  }
820  case CastExpr::CK_DerivedToBase: {
821    const RecordType *DerivedClassTy =
822      E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
823    CXXRecordDecl *DerivedClassDecl =
824      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
825
826    const RecordType *BaseClassTy =
827      DestTy->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
828    CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
829
830    Value *Src = Visit(const_cast<Expr*>(E));
831
832    bool NullCheckValue = ShouldNullCheckClassCastValue(CE);
833    return CGF.GetAddressOfBaseClass(Src, DerivedClassDecl, BaseClassDecl,
834                                     NullCheckValue);
835  }
836  case CastExpr::CK_Dynamic: {
837    Value *V = Visit(const_cast<Expr*>(E));
838    const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
839    return CGF.EmitDynamicCast(V, DCE);
840  }
841  case CastExpr::CK_ToUnion:
842    assert(0 && "Should be unreachable!");
843    break;
844
845  case CastExpr::CK_ArrayToPointerDecay: {
846    assert(E->getType()->isArrayType() &&
847           "Array to pointer decay must have array source type!");
848
849    Value *V = EmitLValue(E).getAddress();  // Bitfields can't be arrays.
850
851    // Note that VLA pointers are always decayed, so we don't need to do
852    // anything here.
853    if (!E->getType()->isVariableArrayType()) {
854      assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
855      assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
856                                 ->getElementType()) &&
857             "Expected pointer to array");
858      V = Builder.CreateStructGEP(V, 0, "arraydecay");
859    }
860
861    return V;
862  }
863  case CastExpr::CK_FunctionToPointerDecay:
864    return EmitLValue(E).getAddress();
865
866  case CastExpr::CK_NullToMemberPointer:
867    return CGF.CGM.EmitNullConstant(DestTy);
868
869  case CastExpr::CK_BaseToDerivedMemberPointer:
870  case CastExpr::CK_DerivedToBaseMemberPointer: {
871    Value *Src = Visit(E);
872
873    // See if we need to adjust the pointer.
874    const CXXRecordDecl *BaseDecl =
875      cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
876                          getClass()->getAs<RecordType>()->getDecl());
877    const CXXRecordDecl *DerivedDecl =
878      cast<CXXRecordDecl>(CE->getType()->getAs<MemberPointerType>()->
879                          getClass()->getAs<RecordType>()->getDecl());
880    if (CE->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
881      std::swap(DerivedDecl, BaseDecl);
882
883    llvm::Constant *Adj = CGF.CGM.GetCXXBaseClassOffset(DerivedDecl, BaseDecl);
884    if (Adj) {
885      if (CE->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
886        Src = Builder.CreateSub(Src, Adj, "adj");
887      else
888        Src = Builder.CreateAdd(Src, Adj, "adj");
889    }
890    return Src;
891  }
892
893  case CastExpr::CK_UserDefinedConversion:
894  case CastExpr::CK_ConstructorConversion:
895    assert(0 && "Should be unreachable!");
896    break;
897
898  case CastExpr::CK_IntegralToPointer: {
899    Value *Src = Visit(const_cast<Expr*>(E));
900
901    // First, convert to the correct width so that we control the kind of
902    // extension.
903    const llvm::Type *MiddleTy =
904      llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
905    bool InputSigned = E->getType()->isSignedIntegerType();
906    llvm::Value* IntResult =
907      Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
908
909    return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
910  }
911  case CastExpr::CK_PointerToIntegral: {
912    Value *Src = Visit(const_cast<Expr*>(E));
913    return Builder.CreatePtrToInt(Src, ConvertType(DestTy));
914  }
915  case CastExpr::CK_ToVoid: {
916    CGF.EmitAnyExpr(E, 0, false, true);
917    return 0;
918  }
919  case CastExpr::CK_VectorSplat: {
920    const llvm::Type *DstTy = ConvertType(DestTy);
921    Value *Elt = Visit(const_cast<Expr*>(E));
922
923    // Insert the element in element zero of an undef vector
924    llvm::Value *UnV = llvm::UndefValue::get(DstTy);
925    llvm::Value *Idx =
926        llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
927    UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
928
929    // Splat the element across to all elements
930    llvm::SmallVector<llvm::Constant*, 16> Args;
931    unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
932    for (unsigned i = 0; i < NumElements; i++)
933      Args.push_back(llvm::ConstantInt::get(
934                                        llvm::Type::getInt32Ty(VMContext), 0));
935
936    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
937    llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
938    return Yay;
939  }
940  case CastExpr::CK_IntegralCast:
941  case CastExpr::CK_IntegralToFloating:
942  case CastExpr::CK_FloatingToIntegral:
943  case CastExpr::CK_FloatingCast:
944    return EmitScalarConversion(Visit(E), E->getType(), DestTy);
945
946  case CastExpr::CK_MemberPointerToBoolean: {
947    const MemberPointerType* T = E->getType()->getAs<MemberPointerType>();
948
949    if (T->getPointeeType()->isFunctionType()) {
950      // We have a member function pointer.
951      llvm::Value *Ptr = CGF.CreateTempAlloca(ConvertType(E->getType()));
952
953      CGF.EmitAggExpr(E, Ptr, /*VolatileDest=*/false);
954
955      // Get the pointer.
956      llvm::Value *FuncPtr = Builder.CreateStructGEP(Ptr, 0, "src.ptr");
957      FuncPtr = Builder.CreateLoad(FuncPtr);
958
959      llvm::Value *IsNotNull =
960        Builder.CreateICmpNE(FuncPtr,
961                             llvm::Constant::getNullValue(FuncPtr->getType()),
962                             "tobool");
963
964      return IsNotNull;
965    }
966
967    // We have a regular member pointer.
968    Value *Ptr = Visit(const_cast<Expr*>(E));
969    llvm::Value *IsNotNull =
970      Builder.CreateICmpNE(Ptr, CGF.CGM.EmitNullConstant(E->getType()),
971                           "tobool");
972    return IsNotNull;
973  }
974  }
975
976  // Handle cases where the source is an non-complex type.
977
978  if (!CGF.hasAggregateLLVMType(E->getType())) {
979    Value *Src = Visit(const_cast<Expr*>(E));
980
981    // Use EmitScalarConversion to perform the conversion.
982    return EmitScalarConversion(Src, E->getType(), DestTy);
983  }
984
985  if (E->getType()->isAnyComplexType()) {
986    // Handle cases where the source is a complex type.
987    bool IgnoreImag = true;
988    bool IgnoreImagAssign = true;
989    bool IgnoreReal = IgnoreResultAssign;
990    bool IgnoreRealAssign = IgnoreResultAssign;
991    if (DestTy->isBooleanType())
992      IgnoreImagAssign = IgnoreImag = false;
993    else if (DestTy->isVoidType()) {
994      IgnoreReal = IgnoreImag = false;
995      IgnoreRealAssign = IgnoreImagAssign = true;
996    }
997    CodeGenFunction::ComplexPairTy V
998      = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign,
999                            IgnoreImagAssign);
1000    return EmitComplexToScalarConversion(V, E->getType(), DestTy);
1001  }
1002
1003  // Okay, this is a cast from an aggregate.  It must be a cast to void.  Just
1004  // evaluate the result and return.
1005  CGF.EmitAggExpr(E, 0, false, true);
1006  return 0;
1007}
1008
1009Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
1010  return CGF.EmitCompoundStmt(*E->getSubStmt(),
1011                              !E->getType()->isVoidType()).getScalarVal();
1012}
1013
1014Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
1015  llvm::Value *V = CGF.GetAddrOfBlockDecl(E);
1016  if (E->getType().isObjCGCWeak())
1017    return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V);
1018  return Builder.CreateLoad(V, false, "tmp");
1019}
1020
1021//===----------------------------------------------------------------------===//
1022//                             Unary Operators
1023//===----------------------------------------------------------------------===//
1024
1025Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
1026                                             bool isInc, bool isPre) {
1027  LValue LV = EmitLValue(E->getSubExpr());
1028  QualType ValTy = E->getSubExpr()->getType();
1029  Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
1030
1031  llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1032
1033  int AmountVal = isInc ? 1 : -1;
1034
1035  if (ValTy->isPointerType() &&
1036      ValTy->getAs<PointerType>()->isVariableArrayType()) {
1037    // The amount of the addition/subtraction needs to account for the VLA size
1038    CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
1039  }
1040
1041  Value *NextVal;
1042  if (const llvm::PointerType *PT =
1043         dyn_cast<llvm::PointerType>(InVal->getType())) {
1044    llvm::Constant *Inc =
1045      llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
1046    if (!isa<llvm::FunctionType>(PT->getElementType())) {
1047      QualType PTEE = ValTy->getPointeeType();
1048      if (const ObjCInterfaceType *OIT =
1049          dyn_cast<ObjCInterfaceType>(PTEE)) {
1050        // Handle interface types, which are not represented with a concrete type.
1051        int size = CGF.getContext().getTypeSize(OIT) / 8;
1052        if (!isInc)
1053          size = -size;
1054        Inc = llvm::ConstantInt::get(Inc->getType(), size);
1055        const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1056        InVal = Builder.CreateBitCast(InVal, i8Ty);
1057        NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
1058        llvm::Value *lhs = LV.getAddress();
1059        lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
1060        LV = LValue::MakeAddr(lhs, CGF.MakeQualifiers(ValTy));
1061      } else
1062        NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
1063    } else {
1064      const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1065      NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
1066      NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
1067      NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
1068    }
1069  } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
1070    // Bool++ is an interesting case, due to promotion rules, we get:
1071    // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
1072    // Bool = ((int)Bool+1) != 0
1073    // An interesting aspect of this is that increment is always true.
1074    // Decrement does not have this property.
1075    NextVal = llvm::ConstantInt::getTrue(VMContext);
1076  } else if (isa<llvm::IntegerType>(InVal->getType())) {
1077    NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
1078
1079    // Signed integer overflow is undefined behavior.
1080    if (ValTy->isSignedIntegerType())
1081      NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
1082    else
1083      NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
1084  } else {
1085    // Add the inc/dec to the real part.
1086    if (InVal->getType()->isFloatTy())
1087      NextVal =
1088        llvm::ConstantFP::get(VMContext,
1089                              llvm::APFloat(static_cast<float>(AmountVal)));
1090    else if (InVal->getType()->isDoubleTy())
1091      NextVal =
1092        llvm::ConstantFP::get(VMContext,
1093                              llvm::APFloat(static_cast<double>(AmountVal)));
1094    else {
1095      llvm::APFloat F(static_cast<float>(AmountVal));
1096      bool ignored;
1097      F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
1098                &ignored);
1099      NextVal = llvm::ConstantFP::get(VMContext, F);
1100    }
1101    NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
1102  }
1103
1104  // Store the updated result through the lvalue.
1105  if (LV.isBitfield())
1106    CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
1107                                       &NextVal);
1108  else
1109    CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
1110
1111  // If this is a postinc, return the value read from memory, otherwise use the
1112  // updated value.
1113  return isPre ? NextVal : InVal;
1114}
1115
1116
1117Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
1118  TestAndClearIgnoreResultAssign();
1119  Value *Op = Visit(E->getSubExpr());
1120  if (Op->getType()->isFPOrFPVector())
1121    return Builder.CreateFNeg(Op, "neg");
1122  return Builder.CreateNeg(Op, "neg");
1123}
1124
1125Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
1126  TestAndClearIgnoreResultAssign();
1127  Value *Op = Visit(E->getSubExpr());
1128  return Builder.CreateNot(Op, "neg");
1129}
1130
1131Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
1132  // Compare operand to zero.
1133  Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
1134
1135  // Invert value.
1136  // TODO: Could dynamically modify easy computations here.  For example, if
1137  // the operand is an icmp ne, turn into icmp eq.
1138  BoolVal = Builder.CreateNot(BoolVal, "lnot");
1139
1140  // ZExt result to the expr type.
1141  return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
1142}
1143
1144/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
1145/// argument of the sizeof expression as an integer.
1146Value *
1147ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1148  QualType TypeToSize = E->getTypeOfArgument();
1149  if (E->isSizeOf()) {
1150    if (const VariableArrayType *VAT =
1151          CGF.getContext().getAsVariableArrayType(TypeToSize)) {
1152      if (E->isArgumentType()) {
1153        // sizeof(type) - make sure to emit the VLA size.
1154        CGF.EmitVLASize(TypeToSize);
1155      } else {
1156        // C99 6.5.3.4p2: If the argument is an expression of type
1157        // VLA, it is evaluated.
1158        CGF.EmitAnyExpr(E->getArgumentExpr());
1159      }
1160
1161      return CGF.GetVLASize(VAT);
1162    }
1163  }
1164
1165  // If this isn't sizeof(vla), the result must be constant; use the constant
1166  // folding logic so we don't have to duplicate it here.
1167  Expr::EvalResult Result;
1168  E->Evaluate(Result, CGF.getContext());
1169  return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
1170}
1171
1172Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
1173  Expr *Op = E->getSubExpr();
1174  if (Op->getType()->isAnyComplexType())
1175    return CGF.EmitComplexExpr(Op, false, true, false, true).first;
1176  return Visit(Op);
1177}
1178Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
1179  Expr *Op = E->getSubExpr();
1180  if (Op->getType()->isAnyComplexType())
1181    return CGF.EmitComplexExpr(Op, true, false, true, false).second;
1182
1183  // __imag on a scalar returns zero.  Emit the subexpr to ensure side
1184  // effects are evaluated, but not the actual value.
1185  if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid)
1186    CGF.EmitLValue(Op);
1187  else
1188    CGF.EmitScalarExpr(Op, true);
1189  return llvm::Constant::getNullValue(ConvertType(E->getType()));
1190}
1191
1192Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) {
1193  Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
1194  const llvm::Type* ResultType = ConvertType(E->getType());
1195  return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
1196}
1197
1198//===----------------------------------------------------------------------===//
1199//                           Binary Operators
1200//===----------------------------------------------------------------------===//
1201
1202BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
1203  TestAndClearIgnoreResultAssign();
1204  BinOpInfo Result;
1205  Result.LHS = Visit(E->getLHS());
1206  Result.RHS = Visit(E->getRHS());
1207  Result.Ty  = E->getType();
1208  Result.E = E;
1209  return Result;
1210}
1211
1212Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
1213                      Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
1214  bool Ignore = TestAndClearIgnoreResultAssign();
1215  QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
1216
1217  BinOpInfo OpInfo;
1218
1219  if (E->getComputationResultType()->isAnyComplexType()) {
1220    // This needs to go through the complex expression emitter, but it's a tad
1221    // complicated to do that... I'm leaving it out for now.  (Note that we do
1222    // actually need the imaginary part of the RHS for multiplication and
1223    // division.)
1224    CGF.ErrorUnsupported(E, "complex compound assignment");
1225    return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
1226  }
1227
1228  // Emit the RHS first.  __block variables need to have the rhs evaluated
1229  // first, plus this should improve codegen a little.
1230  OpInfo.RHS = Visit(E->getRHS());
1231  OpInfo.Ty = E->getComputationResultType();
1232  OpInfo.E = E;
1233  // Load/convert the LHS.
1234  LValue LHSLV = EmitLValue(E->getLHS());
1235  OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
1236  OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
1237                                    E->getComputationLHSType());
1238
1239  // Expand the binary operator.
1240  Value *Result = (this->*Func)(OpInfo);
1241
1242  // Convert the result back to the LHS type.
1243  Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
1244
1245  // Store the result value into the LHS lvalue. Bit-fields are handled
1246  // specially because the result is altered by the store, i.e., [C99 6.5.16p1]
1247  // 'An assignment expression has the value of the left operand after the
1248  // assignment...'.
1249  if (LHSLV.isBitfield()) {
1250    if (!LHSLV.isVolatileQualified()) {
1251      CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
1252                                         &Result);
1253      return Result;
1254    } else
1255      CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy);
1256  } else
1257    CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
1258  if (Ignore)
1259    return 0;
1260  return EmitLoadOfLValue(LHSLV, E->getType());
1261}
1262
1263
1264Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
1265  if (Ops.LHS->getType()->isFPOrFPVector())
1266    return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
1267  else if (Ops.Ty->isUnsignedIntegerType())
1268    return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
1269  else
1270    return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
1271}
1272
1273Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
1274  // Rem in C can't be a floating point type: C99 6.5.5p2.
1275  if (Ops.Ty->isUnsignedIntegerType())
1276    return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
1277  else
1278    return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
1279}
1280
1281Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
1282  unsigned IID;
1283  unsigned OpID = 0;
1284
1285  switch (Ops.E->getOpcode()) {
1286  case BinaryOperator::Add:
1287  case BinaryOperator::AddAssign:
1288    OpID = 1;
1289    IID = llvm::Intrinsic::sadd_with_overflow;
1290    break;
1291  case BinaryOperator::Sub:
1292  case BinaryOperator::SubAssign:
1293    OpID = 2;
1294    IID = llvm::Intrinsic::ssub_with_overflow;
1295    break;
1296  case BinaryOperator::Mul:
1297  case BinaryOperator::MulAssign:
1298    OpID = 3;
1299    IID = llvm::Intrinsic::smul_with_overflow;
1300    break;
1301  default:
1302    assert(false && "Unsupported operation for overflow detection");
1303    IID = 0;
1304  }
1305  OpID <<= 1;
1306  OpID |= 1;
1307
1308  const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
1309
1310  llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
1311
1312  Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
1313  Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
1314  Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
1315
1316  // Branch in case of overflow.
1317  llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
1318  llvm::BasicBlock *overflowBB =
1319    CGF.createBasicBlock("overflow", CGF.CurFn);
1320  llvm::BasicBlock *continueBB =
1321    CGF.createBasicBlock("overflow.continue", CGF.CurFn);
1322
1323  Builder.CreateCondBr(overflow, overflowBB, continueBB);
1324
1325  // Handle overflow
1326
1327  Builder.SetInsertPoint(overflowBB);
1328
1329  // Handler is:
1330  // long long *__overflow_handler)(long long a, long long b, char op,
1331  // char width)
1332  std::vector<const llvm::Type*> handerArgTypes;
1333  handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1334  handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1335  handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1336  handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1337  llvm::FunctionType *handlerTy = llvm::FunctionType::get(
1338      llvm::Type::getInt64Ty(VMContext), handerArgTypes, false);
1339  llvm::Value *handlerFunction =
1340    CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
1341        llvm::PointerType::getUnqual(handlerTy));
1342  handlerFunction = Builder.CreateLoad(handlerFunction);
1343
1344  llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
1345      Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)),
1346      Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)),
1347      llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID),
1348      llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
1349        cast<llvm::IntegerType>(opTy)->getBitWidth()));
1350
1351  handlerResult = Builder.CreateTrunc(handlerResult, opTy);
1352
1353  Builder.CreateBr(continueBB);
1354
1355  // Set up the continuation
1356  Builder.SetInsertPoint(continueBB);
1357  // Get the correct result
1358  llvm::PHINode *phi = Builder.CreatePHI(opTy);
1359  phi->reserveOperandSpace(2);
1360  phi->addIncoming(result, initialBB);
1361  phi->addIncoming(handlerResult, overflowBB);
1362
1363  return phi;
1364}
1365
1366Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
1367  if (!Ops.Ty->isAnyPointerType()) {
1368    if (CGF.getContext().getLangOptions().OverflowChecking &&
1369        Ops.Ty->isSignedIntegerType())
1370      return EmitOverflowCheckedBinOp(Ops);
1371
1372    if (Ops.LHS->getType()->isFPOrFPVector())
1373      return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add");
1374
1375    // Signed integer overflow is undefined behavior.
1376    if (Ops.Ty->isSignedIntegerType())
1377      return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add");
1378
1379    return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
1380  }
1381
1382  if (Ops.Ty->isPointerType() &&
1383      Ops.Ty->getAs<PointerType>()->isVariableArrayType()) {
1384    // The amount of the addition needs to account for the VLA size
1385    CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
1386  }
1387  Value *Ptr, *Idx;
1388  Expr *IdxExp;
1389  const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>();
1390  const ObjCObjectPointerType *OPT =
1391    Ops.E->getLHS()->getType()->getAs<ObjCObjectPointerType>();
1392  if (PT || OPT) {
1393    Ptr = Ops.LHS;
1394    Idx = Ops.RHS;
1395    IdxExp = Ops.E->getRHS();
1396  } else {  // int + pointer
1397    PT = Ops.E->getRHS()->getType()->getAs<PointerType>();
1398    OPT = Ops.E->getRHS()->getType()->getAs<ObjCObjectPointerType>();
1399    assert((PT || OPT) && "Invalid add expr");
1400    Ptr = Ops.RHS;
1401    Idx = Ops.LHS;
1402    IdxExp = Ops.E->getLHS();
1403  }
1404
1405  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
1406  if (Width < CGF.LLVMPointerWidth) {
1407    // Zero or sign extend the pointer value based on whether the index is
1408    // signed or not.
1409    const llvm::Type *IdxType =
1410        llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
1411    if (IdxExp->getType()->isSignedIntegerType())
1412      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1413    else
1414      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1415  }
1416  const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
1417  // Handle interface types, which are not represented with a concrete type.
1418  if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
1419    llvm::Value *InterfaceSize =
1420      llvm::ConstantInt::get(Idx->getType(),
1421                             CGF.getContext().getTypeSize(OIT) / 8);
1422    Idx = Builder.CreateMul(Idx, InterfaceSize);
1423    const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1424    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
1425    Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
1426    return Builder.CreateBitCast(Res, Ptr->getType());
1427  }
1428
1429  // Explicitly handle GNU void* and function pointer arithmetic extensions. The
1430  // GNU void* casts amount to no-ops since our void* type is i8*, but this is
1431  // future proof.
1432  if (ElementType->isVoidType() || ElementType->isFunctionType()) {
1433    const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1434    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
1435    Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
1436    return Builder.CreateBitCast(Res, Ptr->getType());
1437  }
1438
1439  return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr");
1440}
1441
1442Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
1443  if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
1444    if (CGF.getContext().getLangOptions().OverflowChecking
1445        && Ops.Ty->isSignedIntegerType())
1446      return EmitOverflowCheckedBinOp(Ops);
1447
1448    if (Ops.LHS->getType()->isFPOrFPVector())
1449      return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub");
1450    return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
1451  }
1452
1453  if (Ops.E->getLHS()->getType()->isPointerType() &&
1454      Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) {
1455    // The amount of the addition needs to account for the VLA size for
1456    // ptr-int
1457    // The amount of the division needs to account for the VLA size for
1458    // ptr-ptr.
1459    CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
1460  }
1461
1462  const QualType LHSType = Ops.E->getLHS()->getType();
1463  const QualType LHSElementType = LHSType->getPointeeType();
1464  if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
1465    // pointer - int
1466    Value *Idx = Ops.RHS;
1467    unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
1468    if (Width < CGF.LLVMPointerWidth) {
1469      // Zero or sign extend the pointer value based on whether the index is
1470      // signed or not.
1471      const llvm::Type *IdxType =
1472          llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
1473      if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1474        Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1475      else
1476        Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1477    }
1478    Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
1479
1480    // Handle interface types, which are not represented with a concrete type.
1481    if (const ObjCInterfaceType *OIT =
1482        dyn_cast<ObjCInterfaceType>(LHSElementType)) {
1483      llvm::Value *InterfaceSize =
1484        llvm::ConstantInt::get(Idx->getType(),
1485                               CGF.getContext().getTypeSize(OIT) / 8);
1486      Idx = Builder.CreateMul(Idx, InterfaceSize);
1487      const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1488      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1489      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
1490      return Builder.CreateBitCast(Res, Ops.LHS->getType());
1491    }
1492
1493    // Explicitly handle GNU void* and function pointer arithmetic
1494    // extensions. The GNU void* casts amount to no-ops since our void* type is
1495    // i8*, but this is future proof.
1496    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
1497      const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
1498      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1499      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1500      return Builder.CreateBitCast(Res, Ops.LHS->getType());
1501    }
1502
1503    return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr");
1504  } else {
1505    // pointer - pointer
1506    Value *LHS = Ops.LHS;
1507    Value *RHS = Ops.RHS;
1508
1509    uint64_t ElementSize;
1510
1511    // Handle GCC extension for pointer arithmetic on void* and function pointer
1512    // types.
1513    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
1514      ElementSize = 1;
1515    } else {
1516      ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1517    }
1518
1519    const llvm::Type *ResultType = ConvertType(Ops.Ty);
1520    LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1521    RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1522    Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
1523
1524    // Optimize out the shift for element size of 1.
1525    if (ElementSize == 1)
1526      return BytesBetween;
1527
1528    // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
1529    // pointer difference in C is only defined in the case where both operands
1530    // are pointing to elements of an array.
1531    Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
1532    return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
1533  }
1534}
1535
1536Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
1537  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1538  // RHS to the same size as the LHS.
1539  Value *RHS = Ops.RHS;
1540  if (Ops.LHS->getType() != RHS->getType())
1541    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1542
1543  return Builder.CreateShl(Ops.LHS, RHS, "shl");
1544}
1545
1546Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1547  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1548  // RHS to the same size as the LHS.
1549  Value *RHS = Ops.RHS;
1550  if (Ops.LHS->getType() != RHS->getType())
1551    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1552
1553  if (Ops.Ty->isUnsignedIntegerType())
1554    return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1555  return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1556}
1557
1558Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1559                                      unsigned SICmpOpc, unsigned FCmpOpc) {
1560  TestAndClearIgnoreResultAssign();
1561  Value *Result;
1562  QualType LHSTy = E->getLHS()->getType();
1563  if (!LHSTy->isAnyComplexType()) {
1564    Value *LHS = Visit(E->getLHS());
1565    Value *RHS = Visit(E->getRHS());
1566
1567    if (LHS->getType()->isFPOrFPVector()) {
1568      Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1569                                  LHS, RHS, "cmp");
1570    } else if (LHSTy->isSignedIntegerType()) {
1571      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
1572                                  LHS, RHS, "cmp");
1573    } else {
1574      // Unsigned integers and pointers.
1575      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1576                                  LHS, RHS, "cmp");
1577    }
1578
1579    // If this is a vector comparison, sign extend the result to the appropriate
1580    // vector integer type and return it (don't convert to bool).
1581    if (LHSTy->isVectorType())
1582      return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
1583
1584  } else {
1585    // Complex Comparison: can only be an equality comparison.
1586    CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1587    CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1588
1589    QualType CETy = LHSTy->getAs<ComplexType>()->getElementType();
1590
1591    Value *ResultR, *ResultI;
1592    if (CETy->isRealFloatingType()) {
1593      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1594                                   LHS.first, RHS.first, "cmp.r");
1595      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1596                                   LHS.second, RHS.second, "cmp.i");
1597    } else {
1598      // Complex comparisons can only be equality comparisons.  As such, signed
1599      // and unsigned opcodes are the same.
1600      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1601                                   LHS.first, RHS.first, "cmp.r");
1602      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1603                                   LHS.second, RHS.second, "cmp.i");
1604    }
1605
1606    if (E->getOpcode() == BinaryOperator::EQ) {
1607      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1608    } else {
1609      assert(E->getOpcode() == BinaryOperator::NE &&
1610             "Complex comparison other than == or != ?");
1611      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1612    }
1613  }
1614
1615  return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
1616}
1617
1618Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1619  bool Ignore = TestAndClearIgnoreResultAssign();
1620
1621  // __block variables need to have the rhs evaluated first, plus this should
1622  // improve codegen just a little.
1623  Value *RHS = Visit(E->getRHS());
1624  LValue LHS = EmitLValue(E->getLHS());
1625
1626  // Store the value into the LHS.  Bit-fields are handled specially
1627  // because the result is altered by the store, i.e., [C99 6.5.16p1]
1628  // 'An assignment expression has the value of the left operand after
1629  // the assignment...'.
1630  if (LHS.isBitfield()) {
1631    if (!LHS.isVolatileQualified()) {
1632      CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1633                                         &RHS);
1634      return RHS;
1635    } else
1636      CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType());
1637  } else
1638    CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
1639  if (Ignore)
1640    return 0;
1641  return EmitLoadOfLValue(LHS, E->getType());
1642}
1643
1644Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1645  const llvm::Type *ResTy = ConvertType(E->getType());
1646
1647  // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1648  // If we have 1 && X, just emit X without inserting the control flow.
1649  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1650    if (Cond == 1) { // If we have 1 && X, just emit X.
1651      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1652      // ZExt result to int or bool.
1653      return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
1654    }
1655
1656    // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
1657    if (!CGF.ContainsLabel(E->getRHS()))
1658      return llvm::Constant::getNullValue(ResTy);
1659  }
1660
1661  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1662  llvm::BasicBlock *RHSBlock  = CGF.createBasicBlock("land.rhs");
1663
1664  // Branch on the LHS first.  If it is false, go to the failure (cont) block.
1665  CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1666
1667  // Any edges into the ContBlock are now from an (indeterminate number of)
1668  // edges from this first condition.  All of these values will be false.  Start
1669  // setting up the PHI node in the Cont Block for this.
1670  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1671                                            "", ContBlock);
1672  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1673  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1674       PI != PE; ++PI)
1675    PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
1676
1677  CGF.StartConditionalBranch();
1678  CGF.EmitBlock(RHSBlock);
1679  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1680  CGF.FinishConditionalBranch();
1681
1682  // Reaquire the RHS block, as there may be subblocks inserted.
1683  RHSBlock = Builder.GetInsertBlock();
1684
1685  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1686  // into the phi node for the edge with the value of RHSCond.
1687  CGF.EmitBlock(ContBlock);
1688  PN->addIncoming(RHSCond, RHSBlock);
1689
1690  // ZExt result to int.
1691  return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
1692}
1693
1694Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1695  const llvm::Type *ResTy = ConvertType(E->getType());
1696
1697  // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1698  // If we have 0 || X, just emit X without inserting the control flow.
1699  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1700    if (Cond == -1) { // If we have 0 || X, just emit X.
1701      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1702      // ZExt result to int or bool.
1703      return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
1704    }
1705
1706    // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
1707    if (!CGF.ContainsLabel(E->getRHS()))
1708      return llvm::ConstantInt::get(ResTy, 1);
1709  }
1710
1711  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1712  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
1713
1714  // Branch on the LHS first.  If it is true, go to the success (cont) block.
1715  CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1716
1717  // Any edges into the ContBlock are now from an (indeterminate number of)
1718  // edges from this first condition.  All of these values will be true.  Start
1719  // setting up the PHI node in the Cont Block for this.
1720  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1721                                            "", ContBlock);
1722  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1723  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1724       PI != PE; ++PI)
1725    PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
1726
1727  CGF.StartConditionalBranch();
1728
1729  // Emit the RHS condition as a bool value.
1730  CGF.EmitBlock(RHSBlock);
1731  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1732
1733  CGF.FinishConditionalBranch();
1734
1735  // Reaquire the RHS block, as there may be subblocks inserted.
1736  RHSBlock = Builder.GetInsertBlock();
1737
1738  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1739  // into the phi node for the edge with the value of RHSCond.
1740  CGF.EmitBlock(ContBlock);
1741  PN->addIncoming(RHSCond, RHSBlock);
1742
1743  // ZExt result to int.
1744  return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
1745}
1746
1747Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1748  CGF.EmitStmt(E->getLHS());
1749  CGF.EnsureInsertPoint();
1750  return Visit(E->getRHS());
1751}
1752
1753//===----------------------------------------------------------------------===//
1754//                             Other Operators
1755//===----------------------------------------------------------------------===//
1756
1757/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1758/// expression is cheap enough and side-effect-free enough to evaluate
1759/// unconditionally instead of conditionally.  This is used to convert control
1760/// flow into selects in some cases.
1761static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
1762                                                   CodeGenFunction &CGF) {
1763  if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1764    return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr(), CGF);
1765
1766  // TODO: Allow anything we can constant fold to an integer or fp constant.
1767  if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1768      isa<FloatingLiteral>(E))
1769    return true;
1770
1771  // Non-volatile automatic variables too, to get "cond ? X : Y" where
1772  // X and Y are local variables.
1773  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1774    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1775      if (VD->hasLocalStorage() && !(CGF.getContext()
1776                                     .getCanonicalType(VD->getType())
1777                                     .isVolatileQualified()))
1778        return true;
1779
1780  return false;
1781}
1782
1783
1784Value *ScalarExprEmitter::
1785VisitConditionalOperator(const ConditionalOperator *E) {
1786  TestAndClearIgnoreResultAssign();
1787  // If the condition constant folds and can be elided, try to avoid emitting
1788  // the condition and the dead arm.
1789  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
1790    Expr *Live = E->getLHS(), *Dead = E->getRHS();
1791    if (Cond == -1)
1792      std::swap(Live, Dead);
1793
1794    // If the dead side doesn't have labels we need, and if the Live side isn't
1795    // the gnu missing ?: extension (which we could handle, but don't bother
1796    // to), just emit the Live part.
1797    if ((!Dead || !CGF.ContainsLabel(Dead)) &&  // No labels in dead part
1798        Live)                                   // Live part isn't missing.
1799      return Visit(Live);
1800  }
1801
1802
1803  // If this is a really simple expression (like x ? 4 : 5), emit this as a
1804  // select instead of as control flow.  We can only do this if it is cheap and
1805  // safe to evaluate the LHS and RHS unconditionally.
1806  if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS(),
1807                                                            CGF) &&
1808      isCheapEnoughToEvaluateUnconditionally(E->getRHS(), CGF)) {
1809    llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1810    llvm::Value *LHS = Visit(E->getLHS());
1811    llvm::Value *RHS = Visit(E->getRHS());
1812    return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1813  }
1814
1815
1816  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1817  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1818  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1819  Value *CondVal = 0;
1820
1821  // If we don't have the GNU missing condition extension, emit a branch on bool
1822  // the normal way.
1823  if (E->getLHS()) {
1824    // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1825    // the branch on bool.
1826    CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1827  } else {
1828    // Otherwise, for the ?: extension, evaluate the conditional and then
1829    // convert it to bool the hard way.  We do this explicitly because we need
1830    // the unconverted value for the missing middle value of the ?:.
1831    CondVal = CGF.EmitScalarExpr(E->getCond());
1832
1833    // In some cases, EmitScalarConversion will delete the "CondVal" expression
1834    // if there are no extra uses (an optimization).  Inhibit this by making an
1835    // extra dead use, because we're going to add a use of CondVal later.  We
1836    // don't use the builder for this, because we don't want it to get optimized
1837    // away.  This leaves dead code, but the ?: extension isn't common.
1838    new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1839                          Builder.GetInsertBlock());
1840
1841    Value *CondBoolVal =
1842      CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1843                               CGF.getContext().BoolTy);
1844    Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
1845  }
1846
1847  CGF.StartConditionalBranch();
1848  CGF.EmitBlock(LHSBlock);
1849
1850  // Handle the GNU extension for missing LHS.
1851  Value *LHS;
1852  if (E->getLHS())
1853    LHS = Visit(E->getLHS());
1854  else    // Perform promotions, to handle cases like "short ?: int"
1855    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1856
1857  CGF.FinishConditionalBranch();
1858  LHSBlock = Builder.GetInsertBlock();
1859  CGF.EmitBranch(ContBlock);
1860
1861  CGF.StartConditionalBranch();
1862  CGF.EmitBlock(RHSBlock);
1863
1864  Value *RHS = Visit(E->getRHS());
1865  CGF.FinishConditionalBranch();
1866  RHSBlock = Builder.GetInsertBlock();
1867  CGF.EmitBranch(ContBlock);
1868
1869  CGF.EmitBlock(ContBlock);
1870
1871  if (!LHS || !RHS) {
1872    assert(E->getType()->isVoidType() && "Non-void value should have a value");
1873    return 0;
1874  }
1875
1876  // Create a PHI node for the real part.
1877  llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1878  PN->reserveOperandSpace(2);
1879  PN->addIncoming(LHS, LHSBlock);
1880  PN->addIncoming(RHS, RHSBlock);
1881  return PN;
1882}
1883
1884Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1885  return Visit(E->getChosenSubExpr(CGF.getContext()));
1886}
1887
1888Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1889  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
1890  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1891
1892  // If EmitVAArg fails, we fall back to the LLVM instruction.
1893  if (!ArgPtr)
1894    return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1895
1896  // FIXME Volatility.
1897  return Builder.CreateLoad(ArgPtr);
1898}
1899
1900Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
1901  return CGF.BuildBlockLiteralTmp(BE);
1902}
1903
1904//===----------------------------------------------------------------------===//
1905//                         Entry Point into this File
1906//===----------------------------------------------------------------------===//
1907
1908/// EmitScalarExpr - Emit the computation of the specified expression of scalar
1909/// type, ignoring the result.
1910Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
1911  assert(E && !hasAggregateLLVMType(E->getType()) &&
1912         "Invalid scalar expression to emit");
1913
1914  return ScalarExprEmitter(*this, IgnoreResultAssign)
1915    .Visit(const_cast<Expr*>(E));
1916}
1917
1918/// EmitScalarConversion - Emit a conversion from the specified type to the
1919/// specified destination type, both of which are LLVM scalar types.
1920Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1921                                             QualType DstTy) {
1922  assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1923         "Invalid scalar expression to emit");
1924  return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1925}
1926
1927/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
1928/// type to the specified destination type, where the destination type is an
1929/// LLVM scalar type.
1930Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1931                                                      QualType SrcTy,
1932                                                      QualType DstTy) {
1933  assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
1934         "Invalid complex -> scalar conversion");
1935  return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1936                                                                DstTy);
1937}
1938
1939Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1940  assert(V1->getType() == V2->getType() &&
1941         "Vector operands must be of the same type");
1942  unsigned NumElements =
1943    cast<llvm::VectorType>(V1->getType())->getNumElements();
1944
1945  va_list va;
1946  va_start(va, V2);
1947
1948  llvm::SmallVector<llvm::Constant*, 16> Args;
1949  for (unsigned i = 0; i < NumElements; i++) {
1950    int n = va_arg(va, int);
1951    assert(n >= 0 && n < (int)NumElements * 2 &&
1952           "Vector shuffle index out of bounds!");
1953    Args.push_back(llvm::ConstantInt::get(
1954                                         llvm::Type::getInt32Ty(VMContext), n));
1955  }
1956
1957  const char *Name = va_arg(va, const char *);
1958  va_end(va);
1959
1960  llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1961
1962  return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1963}
1964
1965llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
1966                                         unsigned NumVals, bool isSplat) {
1967  llvm::Value *Vec
1968    = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1969
1970  for (unsigned i = 0, e = NumVals; i != e; ++i) {
1971    llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
1972    llvm::Value *Idx = llvm::ConstantInt::get(
1973                                          llvm::Type::getInt32Ty(VMContext), i);
1974    Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
1975  }
1976
1977  return Vec;
1978}
1979