CGExprScalar.cpp revision ab4eff620a9ce0ea62cdf29529a66c2c98d116f5
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/Module.h"
26#include "llvm/Support/Compiler.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 VISIBILITY_HIDDEN ScalarExprEmitter
48  : public StmtVisitor<ScalarExprEmitter, Value*> {
49  CodeGenFunction &CGF;
50  CGBuilderTy &Builder;
51
52public:
53
54  ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
55    Builder(CGF.Builder) {
56  }
57
58  //===--------------------------------------------------------------------===//
59  //                               Utilities
60  //===--------------------------------------------------------------------===//
61
62  const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
63  LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
64
65  Value *EmitLoadOfLValue(LValue LV, QualType T) {
66    return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
67  }
68
69  /// EmitLoadOfLValue - Given an expression with complex type that represents a
70  /// value l-value, this method emits the address of the l-value, then loads
71  /// and returns the result.
72  Value *EmitLoadOfLValue(const Expr *E) {
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    if (CGF.getContext().getLangOptions().OverflowChecking
266        && Ops.Ty->isSignedIntegerType())
267      return EmitOverflowCheckedBinOp(Ops);
268    return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
269  }
270  /// Create a binary op that checks for overflow.
271  /// Currently only supports +, - and *.
272  Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
273  Value *EmitDiv(const BinOpInfo &Ops);
274  Value *EmitRem(const BinOpInfo &Ops);
275  Value *EmitAdd(const BinOpInfo &Ops);
276  Value *EmitSub(const BinOpInfo &Ops);
277  Value *EmitShl(const BinOpInfo &Ops);
278  Value *EmitShr(const BinOpInfo &Ops);
279  Value *EmitAnd(const BinOpInfo &Ops) {
280    return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
281  }
282  Value *EmitXor(const BinOpInfo &Ops) {
283    return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
284  }
285  Value *EmitOr (const BinOpInfo &Ops) {
286    return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
287  }
288
289  BinOpInfo EmitBinOps(const BinaryOperator *E);
290  Value *EmitCompoundAssign(const CompoundAssignOperator *E,
291                            Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
292
293  // Binary operators and binary compound assignment operators.
294#define HANDLEBINOP(OP) \
295  Value *VisitBin ## OP(const BinaryOperator *E) {                         \
296    return Emit ## OP(EmitBinOps(E));                                      \
297  }                                                                        \
298  Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) {       \
299    return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP);          \
300  }
301  HANDLEBINOP(Mul);
302  HANDLEBINOP(Div);
303  HANDLEBINOP(Rem);
304  HANDLEBINOP(Add);
305  HANDLEBINOP(Sub);
306  HANDLEBINOP(Shl);
307  HANDLEBINOP(Shr);
308  HANDLEBINOP(And);
309  HANDLEBINOP(Xor);
310  HANDLEBINOP(Or);
311#undef HANDLEBINOP
312
313  // Comparisons.
314  Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
315                     unsigned SICmpOpc, unsigned FCmpOpc);
316#define VISITCOMP(CODE, UI, SI, FP) \
317    Value *VisitBin##CODE(const BinaryOperator *E) { \
318      return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
319                         llvm::FCmpInst::FP); }
320  VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
321  VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
322  VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
323  VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
324  VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
325  VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
326#undef VISITCOMP
327
328  Value *VisitBinAssign     (const BinaryOperator *E);
329
330  Value *VisitBinLAnd       (const BinaryOperator *E);
331  Value *VisitBinLOr        (const BinaryOperator *E);
332  Value *VisitBinComma      (const BinaryOperator *E);
333
334  // Other Operators.
335  Value *VisitBlockExpr(const BlockExpr *BE);
336  Value *VisitConditionalOperator(const ConditionalOperator *CO);
337  Value *VisitChooseExpr(ChooseExpr *CE);
338  Value *VisitVAArgExpr(VAArgExpr *VE);
339  Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
340    return CGF.EmitObjCStringLiteral(E);
341  }
342};
343}  // end anonymous namespace.
344
345//===----------------------------------------------------------------------===//
346//                                Utilities
347//===----------------------------------------------------------------------===//
348
349/// EmitConversionToBool - Convert the specified expression value to a
350/// boolean (i1) truth value.  This is equivalent to "Val != 0".
351Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
352  assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
353
354  if (SrcType->isRealFloatingType()) {
355    // Compare against 0.0 for fp scalars.
356    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
357    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
358  }
359
360  assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
361         "Unknown scalar type to convert");
362
363  // Because of the type rules of C, we often end up computing a logical value,
364  // then zero extending it to int, then wanting it as a logical value again.
365  // Optimize this common case.
366  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
367    if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
368      Value *Result = ZI->getOperand(0);
369      // If there aren't any more uses, zap the instruction to save space.
370      // Note that there can be more uses, for example if this
371      // is the result of an assignment.
372      if (ZI->use_empty())
373        ZI->eraseFromParent();
374      return Result;
375    }
376  }
377
378  // Compare against an integer or pointer null.
379  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
380  return Builder.CreateICmpNE(Src, Zero, "tobool");
381}
382
383/// EmitScalarConversion - Emit a conversion from the specified type to the
384/// specified destination type, both of which are LLVM scalar types.
385Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
386                                               QualType DstType) {
387  SrcType = CGF.getContext().getCanonicalType(SrcType);
388  DstType = CGF.getContext().getCanonicalType(DstType);
389  if (SrcType == DstType) return Src;
390
391  if (DstType->isVoidType()) return 0;
392
393  // Handle conversions to bool first, they are special: comparisons against 0.
394  if (DstType->isBooleanType())
395    return EmitConversionToBool(Src, SrcType);
396
397  const llvm::Type *DstTy = ConvertType(DstType);
398
399  // Ignore conversions like int -> uint.
400  if (Src->getType() == DstTy)
401    return Src;
402
403  // Handle pointer conversions next: pointers can only be converted
404  // to/from other pointers and integers. Check for pointer types in
405  // terms of LLVM, as some native types (like Obj-C id) may map to a
406  // pointer type.
407  if (isa<llvm::PointerType>(DstTy)) {
408    // The source value may be an integer, or a pointer.
409    if (isa<llvm::PointerType>(Src->getType()))
410      return Builder.CreateBitCast(Src, DstTy, "conv");
411    assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
412    // First, convert to the correct width so that we control the kind of
413    // extension.
414    const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth);
415    bool InputSigned = SrcType->isSignedIntegerType();
416    llvm::Value* IntResult =
417        Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
418    // Then, cast to pointer.
419    return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
420  }
421
422  if (isa<llvm::PointerType>(Src->getType())) {
423    // Must be an ptr to int cast.
424    assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
425    return Builder.CreatePtrToInt(Src, DstTy, "conv");
426  }
427
428  // A scalar can be splatted to an extended vector of the same element type
429  if (DstType->isExtVectorType() && !isa<VectorType>(SrcType)) {
430    // Cast the scalar to element type
431    QualType EltTy = DstType->getAsExtVectorType()->getElementType();
432    llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
433
434    // Insert the element in element zero of an undef vector
435    llvm::Value *UnV = llvm::UndefValue::get(DstTy);
436    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
437    UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
438
439    // Splat the element across to all elements
440    llvm::SmallVector<llvm::Constant*, 16> Args;
441    unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
442    for (unsigned i = 0; i < NumElements; i++)
443      Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
444
445    llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
446    llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
447    return Yay;
448  }
449
450  // Allow bitcast from vector to integer/fp of the same size.
451  if (isa<llvm::VectorType>(Src->getType()) ||
452      isa<llvm::VectorType>(DstTy))
453    return Builder.CreateBitCast(Src, DstTy, "conv");
454
455  // Finally, we have the arithmetic types: real int/float.
456  if (isa<llvm::IntegerType>(Src->getType())) {
457    bool InputSigned = SrcType->isSignedIntegerType();
458    if (isa<llvm::IntegerType>(DstTy))
459      return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
460    else if (InputSigned)
461      return Builder.CreateSIToFP(Src, DstTy, "conv");
462    else
463      return Builder.CreateUIToFP(Src, DstTy, "conv");
464  }
465
466  assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
467  if (isa<llvm::IntegerType>(DstTy)) {
468    if (DstType->isSignedIntegerType())
469      return Builder.CreateFPToSI(Src, DstTy, "conv");
470    else
471      return Builder.CreateFPToUI(Src, DstTy, "conv");
472  }
473
474  assert(DstTy->isFloatingPoint() && "Unknown real conversion");
475  if (DstTy->getTypeID() < Src->getType()->getTypeID())
476    return Builder.CreateFPTrunc(Src, DstTy, "conv");
477  else
478    return Builder.CreateFPExt(Src, DstTy, "conv");
479}
480
481/// EmitComplexToScalarConversion - Emit a conversion from the specified
482/// complex type to the specified destination type, where the destination
483/// type is an LLVM scalar type.
484Value *ScalarExprEmitter::
485EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
486                              QualType SrcTy, QualType DstTy) {
487  // Get the source element type.
488  SrcTy = SrcTy->getAsComplexType()->getElementType();
489
490  // Handle conversions to bool first, they are special: comparisons against 0.
491  if (DstTy->isBooleanType()) {
492    //  Complex != 0  -> (Real != 0) | (Imag != 0)
493    Src.first  = EmitScalarConversion(Src.first, SrcTy, DstTy);
494    Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
495    return Builder.CreateOr(Src.first, Src.second, "tobool");
496  }
497
498  // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
499  // the imaginary part of the complex value is discarded and the value of the
500  // real part is converted according to the conversion rules for the
501  // corresponding real type.
502  return EmitScalarConversion(Src.first, SrcTy, DstTy);
503}
504
505
506//===----------------------------------------------------------------------===//
507//                            Visitor Methods
508//===----------------------------------------------------------------------===//
509
510Value *ScalarExprEmitter::VisitExpr(Expr *E) {
511  CGF.ErrorUnsupported(E, "scalar expression");
512  if (E->getType()->isVoidType())
513    return 0;
514  return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
515}
516
517Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
518  llvm::SmallVector<llvm::Constant*, 32> indices;
519  for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
520    indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
521  }
522  Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
523  Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
524  Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
525  return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
526}
527
528Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
529  // Emit subscript expressions in rvalue context's.  For most cases, this just
530  // loads the lvalue formed by the subscript expr.  However, we have to be
531  // careful, because the base of a vector subscript is occasionally an rvalue,
532  // so we can't get it as an lvalue.
533  if (!E->getBase()->getType()->isVectorType())
534    return EmitLoadOfLValue(E);
535
536  // Handle the vector case.  The base must be a vector, the index must be an
537  // integer value.
538  Value *Base = Visit(E->getBase());
539  Value *Idx  = Visit(E->getIdx());
540  bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
541  Idx = Builder.CreateIntCast(Idx, llvm::Type::Int32Ty, IdxSigned,
542                              "vecidxcast");
543  return Builder.CreateExtractElement(Base, Idx, "vecext");
544}
545
546/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
547/// also handle things like function to pointer-to-function decay, and array to
548/// pointer decay.
549Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
550  const Expr *Op = E->getSubExpr();
551
552  // If this is due to array->pointer conversion, emit the array expression as
553  // an l-value.
554  if (Op->getType()->isArrayType()) {
555    Value *V = EmitLValue(Op).getAddress();  // Bitfields can't be arrays.
556
557    // Note that VLA pointers are always decayed, so we don't need to do
558    // anything here.
559    if (!Op->getType()->isVariableArrayType()) {
560      assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
561      assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
562                                 ->getElementType()) &&
563             "Expected pointer to array");
564      V = Builder.CreateStructGEP(V, 0, "arraydecay");
565    }
566
567    // The resultant pointer type can be implicitly casted to other pointer
568    // types as well (e.g. void*) and can be implicitly converted to integer.
569    const llvm::Type *DestTy = ConvertType(E->getType());
570    if (V->getType() != DestTy) {
571      if (isa<llvm::PointerType>(DestTy))
572        V = Builder.CreateBitCast(V, DestTy, "ptrconv");
573      else {
574        assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
575        V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
576      }
577    }
578    return V;
579  }
580
581  return EmitCastExpr(Op, E->getType());
582}
583
584
585// VisitCastExpr - Emit code for an explicit or implicit cast.  Implicit casts
586// have to handle a more broad range of conversions than explicit casts, as they
587// handle things like function to ptr-to-function decay etc.
588Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
589  // Handle cases where the source is an non-complex type.
590
591  if (!CGF.hasAggregateLLVMType(E->getType())) {
592    Value *Src = Visit(const_cast<Expr*>(E));
593
594    // Use EmitScalarConversion to perform the conversion.
595    return EmitScalarConversion(Src, E->getType(), DestTy);
596  }
597
598  if (E->getType()->isAnyComplexType()) {
599    // Handle cases where the source is a complex type.
600    return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
601                                         DestTy);
602  }
603
604  // Okay, this is a cast from an aggregate.  It must be a cast to void.  Just
605  // evaluate the result and return.
606  CGF.EmitAggExpr(E, 0, false);
607  return 0;
608}
609
610Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
611  return CGF.EmitCompoundStmt(*E->getSubStmt(),
612                              !E->getType()->isVoidType()).getScalarVal();
613}
614
615Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
616  return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
617}
618
619//===----------------------------------------------------------------------===//
620//                             Unary Operators
621//===----------------------------------------------------------------------===//
622
623Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
624                                             bool isInc, bool isPre) {
625  LValue LV = EmitLValue(E->getSubExpr());
626  QualType ValTy = E->getSubExpr()->getType();
627  Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
628
629  int AmountVal = isInc ? 1 : -1;
630
631  if (ValTy->isPointerType() &&
632      ValTy->getAsPointerType()->isVariableArrayType()) {
633    // The amount of the addition/subtraction needs to account for the VLA size
634    CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
635  }
636
637  Value *NextVal;
638  if (const llvm::PointerType *PT =
639         dyn_cast<llvm::PointerType>(InVal->getType())) {
640    llvm::Constant *Inc =llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
641    if (!isa<llvm::FunctionType>(PT->getElementType())) {
642      NextVal = Builder.CreateGEP(InVal, Inc, "ptrincdec");
643    } else {
644      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
645      NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
646      NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
647      NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
648    }
649  } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) {
650    // Bool++ is an interesting case, due to promotion rules, we get:
651    // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
652    // Bool = ((int)Bool+1) != 0
653    // An interesting aspect of this is that increment is always true.
654    // Decrement does not have this property.
655    NextVal = llvm::ConstantInt::getTrue();
656  } else {
657    // Add the inc/dec to the real part.
658    if (isa<llvm::IntegerType>(InVal->getType()))
659      NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
660    else if (InVal->getType() == llvm::Type::FloatTy)
661      NextVal =
662        llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
663    else if (InVal->getType() == llvm::Type::DoubleTy)
664      NextVal =
665        llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
666    else {
667      llvm::APFloat F(static_cast<float>(AmountVal));
668      bool ignored;
669      F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
670                &ignored);
671      NextVal = llvm::ConstantFP::get(F);
672    }
673    NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
674  }
675
676  // Store the updated result through the lvalue.
677  if (LV.isBitfield())
678    CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
679                                       &NextVal);
680  else
681    CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
682
683  // If this is a postinc, return the value read from memory, otherwise use the
684  // updated value.
685  return isPre ? NextVal : InVal;
686}
687
688
689Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
690  Value *Op = Visit(E->getSubExpr());
691  return Builder.CreateNeg(Op, "neg");
692}
693
694Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
695  Value *Op = Visit(E->getSubExpr());
696  return Builder.CreateNot(Op, "neg");
697}
698
699Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
700  // Compare operand to zero.
701  Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
702
703  // Invert value.
704  // TODO: Could dynamically modify easy computations here.  For example, if
705  // the operand is an icmp ne, turn into icmp eq.
706  BoolVal = Builder.CreateNot(BoolVal, "lnot");
707
708  // ZExt result to int.
709  return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
710}
711
712/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
713/// argument of the sizeof expression as an integer.
714Value *
715ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
716  QualType TypeToSize = E->getTypeOfArgument();
717  if (E->isSizeOf()) {
718    if (const VariableArrayType *VAT =
719          CGF.getContext().getAsVariableArrayType(TypeToSize)) {
720      if (E->isArgumentType()) {
721        // sizeof(type) - make sure to emit the VLA size.
722        CGF.EmitVLASize(TypeToSize);
723      }
724
725      return CGF.GetVLASize(VAT);
726    }
727  }
728
729  // If this isn't sizeof(vla), the result must be constant; use the
730  // constant folding logic so we don't have to duplicate it here.
731  Expr::EvalResult Result;
732  E->Evaluate(Result, CGF.getContext());
733  return llvm::ConstantInt::get(Result.Val.getInt());
734}
735
736Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
737  Expr *Op = E->getSubExpr();
738  if (Op->getType()->isAnyComplexType())
739    return CGF.EmitComplexExpr(Op).first;
740  return Visit(Op);
741}
742Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
743  Expr *Op = E->getSubExpr();
744  if (Op->getType()->isAnyComplexType())
745    return CGF.EmitComplexExpr(Op).second;
746
747  // __imag on a scalar returns zero.  Emit it the subexpr to ensure side
748  // effects are evaluated.
749  CGF.EmitScalarExpr(Op);
750  return llvm::Constant::getNullValue(ConvertType(E->getType()));
751}
752
753Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
754{
755  Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
756  const llvm::Type* ResultType = ConvertType(E->getType());
757  return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
758}
759
760//===----------------------------------------------------------------------===//
761//                           Binary Operators
762//===----------------------------------------------------------------------===//
763
764BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
765  BinOpInfo Result;
766  Result.LHS = Visit(E->getLHS());
767  Result.RHS = Visit(E->getRHS());
768  Result.Ty  = E->getType();
769  Result.E = E;
770  return Result;
771}
772
773Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
774                      Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
775  QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
776
777  BinOpInfo OpInfo;
778
779  if (E->getComputationResultType()->isAnyComplexType()) {
780    // This needs to go through the complex expression emitter, but
781    // it's a tad complicated to do that... I'm leaving it out for now.
782    // (Note that we do actually need the imaginary part of the RHS for
783    // multiplication and division.)
784    CGF.ErrorUnsupported(E, "complex compound assignment");
785    return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
786  }
787
788  // Load/convert the LHS.
789  LValue LHSLV = EmitLValue(E->getLHS());
790  OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
791  OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
792                                    E->getComputationLHSType());
793  // Emit the RHS.
794  OpInfo.RHS = Visit(E->getRHS());
795  OpInfo.Ty = E->getComputationResultType();
796  OpInfo.E = E;
797
798  // Expand the binary operator.
799  Value *Result = (this->*Func)(OpInfo);
800
801  // Convert the result back to the LHS type.
802  Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
803
804  // Store the result value into the LHS lvalue. Bit-fields are
805  // handled specially because the result is altered by the store,
806  // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
807  // the left operand after the assignment...'.
808  if (LHSLV.isBitfield())
809    CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
810                                       &Result);
811  else
812    CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
813
814  return Result;
815}
816
817
818Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
819  if (Ops.LHS->getType()->isFPOrFPVector())
820    return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
821  else if (Ops.Ty->isUnsignedIntegerType())
822    return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
823  else
824    return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
825}
826
827Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
828  // Rem in C can't be a floating point type: C99 6.5.5p2.
829  if (Ops.Ty->isUnsignedIntegerType())
830    return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
831  else
832    return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
833}
834
835Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
836  unsigned IID;
837  unsigned OpID = 0;
838
839  switch (Ops.E->getOpcode()) {
840  case BinaryOperator::Add:
841  case BinaryOperator::AddAssign:
842    OpID = 1;
843    IID = llvm::Intrinsic::sadd_with_overflow;
844    break;
845  case BinaryOperator::Sub:
846  case BinaryOperator::SubAssign:
847    OpID = 2;
848    IID = llvm::Intrinsic::ssub_with_overflow;
849    break;
850  case BinaryOperator::Mul:
851  case BinaryOperator::MulAssign:
852    OpID = 3;
853    IID = llvm::Intrinsic::smul_with_overflow;
854    break;
855  default:
856    assert(false && "Unsupported operation for overflow detection");
857    IID = 0;
858  }
859  OpID <<= 1;
860  OpID |= 1;
861
862  const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
863
864  llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
865
866  Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
867  Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
868  Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
869
870  // Branch in case of overflow.
871  llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
872  llvm::BasicBlock *overflowBB =
873    CGF.createBasicBlock("overflow", CGF.CurFn);
874  llvm::BasicBlock *continueBB =
875    CGF.createBasicBlock("overflow.continue", CGF.CurFn);
876
877  Builder.CreateCondBr(overflow, overflowBB, continueBB);
878
879  // Handle overflow
880
881  Builder.SetInsertPoint(overflowBB);
882
883  // Handler is:
884  // long long *__overflow_handler)(long long a, long long b, char op,
885  // char width)
886  std::vector<const llvm::Type*> handerArgTypes;
887  handerArgTypes.push_back(llvm::Type::Int64Ty);
888  handerArgTypes.push_back(llvm::Type::Int64Ty);
889  handerArgTypes.push_back(llvm::Type::Int8Ty);
890  handerArgTypes.push_back(llvm::Type::Int8Ty);
891  llvm::FunctionType *handlerTy = llvm::FunctionType::get(llvm::Type::Int64Ty,
892      handerArgTypes, false);
893  llvm::Value *handlerFunction =
894    CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
895        llvm::PointerType::getUnqual(handlerTy));
896  handlerFunction = Builder.CreateLoad(handlerFunction);
897
898  llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
899      Builder.CreateSExt(Ops.LHS, llvm::Type::Int64Ty),
900      Builder.CreateSExt(Ops.RHS, llvm::Type::Int64Ty),
901      llvm::ConstantInt::get(llvm::Type::Int8Ty, OpID),
902      llvm::ConstantInt::get(llvm::Type::Int8Ty,
903        cast<llvm::IntegerType>(opTy)->getBitWidth()));
904
905  handlerResult = Builder.CreateTrunc(handlerResult, opTy);
906
907  Builder.CreateBr(continueBB);
908
909  // Set up the continuation
910  Builder.SetInsertPoint(continueBB);
911  // Get the correct result
912  llvm::PHINode *phi = Builder.CreatePHI(opTy);
913  phi->reserveOperandSpace(2);
914  phi->addIncoming(result, initialBB);
915  phi->addIncoming(handlerResult, overflowBB);
916
917  return phi;
918}
919
920Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
921  if (!Ops.Ty->isPointerType()) {
922    if (CGF.getContext().getLangOptions().OverflowChecking
923        && Ops.Ty->isSignedIntegerType())
924      return EmitOverflowCheckedBinOp(Ops);
925    return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
926  }
927
928  if (Ops.Ty->getAsPointerType()->isVariableArrayType()) {
929    // The amount of the addition needs to account for the VLA size
930    CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
931  }
932  Value *Ptr, *Idx;
933  Expr *IdxExp;
934  const PointerType *PT;
935  if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
936    Ptr = Ops.LHS;
937    Idx = Ops.RHS;
938    IdxExp = Ops.E->getRHS();
939  } else {                                           // int + pointer
940    PT = Ops.E->getRHS()->getType()->getAsPointerType();
941    assert(PT && "Invalid add expr");
942    Ptr = Ops.RHS;
943    Idx = Ops.LHS;
944    IdxExp = Ops.E->getLHS();
945  }
946
947  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
948  // Only 32 and 64 are valid index widths. So if a target has shorter
949  // pointe width, extend to 32 at least.
950  unsigned IdxValidWidth
951    = (CGF.LLVMPointerWidth < 32) ? 32 : CGF.LLVMPointerWidth;
952  if (Width < IdxValidWidth) {
953    // Zero or sign extend the pointer value based on whether the index is
954    // signed or not.
955    const llvm::Type *IdxType = llvm::IntegerType::get(IdxValidWidth);
956    if (IdxExp->getType()->isSignedIntegerType())
957      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
958    else
959      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
960  }
961
962  // Explicitly handle GNU void* and function pointer arithmetic
963  // extensions. The GNU void* casts amount to no-ops since our void*
964  // type is i8*, but this is future proof.
965  const QualType ElementType = PT->getPointeeType();
966  if (ElementType->isVoidType() || ElementType->isFunctionType()) {
967    const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
968    Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
969    Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
970    return Builder.CreateBitCast(Res, Ptr->getType());
971  }
972
973  return Builder.CreateGEP(Ptr, Idx, "add.ptr");
974}
975
976Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
977  if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
978    if (CGF.getContext().getLangOptions().OverflowChecking
979        && Ops.Ty->isSignedIntegerType())
980      return EmitOverflowCheckedBinOp(Ops);
981    return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
982  }
983
984  if (Ops.E->getLHS()->getType()->getAsPointerType()->isVariableArrayType()) {
985    // The amount of the addition needs to account for the VLA size for
986    // ptr-int
987    // The amount of the division needs to account for the VLA size for
988    // ptr-ptr.
989    CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
990  }
991
992  const QualType LHSType = Ops.E->getLHS()->getType();
993  const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
994  if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
995    // pointer - int
996    Value *Idx = Ops.RHS;
997    unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
998    unsigned IdxValidWidth
999      = (CGF.LLVMPointerWidth < 32) ? 32 : CGF.LLVMPointerWidth;
1000    if (Width < IdxValidWidth) {
1001      // Zero or sign extend the pointer value based on whether the index is
1002      // signed or not.
1003      const llvm::Type *IdxType = llvm::IntegerType::get(IdxValidWidth);
1004      if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1005        Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1006      else
1007        Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1008    }
1009    Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
1010
1011    // Explicitly handle GNU void* and function pointer arithmetic
1012    // extensions. The GNU void* casts amount to no-ops since our
1013    // void* type is i8*, but this is future proof.
1014    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
1015      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1016      Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1017      Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1018      return Builder.CreateBitCast(Res, Ops.LHS->getType());
1019    }
1020
1021    return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
1022  } else {
1023    // pointer - pointer
1024    Value *LHS = Ops.LHS;
1025    Value *RHS = Ops.RHS;
1026
1027    uint64_t ElementSize;
1028
1029    // Handle GCC extension for pointer arithmetic on void* and function pointer
1030    // types.
1031    if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
1032      ElementSize = 1;
1033    } else {
1034      ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1035    }
1036
1037    const llvm::Type *ResultType = ConvertType(Ops.Ty);
1038    LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1039    RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1040    Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
1041
1042    // Optimize out the shift for element size of 1.
1043    if (ElementSize == 1)
1044      return BytesBetween;
1045
1046    // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
1047    // remainder.  As such, we handle common power-of-two cases here to generate
1048    // better code. See PR2247.
1049    if (llvm::isPowerOf2_64(ElementSize)) {
1050      Value *ShAmt =
1051        llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
1052      return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
1053    }
1054
1055    // Otherwise, do a full sdiv.
1056    Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
1057    return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
1058  }
1059}
1060
1061Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
1062  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1063  // RHS to the same size as the LHS.
1064  Value *RHS = Ops.RHS;
1065  if (Ops.LHS->getType() != RHS->getType())
1066    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1067
1068  return Builder.CreateShl(Ops.LHS, RHS, "shl");
1069}
1070
1071Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1072  // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1073  // RHS to the same size as the LHS.
1074  Value *RHS = Ops.RHS;
1075  if (Ops.LHS->getType() != RHS->getType())
1076    RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1077
1078  if (Ops.Ty->isUnsignedIntegerType())
1079    return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1080  return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1081}
1082
1083Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1084                                      unsigned SICmpOpc, unsigned FCmpOpc) {
1085  Value *Result;
1086  QualType LHSTy = E->getLHS()->getType();
1087  if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
1088    Value *LHS = Visit(E->getLHS());
1089    Value *RHS = Visit(E->getRHS());
1090
1091    if (LHS->getType()->isFloatingPoint()) {
1092      Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1093                                  LHS, RHS, "cmp");
1094    } else if (LHSTy->isSignedIntegerType()) {
1095      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
1096                                  LHS, RHS, "cmp");
1097    } else {
1098      // Unsigned integers and pointers.
1099      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1100                                  LHS, RHS, "cmp");
1101    }
1102  } else if (LHSTy->isVectorType()) {
1103    Value *LHS = Visit(E->getLHS());
1104    Value *RHS = Visit(E->getRHS());
1105
1106    if (LHS->getType()->isFPOrFPVector()) {
1107      Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1108                                  LHS, RHS, "cmp");
1109    } else if (LHSTy->isUnsignedIntegerType()) {
1110      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
1111                                  LHS, RHS, "cmp");
1112    } else {
1113      // Signed integers and pointers.
1114      Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
1115                                  LHS, RHS, "cmp");
1116    }
1117    return Result;
1118  } else {
1119    // Complex Comparison: can only be an equality comparison.
1120    CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1121    CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1122
1123    QualType CETy = LHSTy->getAsComplexType()->getElementType();
1124
1125    Value *ResultR, *ResultI;
1126    if (CETy->isRealFloatingType()) {
1127      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1128                                   LHS.first, RHS.first, "cmp.r");
1129      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1130                                   LHS.second, RHS.second, "cmp.i");
1131    } else {
1132      // Complex comparisons can only be equality comparisons.  As such, signed
1133      // and unsigned opcodes are the same.
1134      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1135                                   LHS.first, RHS.first, "cmp.r");
1136      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1137                                   LHS.second, RHS.second, "cmp.i");
1138    }
1139
1140    if (E->getOpcode() == BinaryOperator::EQ) {
1141      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1142    } else {
1143      assert(E->getOpcode() == BinaryOperator::NE &&
1144             "Complex comparison other than == or != ?");
1145      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1146    }
1147  }
1148
1149  return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
1150}
1151
1152Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1153  LValue LHS = EmitLValue(E->getLHS());
1154  Value *RHS = Visit(E->getRHS());
1155
1156  // Store the value into the LHS.  Bit-fields are handled specially
1157  // because the result is altered by the store, i.e., [C99 6.5.16p1]
1158  // 'An assignment expression has the value of the left operand after
1159  // the assignment...'.
1160  if (LHS.isBitfield())
1161    CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1162                                       &RHS);
1163  else
1164    CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
1165
1166  // Return the RHS.
1167  return RHS;
1168}
1169
1170Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1171  // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1172  // If we have 1 && X, just emit X without inserting the control flow.
1173  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1174    if (Cond == 1) { // If we have 1 && X, just emit X.
1175      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1176      // ZExt result to int.
1177      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1178    }
1179
1180    // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1181    if (!CGF.ContainsLabel(E->getRHS()))
1182      return llvm::Constant::getNullValue(CGF.LLVMIntTy);
1183  }
1184
1185  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1186  llvm::BasicBlock *RHSBlock  = CGF.createBasicBlock("land.rhs");
1187
1188  // Branch on the LHS first.  If it is false, go to the failure (cont) block.
1189  CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1190
1191  // Any edges into the ContBlock are now from an (indeterminate number of)
1192  // edges from this first condition.  All of these values will be false.  Start
1193  // setting up the PHI node in the Cont Block for this.
1194  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1195  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1196  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1197       PI != PE; ++PI)
1198    PN->addIncoming(llvm::ConstantInt::getFalse(), *PI);
1199
1200  CGF.EmitBlock(RHSBlock);
1201  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1202
1203  // Reaquire the RHS block, as there may be subblocks inserted.
1204  RHSBlock = Builder.GetInsertBlock();
1205
1206  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1207  // into the phi node for the edge with the value of RHSCond.
1208  CGF.EmitBlock(ContBlock);
1209  PN->addIncoming(RHSCond, RHSBlock);
1210
1211  // ZExt result to int.
1212  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1213}
1214
1215Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1216  // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1217  // If we have 0 || X, just emit X without inserting the control flow.
1218  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1219    if (Cond == -1) { // If we have 0 || X, just emit X.
1220      Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1221      // ZExt result to int.
1222      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1223    }
1224
1225    // 1 || RHS: If it is safe, just elide the RHS, and return 1.
1226    if (!CGF.ContainsLabel(E->getRHS()))
1227      return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
1228  }
1229
1230  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1231  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
1232
1233  // Branch on the LHS first.  If it is true, go to the success (cont) block.
1234  CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1235
1236  // Any edges into the ContBlock are now from an (indeterminate number of)
1237  // edges from this first condition.  All of these values will be true.  Start
1238  // setting up the PHI node in the Cont Block for this.
1239  llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1240  PN->reserveOperandSpace(2);  // Normal case, two inputs.
1241  for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1242       PI != PE; ++PI)
1243    PN->addIncoming(llvm::ConstantInt::getTrue(), *PI);
1244
1245  // Emit the RHS condition as a bool value.
1246  CGF.EmitBlock(RHSBlock);
1247  Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1248
1249  // Reaquire the RHS block, as there may be subblocks inserted.
1250  RHSBlock = Builder.GetInsertBlock();
1251
1252  // Emit an unconditional branch from this block to ContBlock.  Insert an entry
1253  // into the phi node for the edge with the value of RHSCond.
1254  CGF.EmitBlock(ContBlock);
1255  PN->addIncoming(RHSCond, RHSBlock);
1256
1257  // ZExt result to int.
1258  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1259}
1260
1261Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1262  CGF.EmitStmt(E->getLHS());
1263  CGF.EnsureInsertPoint();
1264  return Visit(E->getRHS());
1265}
1266
1267//===----------------------------------------------------------------------===//
1268//                             Other Operators
1269//===----------------------------------------------------------------------===//
1270
1271/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1272/// expression is cheap enough and side-effect-free enough to evaluate
1273/// unconditionally instead of conditionally.  This is used to convert control
1274/// flow into selects in some cases.
1275static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1276  if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1277    return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1278
1279  // TODO: Allow anything we can constant fold to an integer or fp constant.
1280  if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1281      isa<FloatingLiteral>(E))
1282    return true;
1283
1284  // Non-volatile automatic variables too, to get "cond ? X : Y" where
1285  // X and Y are local variables.
1286  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1287    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1288      if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1289        return true;
1290
1291  return false;
1292}
1293
1294
1295Value *ScalarExprEmitter::
1296VisitConditionalOperator(const ConditionalOperator *E) {
1297  // If the condition constant folds and can be elided, try to avoid emitting
1298  // the condition and the dead arm.
1299  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
1300    Expr *Live = E->getLHS(), *Dead = E->getRHS();
1301    if (Cond == -1)
1302      std::swap(Live, Dead);
1303
1304    // If the dead side doesn't have labels we need, and if the Live side isn't
1305    // the gnu missing ?: extension (which we could handle, but don't bother
1306    // to), just emit the Live part.
1307    if ((!Dead || !CGF.ContainsLabel(Dead)) &&  // No labels in dead part
1308        Live)                                   // Live part isn't missing.
1309      return Visit(Live);
1310  }
1311
1312
1313  // If this is a really simple expression (like x ? 4 : 5), emit this as a
1314  // select instead of as control flow.  We can only do this if it is cheap and
1315  // safe to evaluate the LHS and RHS unconditionally.
1316  if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1317      isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1318    llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1319    llvm::Value *LHS = Visit(E->getLHS());
1320    llvm::Value *RHS = Visit(E->getRHS());
1321    return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1322  }
1323
1324
1325  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1326  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1327  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1328  Value *CondVal = 0;
1329
1330  // If we don't have the GNU missing condition extension, emit a branch on
1331  // bool the normal way.
1332  if (E->getLHS()) {
1333    // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1334    // the branch on bool.
1335    CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1336  } else {
1337    // Otherwise, for the ?: extension, evaluate the conditional and then
1338    // convert it to bool the hard way.  We do this explicitly because we need
1339    // the unconverted value for the missing middle value of the ?:.
1340    CondVal = CGF.EmitScalarExpr(E->getCond());
1341
1342    // In some cases, EmitScalarConversion will delete the "CondVal" expression
1343    // if there are no extra uses (an optimization).  Inhibit this by making an
1344    // extra dead use, because we're going to add a use of CondVal later.  We
1345    // don't use the builder for this, because we don't want it to get optimized
1346    // away.  This leaves dead code, but the ?: extension isn't common.
1347    new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1348                          Builder.GetInsertBlock());
1349
1350    Value *CondBoolVal =
1351      CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1352                               CGF.getContext().BoolTy);
1353    Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
1354  }
1355
1356  CGF.EmitBlock(LHSBlock);
1357
1358  // Handle the GNU extension for missing LHS.
1359  Value *LHS;
1360  if (E->getLHS())
1361    LHS = Visit(E->getLHS());
1362  else    // Perform promotions, to handle cases like "short ?: int"
1363    LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1364
1365  LHSBlock = Builder.GetInsertBlock();
1366  CGF.EmitBranch(ContBlock);
1367
1368  CGF.EmitBlock(RHSBlock);
1369
1370  Value *RHS = Visit(E->getRHS());
1371  RHSBlock = Builder.GetInsertBlock();
1372  CGF.EmitBranch(ContBlock);
1373
1374  CGF.EmitBlock(ContBlock);
1375
1376  if (!LHS || !RHS) {
1377    assert(E->getType()->isVoidType() && "Non-void value should have a value");
1378    return 0;
1379  }
1380
1381  // Create a PHI node for the real part.
1382  llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1383  PN->reserveOperandSpace(2);
1384  PN->addIncoming(LHS, LHSBlock);
1385  PN->addIncoming(RHS, RHSBlock);
1386  return PN;
1387}
1388
1389Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1390  return Visit(E->getChosenSubExpr(CGF.getContext()));
1391}
1392
1393Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1394  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
1395  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1396
1397  // If EmitVAArg fails, we fall back to the LLVM instruction.
1398  if (!ArgPtr)
1399    return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1400
1401  return Builder.CreateLoad(ArgPtr);
1402}
1403
1404Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
1405  return CGF.BuildBlockLiteralTmp(BE);
1406}
1407
1408//===----------------------------------------------------------------------===//
1409//                         Entry Point into this File
1410//===----------------------------------------------------------------------===//
1411
1412/// EmitComplexExpr - Emit the computation of the specified expression of
1413/// complex type, ignoring the result.
1414Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1415  assert(E && !hasAggregateLLVMType(E->getType()) &&
1416         "Invalid scalar expression to emit");
1417
1418  return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1419}
1420
1421/// EmitScalarConversion - Emit a conversion from the specified type to the
1422/// specified destination type, both of which are LLVM scalar types.
1423Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1424                                             QualType DstTy) {
1425  assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1426         "Invalid scalar expression to emit");
1427  return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1428}
1429
1430/// EmitComplexToScalarConversion - Emit a conversion from the specified
1431/// complex type to the specified destination type, where the destination
1432/// type is an LLVM scalar type.
1433Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1434                                                      QualType SrcTy,
1435                                                      QualType DstTy) {
1436  assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
1437         "Invalid complex -> scalar conversion");
1438  return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1439                                                                DstTy);
1440}
1441
1442Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1443  assert(V1->getType() == V2->getType() &&
1444         "Vector operands must be of the same type");
1445  unsigned NumElements =
1446    cast<llvm::VectorType>(V1->getType())->getNumElements();
1447
1448  va_list va;
1449  va_start(va, V2);
1450
1451  llvm::SmallVector<llvm::Constant*, 16> Args;
1452  for (unsigned i = 0; i < NumElements; i++) {
1453    int n = va_arg(va, int);
1454    assert(n >= 0 && n < (int)NumElements * 2 &&
1455           "Vector shuffle index out of bounds!");
1456    Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1457  }
1458
1459  const char *Name = va_arg(va, const char *);
1460  va_end(va);
1461
1462  llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1463
1464  return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1465}
1466
1467llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
1468                                         unsigned NumVals, bool isSplat) {
1469  llvm::Value *Vec
1470    = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1471
1472  for (unsigned i = 0, e = NumVals; i != e; ++i) {
1473    llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
1474    llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
1475    Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
1476  }
1477
1478  return Vec;
1479}
1480