CGExprScalar.cpp revision 8339b35ca05dd040a9a0ecfc92e7b49d80c5a96b
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 "CodeGenModule.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/RecordLayout.h"
19#include "clang/AST/StmtVisitor.h"
20#include "clang/Basic/TargetInfo.h"
21#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Target/TargetData.h"
28#include <cstdarg>
29
30using namespace clang;
31using namespace CodeGen;
32using llvm::Value;
33
34//===----------------------------------------------------------------------===//
35//                         Scalar Expression Emitter
36//===----------------------------------------------------------------------===//
37
38struct BinOpInfo {
39  Value *LHS;
40  Value *RHS;
41  QualType Ty;  // Computation Type.
42  const BinaryOperator *E;
43};
44
45namespace {
46class VISIBILITY_HIDDEN ScalarExprEmitter
47  : public StmtVisitor<ScalarExprEmitter, Value*> {
48  CodeGenFunction &CGF;
49  CGBuilderTy &Builder;
50
51public:
52
53  ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
54    Builder(CGF.Builder) {
55  }
56
57  //===--------------------------------------------------------------------===//
58  //                               Utilities
59  //===--------------------------------------------------------------------===//
60
61  const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
62  LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
63
64  Value *EmitLoadOfLValue(LValue LV, QualType T) {
65    return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
66  }
67
68  /// EmitLoadOfLValue - Given an expression with complex type that represents a
69  /// value l-value, this method emits the address of the l-value, then loads
70  /// and returns the result.
71  Value *EmitLoadOfLValue(const Expr *E) {
72    // FIXME: Volatile
73    return EmitLoadOfLValue(EmitLValue(E), E->getType());
74  }
75
76  /// EmitConversionToBool - Convert the specified expression value to a
77  /// boolean (i1) truth value.  This is equivalent to "Val != 0".
78  Value *EmitConversionToBool(Value *Src, QualType DstTy);
79
80  /// EmitScalarConversion - Emit a conversion from the specified type to the
81  /// specified destination type, both of which are LLVM scalar types.
82  Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
83
84  /// EmitComplexToScalarConversion - Emit a conversion from the specified
85  /// complex type to the specified destination type, where the destination
86  /// type is an LLVM scalar type.
87  Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
88                                       QualType SrcTy, QualType DstTy);
89
90  //===--------------------------------------------------------------------===//
91  //                            Visitor Methods
92  //===--------------------------------------------------------------------===//
93
94  Value *VisitStmt(Stmt *S) {
95    S->dump(CGF.getContext().getSourceManager());
96    assert(0 && "Stmt can't have complex result type!");
97    return 0;
98  }
99  Value *VisitExpr(Expr *S);
100  Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
101
102  // Leaves.
103  Value *VisitIntegerLiteral(const IntegerLiteral *E) {
104    return llvm::ConstantInt::get(E->getValue());
105  }
106  Value *VisitFloatingLiteral(const FloatingLiteral *E) {
107    return llvm::ConstantFP::get(E->getValue());
108  }
109  Value *VisitCharacterLiteral(const CharacterLiteral *E) {
110    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111  }
112  Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
113    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
114  }
115  Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
116    return llvm::Constant::getNullValue(ConvertType(E->getType()));
117  }
118  Value *VisitGNUNullExpr(const GNUNullExpr *E) {
119    return llvm::Constant::getNullValue(ConvertType(E->getType()));
120  }
121  Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
122    return llvm::ConstantInt::get(ConvertType(E->getType()),
123                                  CGF.getContext().typesAreCompatible(
124                                    E->getArgType1(), E->getArgType2()));
125  }
126  Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
127  Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
128    llvm::Value *V =
129      llvm::ConstantInt::get(llvm::Type::Int32Ty,
130                             CGF.GetIDForAddrOfLabel(E->getLabel()));
131
132    return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
133  }
134
135  // l-values.
136  Value *VisitDeclRefExpr(DeclRefExpr *E) {
137    if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
138      return llvm::ConstantInt::get(EC->getInitVal());
139    return EmitLoadOfLValue(E);
140  }
141  Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
142    return CGF.EmitObjCSelectorExpr(E);
143  }
144  Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
145    return CGF.EmitObjCProtocolExpr(E);
146  }
147  Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
148    return EmitLoadOfLValue(E);
149  }
150  Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
151    return EmitLoadOfLValue(E);
152  }
153  Value *VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
154    return EmitLoadOfLValue(E);
155  }
156  Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
157    return CGF.EmitObjCMessageExpr(E).getScalarVal();
158  }
159
160  Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
161  Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
162  Value *VisitMemberExpr(Expr *E)           { return EmitLoadOfLValue(E); }
163  Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
164  Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
165    return EmitLoadOfLValue(E);
166  }
167  Value *VisitStringLiteral(Expr *E)  { return EmitLValue(E).getAddress(); }
168  Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
169     return EmitLValue(E).getAddress();
170  }
171
172  Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
173
174  Value *VisitInitListExpr(InitListExpr *E) {
175    unsigned NumInitElements = E->getNumInits();
176
177    if (E->hadArrayRangeDesignator()) {
178      CGF.ErrorUnsupported(E, "GNU array range designator extension");
179    }
180
181    const llvm::VectorType *VType =
182      dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
183
184    // We have a scalar in braces. Just use the first element.
185    if (!VType)
186      return Visit(E->getInit(0));
187
188    unsigned NumVectorElements = VType->getNumElements();
189    const llvm::Type *ElementType = VType->getElementType();
190
191    // Emit individual vector element stores.
192    llvm::Value *V = llvm::UndefValue::get(VType);
193
194    // Emit initializers
195    unsigned i;
196    for (i = 0; i < NumInitElements; ++i) {
197      Value *NewV = Visit(E->getInit(i));
198      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
199      V = Builder.CreateInsertElement(V, NewV, Idx);
200    }
201
202    // Emit remaining default initializers
203    for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
204      Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
205      llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
206      V = Builder.CreateInsertElement(V, NewV, Idx);
207    }
208
209    return V;
210  }
211
212  Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
213    return llvm::Constant::getNullValue(ConvertType(E->getType()));
214  }
215  Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
216  Value *VisitCastExpr(const CastExpr *E) {
217    return EmitCastExpr(E->getSubExpr(), E->getType());
218  }
219  Value *EmitCastExpr(const Expr *E, QualType T);
220
221  Value *VisitCallExpr(const CallExpr *E) {
222    return CGF.EmitCallExpr(E).getScalarVal();
223  }
224
225  Value *VisitStmtExpr(const StmtExpr *E);
226
227  Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
228
229  // Unary Operators.
230  Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
231  Value *VisitUnaryPostDec(const UnaryOperator *E) {
232    return VisitPrePostIncDec(E, false, false);
233  }
234  Value *VisitUnaryPostInc(const UnaryOperator *E) {
235    return VisitPrePostIncDec(E, true, false);
236  }
237  Value *VisitUnaryPreDec(const UnaryOperator *E) {
238    return VisitPrePostIncDec(E, false, true);
239  }
240  Value *VisitUnaryPreInc(const UnaryOperator *E) {
241    return VisitPrePostIncDec(E, true, true);
242  }
243  Value *VisitUnaryAddrOf(const UnaryOperator *E) {
244    return EmitLValue(E->getSubExpr()).getAddress();
245  }
246  Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
247  Value *VisitUnaryPlus(const UnaryOperator *E) {
248    return Visit(E->getSubExpr());
249  }
250  Value *VisitUnaryMinus    (const UnaryOperator *E);
251  Value *VisitUnaryNot      (const UnaryOperator *E);
252  Value *VisitUnaryLNot     (const UnaryOperator *E);
253  Value *VisitUnaryReal     (const UnaryOperator *E);
254  Value *VisitUnaryImag     (const UnaryOperator *E);
255  Value *VisitUnaryExtension(const UnaryOperator *E) {
256    return Visit(E->getSubExpr());
257  }
258  Value *VisitUnaryOffsetOf(const UnaryOperator *E);
259  Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
260    return Visit(DAE->getExpr());
261  }
262
263  // Binary Operators.
264  Value *EmitMul(const BinOpInfo &Ops) {
265    return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
266  }
267  Value *EmitDiv(const BinOpInfo &Ops);
268  Value *EmitRem(const BinOpInfo &Ops);
269  Value *EmitAdd(const BinOpInfo &Ops);
270  Value *EmitSub(const BinOpInfo &Ops);
271  Value *EmitShl(const BinOpInfo &Ops);
272  Value *EmitShr(const BinOpInfo &Ops);
273  Value *EmitAnd(const BinOpInfo &Ops) {
274    return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
275  }
276  Value *EmitXor(const BinOpInfo &Ops) {
277    return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
278  }
279  Value *EmitOr (const BinOpInfo &Ops) {
280    return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
281  }
282
283  BinOpInfo EmitBinOps(const BinaryOperator *E);
284  Value *EmitCompoundAssign(const CompoundAssignOperator *E,
285                            Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
286
287  // Binary operators and binary compound assignment operators.
288#define HANDLEBINOP(OP) \
289  Value *VisitBin ## OP(const BinaryOperator *E) {                         \
290    return Emit ## OP(EmitBinOps(E));                                      \
291  }                                                                        \
292  Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) {       \
293    return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP);          \
294  }
295  HANDLEBINOP(Mul);
296  HANDLEBINOP(Div);
297  HANDLEBINOP(Rem);
298  HANDLEBINOP(Add);
299  HANDLEBINOP(Sub);
300  HANDLEBINOP(Shl);
301  HANDLEBINOP(Shr);
302  HANDLEBINOP(And);
303  HANDLEBINOP(Xor);
304  HANDLEBINOP(Or);
305#undef HANDLEBINOP
306
307  // Comparisons.
308  Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
309                     unsigned SICmpOpc, unsigned FCmpOpc);
310#define VISITCOMP(CODE, UI, SI, FP) \
311    Value *VisitBin##CODE(const BinaryOperator *E) { \
312      return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
313                         llvm::FCmpInst::FP); }
314  VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
315  VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
316  VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
317  VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
318  VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
319  VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
320#undef VISITCOMP
321
322  Value *VisitBinAssign     (const BinaryOperator *E);
323
324  Value *VisitBinLAnd       (const BinaryOperator *E);
325  Value *VisitBinLOr        (const BinaryOperator *E);
326  Value *VisitBinComma      (const BinaryOperator *E);
327
328  // Other Operators.
329  Value *VisitBlockExpr(const BlockExpr *BE);
330  Value *VisitConditionalOperator(const ConditionalOperator *CO);
331  Value *VisitChooseExpr(ChooseExpr *CE);
332  Value *VisitVAArgExpr(VAArgExpr *VE);
333  Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
334    return CGF.EmitObjCStringLiteral(E);
335  }
336};
337}  // end anonymous namespace.
338
339//===----------------------------------------------------------------------===//
340//                                Utilities
341//===----------------------------------------------------------------------===//
342
343/// EmitConversionToBool - Convert the specified expression value to a
344/// boolean (i1) truth value.  This is equivalent to "Val != 0".
345Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
346  assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
347
348  if (SrcType->isRealFloatingType()) {
349    // Compare against 0.0 for fp scalars.
350    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
351    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
352  }
353
354  assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
355         "Unknown scalar type to convert");
356
357  // Because of the type rules of C, we often end up computing a logical value,
358  // then zero extending it to int, then wanting it as a logical value again.
359  // Optimize this common case.
360  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
361    if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
362      Value *Result = ZI->getOperand(0);
363      // If there aren't any more uses, zap the instruction to save space.
364      // Note that there can be more uses, for example if this
365      // is the result of an assignment.
366      if (ZI->use_empty())
367        ZI->eraseFromParent();
368      return Result;
369    }
370  }
371
372  // Compare against an integer or pointer null.
373  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
374  return Builder.CreateICmpNE(Src, Zero, "tobool");
375}
376
377/// EmitScalarConversion - Emit a conversion from the specified type to the
378/// specified destination type, both of which are LLVM scalar types.
379Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
380                                               QualType DstType) {
381  SrcType = CGF.getContext().getCanonicalType(SrcType);
382  DstType = CGF.getContext().getCanonicalType(DstType);
383  if (SrcType == DstType) return Src;
384
385  if (DstType->isVoidType()) return 0;
386
387  // Handle conversions to bool first, they are special: comparisons against 0.
388  if (DstType->isBooleanType())
389    return EmitConversionToBool(Src, SrcType);
390
391  const llvm::Type *DstTy = ConvertType(DstType);
392
393  // Ignore conversions like int -> uint.
394  if (Src->getType() == DstTy)
395    return Src;
396
397  // Handle pointer conversions next: pointers can only be converted
398  // to/from other pointers and integers. Check for pointer types in
399  // terms of LLVM, as some native types (like Obj-C id) may map to a
400  // pointer type.
401  if (isa<llvm::PointerType>(DstTy)) {
402    // The source value may be an integer, or a pointer.
403    if (isa<llvm::PointerType>(Src->getType()))
404      return Builder.CreateBitCast(Src, DstTy, "conv");
405    assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
406    // First, convert to the correct width so that we control the kind of
407    // extension.
408    const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth);
409    bool InputSigned = SrcType->isSignedIntegerType();
410    llvm::Value* IntResult =
411        Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
412    // Then, cast to pointer.
413    return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
414  }
415
416  if (isa<llvm::PointerType>(Src->getType())) {
417    // Must be an ptr to int cast.
418    assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
419    return Builder.CreatePtrToInt(Src, DstTy, "conv");
420  }
421
422  // A scalar can be splatted to an extended vector of the same element type
423  if (DstType->isExtVectorType() && !isa<VectorType>(SrcType)) {
424    // Cast the scalar to element type
425    QualType EltTy = DstType->getAsExtVectorType()->getElementType();
426    llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
427
428    // Insert the element in element zero of an undef vector
429    llvm::Value *UnV = llvm::UndefValue::get(DstTy);
430    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
431    UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
432
433    // Splat the element across to all elements
434    llvm::SmallVector<llvm::Constant*, 16> Args;
435    unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
436    for (unsigned i = 0; i < NumElements; i++)
437      Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
438
439    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
440    llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
441    return Yay;
442  }
443
444  // Allow bitcast from vector to integer/fp of the same size.
445  if (isa<llvm::VectorType>(Src->getType()) ||
446      isa<llvm::VectorType>(DstTy))
447    return Builder.CreateBitCast(Src, DstTy, "conv");
448
449  // Finally, we have the arithmetic types: real int/float.
450  if (isa<llvm::IntegerType>(Src->getType())) {
451    bool InputSigned = SrcType->isSignedIntegerType();
452    if (isa<llvm::IntegerType>(DstTy))
453      return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
454    else if (InputSigned)
455      return Builder.CreateSIToFP(Src, DstTy, "conv");
456    else
457      return Builder.CreateUIToFP(Src, DstTy, "conv");
458  }
459
460  assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
461  if (isa<llvm::IntegerType>(DstTy)) {
462    if (DstType->isSignedIntegerType())
463      return Builder.CreateFPToSI(Src, DstTy, "conv");
464    else
465      return Builder.CreateFPToUI(Src, DstTy, "conv");
466  }
467
468  assert(DstTy->isFloatingPoint() && "Unknown real conversion");
469  if (DstTy->getTypeID() < Src->getType()->getTypeID())
470    return Builder.CreateFPTrunc(Src, DstTy, "conv");
471  else
472    return Builder.CreateFPExt(Src, DstTy, "conv");
473}
474
475/// EmitComplexToScalarConversion - Emit a conversion from the specified
476/// complex type to the specified destination type, where the destination
477/// type is an LLVM scalar type.
478Value *ScalarExprEmitter::
479EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
480                              QualType SrcTy, QualType DstTy) {
481  // Get the source element type.
482  SrcTy = SrcTy->getAsComplexType()->getElementType();
483
484  // Handle conversions to bool first, they are special: comparisons against 0.
485  if (DstTy->isBooleanType()) {
486    //  Complex != 0  -> (Real != 0) | (Imag != 0)
487    Src.first  = EmitScalarConversion(Src.first, SrcTy, DstTy);
488    Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
489    return Builder.CreateOr(Src.first, Src.second, "tobool");
490  }
491
492  // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
493  // the imaginary part of the complex value is discarded and the value of the
494  // real part is converted according to the conversion rules for the
495  // corresponding real type.
496  return EmitScalarConversion(Src.first, SrcTy, DstTy);
497}
498
499
500//===----------------------------------------------------------------------===//
501//                            Visitor Methods
502//===----------------------------------------------------------------------===//
503
504Value *ScalarExprEmitter::VisitExpr(Expr *E) {
505  CGF.ErrorUnsupported(E, "scalar expression");
506  if (E->getType()->isVoidType())
507    return 0;
508  return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
509}
510
511Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
512  llvm::SmallVector<llvm::Constant*, 32> indices;
513  for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
514    indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
515  }
516  Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
517  Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
518  Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
519  return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
520}
521
522Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
523  // Emit subscript expressions in rvalue context's.  For most cases, this just
524  // loads the lvalue formed by the subscript expr.  However, we have to be
525  // careful, because the base of a vector subscript is occasionally an rvalue,
526  // so we can't get it as an lvalue.
527  if (!E->getBase()->getType()->isVectorType())
528    return EmitLoadOfLValue(E);
529
530  // Handle the vector case.  The base must be a vector, the index must be an
531  // integer value.
532  Value *Base = Visit(E->getBase());
533  Value *Idx  = Visit(E->getIdx());
534
535  // FIXME: Convert Idx to i32 type.
536  return Builder.CreateExtractElement(Base, Idx, "vecext");
537}
538
539/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
540/// also handle things like function to pointer-to-function decay, and array to
541/// pointer decay.
542Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
543  const Expr *Op = E->getSubExpr();
544
545  // If this is due to array->pointer conversion, emit the array expression as
546  // an l-value.
547  if (Op->getType()->isArrayType()) {
548    // FIXME: For now we assume that all source arrays map to LLVM arrays.  This
549    // will not true when we add support for VLAs.
550    Value *V = EmitLValue(Op).getAddress();  // Bitfields can't be arrays.
551
552    if (!Op->getType()->isVariableArrayType()) {
553      assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
554      assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
555                                 ->getElementType()) &&
556             "Expected pointer to array");
557      V = Builder.CreateStructGEP(V, 0, "arraydecay");
558    }
559
560    // The resultant pointer type can be implicitly casted to other pointer
561    // types as well (e.g. void*) and can be implicitly converted to integer.
562    const llvm::Type *DestTy = ConvertType(E->getType());
563    if (V->getType() != DestTy) {
564      if (isa<llvm::PointerType>(DestTy))
565        V = Builder.CreateBitCast(V, DestTy, "ptrconv");
566      else {
567        assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
568        V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
569      }
570    }
571    return V;
572
573  } else if (E->getType()->isReferenceType()) {
574    return EmitLValue(Op).getAddress();
575  }
576
577  return EmitCastExpr(Op, E->getType());
578}
579
580
581// VisitCastExpr - Emit code for an explicit or implicit cast.  Implicit casts
582// have to handle a more broad range of conversions than explicit casts, as they
583// handle things like function to ptr-to-function decay etc.
584Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
585  // Handle cases where the source is an non-complex type.
586
587  if (!CGF.hasAggregateLLVMType(E->getType())) {
588    Value *Src = Visit(const_cast<Expr*>(E));
589
590    // Use EmitScalarConversion to perform the conversion.
591    return EmitScalarConversion(Src, E->getType(), DestTy);
592  }
593
594  if (E->getType()->isAnyComplexType()) {
595    // Handle cases where the source is a complex type.
596    return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
597                                         DestTy);
598  }
599
600  // Okay, this is a cast from an aggregate.  It must be a cast to void.  Just
601  // evaluate the result and return.
602  CGF.EmitAggExpr(E, 0, false);
603  return 0;
604}
605
606Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
607  return CGF.EmitCompoundStmt(*E->getSubStmt(),
608                              !E->getType()->isVoidType()).getScalarVal();
609}
610
611Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
612  return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
613}
614
615//===----------------------------------------------------------------------===//
616//                             Unary Operators
617//===----------------------------------------------------------------------===//
618
619Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
620                                             bool isInc, bool isPre) {
621  LValue LV = EmitLValue(E->getSubExpr());
622  // FIXME: Handle volatile!
623  Value *InVal = CGF.EmitLoadOfLValue(LV, // false
624                                     E->getSubExpr()->getType()).getScalarVal();
625
626  int AmountVal = isInc ? 1 : -1;
627
628  Value *NextVal;
629  if (isa<llvm::PointerType>(InVal->getType())) {
630    // FIXME: This isn't right for VLAs.
631    NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
632    NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
633  } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) {
634    // Bool++ is an interesting case, due to promotion rules, we get:
635    // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
636    // Bool = ((int)Bool+1) != 0
637    // An interesting aspect of this is that increment is always true.
638    // Decrement does not have this property.
639    NextVal = llvm::ConstantInt::getTrue();
640  } else {
641    // Add the inc/dec to the real part.
642    if (isa<llvm::IntegerType>(InVal->getType()))
643      NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
644    else if (InVal->getType() == llvm::Type::FloatTy)
645      NextVal =
646        llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
647    else if (InVal->getType() == llvm::Type::DoubleTy)
648      NextVal =
649        llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
650    else {
651      llvm::APFloat F(static_cast<float>(AmountVal));
652      bool ignored;
653      F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
654                &ignored);
655      NextVal = llvm::ConstantFP::get(F);
656    }
657    NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
658  }
659
660  // Store the updated result through the lvalue.
661  CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
662                             E->getSubExpr()->getType());
663
664  // If this is a postinc, return the value read from memory, otherwise use the
665  // updated value.
666  return isPre ? NextVal : InVal;
667}
668
669
670Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
671  Value *Op = Visit(E->getSubExpr());
672  return Builder.CreateNeg(Op, "neg");
673}
674
675Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
676  Value *Op = Visit(E->getSubExpr());
677  return Builder.CreateNot(Op, "neg");
678}
679
680Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
681  // Compare operand to zero.
682  Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
683
684  // Invert value.
685  // TODO: Could dynamically modify easy computations here.  For example, if
686  // the operand is an icmp ne, turn into icmp eq.
687  BoolVal = Builder.CreateNot(BoolVal, "lnot");
688
689  // ZExt result to int.
690  return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
691}
692
693/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
694/// argument of the sizeof expression as an integer.
695Value *
696ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
697  QualType TypeToSize = E->getTypeOfArgument();
698  if (E->isSizeOf()) {
699    if (const VariableArrayType *VAT =
700          CGF.getContext().getAsVariableArrayType(TypeToSize)) {
701      if (E->isArgumentType()) {
702        // sizeof(type) - make sure to emit the VLA size.
703        CGF.EmitVLASize(TypeToSize);
704      }
705
706      return CGF.GetVLASize(VAT);
707    }
708  }
709
710  // If this isn't sizeof(vla), the result must be constant; use the
711  // constant folding logic so we don't have to duplicate it here.
712  Expr::EvalResult Result;
713  E->Evaluate(Result, CGF.getContext());
714  return llvm::ConstantInt::get(Result.Val.getInt());
715}
716
717Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
718  Expr *Op = E->getSubExpr();
719  if (Op->getType()->isAnyComplexType())
720    return CGF.EmitComplexExpr(Op).first;
721  return Visit(Op);
722}
723Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
724  Expr *Op = E->getSubExpr();
725  if (Op->getType()->isAnyComplexType())
726    return CGF.EmitComplexExpr(Op).second;
727
728  // __imag on a scalar returns zero.  Emit it the subexpr to ensure side
729  // effects are evaluated.
730  CGF.EmitScalarExpr(Op);
731  return llvm::Constant::getNullValue(ConvertType(E->getType()));
732}
733
734Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
735{
736  Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
737  const llvm::Type* ResultType = ConvertType(E->getType());
738  return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
739}
740
741//===----------------------------------------------------------------------===//
742//                           Binary Operators
743//===----------------------------------------------------------------------===//
744
745BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
746  BinOpInfo Result;
747  Result.LHS = Visit(E->getLHS());
748  Result.RHS = Visit(E->getRHS());
749  Result.Ty  = E->getType();
750  Result.E = E;
751  return Result;
752}
753
754Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
755                      Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
756  QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
757
758  BinOpInfo OpInfo;
759
760  // Load the LHS and RHS operands.
761  LValue LHSLV = EmitLValue(E->getLHS());
762  OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
763
764  // Determine the computation type.  If the RHS is complex, then this is one of
765  // the add/sub/mul/div operators.  All of these operators can be computed in
766  // with just their real component even though the computation domain really is
767  // complex.
768  QualType ComputeType = E->getComputationType();
769
770  // If the computation type is complex, then the RHS is complex.  Emit the RHS.
771  if (const ComplexType *CT = ComputeType->getAsComplexType()) {
772    ComputeType = CT->getElementType();
773
774    // Emit the RHS, only keeping the real component.
775    OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
776    RHSTy = RHSTy->getAsComplexType()->getElementType();
777  } else {
778    // Otherwise the RHS is a simple scalar value.
779    OpInfo.RHS = Visit(E->getRHS());
780  }
781
782  QualType LComputeTy, RComputeTy, ResultTy;
783
784  // Compound assignment does not contain enough information about all
785  // the types involved for pointer arithmetic cases. Figure it out
786  // here for now.
787  if (E->getLHS()->getType()->isPointerType()) {
788    // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr,
789    assert((E->getOpcode() == BinaryOperator::AddAssign ||
790            E->getOpcode() == BinaryOperator::SubAssign) &&
791           "Invalid compound assignment operator on pointer type.");
792    LComputeTy = E->getLHS()->getType();
793
794    if (E->getRHS()->getType()->isPointerType()) {
795      // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast
796      // extension, the conversion from the pointer difference back to
797      // the LHS type is handled at the end.
798      assert(E->getOpcode() == BinaryOperator::SubAssign &&
799             "Invalid compound assignment operator on pointer type.");
800      RComputeTy = E->getLHS()->getType();
801      ResultTy = CGF.getContext().getPointerDiffType();
802    } else {
803      RComputeTy = E->getRHS()->getType();
804      ResultTy = LComputeTy;
805    }
806  } else if (E->getRHS()->getType()->isPointerType()) {
807    // Degenerate case of (int += ptr) allowed by GCC implicit cast
808    // extension.
809    assert(E->getOpcode() == BinaryOperator::AddAssign &&
810           "Invalid compound assignment operator on pointer type.");
811    LComputeTy = E->getLHS()->getType();
812    RComputeTy = E->getRHS()->getType();
813    ResultTy = RComputeTy;
814  } else {
815    LComputeTy = RComputeTy = ResultTy = ComputeType;
816  }
817
818  // Convert the LHS/RHS values to the computation type.
819  OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy);
820  OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy);
821  OpInfo.Ty = ResultTy;
822  OpInfo.E = E;
823
824  // Expand the binary operator.
825  Value *Result = (this->*Func)(OpInfo);
826
827  // Convert the result back to the LHS type.
828  Result = EmitScalarConversion(Result, ResultTy, LHSTy);
829
830  // Store the result value into the LHS lvalue. Bit-fields are
831  // handled specially because the result is altered by the store,
832  // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
833  // the left operand after the assignment...'.
834  if (LHSLV.isBitfield())
835    CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
836                                       &Result);
837  else
838    CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
839
840  return Result;
841}
842
843
844Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
845  if (Ops.LHS->getType()->isFPOrFPVector())
846    return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
847  else if (Ops.Ty->isUnsignedIntegerType())
848    return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
849  else
850    return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
851}
852
853Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
854  // Rem in C can't be a floating point type: C99 6.5.5p2.
855  if (Ops.Ty->isUnsignedIntegerType())
856    return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
857  else
858    return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
859}
860
861
862Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
863  if (!Ops.Ty->isPointerType())
864    return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
865
866  // FIXME: What about a pointer to a VLA?
867  Value *Ptr, *Idx;
868  Expr *IdxExp;
869  const PointerType *PT;
870  if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
871    Ptr = Ops.LHS;
872    Idx = Ops.RHS;
873    IdxExp = Ops.E->getRHS();
874  } else {                                           // int + pointer
875    PT = Ops.E->getRHS()->getType()->getAsPointerType();
876    assert(PT && "Invalid add expr");
877    Ptr = Ops.RHS;
878    Idx = Ops.LHS;
879    IdxExp = Ops.E->getLHS();
880  }
881
882  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
883  if (Width < CGF.LLVMPointerWidth) {
884    // Zero or sign extend the pointer value based on whether the index is
885    // signed or not.
886    const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
887    if (IdxExp->getType()->isSignedIntegerType())
888      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
889    else
890      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
891  }
892
893  // Explicitly handle GNU void* and function pointer arithmetic
894  // extensions. The GNU void* casts amount to no-ops since our void*
895  // type is i8*, but this is future proof.
896  const QualType ElementType = PT->getPointeeType();
897  if (ElementType->isVoidType() || ElementType->isFunctionType()) {
898    const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
899    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
900    Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
901    return Builder.CreateBitCast(Res, Ptr->getType());
902  }
903
904  return Builder.CreateGEP(Ptr, Idx, "add.ptr");
905}
906
907Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
908  if (!isa<llvm::PointerType>(Ops.LHS->getType()))
909    return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
910
911  const QualType LHSType = Ops.E->getLHS()->getType();
912  const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
913  if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
914    // pointer - int
915    Value *Idx = Ops.RHS;
916    unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
917    if (Width < CGF.LLVMPointerWidth) {
918      // Zero or sign extend the pointer value based on whether the index is
919      // signed or not.
920      const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
921      if (Ops.E->getRHS()->getType()->isSignedIntegerType())
922        Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
923      else
924        Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
925    }
926    Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
927
928    // FIXME: The pointer could point to a VLA.
929
930    // Explicitly handle GNU void* and function pointer arithmetic
931    // extensions. The GNU void* casts amount to no-ops since our
932    // void* type is i8*, but this is future proof.
933    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
934      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
935      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
936      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
937      return Builder.CreateBitCast(Res, Ops.LHS->getType());
938    }
939
940    return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
941  } else {
942    // pointer - pointer
943    Value *LHS = Ops.LHS;
944    Value *RHS = Ops.RHS;
945
946    uint64_t ElementSize;
947
948    // Handle GCC extension for pointer arithmetic on void* and function pointer
949    // types.
950    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
951      ElementSize = 1;
952    } else {
953      ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
954    }
955
956    const llvm::Type *ResultType = ConvertType(Ops.Ty);
957    LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
958    RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
959    Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
960
961    // Optimize out the shift for element size of 1.
962    if (ElementSize == 1)
963      return BytesBetween;
964
965    // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
966    // remainder.  As such, we handle common power-of-two cases here to generate
967    // better code. See PR2247.
968    if (llvm::isPowerOf2_64(ElementSize)) {
969      Value *ShAmt =
970        llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
971      return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
972    }
973
974    // Otherwise, do a full sdiv.
975    Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
976    return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
977  }
978}
979
980Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
981  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
982  // RHS to the same size as the LHS.
983  Value *RHS = Ops.RHS;
984  if (Ops.LHS->getType() != RHS->getType())
985    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
986
987  return Builder.CreateShl(Ops.LHS, RHS, "shl");
988}
989
990Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
991  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
992  // RHS to the same size as the LHS.
993  Value *RHS = Ops.RHS;
994  if (Ops.LHS->getType() != RHS->getType())
995    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
996
997  if (Ops.Ty->isUnsignedIntegerType())
998    return Builder.CreateLShr(Ops.LHS, RHS, "shr");
999  return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1000}
1001
1002Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1003                                      unsigned SICmpOpc, unsigned FCmpOpc) {
1004  Value *Result;
1005  QualType LHSTy = E->getLHS()->getType();
1006  if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
1007    Value *LHS = Visit(E->getLHS());
1008    Value *RHS = Visit(E->getRHS());
1009
1010    if (LHS->getType()->isFloatingPoint()) {
1011      Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1012                                  LHS, RHS, "cmp");
1013    } else if (LHSTy->isSignedIntegerType()) {
1014      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
1015                                  LHS, RHS, "cmp");
1016    } else {
1017      // Unsigned integers and pointers.
1018      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1019                                  LHS, RHS, "cmp");
1020    }
1021  } else if (LHSTy->isVectorType()) {
1022    Value *LHS = Visit(E->getLHS());
1023    Value *RHS = Visit(E->getRHS());
1024
1025    if (LHS->getType()->isFPOrFPVector()) {
1026      Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1027                                  LHS, RHS, "cmp");
1028    } else if (LHSTy->isUnsignedIntegerType()) {
1029      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
1030                                  LHS, RHS, "cmp");
1031    } else {
1032      // Signed integers and pointers.
1033      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
1034                                  LHS, RHS, "cmp");
1035    }
1036    return Result;
1037  } else {
1038    // Complex Comparison: can only be an equality comparison.
1039    CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1040    CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1041
1042    QualType CETy = LHSTy->getAsComplexType()->getElementType();
1043
1044    Value *ResultR, *ResultI;
1045    if (CETy->isRealFloatingType()) {
1046      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1047                                   LHS.first, RHS.first, "cmp.r");
1048      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1049                                   LHS.second, RHS.second, "cmp.i");
1050    } else {
1051      // Complex comparisons can only be equality comparisons.  As such, signed
1052      // and unsigned opcodes are the same.
1053      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1054                                   LHS.first, RHS.first, "cmp.r");
1055      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1056                                   LHS.second, RHS.second, "cmp.i");
1057    }
1058
1059    if (E->getOpcode() == BinaryOperator::EQ) {
1060      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1061    } else {
1062      assert(E->getOpcode() == BinaryOperator::NE &&
1063             "Complex comparison other than == or != ?");
1064      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1065    }
1066  }
1067
1068  return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
1069}
1070
1071Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1072  LValue LHS = EmitLValue(E->getLHS());
1073  Value *RHS = Visit(E->getRHS());
1074
1075  // Store the value into the LHS.  Bit-fields are handled specially
1076  // because the result is altered by the store, i.e., [C99 6.5.16p1]
1077  // 'An assignment expression has the value of the left operand after
1078  // the assignment...'.
1079  // FIXME: Volatility!
1080  if (LHS.isBitfield())
1081    CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1082                                       &RHS);
1083  else
1084    CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
1085
1086  // Return the RHS.
1087  return RHS;
1088}
1089
1090Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1091  // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1092  // If we have 1 && X, just emit X without inserting the control flow.
1093  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1094    if (Cond == 1) { // If we have 1 && X, just emit X.
1095      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1096      // ZExt result to int.
1097      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1098    }
1099
1100    // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1101    if (!CGF.ContainsLabel(E->getRHS()))
1102      return llvm::Constant::getNullValue(CGF.LLVMIntTy);
1103  }
1104
1105  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1106  llvm::BasicBlock *RHSBlock  = CGF.createBasicBlock("land.rhs");
1107
1108  // Branch on the LHS first.  If it is false, go to the failure (cont) block.
1109  CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1110
1111  // Any edges into the ContBlock are now from an (indeterminate number of)
1112  // edges from this first condition.  All of these values will be false.  Start
1113  // setting up the PHI node in the Cont Block for this.
1114  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1115  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1116  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1117       PI != PE; ++PI)
1118    PN->addIncoming(llvm::ConstantInt::getFalse(), *PI);
1119
1120  CGF.EmitBlock(RHSBlock);
1121  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1122
1123  // Reaquire the RHS block, as there may be subblocks inserted.
1124  RHSBlock = Builder.GetInsertBlock();
1125
1126  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1127  // into the phi node for the edge with the value of RHSCond.
1128  CGF.EmitBlock(ContBlock);
1129  PN->addIncoming(RHSCond, RHSBlock);
1130
1131  // ZExt result to int.
1132  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1133}
1134
1135Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1136  // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1137  // If we have 0 || X, just emit X without inserting the control flow.
1138  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1139    if (Cond == -1) { // If we have 0 || X, just emit X.
1140      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1141      // ZExt result to int.
1142      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1143    }
1144
1145    // 1 || RHS: If it is safe, just elide the RHS, and return 1.
1146    if (!CGF.ContainsLabel(E->getRHS()))
1147      return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
1148  }
1149
1150  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1151  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
1152
1153  // Branch on the LHS first.  If it is true, go to the success (cont) block.
1154  CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1155
1156  // Any edges into the ContBlock are now from an (indeterminate number of)
1157  // edges from this first condition.  All of these values will be true.  Start
1158  // setting up the PHI node in the Cont Block for this.
1159  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1160  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1161  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1162       PI != PE; ++PI)
1163    PN->addIncoming(llvm::ConstantInt::getTrue(), *PI);
1164
1165  // Emit the RHS condition as a bool value.
1166  CGF.EmitBlock(RHSBlock);
1167  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1168
1169  // Reaquire the RHS block, as there may be subblocks inserted.
1170  RHSBlock = Builder.GetInsertBlock();
1171
1172  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1173  // into the phi node for the edge with the value of RHSCond.
1174  CGF.EmitBlock(ContBlock);
1175  PN->addIncoming(RHSCond, RHSBlock);
1176
1177  // ZExt result to int.
1178  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1179}
1180
1181Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1182  CGF.EmitStmt(E->getLHS());
1183  CGF.EnsureInsertPoint();
1184  return Visit(E->getRHS());
1185}
1186
1187//===----------------------------------------------------------------------===//
1188//                             Other Operators
1189//===----------------------------------------------------------------------===//
1190
1191/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1192/// expression is cheap enough and side-effect-free enough to evaluate
1193/// unconditionally instead of conditionally.  This is used to convert control
1194/// flow into selects in some cases.
1195static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1196  if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1197    return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1198
1199  // TODO: Allow anything we can constant fold to an integer or fp constant.
1200  if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1201      isa<FloatingLiteral>(E))
1202    return true;
1203
1204  // Non-volatile automatic variables too, to get "cond ? X : Y" where
1205  // X and Y are local variables.
1206  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1207    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1208      if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1209        return true;
1210
1211  return false;
1212}
1213
1214
1215Value *ScalarExprEmitter::
1216VisitConditionalOperator(const ConditionalOperator *E) {
1217  // If the condition constant folds and can be elided, try to avoid emitting
1218  // the condition and the dead arm.
1219  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
1220    Expr *Live = E->getLHS(), *Dead = E->getRHS();
1221    if (Cond == -1)
1222      std::swap(Live, Dead);
1223
1224    // If the dead side doesn't have labels we need, and if the Live side isn't
1225    // the gnu missing ?: extension (which we could handle, but don't bother
1226    // to), just emit the Live part.
1227    if ((!Dead || !CGF.ContainsLabel(Dead)) &&  // No labels in dead part
1228        Live)                                   // Live part isn't missing.
1229      return Visit(Live);
1230  }
1231
1232
1233  // If this is a really simple expression (like x ? 4 : 5), emit this as a
1234  // select instead of as control flow.  We can only do this if it is cheap and
1235  // safe to evaluate the LHS and RHS unconditionally.
1236  if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1237      isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1238    llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1239    llvm::Value *LHS = Visit(E->getLHS());
1240    llvm::Value *RHS = Visit(E->getRHS());
1241    return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1242  }
1243
1244
1245  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1246  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1247  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1248  Value *CondVal = 0;
1249
1250  // If we don't have the GNU missing condition extension, emit a branch on
1251  // bool the normal way.
1252  if (E->getLHS()) {
1253    // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1254    // the branch on bool.
1255    CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1256  } else {
1257    // Otherwise, for the ?: extension, evaluate the conditional and then
1258    // convert it to bool the hard way.  We do this explicitly because we need
1259    // the unconverted value for the missing middle value of the ?:.
1260    CondVal = CGF.EmitScalarExpr(E->getCond());
1261
1262    // In some cases, EmitScalarConversion will delete the "CondVal" expression
1263    // if there are no extra uses (an optimization).  Inhibit this by making an
1264    // extra dead use, because we're going to add a use of CondVal later.  We
1265    // don't use the builder for this, because we don't want it to get optimized
1266    // away.  This leaves dead code, but the ?: extension isn't common.
1267    new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1268                          Builder.GetInsertBlock());
1269
1270    Value *CondBoolVal =
1271      CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1272                               CGF.getContext().BoolTy);
1273    Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
1274  }
1275
1276  CGF.EmitBlock(LHSBlock);
1277
1278  // Handle the GNU extension for missing LHS.
1279  Value *LHS;
1280  if (E->getLHS())
1281    LHS = Visit(E->getLHS());
1282  else    // Perform promotions, to handle cases like "short ?: int"
1283    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1284
1285  LHSBlock = Builder.GetInsertBlock();
1286  CGF.EmitBranch(ContBlock);
1287
1288  CGF.EmitBlock(RHSBlock);
1289
1290  Value *RHS = Visit(E->getRHS());
1291  RHSBlock = Builder.GetInsertBlock();
1292  CGF.EmitBranch(ContBlock);
1293
1294  CGF.EmitBlock(ContBlock);
1295
1296  if (!LHS || !RHS) {
1297    assert(E->getType()->isVoidType() && "Non-void value should have a value");
1298    return 0;
1299  }
1300
1301  // Create a PHI node for the real part.
1302  llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1303  PN->reserveOperandSpace(2);
1304  PN->addIncoming(LHS, LHSBlock);
1305  PN->addIncoming(RHS, RHSBlock);
1306  return PN;
1307}
1308
1309Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1310  return Visit(E->getChosenSubExpr(CGF.getContext()));
1311}
1312
1313Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1314  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
1315  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1316
1317  // If EmitVAArg fails, we fall back to the LLVM instruction.
1318  if (!ArgPtr)
1319    return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1320
1321  // FIXME: volatile?
1322  return Builder.CreateLoad(ArgPtr);
1323}
1324
1325Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
1326  return CGF.BuildBlockLiteralTmp(BE);
1327}
1328
1329//===----------------------------------------------------------------------===//
1330//                         Entry Point into this File
1331//===----------------------------------------------------------------------===//
1332
1333/// EmitComplexExpr - Emit the computation of the specified expression of
1334/// complex type, ignoring the result.
1335Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1336  assert(E && !hasAggregateLLVMType(E->getType()) &&
1337         "Invalid scalar expression to emit");
1338
1339  return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1340}
1341
1342/// EmitScalarConversion - Emit a conversion from the specified type to the
1343/// specified destination type, both of which are LLVM scalar types.
1344Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1345                                             QualType DstTy) {
1346  assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1347         "Invalid scalar expression to emit");
1348  return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1349}
1350
1351/// EmitComplexToScalarConversion - Emit a conversion from the specified
1352/// complex type to the specified destination type, where the destination
1353/// type is an LLVM scalar type.
1354Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1355                                                      QualType SrcTy,
1356                                                      QualType DstTy) {
1357  assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
1358         "Invalid complex -> scalar conversion");
1359  return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1360                                                                DstTy);
1361}
1362
1363Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1364  assert(V1->getType() == V2->getType() &&
1365         "Vector operands must be of the same type");
1366  unsigned NumElements =
1367    cast<llvm::VectorType>(V1->getType())->getNumElements();
1368
1369  va_list va;
1370  va_start(va, V2);
1371
1372  llvm::SmallVector<llvm::Constant*, 16> Args;
1373  for (unsigned i = 0; i < NumElements; i++) {
1374    int n = va_arg(va, int);
1375    assert(n >= 0 && n < (int)NumElements * 2 &&
1376           "Vector shuffle index out of bounds!");
1377    Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1378  }
1379
1380  const char *Name = va_arg(va, const char *);
1381  va_end(va);
1382
1383  llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1384
1385  return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1386}
1387
1388llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
1389                                         unsigned NumVals, bool isSplat) {
1390  llvm::Value *Vec
1391    = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1392
1393  for (unsigned i = 0, e = NumVals; i != e; ++i) {
1394    llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
1395    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
1396    Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
1397  }
1398
1399  return Vec;
1400}
1401