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