blob: 4fb69d045e3ee452cad9bd571ab5c3c1800d9698 [file] [log] [blame]
Chris Lattner9fba49a2007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9fba49a2007-08-24 05:35:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/AST/AST.h"
Chris Lattnerd54d1f22008-04-20 00:50:39 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000021#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerc2126682008-01-03 07:05:49 +000023#include <cstdarg>
Ted Kremenek03cf4df2007-12-10 23:44:32 +000024
Chris Lattner9fba49a2007-08-24 05:35:26 +000025using namespace clang;
26using namespace CodeGen;
27using llvm::Value;
28
29//===----------------------------------------------------------------------===//
30// Scalar Expression Emitter
31//===----------------------------------------------------------------------===//
32
33struct BinOpInfo {
34 Value *LHS;
35 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000036 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000037 const BinaryOperator *E;
38};
39
40namespace {
41class VISIBILITY_HIDDEN ScalarExprEmitter
42 : public StmtVisitor<ScalarExprEmitter, Value*> {
43 CodeGenFunction &CGF;
Chris Lattner676bf212008-04-13 07:32:11 +000044 llvm::IRBuilder &Builder;
Chris Lattnercbfb5512008-03-01 08:45:05 +000045 CGObjCRuntime *Runtime;
46
Chris Lattner9fba49a2007-08-24 05:35:26 +000047public:
48
Chris Lattnercbfb5512008-03-01 08:45:05 +000049 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
50 Builder(CGF.Builder),
51 Runtime(CGF.CGM.getObjCRuntime()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000052 }
Chris Lattner9fba49a2007-08-24 05:35:26 +000053
54 //===--------------------------------------------------------------------===//
55 // Utilities
56 //===--------------------------------------------------------------------===//
57
58 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
59 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
60
61 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000062 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000063 }
64
65 /// EmitLoadOfLValue - Given an expression with complex type that represents a
66 /// value l-value, this method emits the address of the l-value, then loads
67 /// and returns the result.
68 Value *EmitLoadOfLValue(const Expr *E) {
69 // FIXME: Volatile
70 return EmitLoadOfLValue(EmitLValue(E), E->getType());
71 }
72
Chris Lattnerd8d44222007-08-26 16:42:57 +000073 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000074 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000075 Value *EmitConversionToBool(Value *Src, QualType DstTy);
76
Chris Lattner4e05d1e2007-08-26 06:48:56 +000077 /// EmitScalarConversion - Emit a conversion from the specified type to the
78 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000079 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
80
81 /// EmitComplexToScalarConversion - Emit a conversion from the specified
82 /// complex type to the specified destination type, where the destination
83 /// type is an LLVM scalar type.
84 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
85 QualType SrcTy, QualType DstTy);
Chris Lattner4e05d1e2007-08-26 06:48:56 +000086
Chris Lattner9fba49a2007-08-24 05:35:26 +000087 //===--------------------------------------------------------------------===//
88 // Visitor Methods
89 //===--------------------------------------------------------------------===//
90
91 Value *VisitStmt(Stmt *S) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +000092 S->dump(CGF.getContext().getSourceManager());
Chris Lattner9fba49a2007-08-24 05:35:26 +000093 assert(0 && "Stmt can't have complex result type!");
94 return 0;
95 }
96 Value *VisitExpr(Expr *S);
97 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
98
99 // Leaves.
100 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
101 return llvm::ConstantInt::get(E->getValue());
102 }
103 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner70c38672008-04-20 00:45:53 +0000104 return llvm::ConstantFP::get(E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000105 }
106 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
107 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
108 }
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000109 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
110 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000112 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
113 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000114 CGF.getContext().typesAreCompatible(
115 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000116 }
117 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
118 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
119 }
120
121 // l-values.
122 Value *VisitDeclRefExpr(DeclRefExpr *E) {
123 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
124 return llvm::ConstantInt::get(EC->getInitVal());
125 return EmitLoadOfLValue(E);
126 }
Chris Lattnercbfb5512008-03-01 08:45:05 +0000127 Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000128 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Chris Lattner9fba49a2007-08-24 05:35:26 +0000129 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
130 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000131 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Eli Friedmanf3c2cb42008-05-13 23:18:27 +0000132 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000133 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
134 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000135
136 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000137 unsigned NumInitElements = E->getNumInits();
138
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000139 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000140 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
141
142 // We have a scalar in braces. Just use the first element.
143 if (!VType)
144 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000145
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000146 unsigned NumVectorElements = VType->getNumElements();
147 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000148
149 // Emit individual vector element stores.
150 llvm::Value *V = llvm::UndefValue::get(VType);
151
Anders Carlsson323d5682007-12-18 02:45:33 +0000152 // Emit initializers
153 unsigned i;
154 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000155 Value *NewV = Visit(E->getInit(i));
156 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
157 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000158 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000159
160 // Emit remaining default initializers
161 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
162 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
163 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
164 V = Builder.CreateInsertElement(V, NewV, Idx);
165 }
166
Devang Patel32c39832007-10-24 18:05:48 +0000167 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000168 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000169
Chris Lattner9fba49a2007-08-24 05:35:26 +0000170 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
171 Value *VisitCastExpr(const CastExpr *E) {
172 return EmitCastExpr(E->getSubExpr(), E->getType());
173 }
174 Value *EmitCastExpr(const Expr *E, QualType T);
175
176 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000177 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000178 }
179
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000180 Value *VisitStmtExpr(const StmtExpr *E);
181
Chris Lattner9fba49a2007-08-24 05:35:26 +0000182 // Unary Operators.
183 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
184 Value *VisitUnaryPostDec(const UnaryOperator *E) {
185 return VisitPrePostIncDec(E, false, false);
186 }
187 Value *VisitUnaryPostInc(const UnaryOperator *E) {
188 return VisitPrePostIncDec(E, true, false);
189 }
190 Value *VisitUnaryPreDec(const UnaryOperator *E) {
191 return VisitPrePostIncDec(E, false, true);
192 }
193 Value *VisitUnaryPreInc(const UnaryOperator *E) {
194 return VisitPrePostIncDec(E, true, true);
195 }
196 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
197 return EmitLValue(E->getSubExpr()).getAddress();
198 }
199 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
200 Value *VisitUnaryPlus(const UnaryOperator *E) {
201 return Visit(E->getSubExpr());
202 }
203 Value *VisitUnaryMinus (const UnaryOperator *E);
204 Value *VisitUnaryNot (const UnaryOperator *E);
205 Value *VisitUnaryLNot (const UnaryOperator *E);
206 Value *VisitUnarySizeOf (const UnaryOperator *E) {
207 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
208 }
209 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
210 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
211 }
212 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnercfac88d2008-04-02 17:35:06 +0000213 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000214 Value *VisitUnaryReal (const UnaryOperator *E);
215 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000216 Value *VisitUnaryExtension(const UnaryOperator *E) {
217 return Visit(E->getSubExpr());
218 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000219 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000220 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
221 return Visit(DAE->getExpr());
222 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000223
Chris Lattner9fba49a2007-08-24 05:35:26 +0000224 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000225 Value *EmitMul(const BinOpInfo &Ops) {
226 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
227 }
228 Value *EmitDiv(const BinOpInfo &Ops);
229 Value *EmitRem(const BinOpInfo &Ops);
230 Value *EmitAdd(const BinOpInfo &Ops);
231 Value *EmitSub(const BinOpInfo &Ops);
232 Value *EmitShl(const BinOpInfo &Ops);
233 Value *EmitShr(const BinOpInfo &Ops);
234 Value *EmitAnd(const BinOpInfo &Ops) {
235 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
236 }
237 Value *EmitXor(const BinOpInfo &Ops) {
238 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
239 }
240 Value *EmitOr (const BinOpInfo &Ops) {
241 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
242 }
243
Chris Lattner660e31d2007-08-24 21:00:35 +0000244 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000245 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000246 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
247
248 // Binary operators and binary compound assignment operators.
249#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000250 Value *VisitBin ## OP(const BinaryOperator *E) { \
251 return Emit ## OP(EmitBinOps(E)); \
252 } \
253 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
254 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000255 }
256 HANDLEBINOP(Mul);
257 HANDLEBINOP(Div);
258 HANDLEBINOP(Rem);
259 HANDLEBINOP(Add);
260 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
261 HANDLEBINOP(Shl);
262 HANDLEBINOP(Shr);
263 HANDLEBINOP(And);
264 HANDLEBINOP(Xor);
265 HANDLEBINOP(Or);
266#undef HANDLEBINOP
267 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000268 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000269 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
270 }
271
Chris Lattner9fba49a2007-08-24 05:35:26 +0000272 // Comparisons.
273 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
274 unsigned SICmpOpc, unsigned FCmpOpc);
275#define VISITCOMP(CODE, UI, SI, FP) \
276 Value *VisitBin##CODE(const BinaryOperator *E) { \
277 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
278 llvm::FCmpInst::FP); }
279 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
280 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
281 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
282 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
283 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
284 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
285#undef VISITCOMP
286
287 Value *VisitBinAssign (const BinaryOperator *E);
288
289 Value *VisitBinLAnd (const BinaryOperator *E);
290 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000291 Value *VisitBinComma (const BinaryOperator *E);
292
293 // Other Operators.
294 Value *VisitConditionalOperator(const ConditionalOperator *CO);
295 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000296 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson36760332007-10-15 20:28:48 +0000297 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000298 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
299 return CGF.EmitObjCStringLiteral(E);
300 }
Anders Carlsson36f07d82007-10-29 05:01:08 +0000301 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000302};
303} // end anonymous namespace.
304
305//===----------------------------------------------------------------------===//
306// Utilities
307//===----------------------------------------------------------------------===//
308
Chris Lattnerd8d44222007-08-26 16:42:57 +0000309/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000310/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000311Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
312 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
313
314 if (SrcType->isRealFloatingType()) {
315 // Compare against 0.0 for fp scalars.
316 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000317 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
318 }
319
320 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
321 "Unknown scalar type to convert");
322
323 // Because of the type rules of C, we often end up computing a logical value,
324 // then zero extending it to int, then wanting it as a logical value again.
325 // Optimize this common case.
326 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
327 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
328 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000329 // If there aren't any more uses, zap the instruction to save space.
330 // Note that there can be more uses, for example if this
331 // is the result of an assignment.
332 if (ZI->use_empty())
333 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000334 return Result;
335 }
336 }
337
338 // Compare against an integer or pointer null.
339 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
340 return Builder.CreateICmpNE(Src, Zero, "tobool");
341}
342
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000343/// EmitScalarConversion - Emit a conversion from the specified type to the
344/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000345Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
346 QualType DstType) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000347 SrcType = SrcType.getCanonicalType();
348 DstType = DstType.getCanonicalType();
349 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000350
351 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000352
353 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000354 if (DstType->isBooleanType())
355 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000356
357 const llvm::Type *DstTy = ConvertType(DstType);
358
359 // Ignore conversions like int -> uint.
360 if (Src->getType() == DstTy)
361 return Src;
362
363 // Handle pointer conversions next: pointers can only be converted to/from
364 // other pointers and integers.
365 if (isa<PointerType>(DstType)) {
366 // The source value may be an integer, or a pointer.
367 if (isa<llvm::PointerType>(Src->getType()))
368 return Builder.CreateBitCast(Src, DstTy, "conv");
369 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
370 return Builder.CreateIntToPtr(Src, DstTy, "conv");
371 }
372
373 if (isa<PointerType>(SrcType)) {
374 // Must be an ptr to int cast.
375 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000376 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000377 }
378
Nate Begemanaf6ed502008-04-18 23:10:10 +0000379 // A scalar can be splatted to an extended vector of the same element type
380 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner4f025a42008-02-02 04:51:41 +0000381 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begemanec2d1062007-12-30 02:59:45 +0000382 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
383 true);
Nate Begemanec2d1062007-12-30 02:59:45 +0000384
Chris Lattner4f025a42008-02-02 04:51:41 +0000385 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000386 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000387 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000388 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000389
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000390 // Finally, we have the arithmetic types: real int/float.
391 if (isa<llvm::IntegerType>(Src->getType())) {
392 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000393 if (isa<llvm::IntegerType>(DstTy))
394 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
395 else if (InputSigned)
396 return Builder.CreateSIToFP(Src, DstTy, "conv");
397 else
398 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000399 }
400
401 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
402 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000403 if (DstType->isSignedIntegerType())
404 return Builder.CreateFPToSI(Src, DstTy, "conv");
405 else
406 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000407 }
408
409 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000410 if (DstTy->getTypeID() < Src->getType()->getTypeID())
411 return Builder.CreateFPTrunc(Src, DstTy, "conv");
412 else
413 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000414}
415
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000416/// EmitComplexToScalarConversion - Emit a conversion from the specified
417/// complex type to the specified destination type, where the destination
418/// type is an LLVM scalar type.
419Value *ScalarExprEmitter::
420EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
421 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000422 // Get the source element type.
423 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
424
425 // Handle conversions to bool first, they are special: comparisons against 0.
426 if (DstTy->isBooleanType()) {
427 // Complex != 0 -> (Real != 0) | (Imag != 0)
428 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
429 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
430 return Builder.CreateOr(Src.first, Src.second, "tobool");
431 }
432
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000433 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
434 // the imaginary part of the complex value is discarded and the value of the
435 // real part is converted according to the conversion rules for the
436 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000437 return EmitScalarConversion(Src.first, SrcTy, DstTy);
438}
439
440
Chris Lattner9fba49a2007-08-24 05:35:26 +0000441//===----------------------------------------------------------------------===//
442// Visitor Methods
443//===----------------------------------------------------------------------===//
444
445Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000446 CGF.WarnUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000447 if (E->getType()->isVoidType())
448 return 0;
449 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
450}
451
Chris Lattnercbfb5512008-03-01 08:45:05 +0000452Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
453 // Only the lookup mechanism and first two arguments of the method
454 // implementation vary between runtimes. We can get the receiver and
455 // arguments in generic code.
456
457 // Find the receiver
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000458 llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000459
460 // Process the arguments
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000461 unsigned ArgC = E->getNumArgs();
Chris Lattnercbfb5512008-03-01 08:45:05 +0000462 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000463 for (unsigned i = 0; i != ArgC; ++i) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000464 Expr *ArgExpr = E->getArg(i);
465 QualType ArgTy = ArgExpr->getType();
466 if (!CGF.hasAggregateLLVMType(ArgTy)) {
467 // Scalar argument is passed by-value.
468 Args.push_back(CGF.EmitScalarExpr(ArgExpr));
Chris Lattnerde0908b2008-04-04 16:54:41 +0000469 } else if (ArgTy->isAnyComplexType()) {
Chris Lattnercbfb5512008-03-01 08:45:05 +0000470 // Make a temporary alloca to pass the argument.
471 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
472 CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
473 Args.push_back(DestMem);
474 } else {
475 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
476 CGF.EmitAggExpr(ArgExpr, DestMem, false);
477 Args.push_back(DestMem);
478 }
479 }
480
481 // Get the selector string
482 std::string SelStr = E->getSelector().getName();
483 llvm::Constant *Selector = CGF.CGM.GetAddrOfConstantString(SelStr);
Chris Lattnerb326b172008-03-30 23:03:07 +0000484
485 llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0);
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000486 return Runtime->generateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattner6e6a5972008-04-04 04:07:35 +0000487 CGF.LoadObjCSelf(),
Chris Lattnerc61e9f82008-03-30 23:25:33 +0000488 Receiver, SelPtr,
489 &Args[0], Args.size());
Chris Lattnercbfb5512008-03-01 08:45:05 +0000490}
491
Chris Lattner9fba49a2007-08-24 05:35:26 +0000492Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
493 // Emit subscript expressions in rvalue context's. For most cases, this just
494 // loads the lvalue formed by the subscript expr. However, we have to be
495 // careful, because the base of a vector subscript is occasionally an rvalue,
496 // so we can't get it as an lvalue.
497 if (!E->getBase()->getType()->isVectorType())
498 return EmitLoadOfLValue(E);
499
500 // Handle the vector case. The base must be a vector, the index must be an
501 // integer value.
502 Value *Base = Visit(E->getBase());
503 Value *Idx = Visit(E->getIdx());
504
505 // FIXME: Convert Idx to i32 type.
506 return Builder.CreateExtractElement(Base, Idx, "vecext");
507}
508
509/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
510/// also handle things like function to pointer-to-function decay, and array to
511/// pointer decay.
512Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
513 const Expr *Op = E->getSubExpr();
514
515 // If this is due to array->pointer conversion, emit the array expression as
516 // an l-value.
517 if (Op->getType()->isArrayType()) {
518 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
519 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000520 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000521
522 assert(isa<llvm::PointerType>(V->getType()) &&
523 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
524 ->getElementType()) &&
525 "Doesn't support VLAs yet!");
Chris Lattner07307562008-03-19 05:19:41 +0000526 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnere54443b2007-12-12 04:13:20 +0000527
528 // The resultant pointer type can be implicitly casted to other pointer
529 // types as well, for example void*.
530 const llvm::Type *DestPTy = ConvertType(E->getType());
531 assert(isa<llvm::PointerType>(DestPTy) &&
532 "Only expect implicit cast to pointer");
533 if (V->getType() != DestPTy)
534 V = Builder.CreateBitCast(V, DestPTy, "ptrconv");
535 return V;
536
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000537 } else if (E->getType()->isReferenceType()) {
Anders Carlsson88842452007-10-13 05:52:34 +0000538 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
Chris Lattnercfac88d2008-04-02 17:35:06 +0000539 getPointeeType() ==
Anders Carlsson88842452007-10-13 05:52:34 +0000540 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000541
542 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000543 }
544
545 return EmitCastExpr(Op, E->getType());
546}
547
548
549// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
550// have to handle a more broad range of conversions than explicit casts, as they
551// handle things like function to ptr-to-function decay etc.
552Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000553 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000554
555 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000556 Value *Src = Visit(const_cast<Expr*>(E));
557
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000558 // Use EmitScalarConversion to perform the conversion.
559 return EmitScalarConversion(Src, E->getType(), DestTy);
560 }
Chris Lattner77288792008-02-16 23:55:16 +0000561
Chris Lattnerde0908b2008-04-04 16:54:41 +0000562 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000563 // Handle cases where the source is a complex type.
564 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
565 DestTy);
566 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000567
Chris Lattner77288792008-02-16 23:55:16 +0000568 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
569 // evaluate the result and return.
570 CGF.EmitAggExpr(E, 0, false);
571 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000572}
573
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000574Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000575 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000576}
577
578
Chris Lattner9fba49a2007-08-24 05:35:26 +0000579//===----------------------------------------------------------------------===//
580// Unary Operators
581//===----------------------------------------------------------------------===//
582
583Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000584 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000585 LValue LV = EmitLValue(E->getSubExpr());
586 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000587 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000588 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000589
590 int AmountVal = isInc ? 1 : -1;
591
592 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000593 if (isa<llvm::PointerType>(InVal->getType())) {
594 // FIXME: This isn't right for VLAs.
595 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
Chris Lattner07307562008-03-19 05:19:41 +0000596 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000597 } else {
598 // Add the inc/dec to the real part.
599 if (isa<llvm::IntegerType>(InVal->getType()))
600 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000601 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000602 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000603 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000604 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000605 NextVal =
Chris Lattner70c38672008-04-20 00:45:53 +0000606 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000607 else {
608 llvm::APFloat F(static_cast<float>(AmountVal));
609 F.convert(*CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
610 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000611 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000612 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
613 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000614
615 // Store the updated result through the lvalue.
616 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
617 E->getSubExpr()->getType());
618
619 // If this is a postinc, return the value read from memory, otherwise use the
620 // updated value.
621 return isPre ? NextVal : InVal;
622}
623
624
625Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
626 Value *Op = Visit(E->getSubExpr());
627 return Builder.CreateNeg(Op, "neg");
628}
629
630Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
631 Value *Op = Visit(E->getSubExpr());
632 return Builder.CreateNot(Op, "neg");
633}
634
635Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
636 // Compare operand to zero.
637 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
638
639 // Invert value.
640 // TODO: Could dynamically modify easy computations here. For example, if
641 // the operand is an icmp ne, turn into icmp eq.
642 BoolVal = Builder.CreateNot(BoolVal, "lnot");
643
644 // ZExt result to int.
645 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
646}
647
648/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
649/// an integer (RetType).
650Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000651 QualType RetType,bool isSizeOf){
Chris Lattner20515462008-02-21 05:45:29 +0000652 assert(RetType->isIntegerType() && "Result type must be an integer!");
653 uint32_t ResultWidth =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000654 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattner20515462008-02-21 05:45:29 +0000655
656 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
657 if (TypeToSize->isVoidType())
658 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
659
Chris Lattner9fba49a2007-08-24 05:35:26 +0000660 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner8cd0e932008-03-05 18:54:05 +0000661 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000662
663 uint64_t Val = isSizeOf ? Info.first : Info.second;
664 Val /= 8; // Return size in bytes, not bits.
665
Chris Lattner9fba49a2007-08-24 05:35:26 +0000666 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
667}
668
Chris Lattner01211af2007-08-24 21:20:17 +0000669Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
670 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000671 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000672 return CGF.EmitComplexExpr(Op).first;
673 return Visit(Op);
674}
675Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
676 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000677 if (Op->getType()->isAnyComplexType())
Chris Lattner01211af2007-08-24 21:20:17 +0000678 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000679
680 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
681 // effects are evaluated.
682 CGF.EmitScalarExpr(Op);
683 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000684}
685
Anders Carlsson52774ad2008-01-29 15:56:48 +0000686Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
687{
688 int64_t Val = E->evaluateOffsetOf(CGF.getContext());
689
690 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
691
Chris Lattner8cd0e932008-03-05 18:54:05 +0000692 uint32_t ResultWidth =
693 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000694 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
695}
Chris Lattner01211af2007-08-24 21:20:17 +0000696
Chris Lattner9fba49a2007-08-24 05:35:26 +0000697//===----------------------------------------------------------------------===//
698// Binary Operators
699//===----------------------------------------------------------------------===//
700
701BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
702 BinOpInfo Result;
703 Result.LHS = Visit(E->getLHS());
704 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000705 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000706 Result.E = E;
707 return Result;
708}
709
Chris Lattner0d965302007-08-26 21:41:21 +0000710Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000711 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
712 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
713
714 BinOpInfo OpInfo;
715
716 // Load the LHS and RHS operands.
717 LValue LHSLV = EmitLValue(E->getLHS());
718 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000719
720 // Determine the computation type. If the RHS is complex, then this is one of
721 // the add/sub/mul/div operators. All of these operators can be computed in
722 // with just their real component even though the computation domain really is
723 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000724 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000725
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000726 // If the computation type is complex, then the RHS is complex. Emit the RHS.
727 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
728 ComputeType = CT->getElementType();
729
730 // Emit the RHS, only keeping the real component.
731 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
732 RHSTy = RHSTy->getAsComplexType()->getElementType();
733 } else {
734 // Otherwise the RHS is a simple scalar value.
735 OpInfo.RHS = Visit(E->getRHS());
736 }
737
738 // Convert the LHS/RHS values to the computation type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000739 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000740
Devang Patel04011802007-10-25 22:19:13 +0000741 // Do not merge types for -= or += where the LHS is a pointer.
742 if (!(E->getOpcode() == BinaryOperator::SubAssign ||
Devang Patelce6c8372007-10-30 18:31:12 +0000743 E->getOpcode() == BinaryOperator::AddAssign) ||
Chris Lattner42330c32007-08-25 21:56:20 +0000744 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnerb1497062007-08-26 07:08:39 +0000745 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000746 }
747 OpInfo.Ty = ComputeType;
748 OpInfo.E = E;
749
750 // Expand the binary operator.
751 Value *Result = (this->*Func)(OpInfo);
752
753 // Truncate the result back to the LHS type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000754 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000755
756 // Store the result value into the LHS lvalue.
757 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
758
759 return Result;
760}
761
762
Chris Lattner9fba49a2007-08-24 05:35:26 +0000763Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000764 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000765 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000766 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000767 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
768 else
769 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
770}
771
772Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
773 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000774 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000775 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
776 else
777 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
778}
779
780
781Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000782 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000783 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000784
785 // FIXME: What about a pointer to a VLA?
Chris Lattner17c0cb02008-01-03 06:36:51 +0000786 Value *Ptr, *Idx;
787 Expr *IdxExp;
788 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
789 Ptr = Ops.LHS;
790 Idx = Ops.RHS;
791 IdxExp = Ops.E->getRHS();
792 } else { // int + pointer
793 Ptr = Ops.RHS;
794 Idx = Ops.LHS;
795 IdxExp = Ops.E->getLHS();
796 }
797
798 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
799 if (Width < CGF.LLVMPointerWidth) {
800 // Zero or sign extend the pointer value based on whether the index is
801 // signed or not.
802 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
803 if (IdxExp->getType().getCanonicalType()->isSignedIntegerType())
804 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
805 else
806 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
807 }
808
809 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000810}
811
812Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
813 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
814 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
815
Chris Lattner660e31d2007-08-24 21:00:35 +0000816 // pointer - int
817 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
818 "ptr-ptr shouldn't get here");
819 // FIXME: The pointer could point to a VLA.
Chris Lattnere78c1ea2008-01-31 04:12:50 +0000820 Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
821
822 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
823 if (Width < CGF.LLVMPointerWidth) {
824 // Zero or sign extend the pointer value based on whether the index is
825 // signed or not.
826 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
827 if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType())
828 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
829 else
830 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
831 }
832
833 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Chris Lattner660e31d2007-08-24 21:00:35 +0000834}
835
836Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
837 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
838 // the compound assignment case it is invalid, so just handle it here.
839 if (!E->getRHS()->getType()->isPointerType())
840 return EmitSub(EmitBinOps(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000841
842 // pointer - pointer
Chris Lattner660e31d2007-08-24 21:00:35 +0000843 Value *LHS = Visit(E->getLHS());
844 Value *RHS = Visit(E->getRHS());
845
Seo Sanghyeonfcd44772007-12-03 06:23:43 +0000846 const QualType LHSType = E->getLHS()->getType().getCanonicalType();
Seo Sanghyeona570d312007-12-26 05:21:37 +0000847 const QualType LHSElementType = cast<PointerType>(LHSType)->getPointeeType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000848 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
Chris Lattner660e31d2007-08-24 21:00:35 +0000849
850 const llvm::Type *ResultType = ConvertType(E->getType());
851 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
852 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
853 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000854
855 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
856 // remainder. As such, we handle common power-of-two cases here to generate
857 // better code.
858 if (llvm::isPowerOf2_64(ElementSize)) {
859 Value *ShAmt =
860 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
861 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
862 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000863
Chris Lattner9fba49a2007-08-24 05:35:26 +0000864 // Otherwise, do a full sdiv.
865 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
866 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
867}
868
Chris Lattner660e31d2007-08-24 21:00:35 +0000869
Chris Lattner9fba49a2007-08-24 05:35:26 +0000870Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
871 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
872 // RHS to the same size as the LHS.
873 Value *RHS = Ops.RHS;
874 if (Ops.LHS->getType() != RHS->getType())
875 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
876
877 return Builder.CreateShl(Ops.LHS, RHS, "shl");
878}
879
880Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
881 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
882 // RHS to the same size as the LHS.
883 Value *RHS = Ops.RHS;
884 if (Ops.LHS->getType() != RHS->getType())
885 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
886
Chris Lattner660e31d2007-08-24 21:00:35 +0000887 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000888 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
889 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
890}
891
892Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
893 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000894 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000895 QualType LHSTy = E->getLHS()->getType();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000896 if (!LHSTy->isAnyComplexType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000897 Value *LHS = Visit(E->getLHS());
898 Value *RHS = Visit(E->getRHS());
899
900 if (LHS->getType()->isFloatingPoint()) {
901 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
902 LHS, RHS, "cmp");
903 } else if (LHSTy->isUnsignedIntegerType()) {
904 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
905 LHS, RHS, "cmp");
906 } else {
907 // Signed integers and pointers.
908 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
909 LHS, RHS, "cmp");
910 }
911 } else {
912 // Complex Comparison: can only be an equality comparison.
913 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
914 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
915
916 QualType CETy =
917 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
918
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000919 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000920 if (CETy->isRealFloatingType()) {
921 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
922 LHS.first, RHS.first, "cmp.r");
923 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
924 LHS.second, RHS.second, "cmp.i");
925 } else {
926 // Complex comparisons can only be equality comparisons. As such, signed
927 // and unsigned opcodes are the same.
928 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
929 LHS.first, RHS.first, "cmp.r");
930 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
931 LHS.second, RHS.second, "cmp.i");
932 }
933
934 if (E->getOpcode() == BinaryOperator::EQ) {
935 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
936 } else {
937 assert(E->getOpcode() == BinaryOperator::NE &&
938 "Complex comparison other than == or != ?");
939 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
940 }
941 }
942
943 // ZExt result to int.
944 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
945}
946
947Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
948 LValue LHS = EmitLValue(E->getLHS());
949 Value *RHS = Visit(E->getRHS());
950
951 // Store the value into the LHS.
952 // FIXME: Volatility!
953 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
954
955 // Return the RHS.
956 return RHS;
957}
958
959Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
960 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
961
Gabor Greif815e2c12008-04-06 20:42:52 +0000962 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
963 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000964
965 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
966 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
967
968 CGF.EmitBlock(RHSBlock);
969 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
970
971 // Reaquire the RHS block, as there may be subblocks inserted.
972 RHSBlock = Builder.GetInsertBlock();
973 CGF.EmitBlock(ContBlock);
974
975 // Create a PHI node. If we just evaluted the LHS condition, the result is
976 // false. If we evaluated both, the result is the RHS condition.
977 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
978 PN->reserveOperandSpace(2);
979 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
980 PN->addIncoming(RHSCond, RHSBlock);
981
982 // ZExt result to int.
983 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
984}
985
986Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
987 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
988
Gabor Greif815e2c12008-04-06 20:42:52 +0000989 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
990 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000991
992 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
993 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
994
995 CGF.EmitBlock(RHSBlock);
996 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
997
998 // Reaquire the RHS block, as there may be subblocks inserted.
999 RHSBlock = Builder.GetInsertBlock();
1000 CGF.EmitBlock(ContBlock);
1001
1002 // Create a PHI node. If we just evaluted the LHS condition, the result is
1003 // true. If we evaluated both, the result is the RHS condition.
1004 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1005 PN->reserveOperandSpace(2);
1006 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1007 PN->addIncoming(RHSCond, RHSBlock);
1008
1009 // ZExt result to int.
1010 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1011}
1012
1013Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1014 CGF.EmitStmt(E->getLHS());
1015 return Visit(E->getRHS());
1016}
1017
1018//===----------------------------------------------------------------------===//
1019// Other Operators
1020//===----------------------------------------------------------------------===//
1021
1022Value *ScalarExprEmitter::
1023VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +00001024 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1025 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1026 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001027
Chris Lattner98a425c2007-11-26 01:40:58 +00001028 // Evaluate the conditional, then convert it to bool. We do this explicitly
1029 // because we need the unconverted value if this is a GNU ?: expression with
1030 // missing middle value.
1031 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc2126682008-01-03 07:05:49 +00001032 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1033 CGF.getContext().BoolTy);
Chris Lattner98a425c2007-11-26 01:40:58 +00001034 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001035
1036 CGF.EmitBlock(LHSBlock);
1037
1038 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001039 Value *LHS;
1040 if (E->getLHS())
1041 LHS = Visit(E->getLHS());
1042 else // Perform promotions, to handle cases like "short ?: int"
1043 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1044
Chris Lattner9fba49a2007-08-24 05:35:26 +00001045 Builder.CreateBr(ContBlock);
1046 LHSBlock = Builder.GetInsertBlock();
1047
1048 CGF.EmitBlock(RHSBlock);
1049
1050 Value *RHS = Visit(E->getRHS());
1051 Builder.CreateBr(ContBlock);
1052 RHSBlock = Builder.GetInsertBlock();
1053
1054 CGF.EmitBlock(ContBlock);
1055
Chris Lattner307da022007-11-30 17:56:23 +00001056 if (!LHS) {
1057 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1058 return 0;
1059 }
1060
Chris Lattner9fba49a2007-08-24 05:35:26 +00001061 // Create a PHI node for the real part.
1062 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1063 PN->reserveOperandSpace(2);
1064 PN->addIncoming(LHS, LHSBlock);
1065 PN->addIncoming(RHS, RHSBlock);
1066 return PN;
1067}
1068
1069Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001070 // Emit the LHS or RHS as appropriate.
Devang Patel0f2a8fb2007-10-30 20:59:40 +00001071 return
1072 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001073}
1074
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001075Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begemanbd881ef2008-01-30 20:50:20 +00001076 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
1077 E->getNumArgs(CGF.getContext())).getScalarVal();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001078}
1079
Chris Lattner307da022007-11-30 17:56:23 +00001080Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson36760332007-10-15 20:28:48 +00001081 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1082
1083 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1084 return V;
1085}
1086
Chris Lattner307da022007-11-30 17:56:23 +00001087Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001088 std::string str;
Fariborz Jahanian248db262008-01-22 22:44:46 +00001089 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1090 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1091 EncodingRecordTypes);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001092
1093 llvm::Constant *C = llvm::ConstantArray::get(str);
1094 C = new llvm::GlobalVariable(C->getType(), true,
1095 llvm::GlobalValue::InternalLinkage,
1096 C, ".str", &CGF.CGM.getModule());
1097 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1098 llvm::Constant *Zeros[] = { Zero, Zero };
1099 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1100
1101 return C;
1102}
1103
Chris Lattner9fba49a2007-08-24 05:35:26 +00001104//===----------------------------------------------------------------------===//
1105// Entry Point into this File
1106//===----------------------------------------------------------------------===//
1107
1108/// EmitComplexExpr - Emit the computation of the specified expression of
1109/// complex type, ignoring the result.
1110Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1111 assert(E && !hasAggregateLLVMType(E->getType()) &&
1112 "Invalid scalar expression to emit");
1113
1114 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1115}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001116
1117/// EmitScalarConversion - Emit a conversion from the specified type to the
1118/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001119Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1120 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001121 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1122 "Invalid scalar expression to emit");
1123 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1124}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001125
1126/// EmitComplexToScalarConversion - Emit a conversion from the specified
1127/// complex type to the specified destination type, where the destination
1128/// type is an LLVM scalar type.
1129Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1130 QualType SrcTy,
1131 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001132 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001133 "Invalid complex -> scalar conversion");
1134 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1135 DstTy);
1136}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001137
1138Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1139 assert(V1->getType() == V2->getType() &&
1140 "Vector operands must be of the same type");
1141
1142 unsigned NumElements =
1143 cast<llvm::VectorType>(V1->getType())->getNumElements();
1144
1145 va_list va;
1146 va_start(va, V2);
1147
1148 llvm::SmallVector<llvm::Constant*, 16> Args;
1149
1150 for (unsigned i = 0; i < NumElements; i++) {
1151 int n = va_arg(va, int);
1152
1153 assert(n >= 0 && n < (int)NumElements * 2 &&
1154 "Vector shuffle index out of bounds!");
1155
1156 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1157 }
1158
1159 const char *Name = va_arg(va, const char *);
1160 va_end(va);
1161
1162 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1163
1164 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1165}
1166
Anders Carlsson68b8be92007-12-15 21:23:30 +00001167llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Nate Begemanec2d1062007-12-30 02:59:45 +00001168 unsigned NumVals, bool isSplat)
Anders Carlsson68b8be92007-12-15 21:23:30 +00001169{
Anders Carlsson68b8be92007-12-15 21:23:30 +00001170 llvm::Value *Vec
1171 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1172
1173 for (unsigned i = 0, e = NumVals ; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001174 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson68b8be92007-12-15 21:23:30 +00001175 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001176 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001177 }
1178
1179 return Vec;
1180}