blob: 4496c538d3c05efda6fe6585bc57fb3205284682 [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"
Anders Carlsson63f1ad92009-07-18 19:43:29 +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;
Mike Stumpb8fc73e2009-05-29 15:46:01 +000051 bool IgnoreResultAssign;
Owen Anderson73e7f802009-07-14 23:10:40 +000052 llvm::LLVMContext &VMContext;
Chris Lattner9fba49a2007-08-24 05:35:26 +000053public:
54
Mike Stumpb8fc73e2009-05-29 15:46:01 +000055 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
Owen Anderson73e7f802009-07-14 23:10:40 +000056 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
57 VMContext(cgf.getLLVMContext()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000058 }
Chris Lattner9fba49a2007-08-24 05:35:26 +000059
60 //===--------------------------------------------------------------------===//
61 // Utilities
62 //===--------------------------------------------------------------------===//
63
Mike Stumpb8fc73e2009-05-29 15:46:01 +000064 bool TestAndClearIgnoreResultAssign() {
Chris Lattner08ac8522009-07-08 01:08:03 +000065 bool I = IgnoreResultAssign;
66 IgnoreResultAssign = false;
67 return I;
68 }
Mike Stumpb8fc73e2009-05-29 15:46:01 +000069
Chris Lattner9fba49a2007-08-24 05:35:26 +000070 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
71 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
72
73 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000074 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000075 }
76
77 /// EmitLoadOfLValue - Given an expression with complex type that represents a
78 /// value l-value, this method emits the address of the l-value, then loads
79 /// and returns the result.
80 Value *EmitLoadOfLValue(const Expr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +000081 return EmitLoadOfLValue(EmitLValue(E), E->getType());
82 }
83
Chris Lattnerd8d44222007-08-26 16:42:57 +000084 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000085 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000086 Value *EmitConversionToBool(Value *Src, QualType DstTy);
87
Chris Lattner4e05d1e2007-08-26 06:48:56 +000088 /// EmitScalarConversion - Emit a conversion from the specified type to the
89 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000090 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
91
92 /// EmitComplexToScalarConversion - Emit a conversion from the specified
93 /// complex type to the specified destination type, where the destination
94 /// type is an LLVM scalar type.
95 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
96 QualType SrcTy, QualType DstTy);
Mike Stump4eb81dc2009-02-12 18:29:15 +000097
Chris Lattner9fba49a2007-08-24 05:35:26 +000098 //===--------------------------------------------------------------------===//
99 // Visitor Methods
100 //===--------------------------------------------------------------------===//
101
102 Value *VisitStmt(Stmt *S) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000103 S->dump(CGF.getContext().getSourceManager());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000104 assert(0 && "Stmt can't have complex result type!");
105 return 0;
106 }
107 Value *VisitExpr(Expr *S);
108 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
109
110 // Leaves.
111 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000112 return llvm::ConstantInt::get(VMContext, E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000113 }
114 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000115 return llvm::ConstantFP::get(VMContext, E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000116 }
117 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000118 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000119 }
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000120 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000121 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
Nate Begemane9bfe6d2007-11-15 05:40:03 +0000122 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000123 Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Owen Andersonf37b84b2009-07-31 20:28:54 +0000124 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000125 }
Anders Carlsson774f9c72008-12-21 22:39:40 +0000126 Value *VisitGNUNullExpr(const GNUNullExpr *E) {
Owen Andersonf37b84b2009-07-31 20:28:54 +0000127 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Anders Carlsson774f9c72008-12-21 22:39:40 +0000128 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000129 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000130 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000131 CGF.getContext().typesAreCompatible(
132 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000133 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000134 Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
Daniel Dunbar879788d2008-08-04 16:51:22 +0000135 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
Daniel Dunbarb5fda0c2008-08-16 01:41:47 +0000136 llvm::Value *V =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000137 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Daniel Dunbarb5fda0c2008-08-16 01:41:47 +0000138 CGF.GetIDForAddrOfLabel(E->getLabel()));
139
140 return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
Daniel Dunbar879788d2008-08-04 16:51:22 +0000141 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000142
143 // l-values.
144 Value *VisitDeclRefExpr(DeclRefExpr *E) {
145 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
Owen Andersonb17ec712009-07-24 23:12:58 +0000146 return llvm::ConstantInt::get(VMContext, EC->getInitVal());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000147 return EmitLoadOfLValue(E);
148 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000149 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
150 return CGF.EmitObjCSelectorExpr(E);
151 }
152 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
153 return CGF.EmitObjCProtocolExpr(E);
154 }
155 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
156 return EmitLoadOfLValue(E);
157 }
Daniel Dunbar5e105892008-08-23 10:51:21 +0000158 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
Daniel Dunbare6c31752008-08-29 08:11:39 +0000159 return EmitLoadOfLValue(E);
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000160 }
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000161 Value *VisitObjCImplicitSetterGetterRefExpr(
162 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanianb0973da2008-11-22 22:30:21 +0000163 return EmitLoadOfLValue(E);
164 }
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000165 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
166 return CGF.EmitObjCMessageExpr(E).getScalarVal();
Daniel Dunbar5e105892008-08-23 10:51:21 +0000167 }
168
Chris Lattner9fba49a2007-08-24 05:35:26 +0000169 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmand0e9d092008-05-14 19:38:39 +0000170 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000171 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000172 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnera9177982008-10-26 23:53:12 +0000173 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
174 return EmitLoadOfLValue(E);
175 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000176 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
Chris Lattnerc5d32632009-02-24 22:18:39 +0000177 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
178 return EmitLValue(E).getAddress();
179 }
180
Chris Lattner69909292008-08-10 01:53:14 +0000181 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000182
183 Value *VisitInitListExpr(InitListExpr *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000184 bool Ignore = TestAndClearIgnoreResultAssign();
185 (void)Ignore;
186 assert (Ignore == false && "init list ignored");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000187 unsigned NumInitElements = E->getNumInits();
188
Douglas Gregor9fddded2009-01-29 19:42:23 +0000189 if (E->hadArrayRangeDesignator()) {
190 CGF.ErrorUnsupported(E, "GNU array range designator extension");
191 }
192
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000193 const llvm::VectorType *VType =
Anders Carlsson35ab4f92008-01-29 01:15:48 +0000194 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
195
196 // We have a scalar in braces. Just use the first element.
197 if (!VType)
198 return Visit(E->getInit(0));
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000199
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000200 unsigned NumVectorElements = VType->getNumElements();
201 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000202
203 // Emit individual vector element stores.
Owen Andersone0b5eff2009-07-30 23:11:26 +0000204 llvm::Value *V = llvm::UndefValue::get(VType);
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000205
Anders Carlsson323d5682007-12-18 02:45:33 +0000206 // Emit initializers
207 unsigned i;
208 for (i = 0; i < NumInitElements; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000209 Value *NewV = Visit(E->getInit(i));
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000210 Value *Idx =
211 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i);
Devang Patel32c39832007-10-24 18:05:48 +0000212 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000213 }
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000214
215 // Emit remaining default initializers
216 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000217 Value *Idx =
218 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i);
Owen Andersonf37b84b2009-07-31 20:28:54 +0000219 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000220 V = Builder.CreateInsertElement(V, NewV, Idx);
221 }
222
Devang Patel32c39832007-10-24 18:05:48 +0000223 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000224 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000225
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000226 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
Owen Andersonf37b84b2009-07-31 20:28:54 +0000227 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000228 }
Eli Friedmana7ef8e52009-04-20 03:54:15 +0000229 Value *VisitCastExpr(const CastExpr *E) {
Fariborz Jahanianc8a336f2009-08-26 23:31:30 +0000230 if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) {
Fariborz Jahanianec172132009-08-29 19:15:16 +0000231 if (const CXXFunctionalCastExpr *CXXFExpr =
232 dyn_cast<CXXFunctionalCastExpr>(E))
233 return CGF.EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal();
234 assert(isa<CStyleCastExpr>(E) &&
235 "VisitCastExpr - missing CStyleCastExpr");
Fariborz Jahanianc8a336f2009-08-26 23:31:30 +0000236 }
237
Eli Friedmana7ef8e52009-04-20 03:54:15 +0000238 // Make sure to evaluate VLA bounds now so that we have them for later.
239 if (E->getType()->isVariablyModifiedType())
240 CGF.EmitVLASize(E->getType());
241
Anders Carlsson2c61dbe2009-08-24 18:12:39 +0000242 return EmitCastExpr(E->getSubExpr(), E->getType(), E->getCastKind());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000243 }
Anders Carlsson2c61dbe2009-08-24 18:12:39 +0000244 Value *EmitCastExpr(const Expr *E, QualType T, CastExpr::CastKind Kind);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000245
246 Value *VisitCallExpr(const CallExpr *E) {
Anders Carlssoncd295282009-05-27 03:37:57 +0000247 if (E->getCallReturnType()->isReferenceType())
248 return EmitLoadOfLValue(E);
249
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000250 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000251 }
Daniel Dunbara04840b2008-08-23 03:46:30 +0000252
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000253 Value *VisitStmtExpr(const StmtExpr *E);
Mike Stumpfca5da02009-02-21 20:00:35 +0000254
Mike Stump2b6933f2009-02-28 09:07:16 +0000255 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000256
Chris Lattner9fba49a2007-08-24 05:35:26 +0000257 // Unary Operators.
258 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
259 Value *VisitUnaryPostDec(const UnaryOperator *E) {
260 return VisitPrePostIncDec(E, false, false);
261 }
262 Value *VisitUnaryPostInc(const UnaryOperator *E) {
263 return VisitPrePostIncDec(E, true, false);
264 }
265 Value *VisitUnaryPreDec(const UnaryOperator *E) {
266 return VisitPrePostIncDec(E, false, true);
267 }
268 Value *VisitUnaryPreInc(const UnaryOperator *E) {
269 return VisitPrePostIncDec(E, true, true);
270 }
271 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
272 return EmitLValue(E->getSubExpr()).getAddress();
273 }
274 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
275 Value *VisitUnaryPlus(const UnaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000276 // This differs from gcc, though, most likely due to a bug in gcc.
277 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000278 return Visit(E->getSubExpr());
279 }
280 Value *VisitUnaryMinus (const UnaryOperator *E);
281 Value *VisitUnaryNot (const UnaryOperator *E);
282 Value *VisitUnaryLNot (const UnaryOperator *E);
Chris Lattner01211af2007-08-24 21:20:17 +0000283 Value *VisitUnaryReal (const UnaryOperator *E);
284 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000285 Value *VisitUnaryExtension(const UnaryOperator *E) {
286 return Visit(E->getSubExpr());
287 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000288 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Anders Carlsson49d4a572009-04-14 16:58:56 +0000289
290 // C++
Chris Lattner3e254fb2008-04-08 04:40:51 +0000291 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
292 return Visit(DAE->getExpr());
293 }
Anders Carlsson49d4a572009-04-14 16:58:56 +0000294 Value *VisitCXXThisExpr(CXXThisExpr *TE) {
295 return CGF.LoadCXXThis();
296 }
Anders Carlsson52774ad2008-01-29 15:56:48 +0000297
Anders Carlsson272b5f52009-05-19 04:48:36 +0000298 Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlssond6775602009-05-31 00:09:15 +0000299 return CGF.EmitCXXExprWithTemporaries(E).getScalarVal();
Anders Carlsson272b5f52009-05-19 04:48:36 +0000300 }
Anders Carlsson18e88bc2009-05-31 01:40:14 +0000301 Value *VisitCXXNewExpr(const CXXNewExpr *E) {
302 return CGF.EmitCXXNewExpr(E);
303 }
Anders Carlsson133fdaf2009-08-16 21:13:42 +0000304 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
305 CGF.EmitCXXDeleteExpr(E);
306 return 0;
307 }
Anders Carlsson272b5f52009-05-19 04:48:36 +0000308
Chris Lattner9fba49a2007-08-24 05:35:26 +0000309 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000310 Value *EmitMul(const BinOpInfo &Ops) {
Mike Stumpf71b7742009-04-02 18:15:54 +0000311 if (CGF.getContext().getLangOptions().OverflowChecking
312 && Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +0000313 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +0000314 if (Ops.LHS->getType()->isFPOrFPVector())
315 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000316 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
317 }
Mike Stumpdb789912009-04-01 20:28:16 +0000318 /// Create a binary op that checks for overflow.
319 /// Currently only supports +, - and *.
320 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000321 Value *EmitDiv(const BinOpInfo &Ops);
322 Value *EmitRem(const BinOpInfo &Ops);
323 Value *EmitAdd(const BinOpInfo &Ops);
324 Value *EmitSub(const BinOpInfo &Ops);
325 Value *EmitShl(const BinOpInfo &Ops);
326 Value *EmitShr(const BinOpInfo &Ops);
327 Value *EmitAnd(const BinOpInfo &Ops) {
328 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
329 }
330 Value *EmitXor(const BinOpInfo &Ops) {
331 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
332 }
333 Value *EmitOr (const BinOpInfo &Ops) {
334 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
335 }
336
Chris Lattner660e31d2007-08-24 21:00:35 +0000337 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000338 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000339 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
340
341 // Binary operators and binary compound assignment operators.
342#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000343 Value *VisitBin ## OP(const BinaryOperator *E) { \
344 return Emit ## OP(EmitBinOps(E)); \
345 } \
346 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
347 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000348 }
349 HANDLEBINOP(Mul);
350 HANDLEBINOP(Div);
351 HANDLEBINOP(Rem);
352 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000353 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000354 HANDLEBINOP(Shl);
355 HANDLEBINOP(Shr);
356 HANDLEBINOP(And);
357 HANDLEBINOP(Xor);
358 HANDLEBINOP(Or);
359#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000360
Chris Lattner9fba49a2007-08-24 05:35:26 +0000361 // Comparisons.
362 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
363 unsigned SICmpOpc, unsigned FCmpOpc);
364#define VISITCOMP(CODE, UI, SI, FP) \
365 Value *VisitBin##CODE(const BinaryOperator *E) { \
366 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
367 llvm::FCmpInst::FP); }
368 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
369 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
370 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
371 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
372 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
373 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
374#undef VISITCOMP
375
376 Value *VisitBinAssign (const BinaryOperator *E);
377
378 Value *VisitBinLAnd (const BinaryOperator *E);
379 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000380 Value *VisitBinComma (const BinaryOperator *E);
381
382 // Other Operators.
Mike Stump4eb81dc2009-02-12 18:29:15 +0000383 Value *VisitBlockExpr(const BlockExpr *BE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000384 Value *VisitConditionalOperator(const ConditionalOperator *CO);
385 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000386 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000387 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
388 return CGF.EmitObjCStringLiteral(E);
389 }
390};
391} // end anonymous namespace.
392
393//===----------------------------------------------------------------------===//
394// Utilities
395//===----------------------------------------------------------------------===//
396
Chris Lattnerd8d44222007-08-26 16:42:57 +0000397/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000398/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000399Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
400 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
401
402 if (SrcType->isRealFloatingType()) {
403 // Compare against 0.0 for fp scalars.
Owen Andersonf37b84b2009-07-31 20:28:54 +0000404 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000405 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
406 }
407
Anders Carlssone3ee66c2009-08-09 18:26:27 +0000408 if (SrcType->isMemberPointerType()) {
409 // FIXME: This is ABI specific.
410
411 // Compare against -1.
412 llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType());
413 return Builder.CreateICmpNE(Src, NegativeOne, "tobool");
414 }
415
Daniel Dunbar5d54eed2008-08-25 10:38:11 +0000416 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
Chris Lattnerd8d44222007-08-26 16:42:57 +0000417 "Unknown scalar type to convert");
418
419 // Because of the type rules of C, we often end up computing a logical value,
420 // then zero extending it to int, then wanting it as a logical value again.
421 // Optimize this common case.
422 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000423 if (ZI->getOperand(0)->getType() ==
424 llvm::Type::getInt1Ty(CGF.getLLVMContext())) {
Chris Lattnerd8d44222007-08-26 16:42:57 +0000425 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000426 // If there aren't any more uses, zap the instruction to save space.
427 // Note that there can be more uses, for example if this
428 // is the result of an assignment.
429 if (ZI->use_empty())
430 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000431 return Result;
432 }
433 }
434
435 // Compare against an integer or pointer null.
Owen Andersonf37b84b2009-07-31 20:28:54 +0000436 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000437 return Builder.CreateICmpNE(Src, Zero, "tobool");
438}
439
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000440/// EmitScalarConversion - Emit a conversion from the specified type to the
441/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000442Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
443 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000444 SrcType = CGF.getContext().getCanonicalType(SrcType);
445 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000446 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000447
448 if (DstType->isVoidType()) return 0;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000449
450 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000451
452 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000453 if (DstType->isBooleanType())
454 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000455
456 const llvm::Type *DstTy = ConvertType(DstType);
457
458 // Ignore conversions like int -> uint.
459 if (Src->getType() == DstTy)
460 return Src;
461
Daniel Dunbar238335f2008-08-25 09:51:32 +0000462 // Handle pointer conversions next: pointers can only be converted
463 // to/from other pointers and integers. Check for pointer types in
464 // terms of LLVM, as some native types (like Obj-C id) may map to a
465 // pointer type.
466 if (isa<llvm::PointerType>(DstTy)) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000467 // The source value may be an integer, or a pointer.
Fariborz Jahanianf7f6cf82009-07-28 22:00:58 +0000468 if (isa<llvm::PointerType>(Src->getType())) {
469 // Some heavy lifting for derived to base conversion.
Fariborz Jahanian90e18742009-07-29 00:44:13 +0000470 if (const CXXRecordDecl *ClassDecl =
471 SrcType->getCXXRecordDeclForPointerType())
472 if (const CXXRecordDecl *BaseClassDecl =
473 DstType->getCXXRecordDeclForPointerType())
474 Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000475 return Builder.CreateBitCast(Src, DstTy, "conv");
Fariborz Jahanianf7f6cf82009-07-28 22:00:58 +0000476 }
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000477 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
Eli Friedman35bcec82009-03-04 04:02:35 +0000478 // First, convert to the correct width so that we control the kind of
479 // extension.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000480 const llvm::Type *MiddleTy =
481 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Eli Friedman35bcec82009-03-04 04:02:35 +0000482 bool InputSigned = SrcType->isSignedIntegerType();
483 llvm::Value* IntResult =
484 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
485 // Then, cast to pointer.
486 return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000487 }
488
Daniel Dunbar238335f2008-08-25 09:51:32 +0000489 if (isa<llvm::PointerType>(Src->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000490 // Must be an ptr to int cast.
491 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000492 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000493 }
494
Nate Begemanaf6ed502008-04-18 23:10:10 +0000495 // A scalar can be splatted to an extended vector of the same element type
Nate Begemane85f43d2009-08-10 23:49:36 +0000496 if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
Nate Begeman7903d052009-01-18 06:42:49 +0000497 // Cast the scalar to element type
498 QualType EltTy = DstType->getAsExtVectorType()->getElementType();
499 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
500
501 // Insert the element in element zero of an undef vector
Owen Andersone0b5eff2009-07-30 23:11:26 +0000502 llvm::Value *UnV = llvm::UndefValue::get(DstTy);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000503 llvm::Value *Idx =
504 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Nate Begeman7903d052009-01-18 06:42:49 +0000505 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
506
507 // Splat the element across to all elements
508 llvm::SmallVector<llvm::Constant*, 16> Args;
509 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
510 for (unsigned i = 0; i < NumElements; i++)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000511 Args.push_back(llvm::ConstantInt::get(
512 llvm::Type::getInt32Ty(VMContext), 0));
Nate Begeman7903d052009-01-18 06:42:49 +0000513
Owen Anderson17971fa2009-07-28 21:22:35 +0000514 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Nate Begeman7903d052009-01-18 06:42:49 +0000515 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
516 return Yay;
517 }
Nate Begemanec2d1062007-12-30 02:59:45 +0000518
Chris Lattner4f025a42008-02-02 04:51:41 +0000519 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000520 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000521 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000522 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000523
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000524 // Finally, we have the arithmetic types: real int/float.
525 if (isa<llvm::IntegerType>(Src->getType())) {
526 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000527 if (isa<llvm::IntegerType>(DstTy))
528 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
529 else if (InputSigned)
530 return Builder.CreateSIToFP(Src, DstTy, "conv");
531 else
532 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000533 }
534
535 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
536 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000537 if (DstType->isSignedIntegerType())
538 return Builder.CreateFPToSI(Src, DstTy, "conv");
539 else
540 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000541 }
542
543 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000544 if (DstTy->getTypeID() < Src->getType()->getTypeID())
545 return Builder.CreateFPTrunc(Src, DstTy, "conv");
546 else
547 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000548}
549
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000550/// EmitComplexToScalarConversion - Emit a conversion from the specified
551/// complex type to the specified destination type, where the destination
552/// type is an LLVM scalar type.
553Value *ScalarExprEmitter::
554EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
555 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000556 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000557 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000558
559 // Handle conversions to bool first, they are special: comparisons against 0.
560 if (DstTy->isBooleanType()) {
561 // Complex != 0 -> (Real != 0) | (Imag != 0)
562 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
563 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
564 return Builder.CreateOr(Src.first, Src.second, "tobool");
565 }
566
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000567 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
568 // the imaginary part of the complex value is discarded and the value of the
569 // real part is converted according to the conversion rules for the
570 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000571 return EmitScalarConversion(Src.first, SrcTy, DstTy);
572}
573
574
Chris Lattner9fba49a2007-08-24 05:35:26 +0000575//===----------------------------------------------------------------------===//
576// Visitor Methods
577//===----------------------------------------------------------------------===//
578
579Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000580 CGF.ErrorUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000581 if (E->getType()->isVoidType())
582 return 0;
Owen Andersone0b5eff2009-07-30 23:11:26 +0000583 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000584}
585
Eli Friedmand0e9d092008-05-14 19:38:39 +0000586Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
587 llvm::SmallVector<llvm::Constant*, 32> indices;
588 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
589 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
590 }
591 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
592 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
Owen Anderson17971fa2009-07-28 21:22:35 +0000593 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
Eli Friedmand0e9d092008-05-14 19:38:39 +0000594 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
595}
596
Chris Lattner9fba49a2007-08-24 05:35:26 +0000597Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000598 TestAndClearIgnoreResultAssign();
599
Chris Lattner9fba49a2007-08-24 05:35:26 +0000600 // Emit subscript expressions in rvalue context's. For most cases, this just
601 // loads the lvalue formed by the subscript expr. However, we have to be
602 // careful, because the base of a vector subscript is occasionally an rvalue,
603 // so we can't get it as an lvalue.
604 if (!E->getBase()->getType()->isVectorType())
605 return EmitLoadOfLValue(E);
606
607 // Handle the vector case. The base must be a vector, the index must be an
608 // integer value.
609 Value *Base = Visit(E->getBase());
610 Value *Idx = Visit(E->getIdx());
Eli Friedman4a0073b2009-03-28 02:45:41 +0000611 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000612 Idx = Builder.CreateIntCast(Idx,
613 llvm::Type::getInt32Ty(CGF.getLLVMContext()),
614 IdxSigned,
Eli Friedmand4531942009-03-28 03:27:06 +0000615 "vecidxcast");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000616 return Builder.CreateExtractElement(Base, Idx, "vecext");
617}
618
Chris Lattner9fba49a2007-08-24 05:35:26 +0000619// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
620// have to handle a more broad range of conversions than explicit casts, as they
621// handle things like function to ptr-to-function decay etc.
Anders Carlsson2c61dbe2009-08-24 18:12:39 +0000622Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy,
623 CastExpr::CastKind Kind) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000624 if (!DestTy->isVoidType())
625 TestAndClearIgnoreResultAssign();
Anders Carlsson497794f2009-08-24 18:26:39 +0000626
627 switch (Kind) {
628 default:
629 break;
Anders Carlssonf1957c82009-09-01 20:52:42 +0000630 case CastExpr::CK_BitCast: {
631 Value *Src = Visit(const_cast<Expr*>(E));
632 return Builder.CreateBitCast(Src, ConvertType(DestTy));
633 }
Anders Carlssonbade0792009-08-24 18:37:17 +0000634 case CastExpr::CK_ArrayToPointerDecay: {
635 assert(E->getType()->isArrayType() &&
636 "Array to pointer decay must have array source type!");
637
638 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
639
640 // Note that VLA pointers are always decayed, so we don't need to do
641 // anything here.
642 if (!E->getType()->isVariableArrayType()) {
643 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
644 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
645 ->getElementType()) &&
646 "Expected pointer to array");
647 V = Builder.CreateStructGEP(V, 0, "arraydecay");
648 }
649
650 // The resultant pointer type can be implicitly casted to other pointer
651 // types as well (e.g. void*) and can be implicitly converted to integer.
652 const llvm::Type *DestLTy = ConvertType(DestTy);
653 if (V->getType() != DestLTy) {
654 if (isa<llvm::PointerType>(DestLTy))
655 V = Builder.CreateBitCast(V, DestLTy, "ptrconv");
656 else {
657 assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay");
658 V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv");
659 }
660 }
661 return V;
662 }
Anders Carlsson497794f2009-08-24 18:26:39 +0000663 case CastExpr::CK_NullToMemberPointer:
664 return CGF.CGM.EmitNullConstant(DestTy);
665 }
666
Chris Lattner82e10392007-08-26 07:26:12 +0000667 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000668
669 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000670 Value *Src = Visit(const_cast<Expr*>(E));
671
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000672 // Use EmitScalarConversion to perform the conversion.
673 return EmitScalarConversion(Src, E->getType(), DestTy);
674 }
Chris Lattner77288792008-02-16 23:55:16 +0000675
Chris Lattnerde0908b2008-04-04 16:54:41 +0000676 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000677 // Handle cases where the source is a complex type.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000678 bool IgnoreImag = true;
679 bool IgnoreImagAssign = true;
680 bool IgnoreReal = IgnoreResultAssign;
681 bool IgnoreRealAssign = IgnoreResultAssign;
682 if (DestTy->isBooleanType())
683 IgnoreImagAssign = IgnoreImag = false;
684 else if (DestTy->isVoidType()) {
685 IgnoreReal = IgnoreImag = false;
686 IgnoreRealAssign = IgnoreImagAssign = true;
687 }
688 CodeGenFunction::ComplexPairTy V
689 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign,
690 IgnoreImagAssign);
691 return EmitComplexToScalarConversion(V, E->getType(), DestTy);
Chris Lattner77288792008-02-16 23:55:16 +0000692 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000693
Chris Lattner77288792008-02-16 23:55:16 +0000694 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
695 // evaluate the result and return.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000696 CGF.EmitAggExpr(E, 0, false, true);
Chris Lattner77288792008-02-16 23:55:16 +0000697 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000698}
699
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000700Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000701 return CGF.EmitCompoundStmt(*E->getSubStmt(),
702 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000703}
704
Mike Stump2b6933f2009-02-28 09:07:16 +0000705Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
706 return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
Mike Stumpfca5da02009-02-21 20:00:35 +0000707}
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000708
Chris Lattner9fba49a2007-08-24 05:35:26 +0000709//===----------------------------------------------------------------------===//
710// Unary Operators
711//===----------------------------------------------------------------------===//
712
713Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000714 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000715 LValue LV = EmitLValue(E->getSubExpr());
Eli Friedman6a259872009-03-23 03:00:06 +0000716 QualType ValTy = E->getSubExpr()->getType();
717 Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000718
719 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000720
721 int AmountVal = isInc ? 1 : -1;
Eli Friedman4a0073b2009-03-28 02:45:41 +0000722
723 if (ValTy->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000724 ValTy->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +0000725 // The amount of the addition/subtraction needs to account for the VLA size
726 CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
727 }
728
Chris Lattner9fba49a2007-08-24 05:35:26 +0000729 Value *NextVal;
Chris Lattner8360c612009-03-18 04:25:13 +0000730 if (const llvm::PointerType *PT =
731 dyn_cast<llvm::PointerType>(InVal->getType())) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000732 llvm::Constant *Inc =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000733 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
Chris Lattner8360c612009-03-18 04:25:13 +0000734 if (!isa<llvm::FunctionType>(PT->getElementType())) {
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000735 QualType PTEE = ValTy->getPointeeType();
736 if (const ObjCInterfaceType *OIT =
737 dyn_cast<ObjCInterfaceType>(PTEE)) {
738 // Handle interface types, which are not represented with a concrete type.
739 int size = CGF.getContext().getTypeSize(OIT) / 8;
740 if (!isInc)
741 size = -size;
Owen Andersonb17ec712009-07-24 23:12:58 +0000742 Inc = llvm::ConstantInt::get(Inc->getType(), size);
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000743 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000744 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000745 InVal = Builder.CreateBitCast(InVal, i8Ty);
746 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
747 llvm::Value *lhs = LV.getAddress();
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000748 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000749 LV = LValue::MakeAddr(lhs, ValTy.getCVRQualifiers(),
750 CGF.getContext().getObjCGCAttrKind(ValTy));
Mike Stump487ce382009-07-30 22:28:39 +0000751 } else
Dan Gohman5a748242009-08-12 00:33:55 +0000752 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
Chris Lattner8360c612009-03-18 04:25:13 +0000753 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +0000754 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000755 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Chris Lattner8360c612009-03-18 04:25:13 +0000756 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
757 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
758 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
759 }
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000760 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
Chris Lattner49083172009-02-11 07:40:06 +0000761 // Bool++ is an interesting case, due to promotion rules, we get:
762 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
763 // Bool = ((int)Bool+1) != 0
764 // An interesting aspect of this is that increment is always true.
765 // Decrement does not have this property.
Owen Andersond3fd60e2009-07-31 17:39:36 +0000766 NextVal = llvm::ConstantInt::getTrue(VMContext);
Chris Lattner291a2b32009-06-17 06:36:24 +0000767 } else if (isa<llvm::IntegerType>(InVal->getType())) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000768 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Dan Gohmanc87cf1d2009-08-12 01:16:29 +0000769
770 // Signed integer overflow is undefined behavior.
771 if (ValTy->isSignedIntegerType())
772 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
773 else
774 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000775 } else {
776 // Add the inc/dec to the real part.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000777 if (InVal->getType() == llvm::Type::getFloatTy(VMContext))
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000778 NextVal =
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000779 llvm::ConstantFP::get(VMContext,
780 llvm::APFloat(static_cast<float>(AmountVal)));
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000781 else if (InVal->getType() == llvm::Type::getDoubleTy(VMContext))
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000782 NextVal =
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000783 llvm::ConstantFP::get(VMContext,
784 llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000785 else {
786 llvm::APFloat F(static_cast<float>(AmountVal));
Dale Johannesen2461f612008-10-09 23:02:32 +0000787 bool ignored;
788 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
789 &ignored);
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000790 NextVal = llvm::ConstantFP::get(VMContext, F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000791 }
Chris Lattner291a2b32009-06-17 06:36:24 +0000792 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000793 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000794
795 // Store the updated result through the lvalue.
Eli Friedman6a259872009-03-23 03:00:06 +0000796 if (LV.isBitfield())
797 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
798 &NextVal);
799 else
800 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000801
802 // If this is a postinc, return the value read from memory, otherwise use the
803 // updated value.
804 return isPre ? NextVal : InVal;
805}
806
807
808Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000809 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000810 Value *Op = Visit(E->getSubExpr());
Chris Lattner291a2b32009-06-17 06:36:24 +0000811 if (Op->getType()->isFPOrFPVector())
812 return Builder.CreateFNeg(Op, "neg");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000813 return Builder.CreateNeg(Op, "neg");
814}
815
816Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000817 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000818 Value *Op = Visit(E->getSubExpr());
819 return Builder.CreateNot(Op, "neg");
820}
821
822Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
823 // Compare operand to zero.
824 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
825
826 // Invert value.
827 // TODO: Could dynamically modify easy computations here. For example, if
828 // the operand is an icmp ne, turn into icmp eq.
829 BoolVal = Builder.CreateNot(BoolVal, "lnot");
830
Anders Carlsson62943f32009-05-19 18:44:53 +0000831 // ZExt result to the expr type.
832 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000833}
834
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000835/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
836/// argument of the sizeof expression as an integer.
837Value *
838ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000839 QualType TypeToSize = E->getTypeOfArgument();
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000840 if (E->isSizeOf()) {
841 if (const VariableArrayType *VAT =
842 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
843 if (E->isArgumentType()) {
844 // sizeof(type) - make sure to emit the VLA size.
845 CGF.EmitVLASize(TypeToSize);
Eli Friedman04659bd2009-04-20 03:21:44 +0000846 } else {
847 // C99 6.5.3.4p2: If the argument is an expression of type
848 // VLA, it is evaluated.
849 CGF.EmitAnyExpr(E->getArgumentExpr());
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000850 }
Anders Carlssond309f572009-01-30 16:41:04 +0000851
Anders Carlsson8f30de92009-02-05 19:43:10 +0000852 return CGF.GetVLASize(VAT);
Anders Carlsson6cb99b72008-12-21 03:33:21 +0000853 }
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000854 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000855
856 // If this isn't sizeof(vla), the result must be constant; use the
857 // constant folding logic so we don't have to duplicate it here.
858 Expr::EvalResult Result;
859 E->Evaluate(Result, CGF.getContext());
Owen Andersonb17ec712009-07-24 23:12:58 +0000860 return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000861}
862
Chris Lattner01211af2007-08-24 21:20:17 +0000863Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
864 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000865 if (Op->getType()->isAnyComplexType())
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000866 return CGF.EmitComplexExpr(Op, false, true, false, true).first;
Chris Lattner01211af2007-08-24 21:20:17 +0000867 return Visit(Op);
868}
869Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
870 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000871 if (Op->getType()->isAnyComplexType())
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000872 return CGF.EmitComplexExpr(Op, true, false, true, false).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000873
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000874 // __imag on a scalar returns zero. Emit the subexpr to ensure side
875 // effects are evaluated, but not the actual value.
876 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid)
877 CGF.EmitLValue(Op);
878 else
879 CGF.EmitScalarExpr(Op, true);
Owen Andersonf37b84b2009-07-31 20:28:54 +0000880 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000881}
882
Anders Carlsson52774ad2008-01-29 15:56:48 +0000883Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
884{
Eli Friedman342d9432009-02-27 06:44:11 +0000885 Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
Eli Friedmanccffea92009-01-24 22:38:55 +0000886 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedman342d9432009-02-27 06:44:11 +0000887 return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
Anders Carlsson52774ad2008-01-29 15:56:48 +0000888}
Chris Lattner01211af2007-08-24 21:20:17 +0000889
Chris Lattner9fba49a2007-08-24 05:35:26 +0000890//===----------------------------------------------------------------------===//
891// Binary Operators
892//===----------------------------------------------------------------------===//
893
894BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000895 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000896 BinOpInfo Result;
897 Result.LHS = Visit(E->getLHS());
898 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000899 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000900 Result.E = E;
901 return Result;
902}
903
Chris Lattner0d965302007-08-26 21:41:21 +0000904Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000905 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000906 bool Ignore = TestAndClearIgnoreResultAssign();
Chris Lattner660e31d2007-08-24 21:00:35 +0000907 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
908
909 BinOpInfo OpInfo;
910
Eli Friedman3cd92882009-03-28 01:22:36 +0000911 if (E->getComputationResultType()->isAnyComplexType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +0000912 // This needs to go through the complex expression emitter, but
Eli Friedman3cd92882009-03-28 01:22:36 +0000913 // it's a tad complicated to do that... I'm leaving it out for now.
914 // (Note that we do actually need the imaginary part of the RHS for
915 // multiplication and division.)
916 CGF.ErrorUnsupported(E, "complex compound assignment");
Owen Andersone0b5eff2009-07-30 23:11:26 +0000917 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Eli Friedman3cd92882009-03-28 01:22:36 +0000918 }
919
Mike Stump8d962262009-05-22 19:07:20 +0000920 // Emit the RHS first. __block variables need to have the rhs evaluated
921 // first, plus this should improve codegen a little.
922 OpInfo.RHS = Visit(E->getRHS());
923 OpInfo.Ty = E->getComputationResultType();
924 OpInfo.E = E;
Eli Friedman3cd92882009-03-28 01:22:36 +0000925 // Load/convert the LHS.
Chris Lattner660e31d2007-08-24 21:00:35 +0000926 LValue LHSLV = EmitLValue(E->getLHS());
927 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Eli Friedman3cd92882009-03-28 01:22:36 +0000928 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
929 E->getComputationLHSType());
Chris Lattner660e31d2007-08-24 21:00:35 +0000930
931 // Expand the binary operator.
932 Value *Result = (this->*Func)(OpInfo);
933
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000934 // Convert the result back to the LHS type.
Eli Friedman3cd92882009-03-28 01:22:36 +0000935 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
936
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000937 // Store the result value into the LHS lvalue. Bit-fields are
Daniel Dunbar2710fc92008-11-19 11:54:05 +0000938 // handled specially because the result is altered by the store,
939 // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
940 // the left operand after the assignment...'.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000941 if (LHSLV.isBitfield()) {
942 if (!LHSLV.isVolatileQualified()) {
943 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
944 &Result);
945 return Result;
946 } else
947 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy);
948 } else
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000949 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000950 if (Ignore)
951 return 0;
952 return EmitLoadOfLValue(LHSLV, E->getType());
Chris Lattner660e31d2007-08-24 21:00:35 +0000953}
954
955
Chris Lattner9fba49a2007-08-24 05:35:26 +0000956Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000957 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000958 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000959 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000960 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
961 else
962 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
963}
964
965Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
966 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000967 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000968 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
969 else
970 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
971}
972
Mike Stumpdb789912009-04-01 20:28:16 +0000973Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
974 unsigned IID;
975 unsigned OpID = 0;
Mike Stump0f595bb2009-04-02 01:03:55 +0000976
Mike Stumpf71b7742009-04-02 18:15:54 +0000977 switch (Ops.E->getOpcode()) {
978 case BinaryOperator::Add:
979 case BinaryOperator::AddAssign:
980 OpID = 1;
981 IID = llvm::Intrinsic::sadd_with_overflow;
982 break;
983 case BinaryOperator::Sub:
984 case BinaryOperator::SubAssign:
985 OpID = 2;
986 IID = llvm::Intrinsic::ssub_with_overflow;
987 break;
988 case BinaryOperator::Mul:
989 case BinaryOperator::MulAssign:
990 OpID = 3;
991 IID = llvm::Intrinsic::smul_with_overflow;
992 break;
993 default:
994 assert(false && "Unsupported operation for overflow detection");
Daniel Dunbar96e909b2009-04-08 16:23:09 +0000995 IID = 0;
Mike Stumpdb789912009-04-01 20:28:16 +0000996 }
Mike Stumpf71b7742009-04-02 18:15:54 +0000997 OpID <<= 1;
998 OpID |= 1;
999
Mike Stumpdb789912009-04-01 20:28:16 +00001000 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
1001
1002 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
1003
1004 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
1005 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
1006 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
1007
1008 // Branch in case of overflow.
1009 llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
1010 llvm::BasicBlock *overflowBB =
1011 CGF.createBasicBlock("overflow", CGF.CurFn);
1012 llvm::BasicBlock *continueBB =
1013 CGF.createBasicBlock("overflow.continue", CGF.CurFn);
1014
1015 Builder.CreateCondBr(overflow, overflowBB, continueBB);
1016
1017 // Handle overflow
1018
1019 Builder.SetInsertPoint(overflowBB);
1020
1021 // Handler is:
1022 // long long *__overflow_handler)(long long a, long long b, char op,
1023 // char width)
1024 std::vector<const llvm::Type*> handerArgTypes;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001025 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1026 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1027 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1028 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1029 llvm::FunctionType *handlerTy = llvm::FunctionType::get(
1030 llvm::Type::getInt64Ty(VMContext), handerArgTypes, false);
Mike Stumpdb789912009-04-01 20:28:16 +00001031 llvm::Value *handlerFunction =
1032 CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001033 llvm::PointerType::getUnqual(handlerTy));
Mike Stumpdb789912009-04-01 20:28:16 +00001034 handlerFunction = Builder.CreateLoad(handlerFunction);
1035
1036 llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001037 Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)),
1038 Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)),
1039 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID),
1040 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
Mike Stumpdb789912009-04-01 20:28:16 +00001041 cast<llvm::IntegerType>(opTy)->getBitWidth()));
1042
1043 handlerResult = Builder.CreateTrunc(handlerResult, opTy);
1044
1045 Builder.CreateBr(continueBB);
1046
1047 // Set up the continuation
1048 Builder.SetInsertPoint(continueBB);
1049 // Get the correct result
1050 llvm::PHINode *phi = Builder.CreatePHI(opTy);
1051 phi->reserveOperandSpace(2);
1052 phi->addIncoming(result, initialBB);
1053 phi->addIncoming(handlerResult, overflowBB);
1054
1055 return phi;
1056}
Chris Lattner9fba49a2007-08-24 05:35:26 +00001057
1058Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Steve Naroff79ae19a2009-07-14 18:25:06 +00001059 if (!Ops.Ty->isAnyPointerType()) {
Chris Lattner291a2b32009-06-17 06:36:24 +00001060 if (CGF.getContext().getLangOptions().OverflowChecking &&
1061 Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +00001062 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +00001063
1064 if (Ops.LHS->getType()->isFPOrFPVector())
1065 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add");
Dan Gohmanc87cf1d2009-08-12 01:16:29 +00001066
1067 // Signed integer overflow is undefined behavior.
1068 if (Ops.Ty->isSignedIntegerType())
1069 return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add");
1070
Chris Lattner9fba49a2007-08-24 05:35:26 +00001071 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Mike Stumpdb789912009-04-01 20:28:16 +00001072 }
Eli Friedman4a0073b2009-03-28 02:45:41 +00001073
Steve Naroff329ec222009-07-10 23:34:53 +00001074 if (Ops.Ty->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001075 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +00001076 // The amount of the addition needs to account for the VLA size
1077 CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
1078 }
Chris Lattner17c0cb02008-01-03 06:36:51 +00001079 Value *Ptr, *Idx;
1080 Expr *IdxExp;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001081 const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>();
Steve Naroff329ec222009-07-10 23:34:53 +00001082 const ObjCObjectPointerType *OPT =
1083 Ops.E->getLHS()->getType()->getAsObjCObjectPointerType();
1084 if (PT || OPT) {
Chris Lattner17c0cb02008-01-03 06:36:51 +00001085 Ptr = Ops.LHS;
1086 Idx = Ops.RHS;
1087 IdxExp = Ops.E->getRHS();
Steve Naroff329ec222009-07-10 23:34:53 +00001088 } else { // int + pointer
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001089 PT = Ops.E->getRHS()->getType()->getAs<PointerType>();
Steve Naroff329ec222009-07-10 23:34:53 +00001090 OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType();
1091 assert((PT || OPT) && "Invalid add expr");
Chris Lattner17c0cb02008-01-03 06:36:51 +00001092 Ptr = Ops.RHS;
1093 Idx = Ops.LHS;
1094 IdxExp = Ops.E->getLHS();
1095 }
1096
1097 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +00001098 if (Width < CGF.LLVMPointerWidth) {
Chris Lattner17c0cb02008-01-03 06:36:51 +00001099 // Zero or sign extend the pointer value based on whether the index is
1100 // signed or not.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001101 const llvm::Type *IdxType =
1102 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +00001103 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +00001104 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1105 else
1106 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1107 }
Steve Naroff329ec222009-07-10 23:34:53 +00001108 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001109 // Handle interface types, which are not represented with a concrete
1110 // type.
1111 if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
1112 llvm::Value *InterfaceSize =
Owen Andersonb17ec712009-07-24 23:12:58 +00001113 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001114 CGF.getContext().getTypeSize(OIT) / 8);
1115 Idx = Builder.CreateMul(Idx, InterfaceSize);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001116 const llvm::Type *i8Ty =
1117 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001118 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
1119 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
1120 return Builder.CreateBitCast(Res, Ptr->getType());
1121 }
1122
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001123 // Explicitly handle GNU void* and function pointer arithmetic
1124 // extensions. The GNU void* casts amount to no-ops since our void*
1125 // type is i8*, but this is future proof.
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001126 if (ElementType->isVoidType() || ElementType->isFunctionType()) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001127 const llvm::Type *i8Ty =
1128 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001129 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001130 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001131 return Builder.CreateBitCast(Res, Ptr->getType());
1132 }
Chris Lattner17c0cb02008-01-03 06:36:51 +00001133
Dan Gohman5a748242009-08-12 00:33:55 +00001134 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001135}
1136
1137Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
Mike Stumpdb789912009-04-01 20:28:16 +00001138 if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
Mike Stumpf71b7742009-04-02 18:15:54 +00001139 if (CGF.getContext().getLangOptions().OverflowChecking
1140 && Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +00001141 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +00001142
1143 if (Ops.LHS->getType()->isFPOrFPVector())
1144 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001145 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Mike Stumpdb789912009-04-01 20:28:16 +00001146 }
Chris Lattner660e31d2007-08-24 21:00:35 +00001147
Steve Naroff329ec222009-07-10 23:34:53 +00001148 if (Ops.E->getLHS()->getType()->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001149 Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +00001150 // The amount of the addition needs to account for the VLA size for
1151 // ptr-int
1152 // The amount of the division needs to account for the VLA size for
1153 // ptr-ptr.
1154 CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
1155 }
1156
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001157 const QualType LHSType = Ops.E->getLHS()->getType();
Steve Naroff329ec222009-07-10 23:34:53 +00001158 const QualType LHSElementType = LHSType->getPointeeType();
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001159 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
1160 // pointer - int
1161 Value *Idx = Ops.RHS;
1162 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +00001163 if (Width < CGF.LLVMPointerWidth) {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001164 // Zero or sign extend the pointer value based on whether the index is
1165 // signed or not.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001166 const llvm::Type *IdxType =
1167 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001168 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1169 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1170 else
1171 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1172 }
1173 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001174
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001175 // Handle interface types, which are not represented with a concrete
1176 // type.
1177 if (const ObjCInterfaceType *OIT =
1178 dyn_cast<ObjCInterfaceType>(LHSElementType)) {
1179 llvm::Value *InterfaceSize =
Owen Andersonb17ec712009-07-24 23:12:58 +00001180 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001181 CGF.getContext().getTypeSize(OIT) / 8);
1182 Idx = Builder.CreateMul(Idx, InterfaceSize);
Owen Anderson73e7f802009-07-14 23:10:40 +00001183 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001184 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001185 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1186 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
1187 return Builder.CreateBitCast(Res, Ops.LHS->getType());
1188 }
1189
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001190 // Explicitly handle GNU void* and function pointer arithmetic
1191 // extensions. The GNU void* casts amount to no-ops since our
1192 // void* type is i8*, but this is future proof.
1193 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001194 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001195 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001196 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1197 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1198 return Builder.CreateBitCast(Res, Ops.LHS->getType());
1199 }
1200
Dan Gohman5a748242009-08-12 00:33:55 +00001201 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001202 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001203 // pointer - pointer
1204 Value *LHS = Ops.LHS;
1205 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +00001206
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001207 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001208
Chris Lattner6d2e3492009-02-11 07:21:43 +00001209 // Handle GCC extension for pointer arithmetic on void* and function pointer
1210 // types.
1211 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001212 ElementSize = 1;
1213 } else {
1214 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1215 }
1216
1217 const llvm::Type *ResultType = ConvertType(Ops.Ty);
1218 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1219 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1220 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
1221
Chris Lattner6d2e3492009-02-11 07:21:43 +00001222 // Optimize out the shift for element size of 1.
1223 if (ElementSize == 1)
1224 return BytesBetween;
Dan Gohman8f61ba42009-08-11 22:40:09 +00001225
1226 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
1227 // pointer difference in C is only defined in the case where both
1228 // operands are pointing to elements of an array.
Owen Andersonb17ec712009-07-24 23:12:58 +00001229 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
Dan Gohman8f61ba42009-08-11 22:40:09 +00001230 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001231 }
Chris Lattner9fba49a2007-08-24 05:35:26 +00001232}
1233
1234Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
1235 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1236 // RHS to the same size as the LHS.
1237 Value *RHS = Ops.RHS;
1238 if (Ops.LHS->getType() != RHS->getType())
1239 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1240
1241 return Builder.CreateShl(Ops.LHS, RHS, "shl");
1242}
1243
1244Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1245 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1246 // RHS to the same size as the LHS.
1247 Value *RHS = Ops.RHS;
1248 if (Ops.LHS->getType() != RHS->getType())
1249 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1250
Chris Lattner660e31d2007-08-24 21:00:35 +00001251 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +00001252 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1253 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1254}
1255
1256Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1257 unsigned SICmpOpc, unsigned FCmpOpc) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001258 TestAndClearIgnoreResultAssign();
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001259 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001260 QualType LHSTy = E->getLHS()->getType();
Chris Lattner08ac8522009-07-08 01:08:03 +00001261 if (!LHSTy->isAnyComplexType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001262 Value *LHS = Visit(E->getLHS());
1263 Value *RHS = Visit(E->getRHS());
1264
Eli Friedman04865bc2009-07-22 06:07:16 +00001265 if (LHS->getType()->isFPOrFPVector()) {
Nate Begeman1591bc52008-07-25 20:16:05 +00001266 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001267 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +00001268 } else if (LHSTy->isSignedIntegerType()) {
1269 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001270 LHS, RHS, "cmp");
1271 } else {
Eli Friedman850ea372008-05-29 15:09:15 +00001272 // Unsigned integers and pointers.
1273 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001274 LHS, RHS, "cmp");
1275 }
Chris Lattner08ac8522009-07-08 01:08:03 +00001276
1277 // If this is a vector comparison, sign extend the result to the appropriate
1278 // vector integer type and return it (don't convert to bool).
1279 if (LHSTy->isVectorType())
1280 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
Nate Begeman1591bc52008-07-25 20:16:05 +00001281
Chris Lattner9fba49a2007-08-24 05:35:26 +00001282 } else {
1283 // Complex Comparison: can only be an equality comparison.
1284 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1285 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1286
Chris Lattnerc154ac12008-07-26 22:37:01 +00001287 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001288
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001289 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001290 if (CETy->isRealFloatingType()) {
1291 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1292 LHS.first, RHS.first, "cmp.r");
1293 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1294 LHS.second, RHS.second, "cmp.i");
1295 } else {
1296 // Complex comparisons can only be equality comparisons. As such, signed
1297 // and unsigned opcodes are the same.
1298 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1299 LHS.first, RHS.first, "cmp.r");
1300 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1301 LHS.second, RHS.second, "cmp.i");
1302 }
1303
1304 if (E->getOpcode() == BinaryOperator::EQ) {
1305 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1306 } else {
1307 assert(E->getOpcode() == BinaryOperator::NE &&
1308 "Complex comparison other than == or != ?");
1309 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1310 }
1311 }
Nuno Lopes92577002009-01-11 23:22:37 +00001312
1313 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001314}
1315
1316Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001317 bool Ignore = TestAndClearIgnoreResultAssign();
1318
1319 // __block variables need to have the rhs evaluated first, plus this should
1320 // improve codegen just a little.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001321 Value *RHS = Visit(E->getRHS());
Mike Stump68df15c2009-05-21 21:05:15 +00001322 LValue LHS = EmitLValue(E->getLHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001323
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001324 // Store the value into the LHS. Bit-fields are handled specially
Daniel Dunbar2710fc92008-11-19 11:54:05 +00001325 // because the result is altered by the store, i.e., [C99 6.5.16p1]
1326 // 'An assignment expression has the value of the left operand after
Eli Friedman4a0073b2009-03-28 02:45:41 +00001327 // the assignment...'.
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001328 if (LHS.isBitfield()) {
1329 if (!LHS.isVolatileQualified()) {
1330 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1331 &RHS);
1332 return RHS;
1333 } else
1334 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType());
1335 } else
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001336 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001337 if (Ignore)
1338 return 0;
1339 return EmitLoadOfLValue(LHS, E->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001340}
1341
1342Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001343 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1344 // If we have 1 && X, just emit X without inserting the control flow.
1345 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1346 if (Cond == 1) { // If we have 1 && X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001347 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1348 // ZExt result to int.
1349 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1350 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001351
1352 // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1353 if (!CGF.ContainsLabel(E->getRHS()))
Owen Andersonf37b84b2009-07-31 20:28:54 +00001354 return llvm::Constant::getNullValue(CGF.LLVMIntTy);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001355 }
1356
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001357 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1358 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
Chris Lattner715c2a72008-11-12 08:26:50 +00001359
Chris Lattner7f80bb32008-11-12 08:38:24 +00001360 // Branch on the LHS first. If it is false, go to the failure (cont) block.
1361 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1362
1363 // Any edges into the ContBlock are now from an (indeterminate number of)
1364 // edges from this first condition. All of these values will be false. Start
1365 // setting up the PHI node in the Cont Block for this.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001366 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1367 "", ContBlock);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001368 PN->reserveOperandSpace(2); // Normal case, two inputs.
1369 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1370 PI != PE; ++PI)
Owen Andersond3fd60e2009-07-31 17:39:36 +00001371 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001372
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001373 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001374 CGF.EmitBlock(RHSBlock);
1375 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001376 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001377
1378 // Reaquire the RHS block, as there may be subblocks inserted.
1379 RHSBlock = Builder.GetInsertBlock();
Chris Lattner7f80bb32008-11-12 08:38:24 +00001380
1381 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1382 // into the phi node for the edge with the value of RHSCond.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001383 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001384 PN->addIncoming(RHSCond, RHSBlock);
1385
1386 // ZExt result to int.
1387 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1388}
1389
1390Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001391 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1392 // If we have 0 || X, just emit X without inserting the control flow.
1393 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1394 if (Cond == -1) { // If we have 0 || X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001395 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1396 // ZExt result to int.
1397 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1398 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001399
Eli Friedmanea137cd2008-12-02 16:02:46 +00001400 // 1 || RHS: If it is safe, just elide the RHS, and return 1.
Chris Lattner715c2a72008-11-12 08:26:50 +00001401 if (!CGF.ContainsLabel(E->getRHS()))
Owen Andersonb17ec712009-07-24 23:12:58 +00001402 return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001403 }
1404
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001405 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1406 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001407
Chris Lattner7f80bb32008-11-12 08:38:24 +00001408 // Branch on the LHS first. If it is true, go to the success (cont) block.
1409 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1410
1411 // Any edges into the ContBlock are now from an (indeterminate number of)
1412 // edges from this first condition. All of these values will be true. Start
1413 // setting up the PHI node in the Cont Block for this.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001414 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1415 "", ContBlock);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001416 PN->reserveOperandSpace(2); // Normal case, two inputs.
1417 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1418 PI != PE; ++PI)
Owen Andersond3fd60e2009-07-31 17:39:36 +00001419 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001420
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001421 CGF.PushConditionalTempDestruction();
1422
Chris Lattner7f80bb32008-11-12 08:38:24 +00001423 // Emit the RHS condition as a bool value.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001424 CGF.EmitBlock(RHSBlock);
1425 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1426
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001427 CGF.PopConditionalTempDestruction();
1428
Chris Lattner9fba49a2007-08-24 05:35:26 +00001429 // Reaquire the RHS block, as there may be subblocks inserted.
1430 RHSBlock = Builder.GetInsertBlock();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001431
Chris Lattner7f80bb32008-11-12 08:38:24 +00001432 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1433 // into the phi node for the edge with the value of RHSCond.
1434 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001435 PN->addIncoming(RHSCond, RHSBlock);
1436
1437 // ZExt result to int.
1438 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1439}
1440
1441Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1442 CGF.EmitStmt(E->getLHS());
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00001443 CGF.EnsureInsertPoint();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001444 return Visit(E->getRHS());
1445}
1446
1447//===----------------------------------------------------------------------===//
1448// Other Operators
1449//===----------------------------------------------------------------------===//
1450
Chris Lattner504a5282008-11-12 08:55:54 +00001451/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1452/// expression is cheap enough and side-effect-free enough to evaluate
1453/// unconditionally instead of conditionally. This is used to convert control
1454/// flow into selects in some cases.
1455static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1456 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1457 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1458
1459 // TODO: Allow anything we can constant fold to an integer or fp constant.
1460 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1461 isa<FloatingLiteral>(E))
1462 return true;
1463
1464 // Non-volatile automatic variables too, to get "cond ? X : Y" where
1465 // X and Y are local variables.
1466 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1467 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1468 if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1469 return true;
1470
1471 return false;
1472}
1473
1474
Chris Lattner9fba49a2007-08-24 05:35:26 +00001475Value *ScalarExprEmitter::
1476VisitConditionalOperator(const ConditionalOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001477 TestAndClearIgnoreResultAssign();
Chris Lattner3d6606b2008-11-12 08:04:58 +00001478 // If the condition constant folds and can be elided, try to avoid emitting
1479 // the condition and the dead arm.
1480 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
Chris Lattner044bffc2008-11-11 18:56:45 +00001481 Expr *Live = E->getLHS(), *Dead = E->getRHS();
Chris Lattner3d6606b2008-11-12 08:04:58 +00001482 if (Cond == -1)
Chris Lattner044bffc2008-11-11 18:56:45 +00001483 std::swap(Live, Dead);
Chris Lattner3d6606b2008-11-12 08:04:58 +00001484
1485 // If the dead side doesn't have labels we need, and if the Live side isn't
1486 // the gnu missing ?: extension (which we could handle, but don't bother
1487 // to), just emit the Live part.
1488 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part
1489 Live) // Live part isn't missing.
1490 return Visit(Live);
Chris Lattner044bffc2008-11-11 18:56:45 +00001491 }
1492
Chris Lattner504a5282008-11-12 08:55:54 +00001493
1494 // If this is a really simple expression (like x ? 4 : 5), emit this as a
1495 // select instead of as control flow. We can only do this if it is cheap and
Chris Lattner1f11af22008-11-16 06:16:27 +00001496 // safe to evaluate the LHS and RHS unconditionally.
Chris Lattner504a5282008-11-12 08:55:54 +00001497 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1498 isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1499 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1500 llvm::Value *LHS = Visit(E->getLHS());
1501 llvm::Value *RHS = Visit(E->getRHS());
1502 return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1503 }
1504
1505
Daniel Dunbarb23e9922008-11-12 10:13:37 +00001506 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1507 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001508 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner67e22462008-11-12 08:08:13 +00001509 Value *CondVal = 0;
Chris Lattner3d6606b2008-11-12 08:04:58 +00001510
Chris Lattner86031712009-02-13 23:35:32 +00001511 // If we don't have the GNU missing condition extension, emit a branch on
1512 // bool the normal way.
1513 if (E->getLHS()) {
1514 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1515 // the branch on bool.
1516 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1517 } else {
1518 // Otherwise, for the ?: extension, evaluate the conditional and then
1519 // convert it to bool the hard way. We do this explicitly because we need
1520 // the unconverted value for the missing middle value of the ?:.
Chris Lattner67e22462008-11-12 08:08:13 +00001521 CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattner86031712009-02-13 23:35:32 +00001522
1523 // In some cases, EmitScalarConversion will delete the "CondVal" expression
1524 // if there are no extra uses (an optimization). Inhibit this by making an
1525 // extra dead use, because we're going to add a use of CondVal later. We
1526 // don't use the builder for this, because we don't want it to get optimized
1527 // away. This leaves dead code, but the ?: extension isn't common.
1528 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1529 Builder.GetInsertBlock());
1530
Chris Lattner67e22462008-11-12 08:08:13 +00001531 Value *CondBoolVal =
1532 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1533 CGF.getContext().BoolTy);
1534 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner67e22462008-11-12 08:08:13 +00001535 }
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001536
1537 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001538 CGF.EmitBlock(LHSBlock);
1539
1540 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001541 Value *LHS;
1542 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001543 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001544 else // Perform promotions, to handle cases like "short ?: int"
1545 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1546
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001547 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001548 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001549 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001550
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001551 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001552 CGF.EmitBlock(RHSBlock);
1553
Eli Friedmance8d7032008-05-16 20:38:39 +00001554 Value *RHS = Visit(E->getRHS());
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001555 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001556 RHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001557 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001558
1559 CGF.EmitBlock(ContBlock);
1560
Nuno Lopesb62ff242008-06-04 19:15:45 +00001561 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001562 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1563 return 0;
1564 }
1565
Chris Lattner9fba49a2007-08-24 05:35:26 +00001566 // Create a PHI node for the real part.
1567 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1568 PN->reserveOperandSpace(2);
1569 PN->addIncoming(LHS, LHSBlock);
1570 PN->addIncoming(RHS, RHSBlock);
1571 return PN;
1572}
1573
1574Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmand540c112009-03-04 05:52:32 +00001575 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattner9fba49a2007-08-24 05:35:26 +00001576}
1577
Chris Lattner307da022007-11-30 17:56:23 +00001578Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Eli Friedman8f5e8782009-01-20 17:46:04 +00001579 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +00001580 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1581
1582 // If EmitVAArg fails, we fall back to the LLVM instruction.
1583 if (!ArgPtr)
1584 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1585
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001586 // FIXME Volatility.
Anders Carlsson285611e2008-11-04 05:30:00 +00001587 return Builder.CreateLoad(ArgPtr);
Anders Carlsson36760332007-10-15 20:28:48 +00001588}
1589
Mike Stump4eb81dc2009-02-12 18:29:15 +00001590Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
Mike Stump1fa52fe2009-03-07 02:35:30 +00001591 return CGF.BuildBlockLiteralTmp(BE);
Mike Stump4eb81dc2009-02-12 18:29:15 +00001592}
1593
Chris Lattner9fba49a2007-08-24 05:35:26 +00001594//===----------------------------------------------------------------------===//
1595// Entry Point into this File
1596//===----------------------------------------------------------------------===//
1597
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001598/// EmitScalarExpr - Emit the computation of the specified expression of
1599/// scalar type, ignoring the result.
1600Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001601 assert(E && !hasAggregateLLVMType(E->getType()) &&
1602 "Invalid scalar expression to emit");
1603
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001604 return ScalarExprEmitter(*this, IgnoreResultAssign)
1605 .Visit(const_cast<Expr*>(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +00001606}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001607
1608/// EmitScalarConversion - Emit a conversion from the specified type to the
1609/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001610Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1611 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001612 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1613 "Invalid scalar expression to emit");
1614 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1615}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001616
1617/// EmitComplexToScalarConversion - Emit a conversion from the specified
1618/// complex type to the specified destination type, where the destination
1619/// type is an LLVM scalar type.
1620Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1621 QualType SrcTy,
1622 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001623 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001624 "Invalid complex -> scalar conversion");
1625 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1626 DstTy);
1627}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001628
1629Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1630 assert(V1->getType() == V2->getType() &&
1631 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001632 unsigned NumElements =
1633 cast<llvm::VectorType>(V1->getType())->getNumElements();
1634
1635 va_list va;
1636 va_start(va, V2);
1637
1638 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001639 for (unsigned i = 0; i < NumElements; i++) {
1640 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001641 assert(n >= 0 && n < (int)NumElements * 2 &&
1642 "Vector shuffle index out of bounds!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001643 Args.push_back(llvm::ConstantInt::get(
1644 llvm::Type::getInt32Ty(VMContext), n));
Anders Carlssona9234fe2007-12-10 19:35:18 +00001645 }
1646
1647 const char *Name = va_arg(va, const char *);
1648 va_end(va);
1649
Owen Anderson17971fa2009-07-28 21:22:35 +00001650 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001651
1652 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1653}
1654
Anders Carlsson68b8be92007-12-15 21:23:30 +00001655llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001656 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001657 llvm::Value *Vec
Owen Andersone0b5eff2009-07-30 23:11:26 +00001658 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001659
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001660 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001661 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001662 llvm::Value *Idx = llvm::ConstantInt::get(
1663 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001664 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001665 }
1666
1667 return Vec;
1668}