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