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