blob: 63b8efc5dcbcca334492fb2a47296c7070489d88 [file] [log] [blame]
Chris Lattner9fba49a2007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner9fba49a2007-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"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattnerd54d1f22008-04-20 00:50:39 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000022#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000023#include "llvm/Support/Compiler.h"
Chris Lattnerc2126682008-01-03 07:05:49 +000024#include <cstdarg>
Ted Kremenek03cf4df2007-12-10 23:44:32 +000025
Chris Lattner9fba49a2007-08-24 05:35:26 +000026using namespace clang;
27using namespace CodeGen;
28using llvm::Value;
29
30//===----------------------------------------------------------------------===//
31// Scalar Expression Emitter
32//===----------------------------------------------------------------------===//
33
34struct BinOpInfo {
35 Value *LHS;
36 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000037 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000038 const BinaryOperator *E;
39};
40
41namespace {
42class VISIBILITY_HIDDEN ScalarExprEmitter
43 : public StmtVisitor<ScalarExprEmitter, Value*> {
44 CodeGenFunction &CGF;
Chris Lattnerfaf23db2008-08-08 19:57:58 +000045 llvm::IRBuilder<> &Builder;
Chris Lattnercbfb5512008-03-01 08:45:05 +000046 CGObjCRuntime *Runtime;
47
Chris Lattner9fba49a2007-08-24 05:35:26 +000048public:
49
Chris Lattnercbfb5512008-03-01 08:45:05 +000050 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
51 Builder(CGF.Builder),
52 Runtime(CGF.CGM.getObjCRuntime()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000053 }
Chris Lattner9fba49a2007-08-24 05:35:26 +000054
55 //===--------------------------------------------------------------------===//
56 // Utilities
57 //===--------------------------------------------------------------------===//
58
59 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
60 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
61
62 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000063 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000064 }
65
66 /// EmitLoadOfLValue - Given an expression with complex type that represents a
67 /// value l-value, this method emits the address of the l-value, then loads
68 /// and returns the result.
69 Value *EmitLoadOfLValue(const Expr *E) {
70 // FIXME: Volatile
71 return EmitLoadOfLValue(EmitLValue(E), E->getType());
72 }
73
Chris Lattnerd8d44222007-08-26 16:42:57 +000074 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000075 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000076 Value *EmitConversionToBool(Value *Src, QualType DstTy);
77
Chris Lattner4e05d1e2007-08-26 06:48:56 +000078 /// EmitScalarConversion - Emit a conversion from the specified type to the
79 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000080 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
81
82 /// EmitComplexToScalarConversion - Emit a conversion from the specified
83 /// complex type to the specified destination type, where the destination
84 /// type is an LLVM scalar type.
85 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
86 QualType SrcTy, QualType DstTy);
Chris Lattner4e05d1e2007-08-26 06:48:56 +000087
Chris Lattner9fba49a2007-08-24 05:35:26 +000088 //===--------------------------------------------------------------------===//
89 // Visitor Methods
90 //===--------------------------------------------------------------------===//
91
92 Value *VisitStmt(Stmt *S) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +000093 S->dump(CGF.getContext().getSourceManager());
Chris Lattner9fba49a2007-08-24 05:35:26 +000094 assert(0 && "Stmt can't have complex result type!");
95 return 0;
96 }
97 Value *VisitExpr(Expr *S);
98 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
99
100 // Leaves.
101 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
102 return llvm::ConstantInt::get(E->getValue());
103 }
104 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner70c38672008-04-20 00:45:53 +0000105 return llvm::ConstantFP::get(E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000106 }
107 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
108 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
109 }
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000110 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
111 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
112 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000113 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
114 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000115 CGF.getContext().typesAreCompatible(
116 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000117 }
118 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
119 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
120 }
Daniel Dunbar879788d2008-08-04 16:51:22 +0000121 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
122 Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty,
123 CGF.GetIDForAddrOfLabel(E->getLabel()));
124 return Builder.CreateIntToPtr(V,
125 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
126 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000127
128 // l-values.
129 Value *VisitDeclRefExpr(DeclRefExpr *E) {
130 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
131 return llvm::ConstantInt::get(EC->getInitVal());
132 return EmitLoadOfLValue(E);
133 }
Chris Lattnercbfb5512008-03-01 08:45:05 +0000134 Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000135 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Chris Lattner9fba49a2007-08-24 05:35:26 +0000136 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand0e9d092008-05-14 19:38:39 +0000137 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000138 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000139 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000140 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000141 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
Chris Lattner69909292008-08-10 01:53:14 +0000142 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000143
144 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000145 unsigned NumInitElements = E->getNumInits();
146
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000147 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000148 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
149
150 // We have a scalar in braces. Just use the first element.
151 if (!VType)
152 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000153
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000154 unsigned NumVectorElements = VType->getNumElements();
155 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000156
157 // Emit individual vector element stores.
158 llvm::Value *V = llvm::UndefValue::get(VType);
159
Anders Carlsson323d5682007-12-18 02:45:33 +0000160 // Emit initializers
161 unsigned i;
162 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000163 Value *NewV = Visit(E->getInit(i));
164 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
165 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000166 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000167
168 // Emit remaining default initializers
169 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
170 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
171 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
172 V = Builder.CreateInsertElement(V, NewV, Idx);
173 }
174
Devang Patel32c39832007-10-24 18:05:48 +0000175 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000176 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000177
Chris Lattner9fba49a2007-08-24 05:35:26 +0000178 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
179 Value *VisitCastExpr(const CastExpr *E) {
180 return EmitCastExpr(E->getSubExpr(), E->getType());
181 }
182 Value *EmitCastExpr(const Expr *E, QualType T);
183
184 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000185 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000186 }
187
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000188 Value *VisitStmtExpr(const StmtExpr *E);
189
Chris Lattner9fba49a2007-08-24 05:35:26 +0000190 // Unary Operators.
191 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
192 Value *VisitUnaryPostDec(const UnaryOperator *E) {
193 return VisitPrePostIncDec(E, false, false);
194 }
195 Value *VisitUnaryPostInc(const UnaryOperator *E) {
196 return VisitPrePostIncDec(E, true, false);
197 }
198 Value *VisitUnaryPreDec(const UnaryOperator *E) {
199 return VisitPrePostIncDec(E, false, true);
200 }
201 Value *VisitUnaryPreInc(const UnaryOperator *E) {
202 return VisitPrePostIncDec(E, true, true);
203 }
204 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
205 return EmitLValue(E->getSubExpr()).getAddress();
206 }
207 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
208 Value *VisitUnaryPlus(const UnaryOperator *E) {
209 return Visit(E->getSubExpr());
210 }
211 Value *VisitUnaryMinus (const UnaryOperator *E);
212 Value *VisitUnaryNot (const UnaryOperator *E);
213 Value *VisitUnaryLNot (const UnaryOperator *E);
214 Value *VisitUnarySizeOf (const UnaryOperator *E) {
215 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
216 }
217 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
218 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
219 }
220 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnercfac88d2008-04-02 17:35:06 +0000221 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000222 Value *VisitUnaryReal (const UnaryOperator *E);
223 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000224 Value *VisitUnaryExtension(const UnaryOperator *E) {
225 return Visit(E->getSubExpr());
226 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000227 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000228 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
229 return Visit(DAE->getExpr());
230 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000231
Chris Lattner9fba49a2007-08-24 05:35:26 +0000232 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000233 Value *EmitMul(const BinOpInfo &Ops) {
234 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
235 }
236 Value *EmitDiv(const BinOpInfo &Ops);
237 Value *EmitRem(const BinOpInfo &Ops);
238 Value *EmitAdd(const BinOpInfo &Ops);
239 Value *EmitSub(const BinOpInfo &Ops);
240 Value *EmitShl(const BinOpInfo &Ops);
241 Value *EmitShr(const BinOpInfo &Ops);
242 Value *EmitAnd(const BinOpInfo &Ops) {
243 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
244 }
245 Value *EmitXor(const BinOpInfo &Ops) {
246 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
247 }
248 Value *EmitOr (const BinOpInfo &Ops) {
249 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
250 }
251
Chris Lattner660e31d2007-08-24 21:00:35 +0000252 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000253 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000254 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
255
256 // Binary operators and binary compound assignment operators.
257#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000258 Value *VisitBin ## OP(const BinaryOperator *E) { \
259 return Emit ## OP(EmitBinOps(E)); \
260 } \
261 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
262 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000263 }
264 HANDLEBINOP(Mul);
265 HANDLEBINOP(Div);
266 HANDLEBINOP(Rem);
267 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000268 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000269 HANDLEBINOP(Shl);
270 HANDLEBINOP(Shr);
271 HANDLEBINOP(And);
272 HANDLEBINOP(Xor);
273 HANDLEBINOP(Or);
274#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000275
Chris Lattner9fba49a2007-08-24 05:35:26 +0000276 // Comparisons.
277 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
278 unsigned SICmpOpc, unsigned FCmpOpc);
279#define VISITCOMP(CODE, UI, SI, FP) \
280 Value *VisitBin##CODE(const BinaryOperator *E) { \
281 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
282 llvm::FCmpInst::FP); }
283 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
284 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
285 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
286 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
287 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
288 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
289#undef VISITCOMP
290
291 Value *VisitBinAssign (const BinaryOperator *E);
292
293 Value *VisitBinLAnd (const BinaryOperator *E);
294 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000295 Value *VisitBinComma (const BinaryOperator *E);
296
297 // Other Operators.
298 Value *VisitConditionalOperator(const ConditionalOperator *CO);
299 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000300 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson36760332007-10-15 20:28:48 +0000301 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000302 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
303 return CGF.EmitObjCStringLiteral(E);
304 }
Anders Carlsson36f07d82007-10-29 05:01:08 +0000305 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000306};
307} // end anonymous namespace.
308
309//===----------------------------------------------------------------------===//
310// Utilities
311//===----------------------------------------------------------------------===//
312
Chris Lattnerd8d44222007-08-26 16:42:57 +0000313/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000314/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000315Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
316 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
317
318 if (SrcType->isRealFloatingType()) {
319 // Compare against 0.0 for fp scalars.
320 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000321 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
322 }
323
324 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
325 "Unknown scalar type to convert");
326
327 // Because of the type rules of C, we often end up computing a logical value,
328 // then zero extending it to int, then wanting it as a logical value again.
329 // Optimize this common case.
330 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
331 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
332 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000333 // If there aren't any more uses, zap the instruction to save space.
334 // Note that there can be more uses, for example if this
335 // is the result of an assignment.
336 if (ZI->use_empty())
337 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000338 return Result;
339 }
340 }
341
342 // Compare against an integer or pointer null.
343 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
344 return Builder.CreateICmpNE(Src, Zero, "tobool");
345}
346
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000347/// EmitScalarConversion - Emit a conversion from the specified type to the
348/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000349Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
350 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000351 SrcType = CGF.getContext().getCanonicalType(SrcType);
352 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000353 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000354
355 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000356
357 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000358 if (DstType->isBooleanType())
359 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000360
361 const llvm::Type *DstTy = ConvertType(DstType);
362
363 // Ignore conversions like int -> uint.
364 if (Src->getType() == DstTy)
365 return Src;
366
367 // Handle pointer conversions next: pointers can only be converted to/from
368 // other pointers and integers.
369 if (isa<PointerType>(DstType)) {
370 // The source value may be an integer, or a pointer.
371 if (isa<llvm::PointerType>(Src->getType()))
372 return Builder.CreateBitCast(Src, DstTy, "conv");
373 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
374 return Builder.CreateIntToPtr(Src, DstTy, "conv");
375 }
376
377 if (isa<PointerType>(SrcType)) {
378 // Must be an ptr to int cast.
379 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000380 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000381 }
382
Nate Begemanaf6ed502008-04-18 23:10:10 +0000383 // A scalar can be splatted to an extended vector of the same element type
384 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner4f025a42008-02-02 04:51:41 +0000385 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begemanec2d1062007-12-30 02:59:45 +0000386 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
387 true);
Nate Begemanec2d1062007-12-30 02:59:45 +0000388
Chris Lattner4f025a42008-02-02 04:51:41 +0000389 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000390 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000391 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000392 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000393
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000394 // Finally, we have the arithmetic types: real int/float.
395 if (isa<llvm::IntegerType>(Src->getType())) {
396 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000397 if (isa<llvm::IntegerType>(DstTy))
398 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
399 else if (InputSigned)
400 return Builder.CreateSIToFP(Src, DstTy, "conv");
401 else
402 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000403 }
404
405 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
406 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000407 if (DstType->isSignedIntegerType())
408 return Builder.CreateFPToSI(Src, DstTy, "conv");
409 else
410 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000411 }
412
413 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000414 if (DstTy->getTypeID() < Src->getType()->getTypeID())
415 return Builder.CreateFPTrunc(Src, DstTy, "conv");
416 else
417 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000418}
419
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000420/// EmitComplexToScalarConversion - Emit a conversion from the specified
421/// complex type to the specified destination type, where the destination
422/// type is an LLVM scalar type.
423Value *ScalarExprEmitter::
424EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
425 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000426 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000427 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000428
429 // Handle conversions to bool first, they are special: comparisons against 0.
430 if (DstTy->isBooleanType()) {
431 // Complex != 0 -> (Real != 0) | (Imag != 0)
432 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
433 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
434 return Builder.CreateOr(Src.first, Src.second, "tobool");
435 }
436
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000437 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
438 // the imaginary part of the complex value is discarded and the value of the
439 // real part is converted according to the conversion rules for the
440 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000441 return EmitScalarConversion(Src.first, SrcTy, DstTy);
442}
443
444
Chris Lattner9fba49a2007-08-24 05:35:26 +0000445//===----------------------------------------------------------------------===//
446// Visitor Methods
447//===----------------------------------------------------------------------===//
448
449Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000450 CGF.WarnUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000451 if (E->getType()->isVoidType())
452 return 0;
453 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
454}
455
Eli Friedmand0e9d092008-05-14 19:38:39 +0000456Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
457 llvm::SmallVector<llvm::Constant*, 32> indices;
458 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
459 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
460 }
461 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
462 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
463 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
464 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
465}
466
Chris Lattnercbfb5512008-03-01 08:45:05 +0000467Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
468 // Only the lookup mechanism and first two arguments of the method
469 // implementation vary between runtimes. We can get the receiver and
470 // arguments in generic code.
471
472 // Find the receiver
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000473 llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000474
475 // Process the arguments
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000476 unsigned ArgC = E->getNumArgs();
Chris Lattnercbfb5512008-03-01 08:45:05 +0000477 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000478 for (unsigned i = 0; i != ArgC; ++i) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000479 Expr *ArgExpr = E->getArg(i);
480 QualType ArgTy = ArgExpr->getType();
481 if (!CGF.hasAggregateLLVMType(ArgTy)) {
482 // Scalar argument is passed by-value.
483 Args.push_back(CGF.EmitScalarExpr(ArgExpr));
Chris Lattnerde0908b2008-04-04 16:54:41 +0000484 } else if (ArgTy->isAnyComplexType()) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000485 // Make a temporary alloca to pass the argument.
486 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
487 CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
488 Args.push_back(DestMem);
489 } else {
490 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
491 CGF.EmitAggExpr(ArgExpr, DestMem, false);
492 Args.push_back(DestMem);
493 }
494 }
495
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000496 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattner6e6a5972008-04-04 04:07:35 +0000497 CGF.LoadObjCSelf(),
Chris Lattner8384c142008-06-26 04:42:20 +0000498 Receiver, E->getSelector(),
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000499 &Args[0], Args.size());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000500}
501
Chris Lattner9fba49a2007-08-24 05:35:26 +0000502Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
503 // Emit subscript expressions in rvalue context's. For most cases, this just
504 // loads the lvalue formed by the subscript expr. However, we have to be
505 // careful, because the base of a vector subscript is occasionally an rvalue,
506 // so we can't get it as an lvalue.
507 if (!E->getBase()->getType()->isVectorType())
508 return EmitLoadOfLValue(E);
509
510 // Handle the vector case. The base must be a vector, the index must be an
511 // integer value.
512 Value *Base = Visit(E->getBase());
513 Value *Idx = Visit(E->getIdx());
514
515 // FIXME: Convert Idx to i32 type.
516 return Builder.CreateExtractElement(Base, Idx, "vecext");
517}
518
519/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
520/// also handle things like function to pointer-to-function decay, and array to
521/// pointer decay.
522Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
523 const Expr *Op = E->getSubExpr();
524
525 // If this is due to array->pointer conversion, emit the array expression as
526 // an l-value.
527 if (Op->getType()->isArrayType()) {
528 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
529 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000530 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000531
532 assert(isa<llvm::PointerType>(V->getType()) &&
533 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
534 ->getElementType()) &&
535 "Doesn't support VLAs yet!");
Chris Lattner07307562008-03-19 05:19:41 +0000536 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnere54443b2007-12-12 04:13:20 +0000537
538 // The resultant pointer type can be implicitly casted to other pointer
Chris Lattner3b8f5c62008-07-23 06:31:27 +0000539 // types as well (e.g. void*) and can be implicitly converted to integer.
540 const llvm::Type *DestTy = ConvertType(E->getType());
541 if (V->getType() != DestTy) {
542 if (isa<llvm::PointerType>(DestTy))
543 V = Builder.CreateBitCast(V, DestTy, "ptrconv");
544 else {
545 assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
546 V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
547 }
548 }
Chris Lattnere54443b2007-12-12 04:13:20 +0000549 return V;
550
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000551 } else if (E->getType()->isReferenceType()) {
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000552 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000553 }
554
555 return EmitCastExpr(Op, E->getType());
556}
557
558
559// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
560// have to handle a more broad range of conversions than explicit casts, as they
561// handle things like function to ptr-to-function decay etc.
562Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000563 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000564
565 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000566 Value *Src = Visit(const_cast<Expr*>(E));
567
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000568 // Use EmitScalarConversion to perform the conversion.
569 return EmitScalarConversion(Src, E->getType(), DestTy);
570 }
Chris Lattner77288792008-02-16 23:55:16 +0000571
Chris Lattnerde0908b2008-04-04 16:54:41 +0000572 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000573 // Handle cases where the source is a complex type.
574 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
575 DestTy);
576 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000577
Chris Lattner77288792008-02-16 23:55:16 +0000578 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
579 // evaluate the result and return.
580 CGF.EmitAggExpr(E, 0, false);
581 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000582}
583
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000584Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000585 return CGF.EmitCompoundStmt(*E->getSubStmt(),
586 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000587}
588
589
Chris Lattner9fba49a2007-08-24 05:35:26 +0000590//===----------------------------------------------------------------------===//
591// Unary Operators
592//===----------------------------------------------------------------------===//
593
594Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000595 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000596 LValue LV = EmitLValue(E->getSubExpr());
597 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000598 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000599 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000600
601 int AmountVal = isInc ? 1 : -1;
602
603 Value *NextVal;
Chris Lattner0dc11f62007-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 Lattner07307562008-03-19 05:19:41 +0000607 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattner0dc11f62007-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 Lattnerb2a7dab2007-09-13 06:19:18 +0000612 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000613 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000614 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000615 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000616 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000617 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000618 else {
619 llvm::APFloat F(static_cast<float>(AmountVal));
Chris Lattner2a674dc2008-06-30 18:32:54 +0000620 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000621 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000622 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000623 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
624 }
Chris Lattner9fba49a2007-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 Lattner01211af2007-08-24 21:20:17 +0000662 QualType RetType,bool isSizeOf){
Chris Lattner20515462008-02-21 05:45:29 +0000663 assert(RetType->isIntegerType() && "Result type must be an integer!");
664 uint32_t ResultWidth =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000665 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattner20515462008-02-21 05:45:29 +0000666
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000667 // sizeof(void) and __alignof__(void) = 1 as a gcc extension. Also
668 // for function types.
Daniel Dunbar1c73aa22008-07-22 19:44:18 +0000669 // FIXME: what is alignof a function type in gcc?
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000670 if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
Chris Lattner20515462008-02-21 05:45:29 +0000671 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
672
Chris Lattner9fba49a2007-08-24 05:35:26 +0000673 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner8cd0e932008-03-05 18:54:05 +0000674 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner9fba49a2007-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 Lattner9fba49a2007-08-24 05:35:26 +0000679 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
680}
681
Chris Lattner01211af2007-08-24 21:20:17 +0000682Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
683 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000684 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-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 Lattnerde0908b2008-04-04 16:54:41 +0000690 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000691 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-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 Lattner01211af2007-08-24 21:20:17 +0000697}
698
Anders Carlsson52774ad2008-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 Lattner8cd0e932008-03-05 18:54:05 +0000705 uint32_t ResultWidth =
706 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000707 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
708}
Chris Lattner01211af2007-08-24 21:20:17 +0000709
Chris Lattner9fba49a2007-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 Lattner660e31d2007-08-24 21:00:35 +0000718 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000719 Result.E = E;
720 return Result;
721}
722
Chris Lattner0d965302007-08-26 21:41:21 +0000723Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-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 Lattner9c9f4bb2007-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 Lattner0d965302007-08-26 21:41:21 +0000737 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000738
Chris Lattner9c9f4bb2007-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
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000751 QualType LComputeTy, RComputeTy, ResultTy;
752
753 // Compound assignment does not contain enough information about all
754 // the types involved for pointer arithmetic cases. Figure it out
755 // here for now.
756 if (E->getLHS()->getType()->isPointerType()) {
757 // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr,
758 assert((E->getOpcode() == BinaryOperator::AddAssign ||
759 E->getOpcode() == BinaryOperator::SubAssign) &&
760 "Invalid compound assignment operator on pointer type.");
761 LComputeTy = E->getLHS()->getType();
762
763 if (E->getRHS()->getType()->isPointerType()) {
764 // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast
765 // extension, the conversion from the pointer difference back to
766 // the LHS type is handled at the end.
767 assert(E->getOpcode() == BinaryOperator::SubAssign &&
768 "Invalid compound assignment operator on pointer type.");
769 RComputeTy = E->getLHS()->getType();
770 ResultTy = CGF.getContext().getPointerDiffType();
771 } else {
772 RComputeTy = E->getRHS()->getType();
773 ResultTy = LComputeTy;
774 }
775 } else if (E->getRHS()->getType()->isPointerType()) {
776 // Degenerate case of (int += ptr) allowed by GCC implicit cast
777 // extension.
778 assert(E->getOpcode() == BinaryOperator::AddAssign &&
779 "Invalid compound assignment operator on pointer type.");
780 LComputeTy = E->getLHS()->getType();
781 RComputeTy = E->getRHS()->getType();
782 ResultTy = RComputeTy;
783 } else {
784 LComputeTy = RComputeTy = ResultTy = ComputeType;
Chris Lattner660e31d2007-08-24 21:00:35 +0000785 }
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000786
787 // Convert the LHS/RHS values to the computation type.
788 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy);
789 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy);
790 OpInfo.Ty = ResultTy;
Chris Lattner660e31d2007-08-24 21:00:35 +0000791 OpInfo.E = E;
792
793 // Expand the binary operator.
794 Value *Result = (this->*Func)(OpInfo);
795
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000796 // Convert the result back to the LHS type.
797 Result = EmitScalarConversion(Result, ResultTy, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000798
799 // Store the result value into the LHS lvalue.
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000800 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000801
Eli Friedmanf9b930c2008-05-25 14:13:57 +0000802 // For bitfields, we need the value in the bitfield
803 // FIXME: This adds an extra bitfield load
804 if (LHSLV.isBitfield())
805 Result = EmitLoadOfLValue(LHSLV, LHSTy);
806
Chris Lattner660e31d2007-08-24 21:00:35 +0000807 return Result;
808}
809
810
Chris Lattner9fba49a2007-08-24 05:35:26 +0000811Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000812 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000813 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000814 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000815 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
816 else
817 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
818}
819
820Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
821 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000822 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000823 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
824 else
825 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
826}
827
828
829Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000830 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000831 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000832
833 // FIXME: What about a pointer to a VLA?
Chris Lattner17c0cb02008-01-03 06:36:51 +0000834 Value *Ptr, *Idx;
835 Expr *IdxExp;
836 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
837 Ptr = Ops.LHS;
838 Idx = Ops.RHS;
839 IdxExp = Ops.E->getRHS();
840 } else { // int + pointer
841 Ptr = Ops.RHS;
842 Idx = Ops.LHS;
843 IdxExp = Ops.E->getLHS();
844 }
845
846 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
847 if (Width < CGF.LLVMPointerWidth) {
848 // Zero or sign extend the pointer value based on whether the index is
849 // signed or not.
850 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +0000851 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +0000852 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
853 else
854 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
855 }
856
857 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000858}
859
860Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
861 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
862 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner660e31d2007-08-24 21:00:35 +0000863
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000864 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
865 // pointer - int
866 Value *Idx = Ops.RHS;
867 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
868 if (Width < CGF.LLVMPointerWidth) {
869 // Zero or sign extend the pointer value based on whether the index is
870 // signed or not.
871 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
872 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
873 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
874 else
875 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
876 }
877 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
878
879 // FIXME: The pointer could point to a VLA.
880 // The GNU void* - int case is automatically handled here because
881 // our LLVM type for void* is i8*.
882 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000883 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000884 // pointer - pointer
885 Value *LHS = Ops.LHS;
886 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +0000887
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000888 const QualType LHSType = Ops.E->getLHS()->getType();
889 const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
890 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000891
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000892 // Handle GCC extension for pointer arithmetic on void* types.
893 if (LHSElementType->isVoidType()) {
894 ElementSize = 1;
895 } else {
896 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
897 }
898
899 const llvm::Type *ResultType = ConvertType(Ops.Ty);
900 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
901 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
902 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
903
904 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
905 // remainder. As such, we handle common power-of-two cases here to generate
906 // better code. See PR2247.
907 if (llvm::isPowerOf2_64(ElementSize)) {
908 Value *ShAmt =
909 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
910 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
911 }
912
913 // Otherwise, do a full sdiv.
914 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
915 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000916 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000917}
918
919Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
920 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
921 // RHS to the same size as the LHS.
922 Value *RHS = Ops.RHS;
923 if (Ops.LHS->getType() != RHS->getType())
924 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
925
926 return Builder.CreateShl(Ops.LHS, RHS, "shl");
927}
928
929Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
930 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
931 // RHS to the same size as the LHS.
932 Value *RHS = Ops.RHS;
933 if (Ops.LHS->getType() != RHS->getType())
934 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
935
Chris Lattner660e31d2007-08-24 21:00:35 +0000936 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000937 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
938 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
939}
940
941Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
942 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000943 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000944 QualType LHSTy = E->getLHS()->getType();
Nate Begeman1591bc52008-07-25 20:16:05 +0000945 if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000946 Value *LHS = Visit(E->getLHS());
947 Value *RHS = Visit(E->getRHS());
948
949 if (LHS->getType()->isFloatingPoint()) {
Nate Begeman1591bc52008-07-25 20:16:05 +0000950 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000951 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +0000952 } else if (LHSTy->isSignedIntegerType()) {
953 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000954 LHS, RHS, "cmp");
955 } else {
Eli Friedman850ea372008-05-29 15:09:15 +0000956 // Unsigned integers and pointers.
957 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000958 LHS, RHS, "cmp");
959 }
Nate Begeman1591bc52008-07-25 20:16:05 +0000960 } else if (LHSTy->isVectorType()) {
961 Value *LHS = Visit(E->getLHS());
962 Value *RHS = Visit(E->getRHS());
963
964 if (LHS->getType()->isFPOrFPVector()) {
965 Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
966 LHS, RHS, "cmp");
967 } else if (LHSTy->isUnsignedIntegerType()) {
968 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
969 LHS, RHS, "cmp");
970 } else {
971 // Signed integers and pointers.
972 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
973 LHS, RHS, "cmp");
974 }
975 return Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000976 } else {
977 // Complex Comparison: can only be an equality comparison.
978 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
979 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
980
Chris Lattnerc154ac12008-07-26 22:37:01 +0000981 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000982
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000983 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000984 if (CETy->isRealFloatingType()) {
985 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
986 LHS.first, RHS.first, "cmp.r");
987 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
988 LHS.second, RHS.second, "cmp.i");
989 } else {
990 // Complex comparisons can only be equality comparisons. As such, signed
991 // and unsigned opcodes are the same.
992 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
993 LHS.first, RHS.first, "cmp.r");
994 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
995 LHS.second, RHS.second, "cmp.i");
996 }
997
998 if (E->getOpcode() == BinaryOperator::EQ) {
999 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1000 } else {
1001 assert(E->getOpcode() == BinaryOperator::NE &&
1002 "Complex comparison other than == or != ?");
1003 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1004 }
1005 }
1006
1007 // ZExt result to int.
1008 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
1009}
1010
1011Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1012 LValue LHS = EmitLValue(E->getLHS());
1013 Value *RHS = Visit(E->getRHS());
1014
1015 // Store the value into the LHS.
1016 // FIXME: Volatility!
1017 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Eli Friedmanf9b930c2008-05-25 14:13:57 +00001018
1019 // For bitfields, we need the value in the bitfield
1020 // FIXME: This adds an extra bitfield load
1021 if (LHS.isBitfield())
1022 return EmitLoadOfLValue(LHS, E->getLHS()->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001023 // Return the RHS.
1024 return RHS;
1025}
1026
1027Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1028 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1029
Gabor Greif815e2c12008-04-06 20:42:52 +00001030 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
1031 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001032
1033 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1034 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
1035
1036 CGF.EmitBlock(RHSBlock);
1037 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1038
1039 // Reaquire the RHS block, as there may be subblocks inserted.
1040 RHSBlock = Builder.GetInsertBlock();
1041 CGF.EmitBlock(ContBlock);
1042
1043 // Create a PHI node. If we just evaluted the LHS condition, the result is
1044 // false. If we evaluated both, the result is the RHS condition.
1045 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
1046 PN->reserveOperandSpace(2);
1047 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
1048 PN->addIncoming(RHSCond, RHSBlock);
1049
1050 // ZExt result to int.
1051 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1052}
1053
1054Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1055 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1056
Gabor Greif815e2c12008-04-06 20:42:52 +00001057 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
1058 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001059
1060 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1061 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
1062
1063 CGF.EmitBlock(RHSBlock);
1064 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1065
1066 // Reaquire the RHS block, as there may be subblocks inserted.
1067 RHSBlock = Builder.GetInsertBlock();
1068 CGF.EmitBlock(ContBlock);
1069
1070 // Create a PHI node. If we just evaluted the LHS condition, the result is
1071 // true. If we evaluated both, the result is the RHS condition.
1072 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1073 PN->reserveOperandSpace(2);
1074 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1075 PN->addIncoming(RHSCond, RHSBlock);
1076
1077 // ZExt result to int.
1078 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1079}
1080
1081Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1082 CGF.EmitStmt(E->getLHS());
1083 return Visit(E->getRHS());
1084}
1085
1086//===----------------------------------------------------------------------===//
1087// Other Operators
1088//===----------------------------------------------------------------------===//
1089
1090Value *ScalarExprEmitter::
1091VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +00001092 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1093 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1094 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001095
Chris Lattner98a425c2007-11-26 01:40:58 +00001096 // Evaluate the conditional, then convert it to bool. We do this explicitly
1097 // because we need the unconverted value if this is a GNU ?: expression with
1098 // missing middle value.
1099 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc2126682008-01-03 07:05:49 +00001100 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1101 CGF.getContext().BoolTy);
Chris Lattner98a425c2007-11-26 01:40:58 +00001102 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001103
1104 CGF.EmitBlock(LHSBlock);
1105
1106 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001107 Value *LHS;
1108 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001109 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001110 else // Perform promotions, to handle cases like "short ?: int"
1111 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1112
Chris Lattner9fba49a2007-08-24 05:35:26 +00001113 Builder.CreateBr(ContBlock);
1114 LHSBlock = Builder.GetInsertBlock();
1115
1116 CGF.EmitBlock(RHSBlock);
1117
Eli Friedmance8d7032008-05-16 20:38:39 +00001118 Value *RHS = Visit(E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001119 Builder.CreateBr(ContBlock);
1120 RHSBlock = Builder.GetInsertBlock();
1121
1122 CGF.EmitBlock(ContBlock);
1123
Nuno Lopesb62ff242008-06-04 19:15:45 +00001124 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001125 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1126 return 0;
1127 }
1128
Chris Lattner9fba49a2007-08-24 05:35:26 +00001129 // Create a PHI node for the real part.
1130 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1131 PN->reserveOperandSpace(2);
1132 PN->addIncoming(LHS, LHSBlock);
1133 PN->addIncoming(RHS, RHSBlock);
1134 return PN;
1135}
1136
1137Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001138 // Emit the LHS or RHS as appropriate.
Devang Patel0f2a8fb2007-10-30 20:59:40 +00001139 return
1140 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001141}
1142
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001143Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001144 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +00001145 E->arg_end(CGF.getContext())).getScalarVal();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001146}
1147
Chris Lattner307da022007-11-30 17:56:23 +00001148Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson36760332007-10-15 20:28:48 +00001149 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1150
1151 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1152 return V;
1153}
1154
Chris Lattner307da022007-11-30 17:56:23 +00001155Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001156 std::string str;
Fariborz Jahanian248db262008-01-22 22:44:46 +00001157 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1158 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1159 EncodingRecordTypes);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001160
1161 llvm::Constant *C = llvm::ConstantArray::get(str);
1162 C = new llvm::GlobalVariable(C->getType(), true,
1163 llvm::GlobalValue::InternalLinkage,
1164 C, ".str", &CGF.CGM.getModule());
1165 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1166 llvm::Constant *Zeros[] = { Zero, Zero };
1167 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1168
1169 return C;
1170}
1171
Chris Lattner9fba49a2007-08-24 05:35:26 +00001172//===----------------------------------------------------------------------===//
1173// Entry Point into this File
1174//===----------------------------------------------------------------------===//
1175
1176/// EmitComplexExpr - Emit the computation of the specified expression of
1177/// complex type, ignoring the result.
1178Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1179 assert(E && !hasAggregateLLVMType(E->getType()) &&
1180 "Invalid scalar expression to emit");
1181
1182 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1183}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001184
1185/// EmitScalarConversion - Emit a conversion from the specified type to the
1186/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001187Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1188 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001189 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1190 "Invalid scalar expression to emit");
1191 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1192}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001193
1194/// EmitComplexToScalarConversion - Emit a conversion from the specified
1195/// complex type to the specified destination type, where the destination
1196/// type is an LLVM scalar type.
1197Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1198 QualType SrcTy,
1199 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001200 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001201 "Invalid complex -> scalar conversion");
1202 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1203 DstTy);
1204}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001205
1206Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1207 assert(V1->getType() == V2->getType() &&
1208 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001209 unsigned NumElements =
1210 cast<llvm::VectorType>(V1->getType())->getNumElements();
1211
1212 va_list va;
1213 va_start(va, V2);
1214
1215 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001216 for (unsigned i = 0; i < NumElements; i++) {
1217 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001218 assert(n >= 0 && n < (int)NumElements * 2 &&
1219 "Vector shuffle index out of bounds!");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001220 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1221 }
1222
1223 const char *Name = va_arg(va, const char *);
1224 va_end(va);
1225
1226 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1227
1228 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1229}
1230
Anders Carlsson68b8be92007-12-15 21:23:30 +00001231llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001232 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001233 llvm::Value *Vec
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001234 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001235
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001236 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001237 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson68b8be92007-12-15 21:23:30 +00001238 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001239 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001240 }
1241
1242 return Vec;
1243}