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