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