blob: 37579c6da18c50e3d38c2cdb8b66db6ff3a5ddfa [file] [log] [blame]
Chris Lattner7f02f722007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner7f02f722007-08-24 05:35:26 +00007//
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/AST.h"
Chris Lattner25ddea72008-04-20 00:50:39 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner7f02f722007-08-24 05:35:26 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/GlobalVariable.h"
Anders Carlsson7c50aca2007-10-15 20:28:48 +000021#include "llvm/Intrinsics.h"
Chris Lattner7f02f722007-08-24 05:35:26 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerc89bf692008-01-03 07:05:49 +000023#include <cstdarg>
Ted Kremenek6aad91a2007-12-10 23:44:32 +000024
Chris Lattner7f02f722007-08-24 05:35:26 +000025using namespace clang;
26using namespace CodeGen;
27using llvm::Value;
28
29//===----------------------------------------------------------------------===//
30// Scalar Expression Emitter
31//===----------------------------------------------------------------------===//
32
33struct BinOpInfo {
34 Value *LHS;
35 Value *RHS;
Chris Lattner1f1ded92007-08-24 21:00:35 +000036 QualType Ty; // Computation Type.
Chris Lattner7f02f722007-08-24 05:35:26 +000037 const BinaryOperator *E;
38};
39
40namespace {
41class VISIBILITY_HIDDEN ScalarExprEmitter
42 : public StmtVisitor<ScalarExprEmitter, Value*> {
43 CodeGenFunction &CGF;
Chris Lattner50b36742008-04-13 07:32:11 +000044 llvm::IRBuilder &Builder;
Chris Lattner2b94fe32008-03-01 08:45:05 +000045 CGObjCRuntime *Runtime;
46
Chris Lattner7f02f722007-08-24 05:35:26 +000047public:
48
Chris Lattner2b94fe32008-03-01 08:45:05 +000049 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
50 Builder(CGF.Builder),
51 Runtime(CGF.CGM.getObjCRuntime()) {
Chris Lattner7f02f722007-08-24 05:35:26 +000052 }
Chris Lattner7f02f722007-08-24 05:35:26 +000053
54 //===--------------------------------------------------------------------===//
55 // Utilities
56 //===--------------------------------------------------------------------===//
57
58 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
59 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
60
61 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattner9b655512007-08-31 22:49:20 +000062 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +000063 }
64
65 /// EmitLoadOfLValue - Given an expression with complex type that represents a
66 /// value l-value, this method emits the address of the l-value, then loads
67 /// and returns the result.
68 Value *EmitLoadOfLValue(const Expr *E) {
69 // FIXME: Volatile
70 return EmitLoadOfLValue(EmitLValue(E), E->getType());
71 }
72
Chris Lattner9abc84e2007-08-26 16:42:57 +000073 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +000074 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +000075 Value *EmitConversionToBool(Value *Src, QualType DstTy);
76
Chris Lattner3707b252007-08-26 06:48:56 +000077 /// EmitScalarConversion - Emit a conversion from the specified type to the
78 /// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +000079 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
80
81 /// EmitComplexToScalarConversion - Emit a conversion from the specified
82 /// complex type to the specified destination type, where the destination
83 /// type is an LLVM scalar type.
84 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
85 QualType SrcTy, QualType DstTy);
Chris Lattner3707b252007-08-26 06:48:56 +000086
Chris Lattner7f02f722007-08-24 05:35:26 +000087 //===--------------------------------------------------------------------===//
88 // Visitor Methods
89 //===--------------------------------------------------------------------===//
90
91 Value *VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000092 S->dump(CGF.getContext().getSourceManager());
Chris Lattner7f02f722007-08-24 05:35:26 +000093 assert(0 && "Stmt can't have complex result type!");
94 return 0;
95 }
96 Value *VisitExpr(Expr *S);
97 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
98
99 // Leaves.
100 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
101 return llvm::ConstantInt::get(E->getValue());
102 }
103 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner59138ba2008-04-20 00:45:53 +0000104 return llvm::ConstantFP::get(E->getValue());
Chris Lattner7f02f722007-08-24 05:35:26 +0000105 }
106 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
107 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
108 }
Nate Begemane7579b52007-11-15 05:40:03 +0000109 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
110 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000112 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
113 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroffec0550f2007-10-15 20:41:53 +0000114 CGF.getContext().typesAreCompatible(
115 E->getArgType1(), E->getArgType2()));
Chris Lattner7f02f722007-08-24 05:35:26 +0000116 }
117 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
118 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
119 }
120
121 // l-values.
122 Value *VisitDeclRefExpr(DeclRefExpr *E) {
123 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
124 return llvm::ConstantInt::get(EC->getInitVal());
125 return EmitLoadOfLValue(E);
126 }
Chris Lattner2b94fe32008-03-01 08:45:05 +0000127 Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerce5605e2008-03-30 23:25:33 +0000128 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Chris Lattner7f02f722007-08-24 05:35:26 +0000129 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand38617c2008-05-14 19:38:39 +0000130 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000131 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begeman213541a2008-04-18 23:10:10 +0000132 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Eli Friedman06e863f2008-05-13 23:18:27 +0000133 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
Chris Lattner7f02f722007-08-24 05:35:26 +0000134 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
135 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel35634f52007-10-24 17:18:43 +0000136
137 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000138 unsigned NumInitElements = E->getNumInits();
139
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000140 const llvm::VectorType *VType =
Anders Carlssonf6884ac2008-01-29 01:15:48 +0000141 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
142
143 // We have a scalar in braces. Just use the first element.
144 if (!VType)
145 return Visit(E->getInit(0));
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000146
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000147 unsigned NumVectorElements = VType->getNumElements();
148 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000149
150 // Emit individual vector element stores.
151 llvm::Value *V = llvm::UndefValue::get(VType);
152
Anders Carlsson222d2c82007-12-18 02:45:33 +0000153 // Emit initializers
154 unsigned i;
155 for (i = 0; i < NumInitElements; ++i) {
Devang Patela83cc332007-10-24 18:05:48 +0000156 Value *NewV = Visit(E->getInit(i));
157 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
158 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel35634f52007-10-24 17:18:43 +0000159 }
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000160
161 // Emit remaining default initializers
162 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
163 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
164 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
165 V = Builder.CreateInsertElement(V, NewV, Idx);
166 }
167
Devang Patela83cc332007-10-24 18:05:48 +0000168 return V;
Devang Patel35634f52007-10-24 17:18:43 +0000169 }
Chris Lattner04421082008-04-08 04:40:51 +0000170
Chris Lattner7f02f722007-08-24 05:35:26 +0000171 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
172 Value *VisitCastExpr(const CastExpr *E) {
173 return EmitCastExpr(E->getSubExpr(), E->getType());
174 }
175 Value *EmitCastExpr(const Expr *E, QualType T);
176
177 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000178 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000179 }
180
Chris Lattner33793202007-08-31 22:09:40 +0000181 Value *VisitStmtExpr(const StmtExpr *E);
182
Chris Lattner7f02f722007-08-24 05:35:26 +0000183 // Unary Operators.
184 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
185 Value *VisitUnaryPostDec(const UnaryOperator *E) {
186 return VisitPrePostIncDec(E, false, false);
187 }
188 Value *VisitUnaryPostInc(const UnaryOperator *E) {
189 return VisitPrePostIncDec(E, true, false);
190 }
191 Value *VisitUnaryPreDec(const UnaryOperator *E) {
192 return VisitPrePostIncDec(E, false, true);
193 }
194 Value *VisitUnaryPreInc(const UnaryOperator *E) {
195 return VisitPrePostIncDec(E, true, true);
196 }
197 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
198 return EmitLValue(E->getSubExpr()).getAddress();
199 }
200 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
201 Value *VisitUnaryPlus(const UnaryOperator *E) {
202 return Visit(E->getSubExpr());
203 }
204 Value *VisitUnaryMinus (const UnaryOperator *E);
205 Value *VisitUnaryNot (const UnaryOperator *E);
206 Value *VisitUnaryLNot (const UnaryOperator *E);
207 Value *VisitUnarySizeOf (const UnaryOperator *E) {
208 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
209 }
210 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
211 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
212 }
213 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000214 bool isSizeOf);
Chris Lattner46f93d02007-08-24 21:20:17 +0000215 Value *VisitUnaryReal (const UnaryOperator *E);
216 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000217 Value *VisitUnaryExtension(const UnaryOperator *E) {
218 return Visit(E->getSubExpr());
219 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000220 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner04421082008-04-08 04:40:51 +0000221 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
222 return Visit(DAE->getExpr());
223 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000224
Chris Lattner7f02f722007-08-24 05:35:26 +0000225 // Binary Operators.
Chris Lattner7f02f722007-08-24 05:35:26 +0000226 Value *EmitMul(const BinOpInfo &Ops) {
227 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
228 }
229 Value *EmitDiv(const BinOpInfo &Ops);
230 Value *EmitRem(const BinOpInfo &Ops);
231 Value *EmitAdd(const BinOpInfo &Ops);
232 Value *EmitSub(const BinOpInfo &Ops);
233 Value *EmitShl(const BinOpInfo &Ops);
234 Value *EmitShr(const BinOpInfo &Ops);
235 Value *EmitAnd(const BinOpInfo &Ops) {
236 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
237 }
238 Value *EmitXor(const BinOpInfo &Ops) {
239 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
240 }
241 Value *EmitOr (const BinOpInfo &Ops) {
242 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
243 }
244
Chris Lattner1f1ded92007-08-24 21:00:35 +0000245 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000246 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000247 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
248
249 // Binary operators and binary compound assignment operators.
250#define HANDLEBINOP(OP) \
Chris Lattner3ccf7742007-08-26 21:41:21 +0000251 Value *VisitBin ## OP(const BinaryOperator *E) { \
252 return Emit ## OP(EmitBinOps(E)); \
253 } \
254 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
255 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner1f1ded92007-08-24 21:00:35 +0000256 }
257 HANDLEBINOP(Mul);
258 HANDLEBINOP(Div);
259 HANDLEBINOP(Rem);
260 HANDLEBINOP(Add);
261 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
262 HANDLEBINOP(Shl);
263 HANDLEBINOP(Shr);
264 HANDLEBINOP(And);
265 HANDLEBINOP(Xor);
266 HANDLEBINOP(Or);
267#undef HANDLEBINOP
268 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000269 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000270 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
271 }
272
Chris Lattner7f02f722007-08-24 05:35:26 +0000273 // Comparisons.
274 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
275 unsigned SICmpOpc, unsigned FCmpOpc);
276#define VISITCOMP(CODE, UI, SI, FP) \
277 Value *VisitBin##CODE(const BinaryOperator *E) { \
278 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
279 llvm::FCmpInst::FP); }
280 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
281 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
282 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
283 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
284 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
285 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
286#undef VISITCOMP
287
288 Value *VisitBinAssign (const BinaryOperator *E);
289
290 Value *VisitBinLAnd (const BinaryOperator *E);
291 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000292 Value *VisitBinComma (const BinaryOperator *E);
293
294 // Other Operators.
295 Value *VisitConditionalOperator(const ConditionalOperator *CO);
296 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begemane2ce1d92008-01-17 17:46:27 +0000297 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000298 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner7f02f722007-08-24 05:35:26 +0000299 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
300 return CGF.EmitObjCStringLiteral(E);
301 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000302 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000303};
304} // end anonymous namespace.
305
306//===----------------------------------------------------------------------===//
307// Utilities
308//===----------------------------------------------------------------------===//
309
Chris Lattner9abc84e2007-08-26 16:42:57 +0000310/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +0000311/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +0000312Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
313 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
314
315 if (SrcType->isRealFloatingType()) {
316 // Compare against 0.0 for fp scalars.
317 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattner9abc84e2007-08-26 16:42:57 +0000318 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
319 }
320
321 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
322 "Unknown scalar type to convert");
323
324 // Because of the type rules of C, we often end up computing a logical value,
325 // then zero extending it to int, then wanting it as a logical value again.
326 // Optimize this common case.
327 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
328 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
329 Value *Result = ZI->getOperand(0);
Eli Friedman356916e2008-01-29 18:13:51 +0000330 // If there aren't any more uses, zap the instruction to save space.
331 // Note that there can be more uses, for example if this
332 // is the result of an assignment.
333 if (ZI->use_empty())
334 ZI->eraseFromParent();
Chris Lattner9abc84e2007-08-26 16:42:57 +0000335 return Result;
336 }
337 }
338
339 // Compare against an integer or pointer null.
340 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
341 return Builder.CreateICmpNE(Src, Zero, "tobool");
342}
343
Chris Lattner3707b252007-08-26 06:48:56 +0000344/// EmitScalarConversion - Emit a conversion from the specified type to the
345/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000346Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
347 QualType DstType) {
Chris Lattner3707b252007-08-26 06:48:56 +0000348 SrcType = SrcType.getCanonicalType();
349 DstType = DstType.getCanonicalType();
350 if (SrcType == DstType) return Src;
Chris Lattnercf289082007-08-26 07:21:11 +0000351
352 if (DstType->isVoidType()) return 0;
Chris Lattner3707b252007-08-26 06:48:56 +0000353
354 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnered70f0a2007-08-26 16:52:28 +0000355 if (DstType->isBooleanType())
356 return EmitConversionToBool(Src, SrcType);
Chris Lattner3707b252007-08-26 06:48:56 +0000357
358 const llvm::Type *DstTy = ConvertType(DstType);
359
360 // Ignore conversions like int -> uint.
361 if (Src->getType() == DstTy)
362 return Src;
363
364 // Handle pointer conversions next: pointers can only be converted to/from
365 // other pointers and integers.
366 if (isa<PointerType>(DstType)) {
367 // The source value may be an integer, or a pointer.
368 if (isa<llvm::PointerType>(Src->getType()))
369 return Builder.CreateBitCast(Src, DstTy, "conv");
370 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
371 return Builder.CreateIntToPtr(Src, DstTy, "conv");
372 }
373
374 if (isa<PointerType>(SrcType)) {
375 // Must be an ptr to int cast.
376 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson50b5a302007-10-31 23:18:02 +0000377 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000378 }
379
Nate Begeman213541a2008-04-18 23:10:10 +0000380 // A scalar can be splatted to an extended vector of the same element type
381 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner3b1ae002008-02-02 04:51:41 +0000382 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begeman4119d1a2007-12-30 02:59:45 +0000383 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
384 true);
Nate Begeman4119d1a2007-12-30 02:59:45 +0000385
Chris Lattner3b1ae002008-02-02 04:51:41 +0000386 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000387 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner3b1ae002008-02-02 04:51:41 +0000388 isa<llvm::VectorType>(DstTy))
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000389 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000390
Chris Lattner3707b252007-08-26 06:48:56 +0000391 // Finally, we have the arithmetic types: real int/float.
392 if (isa<llvm::IntegerType>(Src->getType())) {
393 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000394 if (isa<llvm::IntegerType>(DstTy))
395 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
396 else if (InputSigned)
397 return Builder.CreateSIToFP(Src, DstTy, "conv");
398 else
399 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000400 }
401
402 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
403 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000404 if (DstType->isSignedIntegerType())
405 return Builder.CreateFPToSI(Src, DstTy, "conv");
406 else
407 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000408 }
409
410 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000411 if (DstTy->getTypeID() < Src->getType()->getTypeID())
412 return Builder.CreateFPTrunc(Src, DstTy, "conv");
413 else
414 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000415}
416
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000417/// EmitComplexToScalarConversion - Emit a conversion from the specified
418/// complex type to the specified destination type, where the destination
419/// type is an LLVM scalar type.
420Value *ScalarExprEmitter::
421EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
422 QualType SrcTy, QualType DstTy) {
Chris Lattnered70f0a2007-08-26 16:52:28 +0000423 // Get the source element type.
424 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
425
426 // Handle conversions to bool first, they are special: comparisons against 0.
427 if (DstTy->isBooleanType()) {
428 // Complex != 0 -> (Real != 0) | (Imag != 0)
429 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
430 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
431 return Builder.CreateOr(Src.first, Src.second, "tobool");
432 }
433
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000434 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
435 // the imaginary part of the complex value is discarded and the value of the
436 // real part is converted according to the conversion rules for the
437 // corresponding real type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000438 return EmitScalarConversion(Src.first, SrcTy, DstTy);
439}
440
441
Chris Lattner7f02f722007-08-24 05:35:26 +0000442//===----------------------------------------------------------------------===//
443// Visitor Methods
444//===----------------------------------------------------------------------===//
445
446Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000447 CGF.WarnUnsupported(E, "scalar expression");
Chris Lattner7f02f722007-08-24 05:35:26 +0000448 if (E->getType()->isVoidType())
449 return 0;
450 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
451}
452
Eli Friedmand38617c2008-05-14 19:38:39 +0000453Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
454 llvm::SmallVector<llvm::Constant*, 32> indices;
455 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
456 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
457 }
458 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
459 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
460 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
461 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
462}
463
Chris Lattner2b94fe32008-03-01 08:45:05 +0000464Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
465 // Only the lookup mechanism and first two arguments of the method
466 // implementation vary between runtimes. We can get the receiver and
467 // arguments in generic code.
468
469 // Find the receiver
Chris Lattnerce5605e2008-03-30 23:25:33 +0000470 llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
Chris Lattner2b94fe32008-03-01 08:45:05 +0000471
472 // Process the arguments
Chris Lattnerce5605e2008-03-30 23:25:33 +0000473 unsigned ArgC = E->getNumArgs();
Chris Lattner2b94fe32008-03-01 08:45:05 +0000474 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattnerce5605e2008-03-30 23:25:33 +0000475 for (unsigned i = 0; i != ArgC; ++i) {
Chris Lattner2b94fe32008-03-01 08:45:05 +0000476 Expr *ArgExpr = E->getArg(i);
477 QualType ArgTy = ArgExpr->getType();
478 if (!CGF.hasAggregateLLVMType(ArgTy)) {
479 // Scalar argument is passed by-value.
480 Args.push_back(CGF.EmitScalarExpr(ArgExpr));
Chris Lattner9b2dc282008-04-04 16:54:41 +0000481 } else if (ArgTy->isAnyComplexType()) {
Chris Lattner2b94fe32008-03-01 08:45:05 +0000482 // Make a temporary alloca to pass the argument.
483 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
484 CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
485 Args.push_back(DestMem);
486 } else {
487 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
488 CGF.EmitAggExpr(ArgExpr, DestMem, false);
489 Args.push_back(DestMem);
490 }
491 }
492
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000493 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000494 CGF.LoadObjCSelf(),
Chris Lattner9384c762008-06-26 04:42:20 +0000495 Receiver, E->getSelector(),
Chris Lattnerce5605e2008-03-30 23:25:33 +0000496 &Args[0], Args.size());
Chris Lattner2b94fe32008-03-01 08:45:05 +0000497}
498
Chris Lattner7f02f722007-08-24 05:35:26 +0000499Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
500 // Emit subscript expressions in rvalue context's. For most cases, this just
501 // loads the lvalue formed by the subscript expr. However, we have to be
502 // careful, because the base of a vector subscript is occasionally an rvalue,
503 // so we can't get it as an lvalue.
504 if (!E->getBase()->getType()->isVectorType())
505 return EmitLoadOfLValue(E);
506
507 // Handle the vector case. The base must be a vector, the index must be an
508 // integer value.
509 Value *Base = Visit(E->getBase());
510 Value *Idx = Visit(E->getIdx());
511
512 // FIXME: Convert Idx to i32 type.
513 return Builder.CreateExtractElement(Base, Idx, "vecext");
514}
515
516/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
517/// also handle things like function to pointer-to-function decay, and array to
518/// pointer decay.
519Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
520 const Expr *Op = E->getSubExpr();
521
522 // If this is due to array->pointer conversion, emit the array expression as
523 // an l-value.
524 if (Op->getType()->isArrayType()) {
525 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
526 // will not true when we add support for VLAs.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000527 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner7f02f722007-08-24 05:35:26 +0000528
529 assert(isa<llvm::PointerType>(V->getType()) &&
530 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
531 ->getElementType()) &&
532 "Doesn't support VLAs yet!");
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000533 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnera9e63722007-12-12 04:13:20 +0000534
535 // The resultant pointer type can be implicitly casted to other pointer
Chris Lattnerf31627f2008-07-23 06:31:27 +0000536 // types as well (e.g. void*) and can be implicitly converted to integer.
537 const llvm::Type *DestTy = ConvertType(E->getType());
538 if (V->getType() != DestTy) {
539 if (isa<llvm::PointerType>(DestTy))
540 V = Builder.CreateBitCast(V, DestTy, "ptrconv");
541 else {
542 assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
543 V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
544 }
545 }
Chris Lattnera9e63722007-12-12 04:13:20 +0000546 return V;
547
Anders Carlsson793680e2007-10-12 23:56:29 +0000548 } else if (E->getType()->isReferenceType()) {
Anders Carlsson23af9f22007-10-13 05:52:34 +0000549 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000550 getPointeeType() ==
Anders Carlsson23af9f22007-10-13 05:52:34 +0000551 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlsson793680e2007-10-12 23:56:29 +0000552
553 return EmitLValue(Op).getAddress();
Chris Lattner7f02f722007-08-24 05:35:26 +0000554 }
555
556 return EmitCastExpr(Op, E->getType());
557}
558
559
560// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
561// have to handle a more broad range of conversions than explicit casts, as they
562// handle things like function to ptr-to-function decay etc.
563Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner58a2e942007-08-26 07:26:12 +0000564 // Handle cases where the source is an non-complex type.
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000565
566 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner3707b252007-08-26 06:48:56 +0000567 Value *Src = Visit(const_cast<Expr*>(E));
568
Chris Lattner3707b252007-08-26 06:48:56 +0000569 // Use EmitScalarConversion to perform the conversion.
570 return EmitScalarConversion(Src, E->getType(), DestTy);
571 }
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000572
Chris Lattner9b2dc282008-04-04 16:54:41 +0000573 if (E->getType()->isAnyComplexType()) {
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000574 // Handle cases where the source is a complex type.
575 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
576 DestTy);
577 }
Chris Lattner10b00cf2007-08-26 07:16:41 +0000578
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000579 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
580 // evaluate the result and return.
581 CGF.EmitAggExpr(E, 0, false);
582 return 0;
Chris Lattner7f02f722007-08-24 05:35:26 +0000583}
584
Chris Lattner33793202007-08-31 22:09:40 +0000585Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000586 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattner33793202007-08-31 22:09:40 +0000587}
588
589
Chris Lattner7f02f722007-08-24 05:35:26 +0000590//===----------------------------------------------------------------------===//
591// Unary Operators
592//===----------------------------------------------------------------------===//
593
594Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdfce2a52007-08-24 16:24:49 +0000595 bool isInc, bool isPre) {
Chris Lattner7f02f722007-08-24 05:35:26 +0000596 LValue LV = EmitLValue(E->getSubExpr());
597 // FIXME: Handle volatile!
Chris Lattnere936cc82007-08-26 05:10:16 +0000598 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattner9b655512007-08-31 22:49:20 +0000599 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000600
601 int AmountVal = isInc ? 1 : -1;
602
603 Value *NextVal;
Chris Lattnere936cc82007-08-26 05:10:16 +0000604 if (isa<llvm::PointerType>(InVal->getType())) {
605 // FIXME: This isn't right for VLAs.
606 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000607 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattnere936cc82007-08-26 05:10:16 +0000608 } else {
609 // Add the inc/dec to the real part.
610 if (isa<llvm::IntegerType>(InVal->getType()))
611 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000612 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patele9b8c0a2007-10-30 20:59:40 +0000613 NextVal =
Chris Lattner59138ba2008-04-20 00:45:53 +0000614 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattner25ddea72008-04-20 00:50:39 +0000615 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patele9b8c0a2007-10-30 20:59:40 +0000616 NextVal =
Chris Lattner59138ba2008-04-20 00:45:53 +0000617 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattner25ddea72008-04-20 00:50:39 +0000618 else {
619 llvm::APFloat F(static_cast<float>(AmountVal));
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000620 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
Chris Lattner25ddea72008-04-20 00:50:39 +0000621 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000622 }
Chris Lattnere936cc82007-08-26 05:10:16 +0000623 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
624 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000625
626 // Store the updated result through the lvalue.
627 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
628 E->getSubExpr()->getType());
629
630 // If this is a postinc, return the value read from memory, otherwise use the
631 // updated value.
632 return isPre ? NextVal : InVal;
633}
634
635
636Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
637 Value *Op = Visit(E->getSubExpr());
638 return Builder.CreateNeg(Op, "neg");
639}
640
641Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
642 Value *Op = Visit(E->getSubExpr());
643 return Builder.CreateNot(Op, "neg");
644}
645
646Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
647 // Compare operand to zero.
648 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
649
650 // Invert value.
651 // TODO: Could dynamically modify easy computations here. For example, if
652 // the operand is an icmp ne, turn into icmp eq.
653 BoolVal = Builder.CreateNot(BoolVal, "lnot");
654
655 // ZExt result to int.
656 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
657}
658
659/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
660/// an integer (RetType).
661Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner46f93d02007-08-24 21:20:17 +0000662 QualType RetType,bool isSizeOf){
Chris Lattnera269ebf2008-02-21 05:45:29 +0000663 assert(RetType->isIntegerType() && "Result type must be an integer!");
664 uint32_t ResultWidth =
Chris Lattner98be4942008-03-05 18:54:05 +0000665 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattnera269ebf2008-02-21 05:45:29 +0000666
Daniel Dunbar91408452008-07-22 01:35:47 +0000667 // sizeof(void) and __alignof__(void) = 1 as a gcc extension. Also
668 // for function types.
Daniel Dunbar8ee6a632008-07-22 19:44:18 +0000669 // FIXME: what is alignof a function type in gcc?
Daniel Dunbar91408452008-07-22 01:35:47 +0000670 if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
Chris Lattnera269ebf2008-02-21 05:45:29 +0000671 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
672
Chris Lattner7f02f722007-08-24 05:35:26 +0000673 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner98be4942008-03-05 18:54:05 +0000674 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner7f02f722007-08-24 05:35:26 +0000675
676 uint64_t Val = isSizeOf ? Info.first : Info.second;
677 Val /= 8; // Return size in bytes, not bits.
678
Chris Lattner7f02f722007-08-24 05:35:26 +0000679 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
680}
681
Chris Lattner46f93d02007-08-24 21:20:17 +0000682Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
683 Expr *Op = E->getSubExpr();
Chris Lattner9b2dc282008-04-04 16:54:41 +0000684 if (Op->getType()->isAnyComplexType())
Chris Lattner46f93d02007-08-24 21:20:17 +0000685 return CGF.EmitComplexExpr(Op).first;
686 return Visit(Op);
687}
688Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
689 Expr *Op = E->getSubExpr();
Chris Lattner9b2dc282008-04-04 16:54:41 +0000690 if (Op->getType()->isAnyComplexType())
Chris Lattner46f93d02007-08-24 21:20:17 +0000691 return CGF.EmitComplexExpr(Op).second;
Chris Lattner36f84062007-08-26 05:29:21 +0000692
693 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
694 // effects are evaluated.
695 CGF.EmitScalarExpr(Op);
696 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner46f93d02007-08-24 21:20:17 +0000697}
698
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000699Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
700{
701 int64_t Val = E->evaluateOffsetOf(CGF.getContext());
702
703 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
704
Chris Lattner98be4942008-03-05 18:54:05 +0000705 uint32_t ResultWidth =
706 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000707 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
708}
Chris Lattner46f93d02007-08-24 21:20:17 +0000709
Chris Lattner7f02f722007-08-24 05:35:26 +0000710//===----------------------------------------------------------------------===//
711// Binary Operators
712//===----------------------------------------------------------------------===//
713
714BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
715 BinOpInfo Result;
716 Result.LHS = Visit(E->getLHS());
717 Result.RHS = Visit(E->getRHS());
Chris Lattner1f1ded92007-08-24 21:00:35 +0000718 Result.Ty = E->getType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000719 Result.E = E;
720 return Result;
721}
722
Chris Lattner3ccf7742007-08-26 21:41:21 +0000723Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000724 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
725 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
726
727 BinOpInfo OpInfo;
728
729 // Load the LHS and RHS operands.
730 LValue LHSLV = EmitLValue(E->getLHS());
731 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner04dc7642007-08-26 22:37:40 +0000732
733 // Determine the computation type. If the RHS is complex, then this is one of
734 // the add/sub/mul/div operators. All of these operators can be computed in
735 // with just their real component even though the computation domain really is
736 // complex.
Chris Lattner3ccf7742007-08-26 21:41:21 +0000737 QualType ComputeType = E->getComputationType();
Chris Lattner1f1ded92007-08-24 21:00:35 +0000738
Chris Lattner04dc7642007-08-26 22:37:40 +0000739 // If the computation type is complex, then the RHS is complex. Emit the RHS.
740 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
741 ComputeType = CT->getElementType();
742
743 // Emit the RHS, only keeping the real component.
744 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
745 RHSTy = RHSTy->getAsComplexType()->getElementType();
746 } else {
747 // Otherwise the RHS is a simple scalar value.
748 OpInfo.RHS = Visit(E->getRHS());
749 }
750
751 // Convert the LHS/RHS values to the computation type.
Chris Lattnere9377122007-08-26 07:08:39 +0000752 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000753
Devang Patelf86206f2007-10-25 22:19:13 +0000754 // Do not merge types for -= or += where the LHS is a pointer.
755 if (!(E->getOpcode() == BinaryOperator::SubAssign ||
Devang Patel03f7c032007-10-30 18:31:12 +0000756 E->getOpcode() == BinaryOperator::AddAssign) ||
Chris Lattner3b44b572007-08-25 21:56:20 +0000757 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnere9377122007-08-26 07:08:39 +0000758 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000759 }
760 OpInfo.Ty = ComputeType;
761 OpInfo.E = E;
762
763 // Expand the binary operator.
764 Value *Result = (this->*Func)(OpInfo);
765
766 // Truncate the result back to the LHS type.
Chris Lattnere9377122007-08-26 07:08:39 +0000767 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000768
769 // Store the result value into the LHS lvalue.
770 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
771
Eli Friedman18491282008-05-25 14:13:57 +0000772 // For bitfields, we need the value in the bitfield
773 // FIXME: This adds an extra bitfield load
774 if (LHSLV.isBitfield())
775 Result = EmitLoadOfLValue(LHSLV, LHSTy);
776
Chris Lattner1f1ded92007-08-24 21:00:35 +0000777 return Result;
778}
779
780
Chris Lattner7f02f722007-08-24 05:35:26 +0000781Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanb3ab8dc2007-12-30 01:28:16 +0000782 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner7f02f722007-08-24 05:35:26 +0000783 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000784 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000785 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
786 else
787 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
788}
789
790Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
791 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner1f1ded92007-08-24 21:00:35 +0000792 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000793 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
794 else
795 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
796}
797
798
799Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000800 if (!Ops.Ty->isPointerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000801 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000802
803 // FIXME: What about a pointer to a VLA?
Chris Lattner8f925282008-01-03 06:36:51 +0000804 Value *Ptr, *Idx;
805 Expr *IdxExp;
806 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
807 Ptr = Ops.LHS;
808 Idx = Ops.RHS;
809 IdxExp = Ops.E->getRHS();
810 } else { // int + pointer
811 Ptr = Ops.RHS;
812 Idx = Ops.LHS;
813 IdxExp = Ops.E->getLHS();
814 }
815
816 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
817 if (Width < CGF.LLVMPointerWidth) {
818 // Zero or sign extend the pointer value based on whether the index is
819 // signed or not.
820 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
821 if (IdxExp->getType().getCanonicalType()->isSignedIntegerType())
822 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
823 else
824 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
825 }
826
827 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner7f02f722007-08-24 05:35:26 +0000828}
829
830Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
831 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
832 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
833
Chris Lattner1f1ded92007-08-24 21:00:35 +0000834 // pointer - int
835 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
836 "ptr-ptr shouldn't get here");
837 // FIXME: The pointer could point to a VLA.
Chris Lattner6860f3c2008-01-31 04:12:50 +0000838 Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
839
840 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
841 if (Width < CGF.LLVMPointerWidth) {
842 // Zero or sign extend the pointer value based on whether the index is
843 // signed or not.
844 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
845 if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType())
846 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
847 else
848 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
849 }
850
851 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000852}
853
854Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
855 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
856 // the compound assignment case it is invalid, so just handle it here.
857 if (!E->getRHS()->getType()->isPointerType())
858 return EmitSub(EmitBinOps(E));
Chris Lattner7f02f722007-08-24 05:35:26 +0000859
860 // pointer - pointer
Chris Lattner1f1ded92007-08-24 21:00:35 +0000861 Value *LHS = Visit(E->getLHS());
862 Value *RHS = Visit(E->getRHS());
863
Seo Sanghyeonec86b972007-12-03 06:23:43 +0000864 const QualType LHSType = E->getLHS()->getType().getCanonicalType();
Seo Sanghyeon9bb947a2007-12-26 05:21:37 +0000865 const QualType LHSElementType = cast<PointerType>(LHSType)->getPointeeType();
Chris Lattner98be4942008-03-05 18:54:05 +0000866 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
Chris Lattner1f1ded92007-08-24 21:00:35 +0000867
868 const llvm::Type *ResultType = ConvertType(E->getType());
869 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
870 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
871 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner7f02f722007-08-24 05:35:26 +0000872
873 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
874 // remainder. As such, we handle common power-of-two cases here to generate
875 // better code.
876 if (llvm::isPowerOf2_64(ElementSize)) {
877 Value *ShAmt =
878 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
879 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
880 }
Chris Lattner1f1ded92007-08-24 21:00:35 +0000881
Chris Lattner7f02f722007-08-24 05:35:26 +0000882 // Otherwise, do a full sdiv.
883 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
884 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
885}
886
Chris Lattner1f1ded92007-08-24 21:00:35 +0000887
Chris Lattner7f02f722007-08-24 05:35:26 +0000888Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
889 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
890 // RHS to the same size as the LHS.
891 Value *RHS = Ops.RHS;
892 if (Ops.LHS->getType() != RHS->getType())
893 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
894
895 return Builder.CreateShl(Ops.LHS, RHS, "shl");
896}
897
898Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
899 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
900 // RHS to the same size as the LHS.
901 Value *RHS = Ops.RHS;
902 if (Ops.LHS->getType() != RHS->getType())
903 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
904
Chris Lattner1f1ded92007-08-24 21:00:35 +0000905 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000906 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
907 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
908}
909
910Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
911 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000912 Value *Result;
Chris Lattner7f02f722007-08-24 05:35:26 +0000913 QualType LHSTy = E->getLHS()->getType();
Nate Begeman7a66d7b2008-07-25 20:16:05 +0000914 if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
Chris Lattner7f02f722007-08-24 05:35:26 +0000915 Value *LHS = Visit(E->getLHS());
916 Value *RHS = Visit(E->getRHS());
917
918 if (LHS->getType()->isFloatingPoint()) {
Nate Begeman7a66d7b2008-07-25 20:16:05 +0000919 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner7f02f722007-08-24 05:35:26 +0000920 LHS, RHS, "cmp");
Eli Friedmanec2c1262008-05-29 15:09:15 +0000921 } else if (LHSTy->isSignedIntegerType()) {
922 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner7f02f722007-08-24 05:35:26 +0000923 LHS, RHS, "cmp");
924 } else {
Eli Friedmanec2c1262008-05-29 15:09:15 +0000925 // Unsigned integers and pointers.
926 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner7f02f722007-08-24 05:35:26 +0000927 LHS, RHS, "cmp");
928 }
Nate Begeman7a66d7b2008-07-25 20:16:05 +0000929 } else if (LHSTy->isVectorType()) {
930 Value *LHS = Visit(E->getLHS());
931 Value *RHS = Visit(E->getRHS());
932
933 if (LHS->getType()->isFPOrFPVector()) {
934 Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
935 LHS, RHS, "cmp");
936 } else if (LHSTy->isUnsignedIntegerType()) {
937 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
938 LHS, RHS, "cmp");
939 } else {
940 // Signed integers and pointers.
941 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
942 LHS, RHS, "cmp");
943 }
944 return Result;
Chris Lattner7f02f722007-08-24 05:35:26 +0000945 } else {
946 // Complex Comparison: can only be an equality comparison.
947 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
948 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
949
950 QualType CETy =
951 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
952
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000953 Value *ResultR, *ResultI;
Chris Lattner7f02f722007-08-24 05:35:26 +0000954 if (CETy->isRealFloatingType()) {
955 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
956 LHS.first, RHS.first, "cmp.r");
957 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
958 LHS.second, RHS.second, "cmp.i");
959 } else {
960 // Complex comparisons can only be equality comparisons. As such, signed
961 // and unsigned opcodes are the same.
962 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
963 LHS.first, RHS.first, "cmp.r");
964 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
965 LHS.second, RHS.second, "cmp.i");
966 }
967
968 if (E->getOpcode() == BinaryOperator::EQ) {
969 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
970 } else {
971 assert(E->getOpcode() == BinaryOperator::NE &&
972 "Complex comparison other than == or != ?");
973 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
974 }
975 }
976
977 // ZExt result to int.
978 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
979}
980
981Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
982 LValue LHS = EmitLValue(E->getLHS());
983 Value *RHS = Visit(E->getRHS());
984
985 // Store the value into the LHS.
986 // FIXME: Volatility!
987 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Eli Friedman18491282008-05-25 14:13:57 +0000988
989 // For bitfields, we need the value in the bitfield
990 // FIXME: This adds an extra bitfield load
991 if (LHS.isBitfield())
992 return EmitLoadOfLValue(LHS, E->getLHS()->getType());
Chris Lattner7f02f722007-08-24 05:35:26 +0000993 // Return the RHS.
994 return RHS;
995}
996
997Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
998 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
999
Gabor Greif984d0b42008-04-06 20:42:52 +00001000 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
1001 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner7f02f722007-08-24 05:35:26 +00001002
1003 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1004 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
1005
1006 CGF.EmitBlock(RHSBlock);
1007 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1008
1009 // Reaquire the RHS block, as there may be subblocks inserted.
1010 RHSBlock = Builder.GetInsertBlock();
1011 CGF.EmitBlock(ContBlock);
1012
1013 // Create a PHI node. If we just evaluted the LHS condition, the result is
1014 // false. If we evaluated both, the result is the RHS condition.
1015 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
1016 PN->reserveOperandSpace(2);
1017 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
1018 PN->addIncoming(RHSCond, RHSBlock);
1019
1020 // ZExt result to int.
1021 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1022}
1023
1024Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1025 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1026
Gabor Greif984d0b42008-04-06 20:42:52 +00001027 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
1028 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner7f02f722007-08-24 05:35:26 +00001029
1030 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1031 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
1032
1033 CGF.EmitBlock(RHSBlock);
1034 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1035
1036 // Reaquire the RHS block, as there may be subblocks inserted.
1037 RHSBlock = Builder.GetInsertBlock();
1038 CGF.EmitBlock(ContBlock);
1039
1040 // Create a PHI node. If we just evaluted the LHS condition, the result is
1041 // true. If we evaluated both, the result is the RHS condition.
1042 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1043 PN->reserveOperandSpace(2);
1044 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1045 PN->addIncoming(RHSCond, RHSBlock);
1046
1047 // ZExt result to int.
1048 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1049}
1050
1051Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1052 CGF.EmitStmt(E->getLHS());
1053 return Visit(E->getRHS());
1054}
1055
1056//===----------------------------------------------------------------------===//
1057// Other Operators
1058//===----------------------------------------------------------------------===//
1059
1060Value *ScalarExprEmitter::
1061VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +00001062 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1063 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1064 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner7f02f722007-08-24 05:35:26 +00001065
Chris Lattnera21ddb32007-11-26 01:40:58 +00001066 // Evaluate the conditional, then convert it to bool. We do this explicitly
1067 // because we need the unconverted value if this is a GNU ?: expression with
1068 // missing middle value.
1069 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc89bf692008-01-03 07:05:49 +00001070 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1071 CGF.getContext().BoolTy);
Chris Lattnera21ddb32007-11-26 01:40:58 +00001072 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner7f02f722007-08-24 05:35:26 +00001073
1074 CGF.EmitBlock(LHSBlock);
1075
1076 // Handle the GNU extension for missing LHS.
Chris Lattnera21ddb32007-11-26 01:40:58 +00001077 Value *LHS;
1078 if (E->getLHS())
Eli Friedman856226c2008-05-16 20:38:39 +00001079 LHS = Visit(E->getLHS());
Chris Lattnera21ddb32007-11-26 01:40:58 +00001080 else // Perform promotions, to handle cases like "short ?: int"
1081 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1082
Chris Lattner7f02f722007-08-24 05:35:26 +00001083 Builder.CreateBr(ContBlock);
1084 LHSBlock = Builder.GetInsertBlock();
1085
1086 CGF.EmitBlock(RHSBlock);
1087
Eli Friedman856226c2008-05-16 20:38:39 +00001088 Value *RHS = Visit(E->getRHS());
Chris Lattner7f02f722007-08-24 05:35:26 +00001089 Builder.CreateBr(ContBlock);
1090 RHSBlock = Builder.GetInsertBlock();
1091
1092 CGF.EmitBlock(ContBlock);
1093
Nuno Lopes108f55d2008-06-04 19:15:45 +00001094 if (!LHS || !RHS) {
Chris Lattner2202bce2007-11-30 17:56:23 +00001095 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1096 return 0;
1097 }
1098
Chris Lattner7f02f722007-08-24 05:35:26 +00001099 // Create a PHI node for the real part.
1100 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1101 PN->reserveOperandSpace(2);
1102 PN->addIncoming(LHS, LHSBlock);
1103 PN->addIncoming(RHS, RHSBlock);
1104 return PN;
1105}
1106
1107Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner7f02f722007-08-24 05:35:26 +00001108 // Emit the LHS or RHS as appropriate.
Devang Patele9b8c0a2007-10-30 20:59:40 +00001109 return
1110 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner7f02f722007-08-24 05:35:26 +00001111}
1112
Nate Begemane2ce1d92008-01-17 17:46:27 +00001113Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begeman67295d02008-01-30 20:50:20 +00001114 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek55499762008-06-17 02:43:46 +00001115 E->arg_end(CGF.getContext())).getScalarVal();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001116}
1117
Chris Lattner2202bce2007-11-30 17:56:23 +00001118Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001119 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1120
1121 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1122 return V;
1123}
1124
Chris Lattner2202bce2007-11-30 17:56:23 +00001125Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001126 std::string str;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001127 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1128 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1129 EncodingRecordTypes);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001130
1131 llvm::Constant *C = llvm::ConstantArray::get(str);
1132 C = new llvm::GlobalVariable(C->getType(), true,
1133 llvm::GlobalValue::InternalLinkage,
1134 C, ".str", &CGF.CGM.getModule());
1135 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1136 llvm::Constant *Zeros[] = { Zero, Zero };
1137 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1138
1139 return C;
1140}
1141
Chris Lattner7f02f722007-08-24 05:35:26 +00001142//===----------------------------------------------------------------------===//
1143// Entry Point into this File
1144//===----------------------------------------------------------------------===//
1145
1146/// EmitComplexExpr - Emit the computation of the specified expression of
1147/// complex type, ignoring the result.
1148Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1149 assert(E && !hasAggregateLLVMType(E->getType()) &&
1150 "Invalid scalar expression to emit");
1151
1152 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1153}
Chris Lattner3707b252007-08-26 06:48:56 +00001154
1155/// EmitScalarConversion - Emit a conversion from the specified type to the
1156/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001157Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1158 QualType DstTy) {
Chris Lattner3707b252007-08-26 06:48:56 +00001159 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1160 "Invalid scalar expression to emit");
1161 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1162}
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001163
1164/// EmitComplexToScalarConversion - Emit a conversion from the specified
1165/// complex type to the specified destination type, where the destination
1166/// type is an LLVM scalar type.
1167Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1168 QualType SrcTy,
1169 QualType DstTy) {
Chris Lattner9b2dc282008-04-04 16:54:41 +00001170 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001171 "Invalid complex -> scalar conversion");
1172 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1173 DstTy);
1174}
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001175
1176Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1177 assert(V1->getType() == V2->getType() &&
1178 "Vector operands must be of the same type");
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001179 unsigned NumElements =
1180 cast<llvm::VectorType>(V1->getType())->getNumElements();
1181
1182 va_list va;
1183 va_start(va, V2);
1184
1185 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001186 for (unsigned i = 0; i < NumElements; i++) {
1187 int n = va_arg(va, int);
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001188 assert(n >= 0 && n < (int)NumElements * 2 &&
1189 "Vector shuffle index out of bounds!");
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001190 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1191 }
1192
1193 const char *Name = va_arg(va, const char *);
1194 va_end(va);
1195
1196 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1197
1198 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1199}
1200
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001201llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattner345f7202008-07-26 20:15:14 +00001202 unsigned NumVals, bool isSplat) {
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001203 llvm::Value *Vec
Chris Lattner345f7202008-07-26 20:15:14 +00001204 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001205
Chris Lattner345f7202008-07-26 20:15:14 +00001206 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001207 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001208 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001209 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001210 }
1211
1212 return Vec;
1213}