blob: 8732dc91309a68a386d3e3554025e0df3e80764f [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 }
Douglas Gregor3e368512009-09-04 17:36:40 +0000308
309 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
310 // C++ [expr.pseudo]p1:
311 // The result shall only be used as the operand for the function call
312 // operator (), and the result of such a call has type void. The only
313 // effect is the evaluation of the postfix-expression before the dot or
314 // arrow.
315 CGF.EmitScalarExpr(E->getBase());
316 return 0;
317 }
318
Chris Lattner9fba49a2007-08-24 05:35:26 +0000319 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000320 Value *EmitMul(const BinOpInfo &Ops) {
Mike Stumpf71b7742009-04-02 18:15:54 +0000321 if (CGF.getContext().getLangOptions().OverflowChecking
322 && Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +0000323 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +0000324 if (Ops.LHS->getType()->isFPOrFPVector())
325 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000326 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
327 }
Mike Stumpdb789912009-04-01 20:28:16 +0000328 /// Create a binary op that checks for overflow.
329 /// Currently only supports +, - and *.
330 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000331 Value *EmitDiv(const BinOpInfo &Ops);
332 Value *EmitRem(const BinOpInfo &Ops);
333 Value *EmitAdd(const BinOpInfo &Ops);
334 Value *EmitSub(const BinOpInfo &Ops);
335 Value *EmitShl(const BinOpInfo &Ops);
336 Value *EmitShr(const BinOpInfo &Ops);
337 Value *EmitAnd(const BinOpInfo &Ops) {
338 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
339 }
340 Value *EmitXor(const BinOpInfo &Ops) {
341 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
342 }
343 Value *EmitOr (const BinOpInfo &Ops) {
344 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
345 }
346
Chris Lattner660e31d2007-08-24 21:00:35 +0000347 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000348 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000349 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
350
351 // Binary operators and binary compound assignment operators.
352#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000353 Value *VisitBin ## OP(const BinaryOperator *E) { \
354 return Emit ## OP(EmitBinOps(E)); \
355 } \
356 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
357 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000358 }
359 HANDLEBINOP(Mul);
360 HANDLEBINOP(Div);
361 HANDLEBINOP(Rem);
362 HANDLEBINOP(Add);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000363 HANDLEBINOP(Sub);
Chris Lattner660e31d2007-08-24 21:00:35 +0000364 HANDLEBINOP(Shl);
365 HANDLEBINOP(Shr);
366 HANDLEBINOP(And);
367 HANDLEBINOP(Xor);
368 HANDLEBINOP(Or);
369#undef HANDLEBINOP
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000370
Chris Lattner9fba49a2007-08-24 05:35:26 +0000371 // Comparisons.
372 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
373 unsigned SICmpOpc, unsigned FCmpOpc);
374#define VISITCOMP(CODE, UI, SI, FP) \
375 Value *VisitBin##CODE(const BinaryOperator *E) { \
376 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
377 llvm::FCmpInst::FP); }
378 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
379 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
380 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
381 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
382 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
383 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
384#undef VISITCOMP
385
386 Value *VisitBinAssign (const BinaryOperator *E);
387
388 Value *VisitBinLAnd (const BinaryOperator *E);
389 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000390 Value *VisitBinComma (const BinaryOperator *E);
391
392 // Other Operators.
Mike Stump4eb81dc2009-02-12 18:29:15 +0000393 Value *VisitBlockExpr(const BlockExpr *BE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000394 Value *VisitConditionalOperator(const ConditionalOperator *CO);
395 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000396 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000397 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
398 return CGF.EmitObjCStringLiteral(E);
399 }
400};
401} // end anonymous namespace.
402
403//===----------------------------------------------------------------------===//
404// Utilities
405//===----------------------------------------------------------------------===//
406
Chris Lattnerd8d44222007-08-26 16:42:57 +0000407/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000408/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000409Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
410 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
411
412 if (SrcType->isRealFloatingType()) {
413 // Compare against 0.0 for fp scalars.
Owen Andersonf37b84b2009-07-31 20:28:54 +0000414 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000415 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
416 }
417
Anders Carlssone3ee66c2009-08-09 18:26:27 +0000418 if (SrcType->isMemberPointerType()) {
419 // FIXME: This is ABI specific.
420
421 // Compare against -1.
422 llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType());
423 return Builder.CreateICmpNE(Src, NegativeOne, "tobool");
424 }
425
Daniel Dunbar5d54eed2008-08-25 10:38:11 +0000426 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
Chris Lattnerd8d44222007-08-26 16:42:57 +0000427 "Unknown scalar type to convert");
428
429 // Because of the type rules of C, we often end up computing a logical value,
430 // then zero extending it to int, then wanting it as a logical value again.
431 // Optimize this common case.
432 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000433 if (ZI->getOperand(0)->getType() ==
434 llvm::Type::getInt1Ty(CGF.getLLVMContext())) {
Chris Lattnerd8d44222007-08-26 16:42:57 +0000435 Value *Result = ZI->getOperand(0);
Eli Friedman24f33972008-01-29 18:13:51 +0000436 // If there aren't any more uses, zap the instruction to save space.
437 // Note that there can be more uses, for example if this
438 // is the result of an assignment.
439 if (ZI->use_empty())
440 ZI->eraseFromParent();
Chris Lattnerd8d44222007-08-26 16:42:57 +0000441 return Result;
442 }
443 }
444
445 // Compare against an integer or pointer null.
Owen Andersonf37b84b2009-07-31 20:28:54 +0000446 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000447 return Builder.CreateICmpNE(Src, Zero, "tobool");
448}
449
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000450/// EmitScalarConversion - Emit a conversion from the specified type to the
451/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000452Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
453 QualType DstType) {
Chris Lattnerc154ac12008-07-26 22:37:01 +0000454 SrcType = CGF.getContext().getCanonicalType(SrcType);
455 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000456 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000457
458 if (DstType->isVoidType()) return 0;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000459
460 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000461
462 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000463 if (DstType->isBooleanType())
464 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000465
466 const llvm::Type *DstTy = ConvertType(DstType);
467
468 // Ignore conversions like int -> uint.
469 if (Src->getType() == DstTy)
470 return Src;
471
Daniel Dunbar238335f2008-08-25 09:51:32 +0000472 // Handle pointer conversions next: pointers can only be converted
473 // to/from other pointers and integers. Check for pointer types in
474 // terms of LLVM, as some native types (like Obj-C id) may map to a
475 // pointer type.
476 if (isa<llvm::PointerType>(DstTy)) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000477 // The source value may be an integer, or a pointer.
Fariborz Jahanianf7f6cf82009-07-28 22:00:58 +0000478 if (isa<llvm::PointerType>(Src->getType())) {
479 // Some heavy lifting for derived to base conversion.
Fariborz Jahanian90e18742009-07-29 00:44:13 +0000480 if (const CXXRecordDecl *ClassDecl =
481 SrcType->getCXXRecordDeclForPointerType())
482 if (const CXXRecordDecl *BaseClassDecl =
483 DstType->getCXXRecordDeclForPointerType())
484 Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000485 return Builder.CreateBitCast(Src, DstTy, "conv");
Fariborz Jahanianf7f6cf82009-07-28 22:00:58 +0000486 }
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000487 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
Eli Friedman35bcec82009-03-04 04:02:35 +0000488 // First, convert to the correct width so that we control the kind of
489 // extension.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000490 const llvm::Type *MiddleTy =
491 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Eli Friedman35bcec82009-03-04 04:02:35 +0000492 bool InputSigned = SrcType->isSignedIntegerType();
493 llvm::Value* IntResult =
494 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
495 // Then, cast to pointer.
496 return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000497 }
498
Daniel Dunbar238335f2008-08-25 09:51:32 +0000499 if (isa<llvm::PointerType>(Src->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000500 // Must be an ptr to int cast.
501 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson44db38f2007-10-31 23:18:02 +0000502 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000503 }
504
Nate Begemanaf6ed502008-04-18 23:10:10 +0000505 // A scalar can be splatted to an extended vector of the same element type
Nate Begemane85f43d2009-08-10 23:49:36 +0000506 if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
Nate Begeman7903d052009-01-18 06:42:49 +0000507 // Cast the scalar to element type
508 QualType EltTy = DstType->getAsExtVectorType()->getElementType();
509 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
510
511 // Insert the element in element zero of an undef vector
Owen Andersone0b5eff2009-07-30 23:11:26 +0000512 llvm::Value *UnV = llvm::UndefValue::get(DstTy);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000513 llvm::Value *Idx =
514 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Nate Begeman7903d052009-01-18 06:42:49 +0000515 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
516
517 // Splat the element across to all elements
518 llvm::SmallVector<llvm::Constant*, 16> Args;
519 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
520 for (unsigned i = 0; i < NumElements; i++)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000521 Args.push_back(llvm::ConstantInt::get(
522 llvm::Type::getInt32Ty(VMContext), 0));
Nate Begeman7903d052009-01-18 06:42:49 +0000523
Owen Anderson17971fa2009-07-28 21:22:35 +0000524 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Nate Begeman7903d052009-01-18 06:42:49 +0000525 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
526 return Yay;
527 }
Nate Begemanec2d1062007-12-30 02:59:45 +0000528
Chris Lattner4f025a42008-02-02 04:51:41 +0000529 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000530 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner4f025a42008-02-02 04:51:41 +0000531 isa<llvm::VectorType>(DstTy))
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000532 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson4513ecb2007-12-05 07:36:10 +0000533
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000534 // Finally, we have the arithmetic types: real int/float.
535 if (isa<llvm::IntegerType>(Src->getType())) {
536 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000537 if (isa<llvm::IntegerType>(DstTy))
538 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
539 else if (InputSigned)
540 return Builder.CreateSIToFP(Src, DstTy, "conv");
541 else
542 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000543 }
544
545 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
546 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000547 if (DstType->isSignedIntegerType())
548 return Builder.CreateFPToSI(Src, DstTy, "conv");
549 else
550 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000551 }
552
553 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlsson4dac3f42007-12-26 18:20:19 +0000554 if (DstTy->getTypeID() < Src->getType()->getTypeID())
555 return Builder.CreateFPTrunc(Src, DstTy, "conv");
556 else
557 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000558}
559
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000560/// EmitComplexToScalarConversion - Emit a conversion from the specified
561/// complex type to the specified destination type, where the destination
562/// type is an LLVM scalar type.
563Value *ScalarExprEmitter::
564EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
565 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000566 // Get the source element type.
Chris Lattnerc154ac12008-07-26 22:37:01 +0000567 SrcTy = SrcTy->getAsComplexType()->getElementType();
Chris Lattnerc39c3652007-08-26 16:52:28 +0000568
569 // Handle conversions to bool first, they are special: comparisons against 0.
570 if (DstTy->isBooleanType()) {
571 // Complex != 0 -> (Real != 0) | (Imag != 0)
572 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
573 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
574 return Builder.CreateOr(Src.first, Src.second, "tobool");
575 }
576
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000577 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
578 // the imaginary part of the complex value is discarded and the value of the
579 // real part is converted according to the conversion rules for the
580 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000581 return EmitScalarConversion(Src.first, SrcTy, DstTy);
582}
583
584
Chris Lattner9fba49a2007-08-24 05:35:26 +0000585//===----------------------------------------------------------------------===//
586// Visitor Methods
587//===----------------------------------------------------------------------===//
588
589Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar9503b782008-08-16 00:56:44 +0000590 CGF.ErrorUnsupported(E, "scalar expression");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000591 if (E->getType()->isVoidType())
592 return 0;
Owen Andersone0b5eff2009-07-30 23:11:26 +0000593 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000594}
595
Eli Friedmand0e9d092008-05-14 19:38:39 +0000596Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
597 llvm::SmallVector<llvm::Constant*, 32> indices;
598 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
599 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
600 }
601 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
602 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
Owen Anderson17971fa2009-07-28 21:22:35 +0000603 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
Eli Friedmand0e9d092008-05-14 19:38:39 +0000604 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
605}
606
Chris Lattner9fba49a2007-08-24 05:35:26 +0000607Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000608 TestAndClearIgnoreResultAssign();
609
Chris Lattner9fba49a2007-08-24 05:35:26 +0000610 // Emit subscript expressions in rvalue context's. For most cases, this just
611 // loads the lvalue formed by the subscript expr. However, we have to be
612 // careful, because the base of a vector subscript is occasionally an rvalue,
613 // so we can't get it as an lvalue.
614 if (!E->getBase()->getType()->isVectorType())
615 return EmitLoadOfLValue(E);
616
617 // Handle the vector case. The base must be a vector, the index must be an
618 // integer value.
619 Value *Base = Visit(E->getBase());
620 Value *Idx = Visit(E->getIdx());
Eli Friedman4a0073b2009-03-28 02:45:41 +0000621 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000622 Idx = Builder.CreateIntCast(Idx,
623 llvm::Type::getInt32Ty(CGF.getLLVMContext()),
624 IdxSigned,
Eli Friedmand4531942009-03-28 03:27:06 +0000625 "vecidxcast");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000626 return Builder.CreateExtractElement(Base, Idx, "vecext");
627}
628
Chris Lattner9fba49a2007-08-24 05:35:26 +0000629// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
630// have to handle a more broad range of conversions than explicit casts, as they
631// handle things like function to ptr-to-function decay etc.
Anders Carlsson2c61dbe2009-08-24 18:12:39 +0000632Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy,
633 CastExpr::CastKind Kind) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000634 if (!DestTy->isVoidType())
635 TestAndClearIgnoreResultAssign();
Anders Carlsson497794f2009-08-24 18:26:39 +0000636
637 switch (Kind) {
638 default:
639 break;
Anders Carlssonf1957c82009-09-01 20:52:42 +0000640 case CastExpr::CK_BitCast: {
641 Value *Src = Visit(const_cast<Expr*>(E));
642 return Builder.CreateBitCast(Src, ConvertType(DestTy));
643 }
Anders Carlssonbade0792009-08-24 18:37:17 +0000644 case CastExpr::CK_ArrayToPointerDecay: {
645 assert(E->getType()->isArrayType() &&
646 "Array to pointer decay must have array source type!");
647
648 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
649
650 // Note that VLA pointers are always decayed, so we don't need to do
651 // anything here.
652 if (!E->getType()->isVariableArrayType()) {
653 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
654 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
655 ->getElementType()) &&
656 "Expected pointer to array");
657 V = Builder.CreateStructGEP(V, 0, "arraydecay");
658 }
659
660 // The resultant pointer type can be implicitly casted to other pointer
661 // types as well (e.g. void*) and can be implicitly converted to integer.
662 const llvm::Type *DestLTy = ConvertType(DestTy);
663 if (V->getType() != DestLTy) {
664 if (isa<llvm::PointerType>(DestLTy))
665 V = Builder.CreateBitCast(V, DestLTy, "ptrconv");
666 else {
667 assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay");
668 V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv");
669 }
670 }
671 return V;
672 }
Anders Carlsson497794f2009-08-24 18:26:39 +0000673 case CastExpr::CK_NullToMemberPointer:
674 return CGF.CGM.EmitNullConstant(DestTy);
675 }
676
Chris Lattner82e10392007-08-26 07:26:12 +0000677 // Handle cases where the source is an non-complex type.
Chris Lattner77288792008-02-16 23:55:16 +0000678
679 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000680 Value *Src = Visit(const_cast<Expr*>(E));
681
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000682 // Use EmitScalarConversion to perform the conversion.
683 return EmitScalarConversion(Src, E->getType(), DestTy);
684 }
Chris Lattner77288792008-02-16 23:55:16 +0000685
Chris Lattnerde0908b2008-04-04 16:54:41 +0000686 if (E->getType()->isAnyComplexType()) {
Chris Lattner77288792008-02-16 23:55:16 +0000687 // Handle cases where the source is a complex type.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000688 bool IgnoreImag = true;
689 bool IgnoreImagAssign = true;
690 bool IgnoreReal = IgnoreResultAssign;
691 bool IgnoreRealAssign = IgnoreResultAssign;
692 if (DestTy->isBooleanType())
693 IgnoreImagAssign = IgnoreImag = false;
694 else if (DestTy->isVoidType()) {
695 IgnoreReal = IgnoreImag = false;
696 IgnoreRealAssign = IgnoreImagAssign = true;
697 }
698 CodeGenFunction::ComplexPairTy V
699 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign,
700 IgnoreImagAssign);
701 return EmitComplexToScalarConversion(V, E->getType(), DestTy);
Chris Lattner77288792008-02-16 23:55:16 +0000702 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000703
Chris Lattner77288792008-02-16 23:55:16 +0000704 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
705 // evaluate the result and return.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000706 CGF.EmitAggExpr(E, 0, false, true);
Chris Lattner77288792008-02-16 23:55:16 +0000707 return 0;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000708}
709
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000710Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner09cee852008-07-26 20:23:23 +0000711 return CGF.EmitCompoundStmt(*E->getSubStmt(),
712 !E->getType()->isVoidType()).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000713}
714
Mike Stump2b6933f2009-02-28 09:07:16 +0000715Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
716 return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
Mike Stumpfca5da02009-02-21 20:00:35 +0000717}
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000718
Chris Lattner9fba49a2007-08-24 05:35:26 +0000719//===----------------------------------------------------------------------===//
720// Unary Operators
721//===----------------------------------------------------------------------===//
722
723Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000724 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000725 LValue LV = EmitLValue(E->getSubExpr());
Eli Friedman6a259872009-03-23 03:00:06 +0000726 QualType ValTy = E->getSubExpr()->getType();
727 Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000728
729 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000730
731 int AmountVal = isInc ? 1 : -1;
Eli Friedman4a0073b2009-03-28 02:45:41 +0000732
733 if (ValTy->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000734 ValTy->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +0000735 // The amount of the addition/subtraction needs to account for the VLA size
736 CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
737 }
738
Chris Lattner9fba49a2007-08-24 05:35:26 +0000739 Value *NextVal;
Chris Lattner8360c612009-03-18 04:25:13 +0000740 if (const llvm::PointerType *PT =
741 dyn_cast<llvm::PointerType>(InVal->getType())) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000742 llvm::Constant *Inc =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000743 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
Chris Lattner8360c612009-03-18 04:25:13 +0000744 if (!isa<llvm::FunctionType>(PT->getElementType())) {
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000745 QualType PTEE = ValTy->getPointeeType();
746 if (const ObjCInterfaceType *OIT =
747 dyn_cast<ObjCInterfaceType>(PTEE)) {
748 // Handle interface types, which are not represented with a concrete type.
749 int size = CGF.getContext().getTypeSize(OIT) / 8;
750 if (!isInc)
751 size = -size;
Owen Andersonb17ec712009-07-24 23:12:58 +0000752 Inc = llvm::ConstantInt::get(Inc->getType(), size);
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000753 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000754 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000755 InVal = Builder.CreateBitCast(InVal, i8Ty);
756 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
757 llvm::Value *lhs = LV.getAddress();
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000758 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
Fariborz Jahanian69b91ca2009-07-16 22:04:59 +0000759 LV = LValue::MakeAddr(lhs, ValTy.getCVRQualifiers(),
760 CGF.getContext().getObjCGCAttrKind(ValTy));
Mike Stump487ce382009-07-30 22:28:39 +0000761 } else
Dan Gohman5a748242009-08-12 00:33:55 +0000762 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
Chris Lattner8360c612009-03-18 04:25:13 +0000763 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +0000764 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000765 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Chris Lattner8360c612009-03-18 04:25:13 +0000766 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
767 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
768 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
769 }
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000770 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
Chris Lattner49083172009-02-11 07:40:06 +0000771 // Bool++ is an interesting case, due to promotion rules, we get:
772 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
773 // Bool = ((int)Bool+1) != 0
774 // An interesting aspect of this is that increment is always true.
775 // Decrement does not have this property.
Owen Andersond3fd60e2009-07-31 17:39:36 +0000776 NextVal = llvm::ConstantInt::getTrue(VMContext);
Chris Lattner291a2b32009-06-17 06:36:24 +0000777 } else if (isa<llvm::IntegerType>(InVal->getType())) {
Owen Andersonb17ec712009-07-24 23:12:58 +0000778 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Dan Gohmanc87cf1d2009-08-12 01:16:29 +0000779
780 // Signed integer overflow is undefined behavior.
781 if (ValTy->isSignedIntegerType())
782 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
783 else
784 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000785 } else {
786 // Add the inc/dec to the real part.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000787 if (InVal->getType() == llvm::Type::getFloatTy(VMContext))
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000788 NextVal =
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000789 llvm::ConstantFP::get(VMContext,
790 llvm::APFloat(static_cast<float>(AmountVal)));
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000791 else if (InVal->getType() == llvm::Type::getDoubleTy(VMContext))
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000792 NextVal =
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000793 llvm::ConstantFP::get(VMContext,
794 llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerd54d1f22008-04-20 00:50:39 +0000795 else {
796 llvm::APFloat F(static_cast<float>(AmountVal));
Dale Johannesen2461f612008-10-09 23:02:32 +0000797 bool ignored;
798 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
799 &ignored);
Owen Andersonb56fd0b2009-07-27 21:00:51 +0000800 NextVal = llvm::ConstantFP::get(VMContext, F);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000801 }
Chris Lattner291a2b32009-06-17 06:36:24 +0000802 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattner0dc11f62007-08-26 05:10:16 +0000803 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000804
805 // Store the updated result through the lvalue.
Eli Friedman6a259872009-03-23 03:00:06 +0000806 if (LV.isBitfield())
807 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
808 &NextVal);
809 else
810 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000811
812 // If this is a postinc, return the value read from memory, otherwise use the
813 // updated value.
814 return isPre ? NextVal : InVal;
815}
816
817
818Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000819 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000820 Value *Op = Visit(E->getSubExpr());
Chris Lattner291a2b32009-06-17 06:36:24 +0000821 if (Op->getType()->isFPOrFPVector())
822 return Builder.CreateFNeg(Op, "neg");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000823 return Builder.CreateNeg(Op, "neg");
824}
825
826Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000827 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000828 Value *Op = Visit(E->getSubExpr());
829 return Builder.CreateNot(Op, "neg");
830}
831
832Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
833 // Compare operand to zero.
834 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
835
836 // Invert value.
837 // TODO: Could dynamically modify easy computations here. For example, if
838 // the operand is an icmp ne, turn into icmp eq.
839 BoolVal = Builder.CreateNot(BoolVal, "lnot");
840
Anders Carlsson62943f32009-05-19 18:44:53 +0000841 // ZExt result to the expr type.
842 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000843}
844
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000845/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
846/// argument of the sizeof expression as an integer.
847Value *
848ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000849 QualType TypeToSize = E->getTypeOfArgument();
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000850 if (E->isSizeOf()) {
851 if (const VariableArrayType *VAT =
852 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
853 if (E->isArgumentType()) {
854 // sizeof(type) - make sure to emit the VLA size.
855 CGF.EmitVLASize(TypeToSize);
Eli Friedman04659bd2009-04-20 03:21:44 +0000856 } else {
857 // C99 6.5.3.4p2: If the argument is an expression of type
858 // VLA, it is evaluated.
859 CGF.EmitAnyExpr(E->getArgumentExpr());
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000860 }
Anders Carlssond309f572009-01-30 16:41:04 +0000861
Anders Carlsson8f30de92009-02-05 19:43:10 +0000862 return CGF.GetVLASize(VAT);
Anders Carlsson6cb99b72008-12-21 03:33:21 +0000863 }
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000864 }
Eli Friedman5a2c38f2009-01-24 22:19:05 +0000865
866 // If this isn't sizeof(vla), the result must be constant; use the
867 // constant folding logic so we don't have to duplicate it here.
868 Expr::EvalResult Result;
869 E->Evaluate(Result, CGF.getContext());
Owen Andersonb17ec712009-07-24 23:12:58 +0000870 return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000871}
872
Chris Lattner01211af2007-08-24 21:20:17 +0000873Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
874 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000875 if (Op->getType()->isAnyComplexType())
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000876 return CGF.EmitComplexExpr(Op, false, true, false, true).first;
Chris Lattner01211af2007-08-24 21:20:17 +0000877 return Visit(Op);
878}
879Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
880 Expr *Op = E->getSubExpr();
Chris Lattnerde0908b2008-04-04 16:54:41 +0000881 if (Op->getType()->isAnyComplexType())
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000882 return CGF.EmitComplexExpr(Op, true, false, true, false).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000883
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000884 // __imag on a scalar returns zero. Emit the subexpr to ensure side
885 // effects are evaluated, but not the actual value.
886 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid)
887 CGF.EmitLValue(Op);
888 else
889 CGF.EmitScalarExpr(Op, true);
Owen Andersonf37b84b2009-07-31 20:28:54 +0000890 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000891}
892
Anders Carlsson52774ad2008-01-29 15:56:48 +0000893Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
894{
Eli Friedman342d9432009-02-27 06:44:11 +0000895 Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
Eli Friedmanccffea92009-01-24 22:38:55 +0000896 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedman342d9432009-02-27 06:44:11 +0000897 return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
Anders Carlsson52774ad2008-01-29 15:56:48 +0000898}
Chris Lattner01211af2007-08-24 21:20:17 +0000899
Chris Lattner9fba49a2007-08-24 05:35:26 +0000900//===----------------------------------------------------------------------===//
901// Binary Operators
902//===----------------------------------------------------------------------===//
903
904BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000905 TestAndClearIgnoreResultAssign();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000906 BinOpInfo Result;
907 Result.LHS = Visit(E->getLHS());
908 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000909 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000910 Result.E = E;
911 return Result;
912}
913
Chris Lattner0d965302007-08-26 21:41:21 +0000914Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000915 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000916 bool Ignore = TestAndClearIgnoreResultAssign();
Chris Lattner660e31d2007-08-24 21:00:35 +0000917 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
918
919 BinOpInfo OpInfo;
920
Eli Friedman3cd92882009-03-28 01:22:36 +0000921 if (E->getComputationResultType()->isAnyComplexType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +0000922 // This needs to go through the complex expression emitter, but
Eli Friedman3cd92882009-03-28 01:22:36 +0000923 // it's a tad complicated to do that... I'm leaving it out for now.
924 // (Note that we do actually need the imaginary part of the RHS for
925 // multiplication and division.)
926 CGF.ErrorUnsupported(E, "complex compound assignment");
Owen Andersone0b5eff2009-07-30 23:11:26 +0000927 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Eli Friedman3cd92882009-03-28 01:22:36 +0000928 }
929
Mike Stump8d962262009-05-22 19:07:20 +0000930 // Emit the RHS first. __block variables need to have the rhs evaluated
931 // first, plus this should improve codegen a little.
932 OpInfo.RHS = Visit(E->getRHS());
933 OpInfo.Ty = E->getComputationResultType();
934 OpInfo.E = E;
Eli Friedman3cd92882009-03-28 01:22:36 +0000935 // Load/convert the LHS.
Chris Lattner660e31d2007-08-24 21:00:35 +0000936 LValue LHSLV = EmitLValue(E->getLHS());
937 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Eli Friedman3cd92882009-03-28 01:22:36 +0000938 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
939 E->getComputationLHSType());
Chris Lattner660e31d2007-08-24 21:00:35 +0000940
941 // Expand the binary operator.
942 Value *Result = (this->*Func)(OpInfo);
943
Daniel Dunbar5d7d0382008-08-06 02:00:38 +0000944 // Convert the result back to the LHS type.
Eli Friedman3cd92882009-03-28 01:22:36 +0000945 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
946
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000947 // Store the result value into the LHS lvalue. Bit-fields are
Daniel Dunbar2710fc92008-11-19 11:54:05 +0000948 // handled specially because the result is altered by the store,
949 // i.e., [C99 6.5.16p1] 'An assignment expression has the value of
950 // the left operand after the assignment...'.
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000951 if (LHSLV.isBitfield()) {
952 if (!LHSLV.isVolatileQualified()) {
953 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
954 &Result);
955 return Result;
956 } else
957 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy);
958 } else
Daniel Dunbar2668dd12008-11-19 09:36:46 +0000959 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Mike Stumpb8fc73e2009-05-29 15:46:01 +0000960 if (Ignore)
961 return 0;
962 return EmitLoadOfLValue(LHSLV, E->getType());
Chris Lattner660e31d2007-08-24 21:00:35 +0000963}
964
965
Chris Lattner9fba49a2007-08-24 05:35:26 +0000966Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanaade3bf2007-12-30 01:28:16 +0000967 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000968 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000969 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000970 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
971 else
972 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
973}
974
975Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
976 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000977 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000978 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
979 else
980 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
981}
982
Mike Stumpdb789912009-04-01 20:28:16 +0000983Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
984 unsigned IID;
985 unsigned OpID = 0;
Mike Stump0f595bb2009-04-02 01:03:55 +0000986
Mike Stumpf71b7742009-04-02 18:15:54 +0000987 switch (Ops.E->getOpcode()) {
988 case BinaryOperator::Add:
989 case BinaryOperator::AddAssign:
990 OpID = 1;
991 IID = llvm::Intrinsic::sadd_with_overflow;
992 break;
993 case BinaryOperator::Sub:
994 case BinaryOperator::SubAssign:
995 OpID = 2;
996 IID = llvm::Intrinsic::ssub_with_overflow;
997 break;
998 case BinaryOperator::Mul:
999 case BinaryOperator::MulAssign:
1000 OpID = 3;
1001 IID = llvm::Intrinsic::smul_with_overflow;
1002 break;
1003 default:
1004 assert(false && "Unsupported operation for overflow detection");
Daniel Dunbar96e909b2009-04-08 16:23:09 +00001005 IID = 0;
Mike Stumpdb789912009-04-01 20:28:16 +00001006 }
Mike Stumpf71b7742009-04-02 18:15:54 +00001007 OpID <<= 1;
1008 OpID |= 1;
1009
Mike Stumpdb789912009-04-01 20:28:16 +00001010 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
1011
1012 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
1013
1014 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
1015 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
1016 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
1017
1018 // Branch in case of overflow.
1019 llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
1020 llvm::BasicBlock *overflowBB =
1021 CGF.createBasicBlock("overflow", CGF.CurFn);
1022 llvm::BasicBlock *continueBB =
1023 CGF.createBasicBlock("overflow.continue", CGF.CurFn);
1024
1025 Builder.CreateCondBr(overflow, overflowBB, continueBB);
1026
1027 // Handle overflow
1028
1029 Builder.SetInsertPoint(overflowBB);
1030
1031 // Handler is:
1032 // long long *__overflow_handler)(long long a, long long b, char op,
1033 // char width)
1034 std::vector<const llvm::Type*> handerArgTypes;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001035 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1036 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
1037 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1038 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
1039 llvm::FunctionType *handlerTy = llvm::FunctionType::get(
1040 llvm::Type::getInt64Ty(VMContext), handerArgTypes, false);
Mike Stumpdb789912009-04-01 20:28:16 +00001041 llvm::Value *handlerFunction =
1042 CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001043 llvm::PointerType::getUnqual(handlerTy));
Mike Stumpdb789912009-04-01 20:28:16 +00001044 handlerFunction = Builder.CreateLoad(handlerFunction);
1045
1046 llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001047 Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)),
1048 Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)),
1049 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID),
1050 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
Mike Stumpdb789912009-04-01 20:28:16 +00001051 cast<llvm::IntegerType>(opTy)->getBitWidth()));
1052
1053 handlerResult = Builder.CreateTrunc(handlerResult, opTy);
1054
1055 Builder.CreateBr(continueBB);
1056
1057 // Set up the continuation
1058 Builder.SetInsertPoint(continueBB);
1059 // Get the correct result
1060 llvm::PHINode *phi = Builder.CreatePHI(opTy);
1061 phi->reserveOperandSpace(2);
1062 phi->addIncoming(result, initialBB);
1063 phi->addIncoming(handlerResult, overflowBB);
1064
1065 return phi;
1066}
Chris Lattner9fba49a2007-08-24 05:35:26 +00001067
1068Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Steve Naroff79ae19a2009-07-14 18:25:06 +00001069 if (!Ops.Ty->isAnyPointerType()) {
Chris Lattner291a2b32009-06-17 06:36:24 +00001070 if (CGF.getContext().getLangOptions().OverflowChecking &&
1071 Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +00001072 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +00001073
1074 if (Ops.LHS->getType()->isFPOrFPVector())
1075 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add");
Dan Gohmanc87cf1d2009-08-12 01:16:29 +00001076
1077 // Signed integer overflow is undefined behavior.
1078 if (Ops.Ty->isSignedIntegerType())
1079 return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add");
1080
Chris Lattner9fba49a2007-08-24 05:35:26 +00001081 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Mike Stumpdb789912009-04-01 20:28:16 +00001082 }
Eli Friedman4a0073b2009-03-28 02:45:41 +00001083
Steve Naroff329ec222009-07-10 23:34:53 +00001084 if (Ops.Ty->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001085 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +00001086 // The amount of the addition needs to account for the VLA size
1087 CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
1088 }
Chris Lattner17c0cb02008-01-03 06:36:51 +00001089 Value *Ptr, *Idx;
1090 Expr *IdxExp;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001091 const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>();
Steve Naroff329ec222009-07-10 23:34:53 +00001092 const ObjCObjectPointerType *OPT =
1093 Ops.E->getLHS()->getType()->getAsObjCObjectPointerType();
1094 if (PT || OPT) {
Chris Lattner17c0cb02008-01-03 06:36:51 +00001095 Ptr = Ops.LHS;
1096 Idx = Ops.RHS;
1097 IdxExp = Ops.E->getRHS();
Steve Naroff329ec222009-07-10 23:34:53 +00001098 } else { // int + pointer
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001099 PT = Ops.E->getRHS()->getType()->getAs<PointerType>();
Steve Naroff329ec222009-07-10 23:34:53 +00001100 OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType();
1101 assert((PT || OPT) && "Invalid add expr");
Chris Lattner17c0cb02008-01-03 06:36:51 +00001102 Ptr = Ops.RHS;
1103 Idx = Ops.LHS;
1104 IdxExp = Ops.E->getLHS();
1105 }
1106
1107 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +00001108 if (Width < CGF.LLVMPointerWidth) {
Chris Lattner17c0cb02008-01-03 06:36:51 +00001109 // Zero or sign extend the pointer value based on whether the index is
1110 // signed or not.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001111 const llvm::Type *IdxType =
1112 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Chris Lattnerc154ac12008-07-26 22:37:01 +00001113 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner17c0cb02008-01-03 06:36:51 +00001114 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1115 else
1116 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1117 }
Steve Naroff329ec222009-07-10 23:34:53 +00001118 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001119 // Handle interface types, which are not represented with a concrete
1120 // type.
1121 if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
1122 llvm::Value *InterfaceSize =
Owen Andersonb17ec712009-07-24 23:12:58 +00001123 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001124 CGF.getContext().getTypeSize(OIT) / 8);
1125 Idx = Builder.CreateMul(Idx, InterfaceSize);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001126 const llvm::Type *i8Ty =
1127 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001128 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
1129 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
1130 return Builder.CreateBitCast(Res, Ptr->getType());
1131 }
1132
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001133 // Explicitly handle GNU void* and function pointer arithmetic
1134 // extensions. The GNU void* casts amount to no-ops since our void*
1135 // type is i8*, but this is future proof.
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001136 if (ElementType->isVoidType() || ElementType->isFunctionType()) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001137 const llvm::Type *i8Ty =
1138 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001139 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001140 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001141 return Builder.CreateBitCast(Res, Ptr->getType());
1142 }
Chris Lattner17c0cb02008-01-03 06:36:51 +00001143
Dan Gohman5a748242009-08-12 00:33:55 +00001144 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001145}
1146
1147Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
Mike Stumpdb789912009-04-01 20:28:16 +00001148 if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
Mike Stumpf71b7742009-04-02 18:15:54 +00001149 if (CGF.getContext().getLangOptions().OverflowChecking
1150 && Ops.Ty->isSignedIntegerType())
Mike Stumpdb789912009-04-01 20:28:16 +00001151 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner291a2b32009-06-17 06:36:24 +00001152
1153 if (Ops.LHS->getType()->isFPOrFPVector())
1154 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001155 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Mike Stumpdb789912009-04-01 20:28:16 +00001156 }
Chris Lattner660e31d2007-08-24 21:00:35 +00001157
Steve Naroff329ec222009-07-10 23:34:53 +00001158 if (Ops.E->getLHS()->getType()->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001159 Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedman4a0073b2009-03-28 02:45:41 +00001160 // The amount of the addition needs to account for the VLA size for
1161 // ptr-int
1162 // The amount of the division needs to account for the VLA size for
1163 // ptr-ptr.
1164 CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
1165 }
1166
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001167 const QualType LHSType = Ops.E->getLHS()->getType();
Steve Naroff329ec222009-07-10 23:34:53 +00001168 const QualType LHSElementType = LHSType->getPointeeType();
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001169 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
1170 // pointer - int
1171 Value *Idx = Ops.RHS;
1172 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Guptacee8fea2009-04-24 02:40:57 +00001173 if (Width < CGF.LLVMPointerWidth) {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001174 // Zero or sign extend the pointer value based on whether the index is
1175 // signed or not.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001176 const llvm::Type *IdxType =
1177 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001178 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1179 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1180 else
1181 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1182 }
1183 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001184
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001185 // Handle interface types, which are not represented with a concrete
1186 // type.
1187 if (const ObjCInterfaceType *OIT =
1188 dyn_cast<ObjCInterfaceType>(LHSElementType)) {
1189 llvm::Value *InterfaceSize =
Owen Andersonb17ec712009-07-24 23:12:58 +00001190 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001191 CGF.getContext().getTypeSize(OIT) / 8);
1192 Idx = Builder.CreateMul(Idx, InterfaceSize);
Owen Anderson73e7f802009-07-14 23:10:40 +00001193 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001194 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar6864c0d2009-04-25 05:08:32 +00001195 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1196 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
1197 return Builder.CreateBitCast(Res, Ops.LHS->getType());
1198 }
1199
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001200 // Explicitly handle GNU void* and function pointer arithmetic
1201 // extensions. The GNU void* casts amount to no-ops since our
1202 // void* type is i8*, but this is future proof.
1203 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001204 const llvm::Type *i8Ty =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001205 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar4fd58ab2009-01-23 18:51:09 +00001206 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1207 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1208 return Builder.CreateBitCast(Res, Ops.LHS->getType());
1209 }
1210
Dan Gohman5a748242009-08-12 00:33:55 +00001211 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001212 } else {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001213 // pointer - pointer
1214 Value *LHS = Ops.LHS;
1215 Value *RHS = Ops.RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +00001216
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001217 uint64_t ElementSize;
Daniel Dunbar0aac9f62008-08-05 00:47:03 +00001218
Chris Lattner6d2e3492009-02-11 07:21:43 +00001219 // Handle GCC extension for pointer arithmetic on void* and function pointer
1220 // types.
1221 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Daniel Dunbar5d7d0382008-08-06 02:00:38 +00001222 ElementSize = 1;
1223 } else {
1224 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1225 }
1226
1227 const llvm::Type *ResultType = ConvertType(Ops.Ty);
1228 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1229 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1230 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
1231
Chris Lattner6d2e3492009-02-11 07:21:43 +00001232 // Optimize out the shift for element size of 1.
1233 if (ElementSize == 1)
1234 return BytesBetween;
Dan Gohman8f61ba42009-08-11 22:40:09 +00001235
1236 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
1237 // pointer difference in C is only defined in the case where both
1238 // operands are pointing to elements of an array.
Owen Andersonb17ec712009-07-24 23:12:58 +00001239 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
Dan Gohman8f61ba42009-08-11 22:40:09 +00001240 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001241 }
Chris Lattner9fba49a2007-08-24 05:35:26 +00001242}
1243
1244Value *ScalarExprEmitter::EmitShl(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
1251 return Builder.CreateShl(Ops.LHS, RHS, "shl");
1252}
1253
1254Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1255 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1256 // RHS to the same size as the LHS.
1257 Value *RHS = Ops.RHS;
1258 if (Ops.LHS->getType() != RHS->getType())
1259 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
1260
Chris Lattner660e31d2007-08-24 21:00:35 +00001261 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +00001262 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1263 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1264}
1265
1266Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1267 unsigned SICmpOpc, unsigned FCmpOpc) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001268 TestAndClearIgnoreResultAssign();
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001269 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001270 QualType LHSTy = E->getLHS()->getType();
Chris Lattner08ac8522009-07-08 01:08:03 +00001271 if (!LHSTy->isAnyComplexType()) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001272 Value *LHS = Visit(E->getLHS());
1273 Value *RHS = Visit(E->getRHS());
1274
Eli Friedman04865bc2009-07-22 06:07:16 +00001275 if (LHS->getType()->isFPOrFPVector()) {
Nate Begeman1591bc52008-07-25 20:16:05 +00001276 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001277 LHS, RHS, "cmp");
Eli Friedman850ea372008-05-29 15:09:15 +00001278 } else if (LHSTy->isSignedIntegerType()) {
1279 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001280 LHS, RHS, "cmp");
1281 } else {
Eli Friedman850ea372008-05-29 15:09:15 +00001282 // Unsigned integers and pointers.
1283 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner9fba49a2007-08-24 05:35:26 +00001284 LHS, RHS, "cmp");
1285 }
Chris Lattner08ac8522009-07-08 01:08:03 +00001286
1287 // If this is a vector comparison, sign extend the result to the appropriate
1288 // vector integer type and return it (don't convert to bool).
1289 if (LHSTy->isVectorType())
1290 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
Nate Begeman1591bc52008-07-25 20:16:05 +00001291
Chris Lattner9fba49a2007-08-24 05:35:26 +00001292 } else {
1293 // Complex Comparison: can only be an equality comparison.
1294 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1295 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
1296
Chris Lattnerc154ac12008-07-26 22:37:01 +00001297 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001298
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001299 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +00001300 if (CETy->isRealFloatingType()) {
1301 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1302 LHS.first, RHS.first, "cmp.r");
1303 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1304 LHS.second, RHS.second, "cmp.i");
1305 } else {
1306 // Complex comparisons can only be equality comparisons. As such, signed
1307 // and unsigned opcodes are the same.
1308 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1309 LHS.first, RHS.first, "cmp.r");
1310 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1311 LHS.second, RHS.second, "cmp.i");
1312 }
1313
1314 if (E->getOpcode() == BinaryOperator::EQ) {
1315 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1316 } else {
1317 assert(E->getOpcode() == BinaryOperator::NE &&
1318 "Complex comparison other than == or != ?");
1319 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1320 }
1321 }
Nuno Lopes92577002009-01-11 23:22:37 +00001322
1323 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001324}
1325
1326Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001327 bool Ignore = TestAndClearIgnoreResultAssign();
1328
1329 // __block variables need to have the rhs evaluated first, plus this should
1330 // improve codegen just a little.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001331 Value *RHS = Visit(E->getRHS());
Mike Stump68df15c2009-05-21 21:05:15 +00001332 LValue LHS = EmitLValue(E->getLHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001333
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001334 // Store the value into the LHS. Bit-fields are handled specially
Daniel Dunbar2710fc92008-11-19 11:54:05 +00001335 // because the result is altered by the store, i.e., [C99 6.5.16p1]
1336 // 'An assignment expression has the value of the left operand after
Eli Friedman4a0073b2009-03-28 02:45:41 +00001337 // the assignment...'.
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001338 if (LHS.isBitfield()) {
1339 if (!LHS.isVolatileQualified()) {
1340 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1341 &RHS);
1342 return RHS;
1343 } else
1344 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType());
1345 } else
Daniel Dunbar2668dd12008-11-19 09:36:46 +00001346 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001347 if (Ignore)
1348 return 0;
1349 return EmitLoadOfLValue(LHS, E->getType());
Chris Lattner9fba49a2007-08-24 05:35:26 +00001350}
1351
1352Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001353 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1354 // If we have 1 && X, just emit X without inserting the control flow.
1355 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1356 if (Cond == 1) { // If we have 1 && X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001357 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1358 // ZExt result to int.
1359 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1360 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001361
1362 // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1363 if (!CGF.ContainsLabel(E->getRHS()))
Owen Andersonf37b84b2009-07-31 20:28:54 +00001364 return llvm::Constant::getNullValue(CGF.LLVMIntTy);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001365 }
1366
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001367 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1368 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
Chris Lattner715c2a72008-11-12 08:26:50 +00001369
Chris Lattner7f80bb32008-11-12 08:38:24 +00001370 // Branch on the LHS first. If it is false, go to the failure (cont) block.
1371 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1372
1373 // Any edges into the ContBlock are now from an (indeterminate number of)
1374 // edges from this first condition. All of these values will be false. Start
1375 // setting up the PHI node in the Cont Block for this.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001376 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1377 "", ContBlock);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001378 PN->reserveOperandSpace(2); // Normal case, two inputs.
1379 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1380 PI != PE; ++PI)
Owen Andersond3fd60e2009-07-31 17:39:36 +00001381 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001382
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001383 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001384 CGF.EmitBlock(RHSBlock);
1385 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001386 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001387
1388 // Reaquire the RHS block, as there may be subblocks inserted.
1389 RHSBlock = Builder.GetInsertBlock();
Chris Lattner7f80bb32008-11-12 08:38:24 +00001390
1391 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1392 // into the phi node for the edge with the value of RHSCond.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001393 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001394 PN->addIncoming(RHSCond, RHSBlock);
1395
1396 // ZExt result to int.
1397 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1398}
1399
1400Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
Chris Lattner715c2a72008-11-12 08:26:50 +00001401 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1402 // If we have 0 || X, just emit X without inserting the control flow.
1403 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1404 if (Cond == -1) { // If we have 0 || X, just emit X.
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001405 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1406 // ZExt result to int.
1407 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1408 }
Chris Lattner715c2a72008-11-12 08:26:50 +00001409
Eli Friedmanea137cd2008-12-02 16:02:46 +00001410 // 1 || RHS: If it is safe, just elide the RHS, and return 1.
Chris Lattner715c2a72008-11-12 08:26:50 +00001411 if (!CGF.ContainsLabel(E->getRHS()))
Owen Andersonb17ec712009-07-24 23:12:58 +00001412 return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
Chris Lattner3f73d0d2008-11-11 07:41:27 +00001413 }
1414
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001415 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1416 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
Chris Lattner9fba49a2007-08-24 05:35:26 +00001417
Chris Lattner7f80bb32008-11-12 08:38:24 +00001418 // Branch on the LHS first. If it is true, go to the success (cont) block.
1419 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1420
1421 // Any edges into the ContBlock are now from an (indeterminate number of)
1422 // edges from this first condition. All of these values will be true. Start
1423 // setting up the PHI node in the Cont Block for this.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001424 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1425 "", ContBlock);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001426 PN->reserveOperandSpace(2); // Normal case, two inputs.
1427 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1428 PI != PE; ++PI)
Owen Andersond3fd60e2009-07-31 17:39:36 +00001429 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
Chris Lattner7f80bb32008-11-12 08:38:24 +00001430
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001431 CGF.PushConditionalTempDestruction();
1432
Chris Lattner7f80bb32008-11-12 08:38:24 +00001433 // Emit the RHS condition as a bool value.
Chris Lattner9fba49a2007-08-24 05:35:26 +00001434 CGF.EmitBlock(RHSBlock);
1435 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1436
Anders Carlssonac36c0e2009-06-04 02:53:13 +00001437 CGF.PopConditionalTempDestruction();
1438
Chris Lattner9fba49a2007-08-24 05:35:26 +00001439 // Reaquire the RHS block, as there may be subblocks inserted.
1440 RHSBlock = Builder.GetInsertBlock();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001441
Chris Lattner7f80bb32008-11-12 08:38:24 +00001442 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1443 // into the phi node for the edge with the value of RHSCond.
1444 CGF.EmitBlock(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001445 PN->addIncoming(RHSCond, RHSBlock);
1446
1447 // ZExt result to int.
1448 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1449}
1450
1451Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1452 CGF.EmitStmt(E->getLHS());
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00001453 CGF.EnsureInsertPoint();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001454 return Visit(E->getRHS());
1455}
1456
1457//===----------------------------------------------------------------------===//
1458// Other Operators
1459//===----------------------------------------------------------------------===//
1460
Chris Lattner504a5282008-11-12 08:55:54 +00001461/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1462/// expression is cheap enough and side-effect-free enough to evaluate
1463/// unconditionally instead of conditionally. This is used to convert control
1464/// flow into selects in some cases.
1465static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1466 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1467 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
1468
1469 // TODO: Allow anything we can constant fold to an integer or fp constant.
1470 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1471 isa<FloatingLiteral>(E))
1472 return true;
1473
1474 // Non-volatile automatic variables too, to get "cond ? X : Y" where
1475 // X and Y are local variables.
1476 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1477 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1478 if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1479 return true;
1480
1481 return false;
1482}
1483
1484
Chris Lattner9fba49a2007-08-24 05:35:26 +00001485Value *ScalarExprEmitter::
1486VisitConditionalOperator(const ConditionalOperator *E) {
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001487 TestAndClearIgnoreResultAssign();
Chris Lattner3d6606b2008-11-12 08:04:58 +00001488 // If the condition constant folds and can be elided, try to avoid emitting
1489 // the condition and the dead arm.
1490 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
Chris Lattner044bffc2008-11-11 18:56:45 +00001491 Expr *Live = E->getLHS(), *Dead = E->getRHS();
Chris Lattner3d6606b2008-11-12 08:04:58 +00001492 if (Cond == -1)
Chris Lattner044bffc2008-11-11 18:56:45 +00001493 std::swap(Live, Dead);
Chris Lattner3d6606b2008-11-12 08:04:58 +00001494
1495 // If the dead side doesn't have labels we need, and if the Live side isn't
1496 // the gnu missing ?: extension (which we could handle, but don't bother
1497 // to), just emit the Live part.
1498 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part
1499 Live) // Live part isn't missing.
1500 return Visit(Live);
Chris Lattner044bffc2008-11-11 18:56:45 +00001501 }
1502
Chris Lattner504a5282008-11-12 08:55:54 +00001503
1504 // If this is a really simple expression (like x ? 4 : 5), emit this as a
1505 // select instead of as control flow. We can only do this if it is cheap and
Chris Lattner1f11af22008-11-16 06:16:27 +00001506 // safe to evaluate the LHS and RHS unconditionally.
Chris Lattner504a5282008-11-12 08:55:54 +00001507 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1508 isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1509 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1510 llvm::Value *LHS = Visit(E->getLHS());
1511 llvm::Value *RHS = Visit(E->getRHS());
1512 return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1513 }
1514
1515
Daniel Dunbarb23e9922008-11-12 10:13:37 +00001516 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1517 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +00001518 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner67e22462008-11-12 08:08:13 +00001519 Value *CondVal = 0;
Chris Lattner3d6606b2008-11-12 08:04:58 +00001520
Chris Lattner86031712009-02-13 23:35:32 +00001521 // If we don't have the GNU missing condition extension, emit a branch on
1522 // bool the normal way.
1523 if (E->getLHS()) {
1524 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1525 // the branch on bool.
1526 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1527 } else {
1528 // Otherwise, for the ?: extension, evaluate the conditional and then
1529 // convert it to bool the hard way. We do this explicitly because we need
1530 // the unconverted value for the missing middle value of the ?:.
Chris Lattner67e22462008-11-12 08:08:13 +00001531 CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattner86031712009-02-13 23:35:32 +00001532
1533 // In some cases, EmitScalarConversion will delete the "CondVal" expression
1534 // if there are no extra uses (an optimization). Inhibit this by making an
1535 // extra dead use, because we're going to add a use of CondVal later. We
1536 // don't use the builder for this, because we don't want it to get optimized
1537 // away. This leaves dead code, but the ?: extension isn't common.
1538 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1539 Builder.GetInsertBlock());
1540
Chris Lattner67e22462008-11-12 08:08:13 +00001541 Value *CondBoolVal =
1542 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1543 CGF.getContext().BoolTy);
1544 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner67e22462008-11-12 08:08:13 +00001545 }
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001546
1547 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001548 CGF.EmitBlock(LHSBlock);
1549
1550 // Handle the GNU extension for missing LHS.
Chris Lattner98a425c2007-11-26 01:40:58 +00001551 Value *LHS;
1552 if (E->getLHS())
Eli Friedmance8d7032008-05-16 20:38:39 +00001553 LHS = Visit(E->getLHS());
Chris Lattner98a425c2007-11-26 01:40:58 +00001554 else // Perform promotions, to handle cases like "short ?: int"
1555 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1556
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001557 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001558 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001559 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001560
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001561 CGF.PushConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001562 CGF.EmitBlock(RHSBlock);
1563
Eli Friedmance8d7032008-05-16 20:38:39 +00001564 Value *RHS = Visit(E->getRHS());
Anders Carlssonbf3b93a2009-06-04 03:00:32 +00001565 CGF.PopConditionalTempDestruction();
Chris Lattner9fba49a2007-08-24 05:35:26 +00001566 RHSBlock = Builder.GetInsertBlock();
Daniel Dunbar5276caa2008-11-11 09:41:28 +00001567 CGF.EmitBranch(ContBlock);
Chris Lattner9fba49a2007-08-24 05:35:26 +00001568
1569 CGF.EmitBlock(ContBlock);
1570
Nuno Lopesb62ff242008-06-04 19:15:45 +00001571 if (!LHS || !RHS) {
Chris Lattner307da022007-11-30 17:56:23 +00001572 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1573 return 0;
1574 }
1575
Chris Lattner9fba49a2007-08-24 05:35:26 +00001576 // Create a PHI node for the real part.
1577 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1578 PN->reserveOperandSpace(2);
1579 PN->addIncoming(LHS, LHSBlock);
1580 PN->addIncoming(RHS, RHSBlock);
1581 return PN;
1582}
1583
1584Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmand540c112009-03-04 05:52:32 +00001585 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattner9fba49a2007-08-24 05:35:26 +00001586}
1587
Chris Lattner307da022007-11-30 17:56:23 +00001588Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Eli Friedman8f5e8782009-01-20 17:46:04 +00001589 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson285611e2008-11-04 05:30:00 +00001590 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1591
1592 // If EmitVAArg fails, we fall back to the LLVM instruction.
1593 if (!ArgPtr)
1594 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1595
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001596 // FIXME Volatility.
Anders Carlsson285611e2008-11-04 05:30:00 +00001597 return Builder.CreateLoad(ArgPtr);
Anders Carlsson36760332007-10-15 20:28:48 +00001598}
1599
Mike Stump4eb81dc2009-02-12 18:29:15 +00001600Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
Mike Stump1fa52fe2009-03-07 02:35:30 +00001601 return CGF.BuildBlockLiteralTmp(BE);
Mike Stump4eb81dc2009-02-12 18:29:15 +00001602}
1603
Chris Lattner9fba49a2007-08-24 05:35:26 +00001604//===----------------------------------------------------------------------===//
1605// Entry Point into this File
1606//===----------------------------------------------------------------------===//
1607
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001608/// EmitScalarExpr - Emit the computation of the specified expression of
1609/// scalar type, ignoring the result.
1610Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
Chris Lattner9fba49a2007-08-24 05:35:26 +00001611 assert(E && !hasAggregateLLVMType(E->getType()) &&
1612 "Invalid scalar expression to emit");
1613
Mike Stumpb8fc73e2009-05-29 15:46:01 +00001614 return ScalarExprEmitter(*this, IgnoreResultAssign)
1615 .Visit(const_cast<Expr*>(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +00001616}
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001617
1618/// EmitScalarConversion - Emit a conversion from the specified type to the
1619/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001620Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1621 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +00001622 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1623 "Invalid scalar expression to emit");
1624 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1625}
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001626
1627/// EmitComplexToScalarConversion - Emit a conversion from the specified
1628/// complex type to the specified destination type, where the destination
1629/// type is an LLVM scalar type.
1630Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1631 QualType SrcTy,
1632 QualType DstTy) {
Chris Lattnerde0908b2008-04-04 16:54:41 +00001633 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattnerfb182ee2007-08-26 16:34:22 +00001634 "Invalid complex -> scalar conversion");
1635 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1636 DstTy);
1637}
Anders Carlssona9234fe2007-12-10 19:35:18 +00001638
1639Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1640 assert(V1->getType() == V2->getType() &&
1641 "Vector operands must be of the same type");
Anders Carlssona9234fe2007-12-10 19:35:18 +00001642 unsigned NumElements =
1643 cast<llvm::VectorType>(V1->getType())->getNumElements();
1644
1645 va_list va;
1646 va_start(va, V2);
1647
1648 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssona9234fe2007-12-10 19:35:18 +00001649 for (unsigned i = 0; i < NumElements; i++) {
1650 int n = va_arg(va, int);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001651 assert(n >= 0 && n < (int)NumElements * 2 &&
1652 "Vector shuffle index out of bounds!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001653 Args.push_back(llvm::ConstantInt::get(
1654 llvm::Type::getInt32Ty(VMContext), n));
Anders Carlssona9234fe2007-12-10 19:35:18 +00001655 }
1656
1657 const char *Name = va_arg(va, const char *);
1658 va_end(va);
1659
Owen Anderson17971fa2009-07-28 21:22:35 +00001660 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Anders Carlssona9234fe2007-12-10 19:35:18 +00001661
1662 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1663}
1664
Anders Carlsson68b8be92007-12-15 21:23:30 +00001665llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001666 unsigned NumVals, bool isSplat) {
Anders Carlsson68b8be92007-12-15 21:23:30 +00001667 llvm::Value *Vec
Owen Andersone0b5eff2009-07-30 23:11:26 +00001668 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Anders Carlsson68b8be92007-12-15 21:23:30 +00001669
Chris Lattnera23eb7b2008-07-26 20:15:14 +00001670 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001671 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001672 llvm::Value *Idx = llvm::ConstantInt::get(
1673 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanec2d1062007-12-30 02:59:45 +00001674 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson68b8be92007-12-15 21:23:30 +00001675 }
1676
1677 return Vec;
1678}