blob: b47d5787346b0a41fdb98a454898e2261b772cbc [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"
Eli Friedmanccffea92009-01-24 22:38:55 +000018#include "clang/AST/RecordLayout.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattnerd54d1f22008-04-20 00:50:39 +000020#include "clang/Basic/TargetInfo.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000024#include "llvm/Intrinsics.h"
Mike Stumpdb789912009-04-01 20:28:16 +000025#include "llvm/Module.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000026#include "llvm/Support/Compiler.h"
Chris Lattner7f80bb32008-11-12 08:38:24 +000027#include "llvm/Support/CFG.h"
Mike Stumpfca5da02009-02-21 20:00:35 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerc2126682008-01-03 07:05:49 +000029#include <cstdarg>
Ted Kremenek03cf4df2007-12-10 23:44:32 +000030
Chris Lattner9fba49a2007-08-24 05:35:26 +000031using namespace clang;
32using namespace CodeGen;
33using llvm::Value;
34
35//===----------------------------------------------------------------------===//
36// Scalar Expression Emitter
37//===----------------------------------------------------------------------===//
38
39struct BinOpInfo {
40 Value *LHS;
41 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000042 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000043 const BinaryOperator *E;
44};
45
46namespace {
47class VISIBILITY_HIDDEN ScalarExprEmitter
48 : public StmtVisitor<ScalarExprEmitter, Value*> {
49 CodeGenFunction &CGF;
Daniel Dunbard916e6e2008-11-01 01:53:16 +000050 CGBuilderTy &Builder;
Chris Lattnercbfb5512008-03-01 08:45:05 +000051
Chris Lattner9fba49a2007-08-24 05:35:26 +000052public:
53
Chris Lattnercbfb5512008-03-01 08:45:05 +000054 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000055 Builder(CGF.Builder) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000056 }
Chris Lattner9fba49a2007-08-24 05:35:26 +000057
58 //===--------------------------------------------------------------------===//
59 // Utilities
60 //===--------------------------------------------------------------------===//
61
62 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
63 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
64
65 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000066 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000067 }
68
69 /// EmitLoadOfLValue - Given an expression with complex type that represents a
70 /// value l-value, this method emits the address of the l-value, then loads
71 /// and returns the result.
72 Value *EmitLoadOfLValue(const Expr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000073 return EmitLoadOfLValue(EmitLValue(E), E->getType());
74 }
75
Chris Lattnerd8d44222007-08-26 16:42:57 +000076 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000077 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000078 Value *EmitConversionToBool(Value *Src, QualType DstTy);
79
Chris Lattner4e05d1e2007-08-26 06:48:56 +000080 /// EmitScalarConversion - Emit a conversion from the specified type to the
81 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000082 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
83
84 /// EmitComplexToScalarConversion - Emit a conversion from the specified
85 /// complex type to the specified destination type, where the destination
86 /// type is an LLVM scalar type.
87 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
88 QualType SrcTy, QualType DstTy);
Mike Stump4eb81dc2009-02-12 18:29:15 +000089
Chris Lattner9fba49a2007-08-24 05:35:26 +000090 //===--------------------------------------------------------------------===//
91 // Visitor Methods
92 //===--------------------------------------------------------------------===//
93
94 Value *VisitStmt(Stmt *S) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +000095 S->dump(CGF.getContext().getSourceManager());
Chris Lattner9fba49a2007-08-24 05:35:26 +000096 assert(0 && "Stmt can't have complex result type!");
97 return 0;
98 }
99 Value *VisitExpr(Expr *S);
100 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
101
102 // Leaves.
103 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
104 return llvm::ConstantInt::get(E->getValue());
105 }
106 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner70c38672008-04-20 00:45:53 +0000107 return llvm::ConstantFP::get(E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000108 }
109 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
110 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111 }
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000112 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
113 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
114 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000115 Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
116 return llvm::Constant::getNullValue(ConvertType(E->getType()));
117 }
Anders Carlsson774f9c72008-12-21 22:39:40 +0000118 Value *VisitGNUNullExpr(const GNUNullExpr *E) {
119 return llvm::Constant::getNullValue(ConvertType(E->getType()));
120 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000121 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
122 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000123 CGF.getContext().typesAreCompatible(
124 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000125 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000126 Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
Daniel Dunbar879788d2008-08-04 16:51:22 +0000127 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
Daniel Dunbarb5fda0c2008-08-16 01:41:47 +0000128 llvm::Value *V =
129 llvm::ConstantInt::get(llvm::Type::Int32Ty,
130 CGF.GetIDForAddrOfLabel(E->getLabel()));
131
132 return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
Daniel Dunbar879788d2008-08-04 16:51:22 +0000133 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000134
135 // l-values.
136 Value *VisitDeclRefExpr(DeclRefExpr *E) {
137 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
138 return llvm::ConstantInt::get(EC->getInitVal());
139 return EmitLoadOfLValue(E);
140 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000141 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
142 return CGF.EmitObjCSelectorExpr(E);
143 }
144 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
145 return CGF.EmitObjCProtocolExpr(E);
146 }
147 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
148 return EmitLoadOfLValue(E);
149 }
Daniel Dunbar5e105892008-08-23 10:51:21 +0000150 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
Daniel Dunbare6c31752008-08-29 08:11:39 +0000151 return EmitLoadOfLValue(E);
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000152 }
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000153 Value *VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
154 return EmitLoadOfLValue(E);
155 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000156 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
157 return CGF.EmitObjCMessageExpr(E).getScalarVal();
Daniel Dunbar5e105892008-08-23 10:51:21 +0000158 }
159
Chris Lattner9fba49a2007-08-24 05:35:26 +0000160 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand0e9d092008-05-14 19:38:39 +0000161 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000162 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000163 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnera9177982008-10-26 23:53:12 +0000164 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
165 return EmitLoadOfLValue(E);
166 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000167 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
Chris Lattnerc5d32632009-02-24 22:18:39 +0000168 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
169 return EmitLValue(E).getAddress();
170 }
171
Chris Lattner69909292008-08-10 01:53:14 +0000172 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000173
174 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000175 unsigned NumInitElements = E->getNumInits();
176
Douglas Gregor9fddded2009-01-29 19:42:23 +0000177 if (E->hadArrayRangeDesignator()) {
178 CGF.ErrorUnsupported(E, "GNU array range designator extension");
179 }
180
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000181 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000182 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
183
184 // We have a scalar in braces. Just use the first element.
185 if (!VType)
186 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000187
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000188 unsigned NumVectorElements = VType->getNumElements();
189 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000190
191 // Emit individual vector element stores.
192 llvm::Value *V = llvm::UndefValue::get(VType);
193
Anders Carlsson323d5682007-12-18 02:45:33 +0000194 // Emit initializers
195 unsigned i;
196 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000197 Value *NewV = Visit(E->getInit(i));
198 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
199 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000200 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000201
202 // Emit remaining default initializers
203 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
204 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
205 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
206 V = Builder.CreateInsertElement(V, NewV, Idx);
207 }
208
Devang Patel32c39832007-10-24 18:05:48 +0000209 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000210 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000211
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000212 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
213 return llvm::Constant::getNullValue(ConvertType(E->getType()));
214 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000215 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
216 Value *VisitCastExpr(const CastExpr *E) {
217 return EmitCastExpr(E->getSubExpr(), E->getType());
218 }
219 Value *EmitCastExpr(const Expr *E, QualType T);
220
221 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000222 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000223 }
Daniel Dunbara04840b2008-08-23 03:46:30 +0000224
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000225 Value *VisitStmtExpr(const StmtExpr *E);
Mike Stumpfca5da02009-02-21 20:00:35 +0000226
Mike Stump2b6933f2009-02-28 09:07:16 +0000227 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000228
Chris Lattner9fba49a2007-08-24 05:35:26 +0000229 // Unary Operators.
230 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
231 Value *VisitUnaryPostDec(const UnaryOperator *E) {
232 return VisitPrePostIncDec(E, false, false);
233 }
234 Value *VisitUnaryPostInc(const UnaryOperator *E) {
235 return VisitPrePostIncDec(E, true, false);
236 }
237 Value *VisitUnaryPreDec(const UnaryOperator *E) {
238 return VisitPrePostIncDec(E, false, true);
239 }
240 Value *VisitUnaryPreInc(const UnaryOperator *E) {
241 return VisitPrePostIncDec(E, true, true);
242 }
243 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
244 return EmitLValue(E->getSubExpr()).getAddress();
245 }
246 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
247 Value *VisitUnaryPlus(const UnaryOperator *E) {
248 return Visit(E->getSubExpr());
249 }
250 Value *VisitUnaryMinus (const UnaryOperator *E);
251 Value *VisitUnaryNot (const UnaryOperator *E);
252 Value *VisitUnaryLNot (const UnaryOperator *E);
Chris Lattner01211af2007-08-24 21:20:17 +0000253 Value *VisitUnaryReal (const UnaryOperator *E);
254 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000255 Value *VisitUnaryExtension(const UnaryOperator *E) {
256 return Visit(E->getSubExpr());
257 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000258 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000259 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
260 return Visit(DAE->getExpr());
261 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000262
Chris Lattner9fba49a2007-08-24 05:35:26 +0000263 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000264 Value *EmitMul(const BinOpInfo &Ops) {
Mike Stumpdb789912009-04-01 20:28:16 +0000265 if (CGF.getContext().getLangOptions().OverflowChecking)
266 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000267 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
268 }
Mike Stumpdb789912009-04-01 20:28:16 +0000269 /// Create a binary op that checks for overflow.
270 /// Currently only supports +, - and *.
271 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000272 Value *EmitDiv(const BinOpInfo &Ops);
273 Value *EmitRem(const BinOpInfo &Ops);
274 Value *EmitAdd(const BinOpInfo &Ops);
275 Value *EmitSub(const BinOpInfo &Ops);
276 Value *EmitShl(const BinOpInfo &Ops);
277 Value *EmitShr(const BinOpInfo &Ops);
278 Value *EmitAnd(const BinOpInfo &Ops) {
279 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
280 }
281 Value *EmitXor(const BinOpInfo &Ops) {
282 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
283 }
284 Value *EmitOr (const BinOpInfo &Ops) {
285 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
286 }
287
Chris Lattner660e31d2007-08-24 21:00:35 +0000288 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000289 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000290 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
291
292 // Binary operators and binary compound assignment operators.
293#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000294 Value *VisitBin ## OP(const BinaryOperator *E) { \
295 return Emit ## OP(EmitBinOps(E)); \
296 } \
297 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
298 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000299 }
300 HANDLEBINOP(Mul);
301 HANDLEBINOP(Div);
302 HANDLEBINOP(Rem);
303 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000304 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000305 HANDLEBINOP(Shl);
306 HANDLEBINOP(Shr);
307 HANDLEBINOP(And);
308 HANDLEBINOP(Xor);
309 HANDLEBINOP(Or);
310#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000311
Chris Lattner9fba49a2007-08-24 05:35:26 +0000312 // Comparisons.
313 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
314 unsigned SICmpOpc, unsigned FCmpOpc);
315#define VISITCOMP(CODE, UI, SI, FP) \
316 Value *VisitBin##CODE(const BinaryOperator *E) { \
317 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
318 llvm::FCmpInst::FP); }
319 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
320 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
321 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
322 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
323 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
324 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
325#undef VISITCOMP
326
327 Value *VisitBinAssign (const BinaryOperator *E);
328
329 Value *VisitBinLAnd (const BinaryOperator *E);
330 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000331 Value *VisitBinComma (const BinaryOperator *E);
332
333 // Other Operators.
Mike Stump4eb81dc2009-02-12 18:29:15 +0000334 Value *VisitBlockExpr(const BlockExpr *BE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000335 Value *VisitConditionalOperator(const ConditionalOperator *CO);
336 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000337 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000338 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
339 return CGF.EmitObjCStringLiteral(E);
340 }
341};
342} // end anonymous namespace.
343
344//===----------------------------------------------------------------------===//
345// Utilities
346//===----------------------------------------------------------------------===//
347
Chris Lattnerd8d44222007-08-26 16:42:57 +0000348/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000349/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000350Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
351 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
352
353 if (SrcType->isRealFloatingType()) {
354 // Compare against 0.0 for fp scalars.
355 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000356 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
357 }
358
Daniel Dunbar5d54eed2008-08-25 10:38:11 +0000359 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
Chris Lattnerd8d44222007-08-26 16:42:57 +0000360 "Unknown scalar type to convert");
361
362 // Because of the type rules of C, we often end up computing a logical value,
363 // then zero extending it to int, then wanting it as a logical value again.
364 // Optimize this common case.
365 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
366 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
367 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000368 // If there aren't any more uses, zap the instruction to save space.
369 // Note that there can be more uses, for example if this
370 // is the result of an assignment.
371 if (ZI->use_empty())
372 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000373 return Result;
374 }
375 }
376
377 // Compare against an integer or pointer null.
378 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
379 return Builder.CreateICmpNE(Src, Zero, "tobool");
380}
381
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000382/// EmitScalarConversion - Emit a conversion from the specified type to the
383/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000384Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
385 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000386 SrcType = CGF.getContext().getCanonicalType(SrcType);
387 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000388 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000389
390 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000391
392 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000393 if (DstType->isBooleanType())
394 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000395
396 const llvm::Type *DstTy = ConvertType(DstType);
397
398 // Ignore conversions like int -> uint.
399 if (Src->getType() == DstTy)
400 return Src;
401
Daniel Dunbar238335f2008-08-25 09:51:32 +0000402 // Handle pointer conversions next: pointers can only be converted
403 // to/from other pointers and integers. Check for pointer types in
404 // terms of LLVM, as some native types (like Obj-C id) may map to a
405 // pointer type.
406 if (isa<llvm::PointerType>(DstTy)) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000407 // The source value may be an integer, or a pointer.
408 if (isa<llvm::PointerType>(Src->getType()))
409 return Builder.CreateBitCast(Src, DstTy, "conv");
410 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
Eli Friedman35bcec82009-03-04 04:02:35 +0000411 // First, convert to the correct width so that we control the kind of
412 // extension.
413 const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth);
414 bool InputSigned = SrcType->isSignedIntegerType();
415 llvm::Value* IntResult =
416 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
417 // Then, cast to pointer.
418 return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000419 }
420
Daniel Dunbar238335f2008-08-25 09:51:32 +0000421 if (isa<llvm::PointerType>(Src->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000422 // Must be an ptr to int cast.
423 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000424 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000425 }
426
Nate Begemanaf6ed502008-04-18 23:10:10 +0000427 // A scalar can be splatted to an extended vector of the same element type
Nate Begeman7903d052009-01-18 06:42:49 +0000428 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType)) {
429 // Cast the scalar to element type
430 QualType EltTy = DstType->getAsExtVectorType()->getElementType();
431 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
432
433 // Insert the element in element zero of an undef vector
434 llvm::Value *UnV = llvm::UndefValue::get(DstTy);
435 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
436 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
437
438 // Splat the element across to all elements
439 llvm::SmallVector<llvm::Constant*, 16> Args;
440 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
441 for (unsigned i = 0; i < NumElements; i++)
442 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
443
444 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
445 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
446 return Yay;
447 }
Nate Begemanec2d1062007-12-30 02:59:45 +0000448
Chris Lattner4f025a42008-02-02 04:51:41 +0000449 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000450 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000451 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000452 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000453
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000454 // Finally, we have the arithmetic types: real int/float.
455 if (isa<llvm::IntegerType>(Src->getType())) {
456 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000457 if (isa<llvm::IntegerType>(DstTy))
458 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
459 else if (InputSigned)
460 return Builder.CreateSIToFP(Src, DstTy, "conv");
461 else
462 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000463 }
464
465 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
466 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000467 if (DstType->isSignedIntegerType())
468 return Builder.CreateFPToSI(Src, DstTy, "conv");
469 else
470 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000471 }
472
473 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000474 if (DstTy->getTypeID() < Src->getType()->getTypeID())
475 return Builder.CreateFPTrunc(Src, DstTy, "conv");
476 else
477 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000478}
479
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000480/// EmitComplexToScalarConversion - Emit a conversion from the specified
481/// complex type to the specified destination type, where the destination
482/// type is an LLVM scalar type.
483Value *ScalarExprEmitter::
484EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
485 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000486 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000487 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000488
489 // Handle conversions to bool first, they are special: comparisons against 0.
490 if (DstTy->isBooleanType()) {
491 // Complex != 0 -> (Real != 0) | (Imag != 0)
492 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
493 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
494 return Builder.CreateOr(Src.first, Src.second, "tobool");
495 }
496
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000497 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
498 // the imaginary part of the complex value is discarded and the value of the
499 // real part is converted according to the conversion rules for the
500 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000501 return EmitScalarConversion(Src.first, SrcTy, DstTy);
502}
503
504
Chris Lattner9fba49a2007-08-24 05:35:26 +0000505//===----------------------------------------------------------------------===//
506// Visitor Methods
507//===----------------------------------------------------------------------===//
508
509Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000510 CGF.ErrorUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000511 if (E->getType()->isVoidType())
512 return 0;
513 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
514}
515
Eli Friedmand0e9d092008-05-14 19:38:39 +0000516Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
517 llvm::SmallVector<llvm::Constant*, 32> indices;
518 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
519 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
520 }
521 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
522 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
523 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
524 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
525}
526
Chris Lattner9fba49a2007-08-24 05:35:26 +0000527Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
528 // Emit subscript expressions in rvalue context's. For most cases, this just
529 // loads the lvalue formed by the subscript expr. However, we have to be
530 // careful, because the base of a vector subscript is occasionally an rvalue,
531 // so we can't get it as an lvalue.
532 if (!E->getBase()->getType()->isVectorType())
533 return EmitLoadOfLValue(E);
534
535 // Handle the vector case. The base must be a vector, the index must be an
536 // integer value.
537 Value *Base = Visit(E->getBase());
538 Value *Idx = Visit(E->getIdx());
Eli Friedman4a0073b2009-03-28 02:45:41 +0000539 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
Eli Friedmand4531942009-03-28 03:27:06 +0000540 Idx = Builder.CreateIntCast(Idx, llvm::Type::Int32Ty, IdxSigned,
541 "vecidxcast");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000542 return Builder.CreateExtractElement(Base, Idx, "vecext");
543}
544
545/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
546/// also handle things like function to pointer-to-function decay, and array to
547/// pointer decay.
548Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
549 const Expr *Op = E->getSubExpr();
550
551 // If this is due to array->pointer conversion, emit the array expression as
552 // an l-value.
553 if (Op->getType()->isArrayType()) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000554 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Eli Friedman8fef47e2008-12-20 23:11:59 +0000555
Eli Friedman4a0073b2009-03-28 02:45:41 +0000556 // Note that VLA pointers are always decayed, so we don't need to do
557 // anything here.
Eli Friedman8fef47e2008-12-20 23:11:59 +0000558 if (!Op->getType()->isVariableArrayType()) {
559 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
560 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
561 ->getElementType()) &&
562 "Expected pointer to array");
563 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Daniel Dunbar952f4732008-08-29 17:28:43 +0000564 }
Chris Lattnere54443b2007-12-12 04:13:20 +0000565
566 // The resultant pointer type can be implicitly casted to other pointer
Chris Lattner3b8f5c62008-07-23 06:31:27 +0000567 // types as well (e.g. void*) and can be implicitly converted to integer.
568 const llvm::Type *DestTy = ConvertType(E->getType());
569 if (V->getType() != DestTy) {
570 if (isa<llvm::PointerType>(DestTy))
571 V = Builder.CreateBitCast(V, DestTy, "ptrconv");
572 else {
573 assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay");
574 V = Builder.CreatePtrToInt(V, DestTy, "ptrconv");
575 }
576 }
Chris Lattnere54443b2007-12-12 04:13:20 +0000577 return V;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000578 }
Eli Friedman4a0073b2009-03-28 02:45:41 +0000579
Chris Lattner9fba49a2007-08-24 05:35:26 +0000580 return EmitCastExpr(Op, E->getType());
581}
582
583
584// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
585// have to handle a more broad range of conversions than explicit casts, as they
586// handle things like function to ptr-to-function decay etc.
587Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000588 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000589
590 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000591 Value *Src = Visit(const_cast<Expr*>(E));
592
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000593 // Use EmitScalarConversion to perform the conversion.
594 return EmitScalarConversion(Src, E->getType(), DestTy);
595 }
Chris Lattner77288792008-02-16 23:55:16 +0000596
Chris Lattnerde0908b2008-04-04 16:54:41 +0000597 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000598 // Handle cases where the source is a complex type.
599 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
600 DestTy);
601 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000602
Chris Lattner77288792008-02-16 23:55:16 +0000603 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
604 // evaluate the result and return.
605 CGF.EmitAggExpr(E, 0, false);
606 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000607}
608
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000609Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000610 return CGF.EmitCompoundStmt(*E->getSubStmt(),
611 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000612}
613
Mike Stump2b6933f2009-02-28 09:07:16 +0000614Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
615 return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
Mike Stumpfca5da02009-02-21 20:00:35 +0000616}
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000617
Chris Lattner9fba49a2007-08-24 05:35:26 +0000618//===----------------------------------------------------------------------===//
619// Unary Operators
620//===----------------------------------------------------------------------===//
621
622Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000623 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000624 LValue LV = EmitLValue(E->getSubExpr());
Eli Friedman6a259872009-03-23 03:00:06 +0000625 QualType ValTy = E->getSubExpr()->getType();
626 Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000627
628 int AmountVal = isInc ? 1 : -1;
Eli Friedman4a0073b2009-03-28 02:45:41 +0000629
630 if (ValTy->isPointerType() &&
631 ValTy->getAsPointerType()->isVariableArrayType()) {
632 // The amount of the addition/subtraction needs to account for the VLA size
633 CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
634 }
635
Chris Lattner9fba49a2007-08-24 05:35:26 +0000636 Value *NextVal;
Chris Lattner8360c612009-03-18 04:25:13 +0000637 if (const llvm::PointerType *PT =
638 dyn_cast<llvm::PointerType>(InVal->getType())) {
Chris Lattner8360c612009-03-18 04:25:13 +0000639 llvm::Constant *Inc =llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
640 if (!isa<llvm::FunctionType>(PT->getElementType())) {
641 NextVal = Builder.CreateGEP(InVal, Inc, "ptrincdec");
642 } else {
643 const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
644 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
645 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
646 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
647 }
Chris Lattner49083172009-02-11 07:40:06 +0000648 } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) {
649 // Bool++ is an interesting case, due to promotion rules, we get:
650 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
651 // Bool = ((int)Bool+1) != 0
652 // An interesting aspect of this is that increment is always true.
653 // Decrement does not have this property.
654 NextVal = llvm::ConstantInt::getTrue();
Chris Lattner0dc11f62007-08-26 05:10:16 +0000655 } else {
656 // Add the inc/dec to the real part.
657 if (isa<llvm::IntegerType>(InVal->getType()))
658 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000659 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000660 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000661 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000662 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000663 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000664 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000665 else {
666 llvm::APFloat F(static_cast<float>(AmountVal));
Dale Johannesen2461f612008-10-09 23:02:32 +0000667 bool ignored;
668 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
669 &ignored);
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000670 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000671 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000672 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
673 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000674
675 // Store the updated result through the lvalue.
Eli Friedman6a259872009-03-23 03:00:06 +0000676 if (LV.isBitfield())
677 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
678 &NextVal);
679 else
680 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000681
682 // If this is a postinc, return the value read from memory, otherwise use the
683 // updated value.
684 return isPre ? NextVal : InVal;
685}
686
687
688Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
689 Value *Op = Visit(E->getSubExpr());
690 return Builder.CreateNeg(Op, "neg");
691}
692
693Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
694 Value *Op = Visit(E->getSubExpr());
695 return Builder.CreateNot(Op, "neg");
696}
697
698Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
699 // Compare operand to zero.
700 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
701
702 // Invert value.
703 // TODO: Could dynamically modify easy computations here. For example, if
704 // the operand is an icmp ne, turn into icmp eq.
705 BoolVal = Builder.CreateNot(BoolVal, "lnot");
706
707 // ZExt result to int.
708 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
709}
710
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000711/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
712/// argument of the sizeof expression as an integer.
713Value *
714ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000715 QualType TypeToSize = E->getTypeOfArgument();
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000716 if (E->isSizeOf()) {
717 if (const VariableArrayType *VAT =
718 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
719 if (E->isArgumentType()) {
720 // sizeof(type) - make sure to emit the VLA size.
721 CGF.EmitVLASize(TypeToSize);
722 }
Anders Carlssond309f572009-01-30 16:41:04 +0000723
Anders Carlsson8f30de92009-02-05 19:43:10 +0000724 return CGF.GetVLASize(VAT);
Anders Carlsson6cb99b72008-12-21 03:33:21 +0000725 }
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000726 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000727
728 // If this isn't sizeof(vla), the result must be constant; use the
729 // constant folding logic so we don't have to duplicate it here.
730 Expr::EvalResult Result;
731 E->Evaluate(Result, CGF.getContext());
732 return llvm::ConstantInt::get(Result.Val.getInt());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000733}
734
Chris Lattner01211af2007-08-24 21:20:17 +0000735Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
736 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000737 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000738 return CGF.EmitComplexExpr(Op).first;
739 return Visit(Op);
740}
741Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
742 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000743 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000744 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000745
746 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
747 // effects are evaluated.
748 CGF.EmitScalarExpr(Op);
749 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000750}
751
Anders Carlsson52774ad2008-01-29 15:56:48 +0000752Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
753{
Eli Friedman342d9432009-02-27 06:44:11 +0000754 Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
Eli Friedmanccffea92009-01-24 22:38:55 +0000755 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedman342d9432009-02-27 06:44:11 +0000756 return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
Anders Carlsson52774ad2008-01-29 15:56:48 +0000757}
Chris Lattner01211af2007-08-24 21:20:17 +0000758
Chris Lattner9fba49a2007-08-24 05:35:26 +0000759//===----------------------------------------------------------------------===//
760// Binary Operators
761//===----------------------------------------------------------------------===//
762
763BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
764 BinOpInfo Result;
765 Result.LHS = Visit(E->getLHS());
766 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000767 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000768 Result.E = E;
769 return Result;
770}
771
Chris Lattner0d965302007-08-26 21:41:21 +0000772Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000773 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
774 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
775
776 BinOpInfo OpInfo;
777
Eli Friedman3cd92882009-03-28 01:22:36 +0000778 if (E->getComputationResultType()->isAnyComplexType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +0000779 // This needs to go through the complex expression emitter, but
Eli Friedman3cd92882009-03-28 01:22:36 +0000780 // it's a tad complicated to do that... I'm leaving it out for now.
781 // (Note that we do actually need the imaginary part of the RHS for
782 // multiplication and division.)
783 CGF.ErrorUnsupported(E, "complex compound assignment");
784 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
785 }
786
787 // Load/convert the LHS.
Chris Lattner660e31d2007-08-24 21:00:35 +0000788 LValue LHSLV = EmitLValue(E->getLHS());
789 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Eli Friedman3cd92882009-03-28 01:22:36 +0000790 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
791 E->getComputationLHSType());
792 // Emit the RHS.
793 OpInfo.RHS = Visit(E->getRHS());
794 OpInfo.Ty = E->getComputationResultType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000795 OpInfo.E = E;
796
797 // Expand the binary operator.
798 Value *Result = (this->*Func)(OpInfo);
799
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000800 // Convert the result back to the LHS type.
Eli Friedman3cd92882009-03-28 01:22:36 +0000801 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
802
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000803 // Store the result value into the LHS lvalue. Bit-fields are
Daniel Dunbar2710fc92008-11-19 11:54:05 +0000804 // handled specially because the result is altered by the store,
805 // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
806 // the left operand after the assignment...'.
Eli Friedmanf9b930c2008-05-25 14:13:57 +0000807 if (LHSLV.isBitfield())
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000808 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
809 &Result);
810 else
811 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
812
Chris Lattner660e31d2007-08-24 21:00:35 +0000813 return Result;
814}
815
816
Chris Lattner9fba49a2007-08-24 05:35:26 +0000817Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000818 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000819 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000820 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000821 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
822 else
823 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
824}
825
826Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
827 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000828 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000829 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
830 else
831 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
832}
833
Mike Stumpdb789912009-04-01 20:28:16 +0000834Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
835 unsigned IID;
836 unsigned OpID = 0;
837 if (Ops.Ty->isSignedIntegerType()) {
838 switch (Ops.E->getOpcode()) {
839 case BinaryOperator::Add:
840 OpID = 1;
841 IID = llvm::Intrinsic::sadd_with_overflow;
842 break;
843 case BinaryOperator::Sub:
844 OpID = 2;
845 IID = llvm::Intrinsic::ssub_with_overflow;
846 break;
847 case BinaryOperator::Mul:
848 OpID = 3;
849 IID = llvm::Intrinsic::smul_with_overflow;
850 break;
851 default:
852 assert(false && "Unsupported operation for overflow detection");
853 }
854 OpID <<= 1;
855 OpID |= 1;
856 }
857 else {
858 assert(Ops.Ty->isUnsignedIntegerType() &&
859 "Must be either a signed or unsigned integer op");
860 switch (Ops.E->getOpcode()) {
861 case BinaryOperator::Add:
862 OpID = 1;
863 IID = llvm::Intrinsic::uadd_with_overflow;
864 break;
865 case BinaryOperator::Sub:
866 OpID = 2;
867 IID = llvm::Intrinsic::usub_with_overflow;
868 break;
869 case BinaryOperator::Mul:
870 OpID = 3;
871 IID = llvm::Intrinsic::umul_with_overflow;
872 break;
873 default:
874 assert(false && "Unsupported operation for overflow detection");
875 }
876 OpID <<= 1;
877 }
878 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
879
880 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
881
882 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
883 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
884 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
885
886 // Branch in case of overflow.
887 llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
888 llvm::BasicBlock *overflowBB =
889 CGF.createBasicBlock("overflow", CGF.CurFn);
890 llvm::BasicBlock *continueBB =
891 CGF.createBasicBlock("overflow.continue", CGF.CurFn);
892
893 Builder.CreateCondBr(overflow, overflowBB, continueBB);
894
895 // Handle overflow
896
897 Builder.SetInsertPoint(overflowBB);
898
899 // Handler is:
900 // long long *__overflow_handler)(long long a, long long b, char op,
901 // char width)
902 std::vector<const llvm::Type*> handerArgTypes;
903 handerArgTypes.push_back(llvm::Type::Int64Ty);
904 handerArgTypes.push_back(llvm::Type::Int64Ty);
905 handerArgTypes.push_back(llvm::Type::Int8Ty);
906 handerArgTypes.push_back(llvm::Type::Int8Ty);
907 llvm::FunctionType *handlerTy = llvm::FunctionType::get(llvm::Type::Int64Ty,
908 handerArgTypes, false);
909 llvm::Value *handlerFunction =
910 CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
911 llvm::PointerType::getUnqual(handlerTy));
912 handlerFunction = Builder.CreateLoad(handlerFunction);
913
914 llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
915 Builder.CreateSExt(Ops.LHS, llvm::Type::Int64Ty),
916 Builder.CreateSExt(Ops.RHS, llvm::Type::Int64Ty),
917 llvm::ConstantInt::get(llvm::Type::Int8Ty, OpID),
918 llvm::ConstantInt::get(llvm::Type::Int8Ty,
919 cast<llvm::IntegerType>(opTy)->getBitWidth()));
920
921 handlerResult = Builder.CreateTrunc(handlerResult, opTy);
922
923 Builder.CreateBr(continueBB);
924
925 // Set up the continuation
926 Builder.SetInsertPoint(continueBB);
927 // Get the correct result
928 llvm::PHINode *phi = Builder.CreatePHI(opTy);
929 phi->reserveOperandSpace(2);
930 phi->addIncoming(result, initialBB);
931 phi->addIncoming(handlerResult, overflowBB);
932
933 return phi;
934}
Chris Lattner9fba49a2007-08-24 05:35:26 +0000935
936Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Mike Stumpdb789912009-04-01 20:28:16 +0000937 if (!Ops.Ty->isPointerType()) {
938 if (CGF.getContext().getLangOptions().OverflowChecking)
939 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000940 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Mike Stumpdb789912009-04-01 20:28:16 +0000941 }
Eli Friedman4a0073b2009-03-28 02:45:41 +0000942
943 if (Ops.Ty->getAsPointerType()->isVariableArrayType()) {
944 // The amount of the addition needs to account for the VLA size
945 CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
946 }
Chris Lattner17c0cb02008-01-03 06:36:51 +0000947 Value *Ptr, *Idx;
948 Expr *IdxExp;
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +0000949 const PointerType *PT;
950 if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
Chris Lattner17c0cb02008-01-03 06:36:51 +0000951 Ptr = Ops.LHS;
952 Idx = Ops.RHS;
953 IdxExp = Ops.E->getRHS();
954 } else { // int + pointer
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +0000955 PT = Ops.E->getRHS()->getType()->getAsPointerType();
956 assert(PT && "Invalid add expr");
Chris Lattner17c0cb02008-01-03 06:36:51 +0000957 Ptr = Ops.RHS;
958 Idx = Ops.LHS;
959 IdxExp = Ops.E->getLHS();
960 }
961
962 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
963 if (Width < CGF.LLVMPointerWidth) {
964 // Zero or sign extend the pointer value based on whether the index is
965 // signed or not.
966 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +0000967 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +0000968 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
969 else
970 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
971 }
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +0000972
973 // Explicitly handle GNU void* and function pointer arithmetic
974 // extensions. The GNU void* casts amount to no-ops since our void*
975 // type is i8*, but this is future proof.
976 const QualType ElementType = PT->getPointeeType();
977 if (ElementType->isVoidType() || ElementType->isFunctionType()) {
978 const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
979 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
980 Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
981 return Builder.CreateBitCast(Res, Ptr->getType());
982 }
Chris Lattner17c0cb02008-01-03 06:36:51 +0000983
984 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000985}
986
987Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
Mike Stumpdb789912009-04-01 20:28:16 +0000988 if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
989 if (CGF.getContext().getLangOptions().OverflowChecking)
990 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000991 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Mike Stumpdb789912009-04-01 20:28:16 +0000992 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000993
Eli Friedman4a0073b2009-03-28 02:45:41 +0000994 if (Ops.E->getLHS()->getType()->getAsPointerType()->isVariableArrayType()) {
995 // The amount of the addition needs to account for the VLA size for
996 // ptr-int
997 // The amount of the division needs to account for the VLA size for
998 // ptr-ptr.
999 CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
1000 }
1001
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001002 const QualType LHSType = Ops.E->getLHS()->getType();
1003 const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001004 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
1005 // pointer - int
1006 Value *Idx = Ops.RHS;
1007 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
1008 if (Width < CGF.LLVMPointerWidth) {
1009 // Zero or sign extend the pointer value based on whether the index is
1010 // signed or not.
1011 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
1012 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1013 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1014 else
1015 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1016 }
1017 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001018
1019 // Explicitly handle GNU void* and function pointer arithmetic
1020 // extensions. The GNU void* casts amount to no-ops since our
1021 // void* type is i8*, but this is future proof.
1022 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
1023 const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1024 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1025 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1026 return Builder.CreateBitCast(Res, Ops.LHS->getType());
1027 }
1028
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001029 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001030 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001031 // pointer - pointer
1032 Value *LHS = Ops.LHS;
1033 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +00001034
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001035 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001036
Chris Lattner6d2e3492009-02-11 07:21:43 +00001037 // Handle GCC extension for pointer arithmetic on void* and function pointer
1038 // types.
1039 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001040 ElementSize = 1;
1041 } else {
1042 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1043 }
1044
1045 const llvm::Type *ResultType = ConvertType(Ops.Ty);
1046 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1047 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1048 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
1049
Chris Lattner6d2e3492009-02-11 07:21:43 +00001050 // Optimize out the shift for element size of 1.
1051 if (ElementSize == 1)
1052 return BytesBetween;
1053
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001054 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
1055 // remainder. As such, we handle common power-of-two cases here to generate
1056 // better code. See PR2247.
1057 if (llvm::isPowerOf2_64(ElementSize)) {
1058 Value *ShAmt =
1059 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
1060 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
1061 }
1062
1063 // Otherwise, do a full sdiv.
1064 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
1065 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001066 }
Chris Lattner9fba49a2007-08-24 05:35:26 +00001067}
1068
1069Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
1070 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1071 // RHS to the same size as the LHS.
1072 Value *RHS = Ops.RHS;
1073 if (Ops.LHS->getType() != RHS->getType())
1074 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1075
1076 return Builder.CreateShl(Ops.LHS, RHS, "shl");
1077}
1078
1079Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1080 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1081 // RHS to the same size as the LHS.
1082 Value *RHS = Ops.RHS;
1083 if (Ops.LHS->getType() != RHS->getType())
1084 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1085
Chris Lattner660e31d2007-08-24 21:00:35 +00001086 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +00001087 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1088 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1089}
1090
1091Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1092 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001093 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001094 QualType LHSTy = E->getLHS()->getType();
Nate Begeman1591bc52008-07-25 20:16:05 +00001095 if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001096 Value *LHS = Visit(E->getLHS());
1097 Value *RHS = Visit(E->getRHS());
1098
1099 if (LHS->getType()->isFloatingPoint()) {
Nate Begeman1591bc52008-07-25 20:16:05 +00001100 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001101 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +00001102 } else if (LHSTy->isSignedIntegerType()) {
1103 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001104 LHS, RHS, "cmp");
1105 } else {
Eli Friedman850ea372008-05-29 15:09:15 +00001106 // Unsigned integers and pointers.
1107 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001108 LHS, RHS, "cmp");
1109 }
Nate Begeman1591bc52008-07-25 20:16:05 +00001110 } else if (LHSTy->isVectorType()) {
1111 Value *LHS = Visit(E->getLHS());
1112 Value *RHS = Visit(E->getRHS());
1113
1114 if (LHS->getType()->isFPOrFPVector()) {
1115 Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc,
1116 LHS, RHS, "cmp");
1117 } else if (LHSTy->isUnsignedIntegerType()) {
1118 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc,
1119 LHS, RHS, "cmp");
1120 } else {
1121 // Signed integers and pointers.
1122 Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc,
1123 LHS, RHS, "cmp");
1124 }
1125 return Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001126 } else {
1127 // Complex Comparison: can only be an equality comparison.
1128 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1129 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1130
Chris Lattnerc154ac12008-07-26 22:37:01 +00001131 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001132
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001133 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001134 if (CETy->isRealFloatingType()) {
1135 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1136 LHS.first, RHS.first, "cmp.r");
1137 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1138 LHS.second, RHS.second, "cmp.i");
1139 } else {
1140 // Complex comparisons can only be equality comparisons. As such, signed
1141 // and unsigned opcodes are the same.
1142 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1143 LHS.first, RHS.first, "cmp.r");
1144 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1145 LHS.second, RHS.second, "cmp.i");
1146 }
1147
1148 if (E->getOpcode() == BinaryOperator::EQ) {
1149 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1150 } else {
1151 assert(E->getOpcode() == BinaryOperator::NE &&
1152 "Complex comparison other than == or != ?");
1153 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1154 }
1155 }
Nuno Lopes92577002009-01-11 23:22:37 +00001156
1157 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001158}
1159
1160Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1161 LValue LHS = EmitLValue(E->getLHS());
1162 Value *RHS = Visit(E->getRHS());
1163
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001164 // Store the value into the LHS. Bit-fields are handled specially
Daniel Dunbar2710fc92008-11-19 11:54:05 +00001165 // because the result is altered by the store, i.e., [C99 6.5.16p1]
1166 // 'An assignment expression has the value of the left operand after
Eli Friedman4a0073b2009-03-28 02:45:41 +00001167 // the assignment...'.
Eli Friedmanf9b930c2008-05-25 14:13:57 +00001168 if (LHS.isBitfield())
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001169 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1170 &RHS);
1171 else
1172 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Daniel Dunbare6c31752008-08-29 08:11:39 +00001173
Chris Lattner9fba49a2007-08-24 05:35:26 +00001174 // Return the RHS.
1175 return RHS;
1176}
1177
1178Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001179 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1180 // If we have 1 && X, just emit X without inserting the control flow.
1181 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1182 if (Cond == 1) { // If we have 1 && X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001183 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1184 // ZExt result to int.
1185 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1186 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001187
1188 // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1189 if (!CGF.ContainsLabel(E->getRHS()))
1190 return llvm::Constant::getNullValue(CGF.LLVMIntTy);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001191 }
1192
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001193 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1194 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
Chris Lattner715c2a72008-11-12 08:26:50 +00001195
Chris Lattner7f80bb32008-11-12 08:38:24 +00001196 // Branch on the LHS first. If it is false, go to the failure (cont) block.
1197 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1198
1199 // Any edges into the ContBlock are now from an (indeterminate number of)
1200 // edges from this first condition. All of these values will be false. Start
1201 // setting up the PHI node in the Cont Block for this.
1202 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1203 PN->reserveOperandSpace(2); // Normal case, two inputs.
1204 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1205 PI != PE; ++PI)
1206 PN->addIncoming(llvm::ConstantInt::getFalse(), *PI);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001207
1208 CGF.EmitBlock(RHSBlock);
1209 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1210
1211 // Reaquire the RHS block, as there may be subblocks inserted.
1212 RHSBlock = Builder.GetInsertBlock();
Chris Lattner7f80bb32008-11-12 08:38:24 +00001213
1214 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1215 // into the phi node for the edge with the value of RHSCond.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001216 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001217 PN->addIncoming(RHSCond, RHSBlock);
1218
1219 // ZExt result to int.
1220 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1221}
1222
1223Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001224 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1225 // If we have 0 || X, just emit X without inserting the control flow.
1226 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1227 if (Cond == -1) { // If we have 0 || X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001228 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1229 // ZExt result to int.
1230 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1231 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001232
Eli Friedmanea137cd2008-12-02 16:02:46 +00001233 // 1 || RHS: If it is safe, just elide the RHS, and return 1.
Chris Lattner715c2a72008-11-12 08:26:50 +00001234 if (!CGF.ContainsLabel(E->getRHS()))
Eli Friedmanea137cd2008-12-02 16:02:46 +00001235 return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001236 }
1237
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001238 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1239 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001240
Chris Lattner7f80bb32008-11-12 08:38:24 +00001241 // Branch on the LHS first. If it is true, go to the success (cont) block.
1242 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1243
1244 // Any edges into the ContBlock are now from an (indeterminate number of)
1245 // edges from this first condition. All of these values will be true. Start
1246 // setting up the PHI node in the Cont Block for this.
1247 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock);
1248 PN->reserveOperandSpace(2); // Normal case, two inputs.
1249 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1250 PI != PE; ++PI)
1251 PN->addIncoming(llvm::ConstantInt::getTrue(), *PI);
1252
1253 // Emit the RHS condition as a bool value.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001254 CGF.EmitBlock(RHSBlock);
1255 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1256
1257 // Reaquire the RHS block, as there may be subblocks inserted.
1258 RHSBlock = Builder.GetInsertBlock();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001259
Chris Lattner7f80bb32008-11-12 08:38:24 +00001260 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1261 // into the phi node for the edge with the value of RHSCond.
1262 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001263 PN->addIncoming(RHSCond, RHSBlock);
1264
1265 // ZExt result to int.
1266 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1267}
1268
1269Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1270 CGF.EmitStmt(E->getLHS());
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00001271 CGF.EnsureInsertPoint();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001272 return Visit(E->getRHS());
1273}
1274
1275//===----------------------------------------------------------------------===//
1276// Other Operators
1277//===----------------------------------------------------------------------===//
1278
Chris Lattner504a5282008-11-12 08:55:54 +00001279/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1280/// expression is cheap enough and side-effect-free enough to evaluate
1281/// unconditionally instead of conditionally. This is used to convert control
1282/// flow into selects in some cases.
1283static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1284 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1285 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1286
1287 // TODO: Allow anything we can constant fold to an integer or fp constant.
1288 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1289 isa<FloatingLiteral>(E))
1290 return true;
1291
1292 // Non-volatile automatic variables too, to get "cond ? X : Y" where
1293 // X and Y are local variables.
1294 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1295 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1296 if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1297 return true;
1298
1299 return false;
1300}
1301
1302
Chris Lattner9fba49a2007-08-24 05:35:26 +00001303Value *ScalarExprEmitter::
1304VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattner3d6606b2008-11-12 08:04:58 +00001305 // If the condition constant folds and can be elided, try to avoid emitting
1306 // the condition and the dead arm.
1307 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
Chris Lattner044bffc2008-11-11 18:56:45 +00001308 Expr *Live = E->getLHS(), *Dead = E->getRHS();
Chris Lattner3d6606b2008-11-12 08:04:58 +00001309 if (Cond == -1)
Chris Lattner044bffc2008-11-11 18:56:45 +00001310 std::swap(Live, Dead);
Chris Lattner3d6606b2008-11-12 08:04:58 +00001311
1312 // If the dead side doesn't have labels we need, and if the Live side isn't
1313 // the gnu missing ?: extension (which we could handle, but don't bother
1314 // to), just emit the Live part.
1315 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part
1316 Live) // Live part isn't missing.
1317 return Visit(Live);
Chris Lattner044bffc2008-11-11 18:56:45 +00001318 }
1319
Chris Lattner504a5282008-11-12 08:55:54 +00001320
1321 // If this is a really simple expression (like x ? 4 : 5), emit this as a
1322 // select instead of as control flow. We can only do this if it is cheap and
Chris Lattner1f11af22008-11-16 06:16:27 +00001323 // safe to evaluate the LHS and RHS unconditionally.
Chris Lattner504a5282008-11-12 08:55:54 +00001324 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1325 isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1326 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1327 llvm::Value *LHS = Visit(E->getLHS());
1328 llvm::Value *RHS = Visit(E->getRHS());
1329 return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1330 }
1331
1332
Daniel Dunbarb23e9922008-11-12 10:13:37 +00001333 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1334 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001335 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner67e22462008-11-12 08:08:13 +00001336 Value *CondVal = 0;
Chris Lattner3d6606b2008-11-12 08:04:58 +00001337
Chris Lattner86031712009-02-13 23:35:32 +00001338 // If we don't have the GNU missing condition extension, emit a branch on
1339 // bool the normal way.
1340 if (E->getLHS()) {
1341 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1342 // the branch on bool.
1343 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1344 } else {
1345 // Otherwise, for the ?: extension, evaluate the conditional and then
1346 // convert it to bool the hard way. We do this explicitly because we need
1347 // the unconverted value for the missing middle value of the ?:.
Chris Lattner67e22462008-11-12 08:08:13 +00001348 CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattner86031712009-02-13 23:35:32 +00001349
1350 // In some cases, EmitScalarConversion will delete the "CondVal" expression
1351 // if there are no extra uses (an optimization). Inhibit this by making an
1352 // extra dead use, because we're going to add a use of CondVal later. We
1353 // don't use the builder for this, because we don't want it to get optimized
1354 // away. This leaves dead code, but the ?: extension isn't common.
1355 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1356 Builder.GetInsertBlock());
1357
Chris Lattner67e22462008-11-12 08:08:13 +00001358 Value *CondBoolVal =
1359 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1360 CGF.getContext().BoolTy);
1361 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner67e22462008-11-12 08:08:13 +00001362 }
Chris Lattner9fba49a2007-08-24 05:35:26 +00001363
1364 CGF.EmitBlock(LHSBlock);
1365
1366 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001367 Value *LHS;
1368 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001369 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001370 else // Perform promotions, to handle cases like "short ?: int"
1371 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1372
Chris Lattner9fba49a2007-08-24 05:35:26 +00001373 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001374 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001375
1376 CGF.EmitBlock(RHSBlock);
1377
Eli Friedmance8d7032008-05-16 20:38:39 +00001378 Value *RHS = Visit(E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001379 RHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001380 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001381
1382 CGF.EmitBlock(ContBlock);
1383
Nuno Lopesb62ff242008-06-04 19:15:45 +00001384 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001385 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1386 return 0;
1387 }
1388
Chris Lattner9fba49a2007-08-24 05:35:26 +00001389 // Create a PHI node for the real part.
1390 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1391 PN->reserveOperandSpace(2);
1392 PN->addIncoming(LHS, LHSBlock);
1393 PN->addIncoming(RHS, RHSBlock);
1394 return PN;
1395}
1396
1397Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmand540c112009-03-04 05:52:32 +00001398 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattner9fba49a2007-08-24 05:35:26 +00001399}
1400
Chris Lattner307da022007-11-30 17:56:23 +00001401Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Eli Friedman8f5e8782009-01-20 17:46:04 +00001402 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +00001403 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1404
1405 // If EmitVAArg fails, we fall back to the LLVM instruction.
1406 if (!ArgPtr)
1407 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1408
Anders Carlsson285611e2008-11-04 05:30:00 +00001409 return Builder.CreateLoad(ArgPtr);
Anders Carlsson36760332007-10-15 20:28:48 +00001410}
1411
Mike Stump4eb81dc2009-02-12 18:29:15 +00001412Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
Mike Stump1fa52fe2009-03-07 02:35:30 +00001413 return CGF.BuildBlockLiteralTmp(BE);
Mike Stump4eb81dc2009-02-12 18:29:15 +00001414}
1415
Chris Lattner9fba49a2007-08-24 05:35:26 +00001416//===----------------------------------------------------------------------===//
1417// Entry Point into this File
1418//===----------------------------------------------------------------------===//
1419
1420/// EmitComplexExpr - Emit the computation of the specified expression of
1421/// complex type, ignoring the result.
1422Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1423 assert(E && !hasAggregateLLVMType(E->getType()) &&
1424 "Invalid scalar expression to emit");
1425
1426 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1427}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001428
1429/// EmitScalarConversion - Emit a conversion from the specified type to the
1430/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001431Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1432 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001433 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1434 "Invalid scalar expression to emit");
1435 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1436}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001437
1438/// EmitComplexToScalarConversion - Emit a conversion from the specified
1439/// complex type to the specified destination type, where the destination
1440/// type is an LLVM scalar type.
1441Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1442 QualType SrcTy,
1443 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001444 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001445 "Invalid complex -> scalar conversion");
1446 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1447 DstTy);
1448}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001449
1450Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1451 assert(V1->getType() == V2->getType() &&
1452 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001453 unsigned NumElements =
1454 cast<llvm::VectorType>(V1->getType())->getNumElements();
1455
1456 va_list va;
1457 va_start(va, V2);
1458
1459 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001460 for (unsigned i = 0; i < NumElements; i++) {
1461 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001462 assert(n >= 0 && n < (int)NumElements * 2 &&
1463 "Vector shuffle index out of bounds!");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001464 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1465 }
1466
1467 const char *Name = va_arg(va, const char *);
1468 va_end(va);
1469
1470 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1471
1472 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1473}
1474
Anders Carlsson68b8be92007-12-15 21:23:30 +00001475llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001476 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001477 llvm::Value *Vec
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001478 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001479
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001480 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001481 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson68b8be92007-12-15 21:23:30 +00001482 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001483 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001484 }
1485
1486 return Vec;
1487}