blob: 3f7b7377523fdd0b304c76e2569ebc79093bece8 [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"
Daniel Dunbarfa456242008-08-12 05:08:18 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattnerd54d1f22008-04-20 00:50:39 +000019#include "clang/Basic/TargetInfo.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000020#include "llvm/Constants.h"
21#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000023#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerc2126682008-01-03 07:05:49 +000025#include <cstdarg>
Ted Kremenek03cf4df2007-12-10 23:44:32 +000026
Chris Lattner9fba49a2007-08-24 05:35:26 +000027using namespace clang;
28using namespace CodeGen;
29using llvm::Value;
30
31//===----------------------------------------------------------------------===//
32// Scalar Expression Emitter
33//===----------------------------------------------------------------------===//
34
35struct BinOpInfo {
36 Value *LHS;
37 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000038 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000039 const BinaryOperator *E;
40};
41
42namespace {
43class VISIBILITY_HIDDEN ScalarExprEmitter
44 : public StmtVisitor<ScalarExprEmitter, Value*> {
45 CodeGenFunction &CGF;
Chris Lattnerfaf23db2008-08-08 19:57:58 +000046 llvm::IRBuilder<> &Builder;
Chris Lattnercbfb5512008-03-01 08:45:05 +000047
Chris Lattner9fba49a2007-08-24 05:35:26 +000048public:
49
Chris Lattnercbfb5512008-03-01 08:45:05 +000050 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000051 Builder(CGF.Builder) {
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) {
Daniel Dunbarb5fda0c2008-08-16 01:41:47 +0000121 llvm::Value *V =
122 llvm::ConstantInt::get(llvm::Type::Int32Ty,
123 CGF.GetIDForAddrOfLabel(E->getLabel()));
124
125 return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
Daniel Dunbar879788d2008-08-04 16:51:22 +0000126 }
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 }
Daniel Dunbara5a0cdb2008-08-12 03:55:34 +0000134 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E);
Daniel Dunbarfa456242008-08-12 05:08:18 +0000135 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000136 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Daniel Dunbar5e105892008-08-23 10:51:21 +0000137 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
138 CGF.ErrorUnsupported(E, "scalar expression (Objective-C property reference)");
139 if (E->getType()->isVoidType())
140 return 0;
141 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
142 }
143
Chris Lattner9fba49a2007-08-24 05:35:26 +0000144 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand0e9d092008-05-14 19:38:39 +0000145 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000146 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000147 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000148 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000149 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
Chris Lattner69909292008-08-10 01:53:14 +0000150 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000151
152 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000153 unsigned NumInitElements = E->getNumInits();
154
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000155 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000156 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
157
158 // We have a scalar in braces. Just use the first element.
159 if (!VType)
160 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000161
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000162 unsigned NumVectorElements = VType->getNumElements();
163 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000164
165 // Emit individual vector element stores.
166 llvm::Value *V = llvm::UndefValue::get(VType);
167
Anders Carlsson323d5682007-12-18 02:45:33 +0000168 // Emit initializers
169 unsigned i;
170 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000171 Value *NewV = Visit(E->getInit(i));
172 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
173 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000174 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000175
176 // Emit remaining default initializers
177 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
178 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
179 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
180 V = Builder.CreateInsertElement(V, NewV, Idx);
181 }
182
Devang Patel32c39832007-10-24 18:05:48 +0000183 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000184 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000185
Chris Lattner9fba49a2007-08-24 05:35:26 +0000186 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
187 Value *VisitCastExpr(const CastExpr *E) {
188 return EmitCastExpr(E->getSubExpr(), E->getType());
189 }
190 Value *EmitCastExpr(const Expr *E, QualType T);
191
192 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000193 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000194 }
Daniel Dunbara04840b2008-08-23 03:46:30 +0000195
196 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
197 return CGF.EmitObjCMessageExpr(E).getScalarVal();
198 }
199
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000200 Value *VisitStmtExpr(const StmtExpr *E);
201
Chris Lattner9fba49a2007-08-24 05:35:26 +0000202 // Unary Operators.
203 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
204 Value *VisitUnaryPostDec(const UnaryOperator *E) {
205 return VisitPrePostIncDec(E, false, false);
206 }
207 Value *VisitUnaryPostInc(const UnaryOperator *E) {
208 return VisitPrePostIncDec(E, true, false);
209 }
210 Value *VisitUnaryPreDec(const UnaryOperator *E) {
211 return VisitPrePostIncDec(E, false, true);
212 }
213 Value *VisitUnaryPreInc(const UnaryOperator *E) {
214 return VisitPrePostIncDec(E, true, true);
215 }
216 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
217 return EmitLValue(E->getSubExpr()).getAddress();
218 }
219 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
220 Value *VisitUnaryPlus(const UnaryOperator *E) {
221 return Visit(E->getSubExpr());
222 }
223 Value *VisitUnaryMinus (const UnaryOperator *E);
224 Value *VisitUnaryNot (const UnaryOperator *E);
225 Value *VisitUnaryLNot (const UnaryOperator *E);
226 Value *VisitUnarySizeOf (const UnaryOperator *E) {
227 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
228 }
229 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
230 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
231 }
232 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnercfac88d2008-04-02 17:35:06 +0000233 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000234 Value *VisitUnaryReal (const UnaryOperator *E);
235 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000236 Value *VisitUnaryExtension(const UnaryOperator *E) {
237 return Visit(E->getSubExpr());
238 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000239 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000240 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
241 return Visit(DAE->getExpr());
242 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000243
Chris Lattner9fba49a2007-08-24 05:35:26 +0000244 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000245 Value *EmitMul(const BinOpInfo &Ops) {
246 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
247 }
248 Value *EmitDiv(const BinOpInfo &Ops);
249 Value *EmitRem(const BinOpInfo &Ops);
250 Value *EmitAdd(const BinOpInfo &Ops);
251 Value *EmitSub(const BinOpInfo &Ops);
252 Value *EmitShl(const BinOpInfo &Ops);
253 Value *EmitShr(const BinOpInfo &Ops);
254 Value *EmitAnd(const BinOpInfo &Ops) {
255 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
256 }
257 Value *EmitXor(const BinOpInfo &Ops) {
258 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
259 }
260 Value *EmitOr (const BinOpInfo &Ops) {
261 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
262 }
263
Chris Lattner660e31d2007-08-24 21:00:35 +0000264 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000265 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000266 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
267
268 // Binary operators and binary compound assignment operators.
269#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000270 Value *VisitBin ## OP(const BinaryOperator *E) { \
271 return Emit ## OP(EmitBinOps(E)); \
272 } \
273 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
274 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000275 }
276 HANDLEBINOP(Mul);
277 HANDLEBINOP(Div);
278 HANDLEBINOP(Rem);
279 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000280 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000281 HANDLEBINOP(Shl);
282 HANDLEBINOP(Shr);
283 HANDLEBINOP(And);
284 HANDLEBINOP(Xor);
285 HANDLEBINOP(Or);
286#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000287
Chris Lattner9fba49a2007-08-24 05:35:26 +0000288 // Comparisons.
289 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
290 unsigned SICmpOpc, unsigned FCmpOpc);
291#define VISITCOMP(CODE, UI, SI, FP) \
292 Value *VisitBin##CODE(const BinaryOperator *E) { \
293 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
294 llvm::FCmpInst::FP); }
295 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
296 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
297 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
298 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
299 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
300 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
301#undef VISITCOMP
302
303 Value *VisitBinAssign (const BinaryOperator *E);
304
305 Value *VisitBinLAnd (const BinaryOperator *E);
306 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000307 Value *VisitBinComma (const BinaryOperator *E);
308
309 // Other Operators.
310 Value *VisitConditionalOperator(const ConditionalOperator *CO);
311 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000312 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson36760332007-10-15 20:28:48 +0000313 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000314 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
315 return CGF.EmitObjCStringLiteral(E);
316 }
Anders Carlsson36f07d82007-10-29 05:01:08 +0000317 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000318};
319} // end anonymous namespace.
320
321//===----------------------------------------------------------------------===//
322// Utilities
323//===----------------------------------------------------------------------===//
324
Chris Lattnerd8d44222007-08-26 16:42:57 +0000325/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000326/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000327Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
328 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
329
330 if (SrcType->isRealFloatingType()) {
331 // Compare against 0.0 for fp scalars.
332 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000333 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
334 }
335
336 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
337 "Unknown scalar type to convert");
338
339 // Because of the type rules of C, we often end up computing a logical value,
340 // then zero extending it to int, then wanting it as a logical value again.
341 // Optimize this common case.
342 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
343 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
344 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000345 // If there aren't any more uses, zap the instruction to save space.
346 // Note that there can be more uses, for example if this
347 // is the result of an assignment.
348 if (ZI->use_empty())
349 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000350 return Result;
351 }
352 }
353
354 // Compare against an integer or pointer null.
355 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
356 return Builder.CreateICmpNE(Src, Zero, "tobool");
357}
358
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000359/// EmitScalarConversion - Emit a conversion from the specified type to the
360/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000361Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
362 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000363 SrcType = CGF.getContext().getCanonicalType(SrcType);
364 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000365 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000366
367 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000368
369 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000370 if (DstType->isBooleanType())
371 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000372
373 const llvm::Type *DstTy = ConvertType(DstType);
374
375 // Ignore conversions like int -> uint.
376 if (Src->getType() == DstTy)
377 return Src;
378
379 // Handle pointer conversions next: pointers can only be converted to/from
380 // other pointers and integers.
381 if (isa<PointerType>(DstType)) {
382 // The source value may be an integer, or a pointer.
383 if (isa<llvm::PointerType>(Src->getType()))
384 return Builder.CreateBitCast(Src, DstTy, "conv");
385 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
386 return Builder.CreateIntToPtr(Src, DstTy, "conv");
387 }
388
389 if (isa<PointerType>(SrcType)) {
390 // Must be an ptr to int cast.
391 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000392 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000393 }
394
Nate Begemanaf6ed502008-04-18 23:10:10 +0000395 // A scalar can be splatted to an extended vector of the same element type
396 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner4f025a42008-02-02 04:51:41 +0000397 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begemanec2d1062007-12-30 02:59:45 +0000398 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
399 true);
Nate Begemanec2d1062007-12-30 02:59:45 +0000400
Chris Lattner4f025a42008-02-02 04:51:41 +0000401 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000402 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000403 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000404 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000405
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000406 // Finally, we have the arithmetic types: real int/float.
407 if (isa<llvm::IntegerType>(Src->getType())) {
408 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000409 if (isa<llvm::IntegerType>(DstTy))
410 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
411 else if (InputSigned)
412 return Builder.CreateSIToFP(Src, DstTy, "conv");
413 else
414 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000415 }
416
417 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
418 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000419 if (DstType->isSignedIntegerType())
420 return Builder.CreateFPToSI(Src, DstTy, "conv");
421 else
422 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000423 }
424
425 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000426 if (DstTy->getTypeID() < Src->getType()->getTypeID())
427 return Builder.CreateFPTrunc(Src, DstTy, "conv");
428 else
429 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000430}
431
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000432/// EmitComplexToScalarConversion - Emit a conversion from the specified
433/// complex type to the specified destination type, where the destination
434/// type is an LLVM scalar type.
435Value *ScalarExprEmitter::
436EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
437 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000438 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000439 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000440
441 // Handle conversions to bool first, they are special: comparisons against 0.
442 if (DstTy->isBooleanType()) {
443 // Complex != 0 -> (Real != 0) | (Imag != 0)
444 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
445 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
446 return Builder.CreateOr(Src.first, Src.second, "tobool");
447 }
448
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000449 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
450 // the imaginary part of the complex value is discarded and the value of the
451 // real part is converted according to the conversion rules for the
452 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000453 return EmitScalarConversion(Src.first, SrcTy, DstTy);
454}
455
456
Chris Lattner9fba49a2007-08-24 05:35:26 +0000457//===----------------------------------------------------------------------===//
458// Visitor Methods
459//===----------------------------------------------------------------------===//
460
461Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000462 CGF.ErrorUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000463 if (E->getType()->isVoidType())
464 return 0;
465 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
466}
467
Eli Friedmand0e9d092008-05-14 19:38:39 +0000468Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
469 llvm::SmallVector<llvm::Constant*, 32> indices;
470 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
471 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
472 }
473 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
474 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
475 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
476 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
477}
478
Daniel Dunbara5a0cdb2008-08-12 03:55:34 +0000479Value *ScalarExprEmitter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
Daniel Dunbarf1f7f192008-08-20 00:28:19 +0000480 return CGF.EmitObjCSelectorExpr(E);
Daniel Dunbara5a0cdb2008-08-12 03:55:34 +0000481}
482
Daniel Dunbarfa456242008-08-12 05:08:18 +0000483Value *ScalarExprEmitter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
Daniel Dunbarf1f7f192008-08-20 00:28:19 +0000484 return CGF.EmitObjCProtocolExpr(E);
Daniel Dunbarfa456242008-08-12 05:08:18 +0000485}
486
Chris Lattner9fba49a2007-08-24 05:35:26 +0000487Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
488 // Emit subscript expressions in rvalue context's. For most cases, this just
489 // loads the lvalue formed by the subscript expr. However, we have to be
490 // careful, because the base of a vector subscript is occasionally an rvalue,
491 // so we can't get it as an lvalue.
492 if (!E->getBase()->getType()->isVectorType())
493 return EmitLoadOfLValue(E);
494
495 // Handle the vector case. The base must be a vector, the index must be an
496 // integer value.
497 Value *Base = Visit(E->getBase());
498 Value *Idx = Visit(E->getIdx());
499
500 // FIXME: Convert Idx to i32 type.
501 return Builder.CreateExtractElement(Base, Idx, "vecext");
502}
503
504/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
505/// also handle things like function to pointer-to-function decay, and array to
506/// pointer decay.
507Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
508 const Expr *Op = E->getSubExpr();
509
510 // If this is due to array->pointer conversion, emit the array expression as
511 // an l-value.
512 if (Op->getType()->isArrayType()) {
513 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
514 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000515 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000516
517 assert(isa<llvm::PointerType>(V->getType()) &&
518 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
519 ->getElementType()) &&
520 "Doesn't support VLAs yet!");
Chris Lattner07307562008-03-19 05:19:41 +0000521 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnere54443b2007-12-12 04:13:20 +0000522
523 // The resultant pointer type can be implicitly casted to other pointer
Chris Lattner3b8f5c62008-07-23 06:31:27 +0000524 // types as well (e.g. void*) and can be implicitly converted to integer.
525 const llvm::Type *DestTy = ConvertType(E->getType());
526 if (V->getType() != DestTy) {
527 if (isa<llvm::PointerType>(DestTy))
528 V = Builder.CreateBitCast(V, DestTy, "ptrconv");
529 else {
530 assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
531 V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
532 }
533 }
Chris Lattnere54443b2007-12-12 04:13:20 +0000534 return V;
535
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000536 } else if (E->getType()->isReferenceType()) {
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000537 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000538 }
539
540 return EmitCastExpr(Op, E->getType());
541}
542
543
544// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
545// have to handle a more broad range of conversions than explicit casts, as they
546// handle things like function to ptr-to-function decay etc.
547Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000548 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000549
550 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000551 Value *Src = Visit(const_cast<Expr*>(E));
552
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000553 // Use EmitScalarConversion to perform the conversion.
554 return EmitScalarConversion(Src, E->getType(), DestTy);
555 }
Chris Lattner77288792008-02-16 23:55:16 +0000556
Chris Lattnerde0908b2008-04-04 16:54:41 +0000557 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000558 // Handle cases where the source is a complex type.
559 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
560 DestTy);
561 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000562
Chris Lattner77288792008-02-16 23:55:16 +0000563 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
564 // evaluate the result and return.
565 CGF.EmitAggExpr(E, 0, false);
566 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000567}
568
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000569Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000570 return CGF.EmitCompoundStmt(*E->getSubStmt(),
571 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000572}
573
574
Chris Lattner9fba49a2007-08-24 05:35:26 +0000575//===----------------------------------------------------------------------===//
576// Unary Operators
577//===----------------------------------------------------------------------===//
578
579Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000580 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000581 LValue LV = EmitLValue(E->getSubExpr());
582 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000583 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000584 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000585
586 int AmountVal = isInc ? 1 : -1;
587
588 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000589 if (isa<llvm::PointerType>(InVal->getType())) {
590 // FIXME: This isn't right for VLAs.
591 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
Chris Lattner07307562008-03-19 05:19:41 +0000592 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000593 } else {
594 // Add the inc/dec to the real part.
595 if (isa<llvm::IntegerType>(InVal->getType()))
596 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000597 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000598 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000599 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000600 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000601 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000602 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000603 else {
604 llvm::APFloat F(static_cast<float>(AmountVal));
Chris Lattner2a674dc2008-06-30 18:32:54 +0000605 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000606 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000607 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000608 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
609 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000610
611 // Store the updated result through the lvalue.
612 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
613 E->getSubExpr()->getType());
614
615 // If this is a postinc, return the value read from memory, otherwise use the
616 // updated value.
617 return isPre ? NextVal : InVal;
618}
619
620
621Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
622 Value *Op = Visit(E->getSubExpr());
623 return Builder.CreateNeg(Op, "neg");
624}
625
626Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
627 Value *Op = Visit(E->getSubExpr());
628 return Builder.CreateNot(Op, "neg");
629}
630
631Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
632 // Compare operand to zero.
633 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
634
635 // Invert value.
636 // TODO: Could dynamically modify easy computations here. For example, if
637 // the operand is an icmp ne, turn into icmp eq.
638 BoolVal = Builder.CreateNot(BoolVal, "lnot");
639
640 // ZExt result to int.
641 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
642}
643
644/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
645/// an integer (RetType).
646Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000647 QualType RetType,bool isSizeOf){
Chris Lattner20515462008-02-21 05:45:29 +0000648 assert(RetType->isIntegerType() && "Result type must be an integer!");
649 uint32_t ResultWidth =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000650 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattner20515462008-02-21 05:45:29 +0000651
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000652 // sizeof(void) and __alignof__(void) = 1 as a gcc extension. Also
653 // for function types.
Daniel Dunbar1c73aa22008-07-22 19:44:18 +0000654 // FIXME: what is alignof a function type in gcc?
Daniel Dunbarfdb7acd2008-07-22 01:35:47 +0000655 if (TypeToSize->isVoidType() || TypeToSize->isFunctionType())
Chris Lattner20515462008-02-21 05:45:29 +0000656 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
657
Chris Lattner9fba49a2007-08-24 05:35:26 +0000658 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner8cd0e932008-03-05 18:54:05 +0000659 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000660
661 uint64_t Val = isSizeOf ? Info.first : Info.second;
662 Val /= 8; // Return size in bytes, not bits.
663
Chris Lattner9fba49a2007-08-24 05:35:26 +0000664 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
665}
666
Chris Lattner01211af2007-08-24 21:20:17 +0000667Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
668 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000669 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000670 return CGF.EmitComplexExpr(Op).first;
671 return Visit(Op);
672}
673Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
674 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000675 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000676 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000677
678 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
679 // effects are evaluated.
680 CGF.EmitScalarExpr(Op);
681 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000682}
683
Anders Carlsson52774ad2008-01-29 15:56:48 +0000684Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
685{
686 int64_t Val = E->evaluateOffsetOf(CGF.getContext());
687
688 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
689
Chris Lattner8cd0e932008-03-05 18:54:05 +0000690 uint32_t ResultWidth =
691 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000692 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
693}
Chris Lattner01211af2007-08-24 21:20:17 +0000694
Chris Lattner9fba49a2007-08-24 05:35:26 +0000695//===----------------------------------------------------------------------===//
696// Binary Operators
697//===----------------------------------------------------------------------===//
698
699BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
700 BinOpInfo Result;
701 Result.LHS = Visit(E->getLHS());
702 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000703 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000704 Result.E = E;
705 return Result;
706}
707
Chris Lattner0d965302007-08-26 21:41:21 +0000708Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000709 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
710 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
711
712 BinOpInfo OpInfo;
713
714 // Load the LHS and RHS operands.
715 LValue LHSLV = EmitLValue(E->getLHS());
716 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000717
718 // Determine the computation type. If the RHS is complex, then this is one of
719 // the add/sub/mul/div operators. All of these operators can be computed in
720 // with just their real component even though the computation domain really is
721 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000722 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000723
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000724 // If the computation type is complex, then the RHS is complex. Emit the RHS.
725 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
726 ComputeType = CT->getElementType();
727
728 // Emit the RHS, only keeping the real component.
729 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
730 RHSTy = RHSTy->getAsComplexType()->getElementType();
731 } else {
732 // Otherwise the RHS is a simple scalar value.
733 OpInfo.RHS = Visit(E->getRHS());
734 }
735
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000736 QualType LComputeTy, RComputeTy, ResultTy;
737
738 // Compound assignment does not contain enough information about all
739 // the types involved for pointer arithmetic cases. Figure it out
740 // here for now.
741 if (E->getLHS()->getType()->isPointerType()) {
742 // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr,
743 assert((E->getOpcode() == BinaryOperator::AddAssign ||
744 E->getOpcode() == BinaryOperator::SubAssign) &&
745 "Invalid compound assignment operator on pointer type.");
746 LComputeTy = E->getLHS()->getType();
747
748 if (E->getRHS()->getType()->isPointerType()) {
749 // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast
750 // extension, the conversion from the pointer difference back to
751 // the LHS type is handled at the end.
752 assert(E->getOpcode() == BinaryOperator::SubAssign &&
753 "Invalid compound assignment operator on pointer type.");
754 RComputeTy = E->getLHS()->getType();
755 ResultTy = CGF.getContext().getPointerDiffType();
756 } else {
757 RComputeTy = E->getRHS()->getType();
758 ResultTy = LComputeTy;
759 }
760 } else if (E->getRHS()->getType()->isPointerType()) {
761 // Degenerate case of (int += ptr) allowed by GCC implicit cast
762 // extension.
763 assert(E->getOpcode() == BinaryOperator::AddAssign &&
764 "Invalid compound assignment operator on pointer type.");
765 LComputeTy = E->getLHS()->getType();
766 RComputeTy = E->getRHS()->getType();
767 ResultTy = RComputeTy;
768 } else {
769 LComputeTy = RComputeTy = ResultTy = ComputeType;
Chris Lattner660e31d2007-08-24 21:00:35 +0000770 }
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000771
772 // Convert the LHS/RHS values to the computation type.
773 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy);
774 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy);
775 OpInfo.Ty = ResultTy;
Chris Lattner660e31d2007-08-24 21:00:35 +0000776 OpInfo.E = E;
777
778 // Expand the binary operator.
779 Value *Result = (this->*Func)(OpInfo);
780
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000781 // Convert the result back to the LHS type.
782 Result = EmitScalarConversion(Result, ResultTy, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000783
784 // Store the result value into the LHS lvalue.
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000785 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000786
Eli Friedmanf9b930c2008-05-25 14:13:57 +0000787 // For bitfields, we need the value in the bitfield
788 // FIXME: This adds an extra bitfield load
789 if (LHSLV.isBitfield())
790 Result = EmitLoadOfLValue(LHSLV, LHSTy);
791
Chris Lattner660e31d2007-08-24 21:00:35 +0000792 return Result;
793}
794
795
Chris Lattner9fba49a2007-08-24 05:35:26 +0000796Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000797 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000798 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000799 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000800 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
801 else
802 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
803}
804
805Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
806 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000807 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000808 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
809 else
810 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
811}
812
813
814Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000815 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000816 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000817
818 // FIXME: What about a pointer to a VLA?
Chris Lattner17c0cb02008-01-03 06:36:51 +0000819 Value *Ptr, *Idx;
820 Expr *IdxExp;
821 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
822 Ptr = Ops.LHS;
823 Idx = Ops.RHS;
824 IdxExp = Ops.E->getRHS();
825 } else { // int + pointer
826 Ptr = Ops.RHS;
827 Idx = Ops.LHS;
828 IdxExp = Ops.E->getLHS();
829 }
830
831 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
832 if (Width < CGF.LLVMPointerWidth) {
833 // Zero or sign extend the pointer value based on whether the index is
834 // signed or not.
835 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +0000836 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +0000837 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
838 else
839 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
840 }
841
842 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000843}
844
845Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
846 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
847 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner660e31d2007-08-24 21:00:35 +0000848
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000849 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
850 // pointer - int
851 Value *Idx = Ops.RHS;
852 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
853 if (Width < CGF.LLVMPointerWidth) {
854 // Zero or sign extend the pointer value based on whether the index is
855 // signed or not.
856 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
857 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
858 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
859 else
860 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
861 }
862 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
863
864 // FIXME: The pointer could point to a VLA.
865 // The GNU void* - int case is automatically handled here because
866 // our LLVM type for void* is i8*.
867 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000868 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000869 // pointer - pointer
870 Value *LHS = Ops.LHS;
871 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +0000872
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000873 const QualType LHSType = Ops.E->getLHS()->getType();
874 const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
875 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +0000876
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000877 // Handle GCC extension for pointer arithmetic on void* types.
878 if (LHSElementType->isVoidType()) {
879 ElementSize = 1;
880 } else {
881 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
882 }
883
884 const llvm::Type *ResultType = ConvertType(Ops.Ty);
885 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
886 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
887 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
888
889 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
890 // remainder. As such, we handle common power-of-two cases here to generate
891 // better code. See PR2247.
892 if (llvm::isPowerOf2_64(ElementSize)) {
893 Value *ShAmt =
894 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
895 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
896 }
897
898 // Otherwise, do a full sdiv.
899 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
900 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000901 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000902}
903
904Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
905 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
906 // RHS to the same size as the LHS.
907 Value *RHS = Ops.RHS;
908 if (Ops.LHS->getType() != RHS->getType())
909 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
910
911 return Builder.CreateShl(Ops.LHS, RHS, "shl");
912}
913
914Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
915 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
916 // RHS to the same size as the LHS.
917 Value *RHS = Ops.RHS;
918 if (Ops.LHS->getType() != RHS->getType())
919 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
920
Chris Lattner660e31d2007-08-24 21:00:35 +0000921 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000922 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
923 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
924}
925
926Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
927 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000928 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000929 QualType LHSTy = E->getLHS()->getType();
Nate Begeman1591bc52008-07-25 20:16:05 +0000930 if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000931 Value *LHS = Visit(E->getLHS());
932 Value *RHS = Visit(E->getRHS());
933
934 if (LHS->getType()->isFloatingPoint()) {
Nate Begeman1591bc52008-07-25 20:16:05 +0000935 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000936 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +0000937 } else if (LHSTy->isSignedIntegerType()) {
938 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000939 LHS, RHS, "cmp");
940 } else {
Eli Friedman850ea372008-05-29 15:09:15 +0000941 // Unsigned integers and pointers.
942 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +0000943 LHS, RHS, "cmp");
944 }
Nate Begeman1591bc52008-07-25 20:16:05 +0000945 } else if (LHSTy->isVectorType()) {
946 Value *LHS = Visit(E->getLHS());
947 Value *RHS = Visit(E->getRHS());
948
949 if (LHS->getType()->isFPOrFPVector()) {
950 Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
951 LHS, RHS, "cmp");
952 } else if (LHSTy->isUnsignedIntegerType()) {
953 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
954 LHS, RHS, "cmp");
955 } else {
956 // Signed integers and pointers.
957 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
958 LHS, RHS, "cmp");
959 }
960 return Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000961 } else {
962 // Complex Comparison: can only be an equality comparison.
963 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
964 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
965
Chris Lattnerc154ac12008-07-26 22:37:01 +0000966 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000967
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000968 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000969 if (CETy->isRealFloatingType()) {
970 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
971 LHS.first, RHS.first, "cmp.r");
972 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
973 LHS.second, RHS.second, "cmp.i");
974 } else {
975 // Complex comparisons can only be equality comparisons. As such, signed
976 // and unsigned opcodes are the same.
977 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
978 LHS.first, RHS.first, "cmp.r");
979 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
980 LHS.second, RHS.second, "cmp.i");
981 }
982
983 if (E->getOpcode() == BinaryOperator::EQ) {
984 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
985 } else {
986 assert(E->getOpcode() == BinaryOperator::NE &&
987 "Complex comparison other than == or != ?");
988 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
989 }
990 }
991
992 // ZExt result to int.
993 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
994}
995
996Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
997 LValue LHS = EmitLValue(E->getLHS());
998 Value *RHS = Visit(E->getRHS());
999
1000 // Store the value into the LHS.
1001 // FIXME: Volatility!
1002 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Eli Friedmanf9b930c2008-05-25 14:13:57 +00001003
1004 // For bitfields, we need the value in the bitfield
1005 // FIXME: This adds an extra bitfield load
1006 if (LHS.isBitfield())
1007 return EmitLoadOfLValue(LHS, E->getLHS()->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001008 // Return the RHS.
1009 return RHS;
1010}
1011
1012Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
1013 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1014
Gabor Greif815e2c12008-04-06 20:42:52 +00001015 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
1016 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001017
1018 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1019 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
1020
1021 CGF.EmitBlock(RHSBlock);
1022 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1023
1024 // Reaquire the RHS block, as there may be subblocks inserted.
1025 RHSBlock = Builder.GetInsertBlock();
1026 CGF.EmitBlock(ContBlock);
1027
1028 // Create a PHI node. If we just evaluted the LHS condition, the result is
1029 // false. If we evaluated both, the result is the RHS condition.
1030 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
1031 PN->reserveOperandSpace(2);
1032 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
1033 PN->addIncoming(RHSCond, RHSBlock);
1034
1035 // ZExt result to int.
1036 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1037}
1038
1039Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
1040 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
1041
Gabor Greif815e2c12008-04-06 20:42:52 +00001042 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
1043 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001044
1045 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
1046 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
1047
1048 CGF.EmitBlock(RHSBlock);
1049 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1050
1051 // Reaquire the RHS block, as there may be subblocks inserted.
1052 RHSBlock = Builder.GetInsertBlock();
1053 CGF.EmitBlock(ContBlock);
1054
1055 // Create a PHI node. If we just evaluted the LHS condition, the result is
1056 // true. If we evaluated both, the result is the RHS condition.
1057 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1058 PN->reserveOperandSpace(2);
1059 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1060 PN->addIncoming(RHSCond, RHSBlock);
1061
1062 // ZExt result to int.
1063 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1064}
1065
1066Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1067 CGF.EmitStmt(E->getLHS());
1068 return Visit(E->getRHS());
1069}
1070
1071//===----------------------------------------------------------------------===//
1072// Other Operators
1073//===----------------------------------------------------------------------===//
1074
1075Value *ScalarExprEmitter::
1076VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +00001077 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1078 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1079 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001080
Chris Lattner98a425c2007-11-26 01:40:58 +00001081 // Evaluate the conditional, then convert it to bool. We do this explicitly
1082 // because we need the unconverted value if this is a GNU ?: expression with
1083 // missing middle value.
1084 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc2126682008-01-03 07:05:49 +00001085 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1086 CGF.getContext().BoolTy);
Chris Lattner98a425c2007-11-26 01:40:58 +00001087 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001088
1089 CGF.EmitBlock(LHSBlock);
1090
1091 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001092 Value *LHS;
1093 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001094 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001095 else // Perform promotions, to handle cases like "short ?: int"
1096 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1097
Chris Lattner9fba49a2007-08-24 05:35:26 +00001098 Builder.CreateBr(ContBlock);
1099 LHSBlock = Builder.GetInsertBlock();
1100
1101 CGF.EmitBlock(RHSBlock);
1102
Eli Friedmance8d7032008-05-16 20:38:39 +00001103 Value *RHS = Visit(E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001104 Builder.CreateBr(ContBlock);
1105 RHSBlock = Builder.GetInsertBlock();
1106
1107 CGF.EmitBlock(ContBlock);
1108
Nuno Lopesb62ff242008-06-04 19:15:45 +00001109 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001110 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1111 return 0;
1112 }
1113
Chris Lattner9fba49a2007-08-24 05:35:26 +00001114 // Create a PHI node for the real part.
1115 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1116 PN->reserveOperandSpace(2);
1117 PN->addIncoming(LHS, LHSBlock);
1118 PN->addIncoming(RHS, RHSBlock);
1119 return PN;
1120}
1121
1122Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001123 // Emit the LHS or RHS as appropriate.
Devang Patel0f2a8fb2007-10-30 20:59:40 +00001124 return
1125 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001126}
1127
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001128Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001129 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
Ted Kremenek2719e982008-06-17 02:43:46 +00001130 E->arg_end(CGF.getContext())).getScalarVal();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001131}
1132
Chris Lattner307da022007-11-30 17:56:23 +00001133Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson36760332007-10-15 20:28:48 +00001134 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1135
1136 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1137 return V;
1138}
1139
Chris Lattner307da022007-11-30 17:56:23 +00001140Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001141 std::string str;
Fariborz Jahanian248db262008-01-22 22:44:46 +00001142 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1143 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1144 EncodingRecordTypes);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001145
1146 llvm::Constant *C = llvm::ConstantArray::get(str);
1147 C = new llvm::GlobalVariable(C->getType(), true,
1148 llvm::GlobalValue::InternalLinkage,
1149 C, ".str", &CGF.CGM.getModule());
1150 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1151 llvm::Constant *Zeros[] = { Zero, Zero };
1152 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1153
1154 return C;
1155}
1156
Chris Lattner9fba49a2007-08-24 05:35:26 +00001157//===----------------------------------------------------------------------===//
1158// Entry Point into this File
1159//===----------------------------------------------------------------------===//
1160
1161/// EmitComplexExpr - Emit the computation of the specified expression of
1162/// complex type, ignoring the result.
1163Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1164 assert(E && !hasAggregateLLVMType(E->getType()) &&
1165 "Invalid scalar expression to emit");
1166
1167 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1168}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001169
1170/// EmitScalarConversion - Emit a conversion from the specified type to the
1171/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001172Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1173 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001174 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1175 "Invalid scalar expression to emit");
1176 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1177}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001178
1179/// EmitComplexToScalarConversion - Emit a conversion from the specified
1180/// complex type to the specified destination type, where the destination
1181/// type is an LLVM scalar type.
1182Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1183 QualType SrcTy,
1184 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001185 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001186 "Invalid complex -> scalar conversion");
1187 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1188 DstTy);
1189}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001190
1191Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1192 assert(V1->getType() == V2->getType() &&
1193 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001194 unsigned NumElements =
1195 cast<llvm::VectorType>(V1->getType())->getNumElements();
1196
1197 va_list va;
1198 va_start(va, V2);
1199
1200 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001201 for (unsigned i = 0; i < NumElements; i++) {
1202 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001203 assert(n >= 0 && n < (int)NumElements * 2 &&
1204 "Vector shuffle index out of bounds!");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001205 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1206 }
1207
1208 const char *Name = va_arg(va, const char *);
1209 va_end(va);
1210
1211 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1212
1213 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1214}
1215
Anders Carlsson68b8be92007-12-15 21:23:30 +00001216llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001217 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001218 llvm::Value *Vec
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001219 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001220
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001221 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001222 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson68b8be92007-12-15 21:23:30 +00001223 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001224 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001225 }
1226
1227 return Vec;
1228}