blob: cfe23370d8c33fa781b089275f71b675cc1f4e0a [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"
16#include "clang/AST/AST.h"
Chris Lattnerd54d1f22008-04-20 00:50:39 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000021#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerc2126682008-01-03 07:05:49 +000023#include <cstdarg>
Ted Kremenek03cf4df2007-12-10 23:44:32 +000024
Chris Lattner9fba49a2007-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 Lattner660e31d2007-08-24 21:00:35 +000036 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-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 Lattner676bf212008-04-13 07:32:11 +000044 llvm::IRBuilder &Builder;
Chris Lattnercbfb5512008-03-01 08:45:05 +000045 CGObjCRuntime *Runtime;
46
Chris Lattner9fba49a2007-08-24 05:35:26 +000047public:
48
Chris Lattnercbfb5512008-03-01 08:45:05 +000049 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
50 Builder(CGF.Builder),
51 Runtime(CGF.CGM.getObjCRuntime()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000052 }
Chris Lattner9fba49a2007-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 Lattnere24c4cf2007-08-31 22:49:20 +000062 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-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 Lattnerd8d44222007-08-26 16:42:57 +000073 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000074 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000075 Value *EmitConversionToBool(Value *Src, QualType DstTy);
76
Chris Lattner4e05d1e2007-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 Lattnerfb182ee2007-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 Lattner4e05d1e2007-08-26 06:48:56 +000086
Chris Lattner9fba49a2007-08-24 05:35:26 +000087 //===--------------------------------------------------------------------===//
88 // Visitor Methods
89 //===--------------------------------------------------------------------===//
90
91 Value *VisitStmt(Stmt *S) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +000092 S->dump(CGF.getContext().getSourceManager());
Chris Lattner9fba49a2007-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 Lattner70c38672008-04-20 00:45:53 +0000104 return llvm::ConstantFP::get(E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000105 }
106 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
107 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
108 }
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000109 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
110 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000112 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
113 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000114 CGF.getContext().typesAreCompatible(
115 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000116 }
117 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
118 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
119 }
Daniel Dunbar879788d2008-08-04 16:51:22 +0000120 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
121 Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty,
122 CGF.GetIDForAddrOfLabel(E->getLabel()));
123 return Builder.CreateIntToPtr(V,
124 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
125 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000126
127 // l-values.
128 Value *VisitDeclRefExpr(DeclRefExpr *E) {
129 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
130 return llvm::ConstantInt::get(EC->getInitVal());
131 return EmitLoadOfLValue(E);
132 }
Chris Lattnercbfb5512008-03-01 08:45:05 +0000133 Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000134 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Chris Lattner9fba49a2007-08-24 05:35:26 +0000135 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand0e9d092008-05-14 19:38:39 +0000136 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000137 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000138 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000139 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000140 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
141 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000142
143 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000144 unsigned NumInitElements = E->getNumInits();
145
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000146 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000147 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
148
149 // We have a scalar in braces. Just use the first element.
150 if (!VType)
151 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000152
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000153 unsigned NumVectorElements = VType->getNumElements();
154 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000155
156 // Emit individual vector element stores.
157 llvm::Value *V = llvm::UndefValue::get(VType);
158
Anders Carlsson323d5682007-12-18 02:45:33 +0000159 // Emit initializers
160 unsigned i;
161 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000162 Value *NewV = Visit(E->getInit(i));
163 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
164 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000165 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000166
167 // Emit remaining default initializers
168 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
169 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
170 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
171 V = Builder.CreateInsertElement(V, NewV, Idx);
172 }
173
Devang Patel32c39832007-10-24 18:05:48 +0000174 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000175 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000176
Chris Lattner9fba49a2007-08-24 05:35:26 +0000177 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
178 Value *VisitCastExpr(const CastExpr *E) {
179 return EmitCastExpr(E->getSubExpr(), E->getType());
180 }
181 Value *EmitCastExpr(const Expr *E, QualType T);
182
183 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000184 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000185 }
186
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000187 Value *VisitStmtExpr(const StmtExpr *E);
188
Chris Lattner9fba49a2007-08-24 05:35:26 +0000189 // Unary Operators.
190 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
191 Value *VisitUnaryPostDec(const UnaryOperator *E) {
192 return VisitPrePostIncDec(E, false, false);
193 }
194 Value *VisitUnaryPostInc(const UnaryOperator *E) {
195 return VisitPrePostIncDec(E, true, false);
196 }
197 Value *VisitUnaryPreDec(const UnaryOperator *E) {
198 return VisitPrePostIncDec(E, false, true);
199 }
200 Value *VisitUnaryPreInc(const UnaryOperator *E) {
201 return VisitPrePostIncDec(E, true, true);
202 }
203 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
204 return EmitLValue(E->getSubExpr()).getAddress();
205 }
206 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
207 Value *VisitUnaryPlus(const UnaryOperator *E) {
208 return Visit(E->getSubExpr());
209 }
210 Value *VisitUnaryMinus (const UnaryOperator *E);
211 Value *VisitUnaryNot (const UnaryOperator *E);
212 Value *VisitUnaryLNot (const UnaryOperator *E);
213 Value *VisitUnarySizeOf (const UnaryOperator *E) {
214 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
215 }
216 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
217 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
218 }
219 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnercfac88d2008-04-02 17:35:06 +0000220 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000221 Value *VisitUnaryReal (const UnaryOperator *E);
222 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000223 Value *VisitUnaryExtension(const UnaryOperator *E) {
224 return Visit(E->getSubExpr());
225 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000226 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000227 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
228 return Visit(DAE->getExpr());
229 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000230
Chris Lattner9fba49a2007-08-24 05:35:26 +0000231 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000232 Value *EmitMul(const BinOpInfo &Ops) {
233 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
234 }
235 Value *EmitDiv(const BinOpInfo &Ops);
236 Value *EmitRem(const BinOpInfo &Ops);
237 Value *EmitAdd(const BinOpInfo &Ops);
238 Value *EmitSub(const BinOpInfo &Ops);
239 Value *EmitShl(const BinOpInfo &Ops);
240 Value *EmitShr(const BinOpInfo &Ops);
241 Value *EmitAnd(const BinOpInfo &Ops) {
242 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
243 }
244 Value *EmitXor(const BinOpInfo &Ops) {
245 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
246 }
247 Value *EmitOr (const BinOpInfo &Ops) {
248 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
249 }
250
Chris Lattner660e31d2007-08-24 21:00:35 +0000251 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000252 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000253 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
254
255 // Binary operators and binary compound assignment operators.
256#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000257 Value *VisitBin ## OP(const BinaryOperator *E) { \
258 return Emit ## OP(EmitBinOps(E)); \
259 } \
260 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
261 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000262 }
263 HANDLEBINOP(Mul);
264 HANDLEBINOP(Div);
265 HANDLEBINOP(Rem);
266 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000267 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000268 HANDLEBINOP(Shl);
269 HANDLEBINOP(Shr);
270 HANDLEBINOP(And);
271 HANDLEBINOP(Xor);
272 HANDLEBINOP(Or);
273#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000274
Chris Lattner9fba49a2007-08-24 05:35:26 +0000275 // Comparisons.
276 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
277 unsigned SICmpOpc, unsigned FCmpOpc);
278#define VISITCOMP(CODE, UI, SI, FP) \
279 Value *VisitBin##CODE(const BinaryOperator *E) { \
280 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
281 llvm::FCmpInst::FP); }
282 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
283 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
284 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
285 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
286 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
287 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
288#undef VISITCOMP
289
290 Value *VisitBinAssign (const BinaryOperator *E);
291
292 Value *VisitBinLAnd (const BinaryOperator *E);
293 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000294 Value *VisitBinComma (const BinaryOperator *E);
295
296 // Other Operators.
297 Value *VisitConditionalOperator(const ConditionalOperator *CO);
298 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000299 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson36760332007-10-15 20:28:48 +0000300 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000301 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
302 return CGF.EmitObjCStringLiteral(E);
303 }
Anders Carlsson36f07d82007-10-29 05:01:08 +0000304 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000305};
306} // end anonymous namespace.
307
308//===----------------------------------------------------------------------===//
309// Utilities
310//===----------------------------------------------------------------------===//
311
Chris Lattnerd8d44222007-08-26 16:42:57 +0000312/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000313/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000314Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
315 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
316
317 if (SrcType->isRealFloatingType()) {
318 // Compare against 0.0 for fp scalars.
319 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000320 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
321 }
322
323 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
324 "Unknown scalar type to convert");
325
326 // Because of the type rules of C, we often end up computing a logical value,
327 // then zero extending it to int, then wanting it as a logical value again.
328 // Optimize this common case.
329 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
330 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
331 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000332 // If there aren't any more uses, zap the instruction to save space.
333 // Note that there can be more uses, for example if this
334 // is the result of an assignment.
335 if (ZI->use_empty())
336 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000337 return Result;
338 }
339 }
340
341 // Compare against an integer or pointer null.
342 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
343 return Builder.CreateICmpNE(Src, Zero, "tobool");
344}
345
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000346/// EmitScalarConversion - Emit a conversion from the specified type to the
347/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000348Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
349 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000350 SrcType = CGF.getContext().getCanonicalType(SrcType);
351 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000352 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000353
354 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000355
356 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000357 if (DstType->isBooleanType())
358 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000359
360 const llvm::Type *DstTy = ConvertType(DstType);
361
362 // Ignore conversions like int -> uint.
363 if (Src->getType() == DstTy)
364 return Src;
365
366 // Handle pointer conversions next: pointers can only be converted to/from
367 // other pointers and integers.
368 if (isa<PointerType>(DstType)) {
369 // The source value may be an integer, or a pointer.
370 if (isa<llvm::PointerType>(Src->getType()))
371 return Builder.CreateBitCast(Src, DstTy, "conv");
372 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
373 return Builder.CreateIntToPtr(Src, DstTy, "conv");
374 }
375
376 if (isa<PointerType>(SrcType)) {
377 // Must be an ptr to int cast.
378 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000379 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000380 }
381
Nate Begemanaf6ed502008-04-18 23:10:10 +0000382 // A scalar can be splatted to an extended vector of the same element type
383 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner4f025a42008-02-02 04:51:41 +0000384 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begemanec2d1062007-12-30 02:59:45 +0000385 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
386 true);
Nate Begemanec2d1062007-12-30 02:59:45 +0000387
Chris Lattner4f025a42008-02-02 04:51:41 +0000388 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000389 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000390 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000391 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000392
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000393 // Finally, we have the arithmetic types: real int/float.
394 if (isa<llvm::IntegerType>(Src->getType())) {
395 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000396 if (isa<llvm::IntegerType>(DstTy))
397 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
398 else if (InputSigned)
399 return Builder.CreateSIToFP(Src, DstTy, "conv");
400 else
401 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000402 }
403
404 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
405 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000406 if (DstType->isSignedIntegerType())
407 return Builder.CreateFPToSI(Src, DstTy, "conv");
408 else
409 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000410 }
411
412 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000413 if (DstTy->getTypeID() < Src->getType()->getTypeID())
414 return Builder.CreateFPTrunc(Src, DstTy, "conv");
415 else
416 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000417}
418
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000419/// EmitComplexToScalarConversion - Emit a conversion from the specified
420/// complex type to the specified destination type, where the destination
421/// type is an LLVM scalar type.
422Value *ScalarExprEmitter::
423EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
424 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000425 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000426 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000427
428 // Handle conversions to bool first, they are special: comparisons against 0.
429 if (DstTy->isBooleanType()) {
430 // Complex != 0 -> (Real != 0) | (Imag != 0)
431 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
432 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
433 return Builder.CreateOr(Src.first, Src.second, "tobool");
434 }
435
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000436 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
437 // the imaginary part of the complex value is discarded and the value of the
438 // real part is converted according to the conversion rules for the
439 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000440 return EmitScalarConversion(Src.first, SrcTy, DstTy);
441}
442
443
Chris Lattner9fba49a2007-08-24 05:35:26 +0000444//===----------------------------------------------------------------------===//
445// Visitor Methods
446//===----------------------------------------------------------------------===//
447
448Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000449 CGF.WarnUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000450 if (E->getType()->isVoidType())
451 return 0;
452 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
453}
454
Eli Friedmand0e9d092008-05-14 19:38:39 +0000455Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
456 llvm::SmallVector<llvm::Constant*, 32> indices;
457 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
458 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
459 }
460 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
461 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
462 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
463 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
464}
465
Chris Lattnercbfb5512008-03-01 08:45:05 +0000466Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
467 // Only the lookup mechanism and first two arguments of the method
468 // implementation vary between runtimes. We can get the receiver and
469 // arguments in generic code.
470
471 // Find the receiver
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000472 llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000473
474 // Process the arguments
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000475 unsigned ArgC = E->getNumArgs();
Chris Lattnercbfb5512008-03-01 08:45:05 +0000476 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000477 for (unsigned i = 0; i != ArgC; ++i) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000478 Expr *ArgExpr = E->getArg(i);
479 QualType ArgTy = ArgExpr->getType();
480 if (!CGF.hasAggregateLLVMType(ArgTy)) {
481 // Scalar argument is passed by-value.
482 Args.push_back(CGF.EmitScalarExpr(ArgExpr));
Chris Lattnerde0908b2008-04-04 16:54:41 +0000483 } else if (ArgTy->isAnyComplexType()) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000484 // Make a temporary alloca to pass the argument.
485 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
486 CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
487 Args.push_back(DestMem);
488 } else {
489 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
490 CGF.EmitAggExpr(ArgExpr, DestMem, false);
491 Args.push_back(DestMem);
492 }
493 }
494
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000495 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattner6e6a5972008-04-04 04:07:35 +0000496 CGF.LoadObjCSelf(),
Chris Lattner8384c142008-06-26 04:42:20 +0000497 Receiver, E->getSelector(),
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000498 &Args[0], Args.size());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000499}
500
Chris Lattner9fba49a2007-08-24 05:35:26 +0000501Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
502 // Emit subscript expressions in rvalue context's. For most cases, this just
503 // loads the lvalue formed by the subscript expr. However, we have to be
504 // careful, because the base of a vector subscript is occasionally an rvalue,
505 // so we can't get it as an lvalue.
506 if (!E->getBase()->getType()->isVectorType())
507 return EmitLoadOfLValue(E);
508
509 // Handle the vector case. The base must be a vector, the index must be an
510 // integer value.
511 Value *Base = Visit(E->getBase());
512 Value *Idx = Visit(E->getIdx());
513
514 // FIXME: Convert Idx to i32 type.
515 return Builder.CreateExtractElement(Base, Idx, "vecext");
516}
517
518/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
519/// also handle things like function to pointer-to-function decay, and array to
520/// pointer decay.
521Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
522 const Expr *Op = E->getSubExpr();
523
524 // If this is due to array->pointer conversion, emit the array expression as
525 // an l-value.
526 if (Op->getType()->isArrayType()) {
527 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
528 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000529 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000530
531 assert(isa<llvm::PointerType>(V->getType()) &&
532 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
533 ->getElementType()) &&
534 "Doesn't support VLAs yet!");
Chris Lattner07307562008-03-19 05:19:41 +0000535 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnere54443b2007-12-12 04:13:20 +0000536
537 // The resultant pointer type can be implicitly casted to other pointer
Chris Lattner3b8f5c62008-07-23 06:31:27 +0000538 // types as well (e.g. void*) and can be implicitly converted to integer.
539 const llvm::Type *DestTy = ConvertType(E->getType());
540 if (V->getType() != DestTy) {
541 if (isa<llvm::PointerType>(DestTy))
542 V = Builder.CreateBitCast(V, DestTy, "ptrconv");
543 else {
544 assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
545 V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
546 }
547 }
Chris Lattnere54443b2007-12-12 04:13:20 +0000548 return V;
549
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000550 } else if (E->getType()->isReferenceType()) {
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000551 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000552 }
553
554 return EmitCastExpr(Op, E->getType());
555}
556
557
558// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
559// have to handle a more broad range of conversions than explicit casts, as they
560// handle things like function to ptr-to-function decay etc.
561Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000562 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000563
564 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000565 Value *Src = Visit(const_cast<Expr*>(E));
566
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000567 // Use EmitScalarConversion to perform the conversion.
568 return EmitScalarConversion(Src, E->getType(), DestTy);
569 }
Chris Lattner77288792008-02-16 23:55:16 +0000570
Chris Lattnerde0908b2008-04-04 16:54:41 +0000571 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000572 // Handle cases where the source is a complex type.
573 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
574 DestTy);
575 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000576
Chris Lattner77288792008-02-16 23:55:16 +0000577 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
578 // evaluate the result and return.
579 CGF.EmitAggExpr(E, 0, false);
580 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000581}
582
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000583Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000584 return CGF.EmitCompoundStmt(*E->getSubStmt(),
585 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000586}
587
588
Chris Lattner9fba49a2007-08-24 05:35:26 +0000589//===----------------------------------------------------------------------===//
590// Unary Operators
591//===----------------------------------------------------------------------===//
592
593Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000594 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000595 LValue LV = EmitLValue(E->getSubExpr());
596 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000597 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000598 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000599
600 int AmountVal = isInc ? 1 : -1;
601
602 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000603 if (isa<llvm::PointerType>(InVal->getType())) {
604 // FIXME: This isn't right for VLAs.
605 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
Chris Lattner07307562008-03-19 05:19:41 +0000606 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000607 } else {
608 // Add the inc/dec to the real part.
609 if (isa<llvm::IntegerType>(InVal->getType()))
610 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000611 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000612 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000613 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000614 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000615 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000616 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000617 else {
618 llvm::APFloat F(static_cast<float>(AmountVal));
Chris Lattner2a674dc2008-06-30 18:32:54 +0000619 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000620 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000621 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000622 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
623 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000624
625 // Store the updated result through the lvalue.
626 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
627 E->getSubExpr()->getType());
628
629 // If this is a postinc, return the value read from memory, otherwise use the
630 // updated value.
631 return isPre ? NextVal : InVal;
632}
633
634
635Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
636 Value *Op = Visit(E->getSubExpr());
637 return Builder.CreateNeg(Op, "neg");
638}
639
640Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
641 Value *Op = Visit(E->getSubExpr());
642 return Builder.CreateNot(Op, "neg");
643}
644
645Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
646 // Compare operand to zero.
647 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
648
649 // Invert value.
650 // TODO: Could dynamically modify easy computations here. For example, if
651 // the operand is an icmp ne, turn into icmp eq.
652 BoolVal = Builder.CreateNot(BoolVal, "lnot");
653
654 // ZExt result to int.
655 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
656}
657
658/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
659/// an integer (RetType).
660Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000661 QualType RetType,bool isSizeOf){
Chris Lattner20515462008-02-21 05:45:29 +0000662 assert(RetType->isIntegerType() && "Result type must be an integer!");
663 uint32_t ResultWidth =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000664 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattner20515462008-02-21 05:45:29 +0000665
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000666 // sizeof(void) and __alignof__(void) = 1 as a gcc extension. Also
667 // for function types.
Daniel Dunbar1c73aa22008-07-22 19:44:18 +0000668 // FIXME: what is alignof a function type in gcc?
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000669 if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
Chris Lattner20515462008-02-21 05:45:29 +0000670 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
671
Chris Lattner9fba49a2007-08-24 05:35:26 +0000672 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner8cd0e932008-03-05 18:54:05 +0000673 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000674
675 uint64_t Val = isSizeOf ? Info.first : Info.second;
676 Val /= 8; // Return size in bytes, not bits.
677
Chris Lattner9fba49a2007-08-24 05:35:26 +0000678 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
679}
680
Chris Lattner01211af2007-08-24 21:20:17 +0000681Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
682 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000683 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000684 return CGF.EmitComplexExpr(Op).first;
685 return Visit(Op);
686}
687Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
688 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000689 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000690 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000691
692 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
693 // effects are evaluated.
694 CGF.EmitScalarExpr(Op);
695 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000696}
697
Anders Carlsson52774ad2008-01-29 15:56:48 +0000698Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
699{
700 int64_t Val = E->evaluateOffsetOf(CGF.getContext());
701
702 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
703
Chris Lattner8cd0e932008-03-05 18:54:05 +0000704 uint32_t ResultWidth =
705 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000706 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
707}
Chris Lattner01211af2007-08-24 21:20:17 +0000708
Chris Lattner9fba49a2007-08-24 05:35:26 +0000709//===----------------------------------------------------------------------===//
710// Binary Operators
711//===----------------------------------------------------------------------===//
712
713BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
714 BinOpInfo Result;
715 Result.LHS = Visit(E->getLHS());
716 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000717 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000718 Result.E = E;
719 return Result;
720}
721
Chris Lattner0d965302007-08-26 21:41:21 +0000722Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000723 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
724 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
725
726 BinOpInfo OpInfo;
727
728 // Load the LHS and RHS operands.
729 LValue LHSLV = EmitLValue(E->getLHS());
730 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000731
732 // Determine the computation type. If the RHS is complex, then this is one of
733 // the add/sub/mul/div operators. All of these operators can be computed in
734 // with just their real component even though the computation domain really is
735 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000736 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000737
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000738 // If the computation type is complex, then the RHS is complex. Emit the RHS.
739 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
740 ComputeType = CT->getElementType();
741
742 // Emit the RHS, only keeping the real component.
743 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
744 RHSTy = RHSTy->getAsComplexType()->getElementType();
745 } else {
746 // Otherwise the RHS is a simple scalar value.
747 OpInfo.RHS = Visit(E->getRHS());
748 }
749
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000750 QualType LComputeTy, RComputeTy, ResultTy;
751
752 // Compound assignment does not contain enough information about all
753 // the types involved for pointer arithmetic cases. Figure it out
754 // here for now.
755 if (E->getLHS()->getType()->isPointerType()) {
756 // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr,
757 assert((E->getOpcode() == BinaryOperator::AddAssign ||
758 E->getOpcode() == BinaryOperator::SubAssign) &&
759 "Invalid compound assignment operator on pointer type.");
760 LComputeTy = E->getLHS()->getType();
761
762 if (E->getRHS()->getType()->isPointerType()) {
763 // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast
764 // extension, the conversion from the pointer difference back to
765 // the LHS type is handled at the end.
766 assert(E->getOpcode() == BinaryOperator::SubAssign &&
767 "Invalid compound assignment operator on pointer type.");
768 RComputeTy = E->getLHS()->getType();
769 ResultTy = CGF.getContext().getPointerDiffType();
770 } else {
771 RComputeTy = E->getRHS()->getType();
772 ResultTy = LComputeTy;
773 }
774 } else if (E->getRHS()->getType()->isPointerType()) {
775 // Degenerate case of (int += ptr) allowed by GCC implicit cast
776 // extension.
777 assert(E->getOpcode() == BinaryOperator::AddAssign &&
778 "Invalid compound assignment operator on pointer type.");
779 LComputeTy = E->getLHS()->getType();
780 RComputeTy = E->getRHS()->getType();
781 ResultTy = RComputeTy;
782 } else {
783 LComputeTy = RComputeTy = ResultTy = ComputeType;
Chris Lattner660e31d2007-08-24 21:00:35 +0000784 }
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000785
786 // Convert the LHS/RHS values to the computation type.
787 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy);
788 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy);
789 OpInfo.Ty = ResultTy;
Chris Lattner660e31d2007-08-24 21:00:35 +0000790 OpInfo.E = E;
791
792 // Expand the binary operator.
793 Value *Result = (this->*Func)(OpInfo);
794
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000795 // Convert the result back to the LHS type.
796 Result = EmitScalarConversion(Result, ResultTy, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000797
798 // Store the result value into the LHS lvalue.
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000799 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000800
Eli Friedmanf9b930c2008-05-25 14:13:57 +0000801 // For bitfields, we need the value in the bitfield
802 // FIXME: This adds an extra bitfield load
803 if (LHSLV.isBitfield())
804 Result = EmitLoadOfLValue(LHSLV, LHSTy);
805
Chris Lattner660e31d2007-08-24 21:00:35 +0000806 return Result;
807}
808
809
Chris Lattner9fba49a2007-08-24 05:35:26 +0000810Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000811 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000812 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000813 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000814 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
815 else
816 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
817}
818
819Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
820 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000821 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000822 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
823 else
824 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
825}
826
827
828Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000829 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000830 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000831
832 // FIXME: What about a pointer to a VLA?
Chris Lattner17c0cb02008-01-03 06:36:51 +0000833 Value *Ptr, *Idx;
834 Expr *IdxExp;
835 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
836 Ptr = Ops.LHS;
837 Idx = Ops.RHS;
838 IdxExp = Ops.E->getRHS();
839 } else { // int + pointer
840 Ptr = Ops.RHS;
841 Idx = Ops.LHS;
842 IdxExp = Ops.E->getLHS();
843 }
844
845 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
846 if (Width < CGF.LLVMPointerWidth) {
847 // Zero or sign extend the pointer value based on whether the index is
848 // signed or not.
849 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +0000850 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +0000851 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
852 else
853 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
854 }
855
856 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000857}
858
859Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
860 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
861 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner660e31d2007-08-24 21:00:35 +0000862
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000863 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
864 // pointer - int
865 Value *Idx = Ops.RHS;
866 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
867 if (Width < CGF.LLVMPointerWidth) {
868 // Zero or sign extend the pointer value based on whether the index is
869 // signed or not.
870 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
871 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
872 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
873 else
874 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
875 }
876 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
877
878 // FIXME: The pointer could point to a VLA.
879 // The GNU void* - int case is automatically handled here because
880 // our LLVM type for void* is i8*.
881 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000882 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000883 // pointer - pointer
884 Value *LHS = Ops.LHS;
885 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +0000886
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000887 const QualType LHSType = Ops.E->getLHS()->getType();
888 const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
889 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000890
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000891 // Handle GCC extension for pointer arithmetic on void* types.
892 if (LHSElementType->isVoidType()) {
893 ElementSize = 1;
894 } else {
895 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
896 }
897
898 const llvm::Type *ResultType = ConvertType(Ops.Ty);
899 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
900 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
901 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
902
903 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
904 // remainder. As such, we handle common power-of-two cases here to generate
905 // better code. See PR2247.
906 if (llvm::isPowerOf2_64(ElementSize)) {
907 Value *ShAmt =
908 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
909 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
910 }
911
912 // Otherwise, do a full sdiv.
913 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
914 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000915 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000916}
917
918Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
919 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
920 // RHS to the same size as the LHS.
921 Value *RHS = Ops.RHS;
922 if (Ops.LHS->getType() != RHS->getType())
923 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
924
925 return Builder.CreateShl(Ops.LHS, RHS, "shl");
926}
927
928Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
929 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
930 // RHS to the same size as the LHS.
931 Value *RHS = Ops.RHS;
932 if (Ops.LHS->getType() != RHS->getType())
933 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
934
Chris Lattner660e31d2007-08-24 21:00:35 +0000935 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000936 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
937 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
938}
939
940Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
941 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000942 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000943 QualType LHSTy = E->getLHS()->getType();
Nate Begeman1591bc52008-07-25 20:16:05 +0000944 if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000945 Value *LHS = Visit(E->getLHS());
946 Value *RHS = Visit(E->getRHS());
947
948 if (LHS->getType()->isFloatingPoint()) {
Nate Begeman1591bc52008-07-25 20:16:05 +0000949 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000950 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +0000951 } else if (LHSTy->isSignedIntegerType()) {
952 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000953 LHS, RHS, "cmp");
954 } else {
Eli Friedman850ea372008-05-29 15:09:15 +0000955 // Unsigned integers and pointers.
956 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000957 LHS, RHS, "cmp");
958 }
Nate Begeman1591bc52008-07-25 20:16:05 +0000959 } else if (LHSTy->isVectorType()) {
960 Value *LHS = Visit(E->getLHS());
961 Value *RHS = Visit(E->getRHS());
962
963 if (LHS->getType()->isFPOrFPVector()) {
964 Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
965 LHS, RHS, "cmp");
966 } else if (LHSTy->isUnsignedIntegerType()) {
967 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
968 LHS, RHS, "cmp");
969 } else {
970 // Signed integers and pointers.
971 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
972 LHS, RHS, "cmp");
973 }
974 return Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000975 } else {
976 // Complex Comparison: can only be an equality comparison.
977 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
978 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
979
Chris Lattnerc154ac12008-07-26 22:37:01 +0000980 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000981
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000982 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000983 if (CETy->isRealFloatingType()) {
984 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
985 LHS.first, RHS.first, "cmp.r");
986 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
987 LHS.second, RHS.second, "cmp.i");
988 } else {
989 // Complex comparisons can only be equality comparisons. As such, signed
990 // and unsigned opcodes are the same.
991 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
992 LHS.first, RHS.first, "cmp.r");
993 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
994 LHS.second, RHS.second, "cmp.i");
995 }
996
997 if (E->getOpcode() == BinaryOperator::EQ) {
998 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
999 } else {
1000 assert(E->getOpcode() == BinaryOperator::NE &&
1001 "Complex comparison other than == or != ?");
1002 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1003 }
1004 }
1005
1006 // ZExt result to int.
1007 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
1008}
1009
1010Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1011 LValue LHS = EmitLValue(E->getLHS());
1012 Value *RHS = Visit(E->getRHS());
1013
1014 // Store the value into the LHS.
1015 // FIXME: Volatility!
1016 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Eli Friedmanf9b930c2008-05-25 14:13:57 +00001017
1018 // For bitfields, we need the value in the bitfield
1019 // FIXME: This adds an extra bitfield load
1020 if (LHS.isBitfield())
1021 return EmitLoadOfLValue(LHS, E->getLHS()->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001022 // Return the RHS.
1023 return RHS;
1024}
1025
1026Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1027 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1028
Gabor Greif815e2c12008-04-06 20:42:52 +00001029 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
1030 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001031
1032 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1033 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
1034
1035 CGF.EmitBlock(RHSBlock);
1036 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1037
1038 // Reaquire the RHS block, as there may be subblocks inserted.
1039 RHSBlock = Builder.GetInsertBlock();
1040 CGF.EmitBlock(ContBlock);
1041
1042 // Create a PHI node. If we just evaluted the LHS condition, the result is
1043 // false. If we evaluated both, the result is the RHS condition.
1044 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
1045 PN->reserveOperandSpace(2);
1046 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
1047 PN->addIncoming(RHSCond, RHSBlock);
1048
1049 // ZExt result to int.
1050 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1051}
1052
1053Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1054 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1055
Gabor Greif815e2c12008-04-06 20:42:52 +00001056 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
1057 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001058
1059 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1060 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
1061
1062 CGF.EmitBlock(RHSBlock);
1063 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1064
1065 // Reaquire the RHS block, as there may be subblocks inserted.
1066 RHSBlock = Builder.GetInsertBlock();
1067 CGF.EmitBlock(ContBlock);
1068
1069 // Create a PHI node. If we just evaluted the LHS condition, the result is
1070 // true. If we evaluated both, the result is the RHS condition.
1071 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1072 PN->reserveOperandSpace(2);
1073 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1074 PN->addIncoming(RHSCond, RHSBlock);
1075
1076 // ZExt result to int.
1077 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1078}
1079
1080Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1081 CGF.EmitStmt(E->getLHS());
1082 return Visit(E->getRHS());
1083}
1084
1085//===----------------------------------------------------------------------===//
1086// Other Operators
1087//===----------------------------------------------------------------------===//
1088
1089Value *ScalarExprEmitter::
1090VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +00001091 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1092 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1093 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001094
Chris Lattner98a425c2007-11-26 01:40:58 +00001095 // Evaluate the conditional, then convert it to bool. We do this explicitly
1096 // because we need the unconverted value if this is a GNU ?: expression with
1097 // missing middle value.
1098 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc2126682008-01-03 07:05:49 +00001099 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1100 CGF.getContext().BoolTy);
Chris Lattner98a425c2007-11-26 01:40:58 +00001101 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001102
1103 CGF.EmitBlock(LHSBlock);
1104
1105 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001106 Value *LHS;
1107 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001108 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001109 else // Perform promotions, to handle cases like "short ?: int"
1110 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1111
Chris Lattner9fba49a2007-08-24 05:35:26 +00001112 Builder.CreateBr(ContBlock);
1113 LHSBlock = Builder.GetInsertBlock();
1114
1115 CGF.EmitBlock(RHSBlock);
1116
Eli Friedmance8d7032008-05-16 20:38:39 +00001117 Value *RHS = Visit(E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001118 Builder.CreateBr(ContBlock);
1119 RHSBlock = Builder.GetInsertBlock();
1120
1121 CGF.EmitBlock(ContBlock);
1122
Nuno Lopesb62ff242008-06-04 19:15:45 +00001123 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001124 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1125 return 0;
1126 }
1127
Chris Lattner9fba49a2007-08-24 05:35:26 +00001128 // Create a PHI node for the real part.
1129 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1130 PN->reserveOperandSpace(2);
1131 PN->addIncoming(LHS, LHSBlock);
1132 PN->addIncoming(RHS, RHSBlock);
1133 return PN;
1134}
1135
1136Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001137 // Emit the LHS or RHS as appropriate.
Devang Patel0f2a8fb2007-10-30 20:59:40 +00001138 return
1139 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001140}
1141
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001142Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001143 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +00001144 E->arg_end(CGF.getContext())).getScalarVal();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001145}
1146
Chris Lattner307da022007-11-30 17:56:23 +00001147Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson36760332007-10-15 20:28:48 +00001148 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1149
1150 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1151 return V;
1152}
1153
Chris Lattner307da022007-11-30 17:56:23 +00001154Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001155 std::string str;
Fariborz Jahanian248db262008-01-22 22:44:46 +00001156 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1157 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1158 EncodingRecordTypes);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001159
1160 llvm::Constant *C = llvm::ConstantArray::get(str);
1161 C = new llvm::GlobalVariable(C->getType(), true,
1162 llvm::GlobalValue::InternalLinkage,
1163 C, ".str", &CGF.CGM.getModule());
1164 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1165 llvm::Constant *Zeros[] = { Zero, Zero };
1166 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1167
1168 return C;
1169}
1170
Chris Lattner9fba49a2007-08-24 05:35:26 +00001171//===----------------------------------------------------------------------===//
1172// Entry Point into this File
1173//===----------------------------------------------------------------------===//
1174
1175/// EmitComplexExpr - Emit the computation of the specified expression of
1176/// complex type, ignoring the result.
1177Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1178 assert(E && !hasAggregateLLVMType(E->getType()) &&
1179 "Invalid scalar expression to emit");
1180
1181 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1182}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001183
1184/// EmitScalarConversion - Emit a conversion from the specified type to the
1185/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001186Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1187 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001188 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1189 "Invalid scalar expression to emit");
1190 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1191}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001192
1193/// EmitComplexToScalarConversion - Emit a conversion from the specified
1194/// complex type to the specified destination type, where the destination
1195/// type is an LLVM scalar type.
1196Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1197 QualType SrcTy,
1198 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001199 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001200 "Invalid complex -> scalar conversion");
1201 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1202 DstTy);
1203}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001204
1205Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1206 assert(V1->getType() == V2->getType() &&
1207 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001208 unsigned NumElements =
1209 cast<llvm::VectorType>(V1->getType())->getNumElements();
1210
1211 va_list va;
1212 va_start(va, V2);
1213
1214 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001215 for (unsigned i = 0; i < NumElements; i++) {
1216 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001217 assert(n >= 0 && n < (int)NumElements * 2 &&
1218 "Vector shuffle index out of bounds!");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001219 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1220 }
1221
1222 const char *Name = va_arg(va, const char *);
1223 va_end(va);
1224
1225 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1226
1227 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1228}
1229
Anders Carlsson68b8be92007-12-15 21:23:30 +00001230llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001231 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001232 llvm::Value *Vec
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001233 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001234
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001235 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001236 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson68b8be92007-12-15 21:23:30 +00001237 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001238 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001239 }
1240
1241 return Vec;
1242}