blob: 7adbc9fd3b307008be6a63a89e6d7bd07259b828 [file] [log] [blame]
Chris Lattner2da04b32007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner2da04b32007-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 Dunbarad319a72008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar6630e102008-08-12 05:08:18 +000017#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattnerff2367c2008-04-20 00:50:39 +000020#include "clang/Basic/TargetInfo.h"
Chris Lattner2da04b32007-08-24 05:35:26 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
Anders Carlssond8499822007-10-29 05:01:08 +000023#include "llvm/GlobalVariable.h"
Anders Carlsson7e13ab82007-10-15 20:28:48 +000024#include "llvm/Intrinsics.h"
Mike Stump0c61b732009-04-01 20:28:16 +000025#include "llvm/Module.h"
Chris Lattner2da04b32007-08-24 05:35:26 +000026#include "llvm/Support/Compiler.h"
Chris Lattner35710d182008-11-12 08:38:24 +000027#include "llvm/Support/CFG.h"
Mike Stumpcb2fbcb2009-02-21 20:00:35 +000028#include "llvm/Target/TargetData.h"
Chris Lattner1800c182008-01-03 07:05:49 +000029#include <cstdarg>
Ted Kremenekf182e812007-12-10 23:44:32 +000030
Chris Lattner2da04b32007-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 Lattner3d966d62007-08-24 21:00:35 +000042 QualType Ty; // Computation Type.
Chris Lattner2da04b32007-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 Dunbarcb463852008-11-01 01:53:16 +000050 CGBuilderTy &Builder;
Mike Stumpdf0fe272009-05-29 15:46:01 +000051 bool IgnoreResultAssign;
Owen Anderson170229f2009-07-14 23:10:40 +000052 llvm::LLVMContext &VMContext;
Chris Lattner2da04b32007-08-24 05:35:26 +000053public:
54
Mike Stumpdf0fe272009-05-29 15:46:01 +000055 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
Mike Stump4a3999f2009-09-09 13:00:44 +000056 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
Owen Anderson170229f2009-07-14 23:10:40 +000057 VMContext(cgf.getLLVMContext()) {
Chris Lattner2da04b32007-08-24 05:35:26 +000058 }
Mike Stump4a3999f2009-09-09 13:00:44 +000059
Chris Lattner2da04b32007-08-24 05:35:26 +000060 //===--------------------------------------------------------------------===//
61 // Utilities
62 //===--------------------------------------------------------------------===//
63
Mike Stumpdf0fe272009-05-29 15:46:01 +000064 bool TestAndClearIgnoreResultAssign() {
Chris Lattner2a7deb62009-07-08 01:08:03 +000065 bool I = IgnoreResultAssign;
66 IgnoreResultAssign = false;
67 return I;
68 }
Mike Stumpdf0fe272009-05-29 15:46:01 +000069
Chris Lattner2da04b32007-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 Lattner4647a212007-08-31 22:49:20 +000074 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner2da04b32007-08-24 05:35:26 +000075 }
Mike Stump4a3999f2009-09-09 13:00:44 +000076
Chris Lattner2da04b32007-08-24 05:35:26 +000077 /// 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 Lattner2da04b32007-08-24 05:35:26 +000081 return EmitLoadOfLValue(EmitLValue(E), E->getType());
82 }
Mike Stump4a3999f2009-09-09 13:00:44 +000083
Chris Lattnere0044382007-08-26 16:42:57 +000084 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner2e928882007-08-26 17:25:57 +000085 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnere0044382007-08-26 16:42:57 +000086 Value *EmitConversionToBool(Value *Src, QualType DstTy);
Mike Stump4a3999f2009-09-09 13:00:44 +000087
Chris Lattner3474c202007-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 Lattner42e6b812007-08-26 16:34:22 +000090 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
91
92 /// EmitComplexToScalarConversion - Emit a conversion from the specified
Mike Stump4a3999f2009-09-09 13:00:44 +000093 /// complex type to the specified destination type, where the destination type
94 /// is an LLVM scalar type.
Chris Lattner42e6b812007-08-26 16:34:22 +000095 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
96 QualType SrcTy, QualType DstTy);
Mike Stumpab3afd82009-02-12 18:29:15 +000097
Chris Lattner2da04b32007-08-24 05:35:26 +000098 //===--------------------------------------------------------------------===//
99 // Visitor Methods
100 //===--------------------------------------------------------------------===//
101
102 Value *VisitStmt(Stmt *S) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000103 S->dump(CGF.getContext().getSourceManager());
Chris Lattner2da04b32007-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 Andersonb7a2fe62009-07-24 23:12:58 +0000112 return llvm::ConstantInt::get(VMContext, E->getValue());
Chris Lattner2da04b32007-08-24 05:35:26 +0000113 }
114 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Owen Andersone05f2ed2009-07-27 21:00:51 +0000115 return llvm::ConstantFP::get(VMContext, E->getValue());
Chris Lattner2da04b32007-08-24 05:35:26 +0000116 }
117 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000118 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
Chris Lattner2da04b32007-08-24 05:35:26 +0000119 }
Nate Begeman4c18c232007-11-15 05:40:03 +0000120 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000121 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
Nate Begeman4c18c232007-11-15 05:40:03 +0000122 }
Argyrios Kyrtzidisce4528f2008-08-23 19:35:47 +0000123 Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Owen Anderson0b75f232009-07-31 20:28:54 +0000124 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Argyrios Kyrtzidisce4528f2008-08-23 19:35:47 +0000125 }
Anders Carlsson39def3a2008-12-21 22:39:40 +0000126 Value *VisitGNUNullExpr(const GNUNullExpr *E) {
Owen Anderson0b75f232009-07-31 20:28:54 +0000127 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Anders Carlsson39def3a2008-12-21 22:39:40 +0000128 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000129 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000130 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff32e44c02007-10-15 20:41:53 +0000131 CGF.getContext().typesAreCompatible(
132 E->getArgType1(), E->getArgType2()));
Chris Lattner2da04b32007-08-24 05:35:26 +0000133 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000134 Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000135 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000136 llvm::Value *V =
Owen Anderson41a75022009-08-13 21:57:51 +0000137 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Daniel Dunbar8bc821a2008-08-16 01:41:47 +0000138 CGF.GetIDForAddrOfLabel(E->getLabel()));
Mike Stump4a3999f2009-09-09 13:00:44 +0000139
Daniel Dunbar8bc821a2008-08-16 01:41:47 +0000140 return Builder.CreateIntToPtr(V, ConvertType(E->getType()));
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000141 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000142
Chris Lattner2da04b32007-08-24 05:35:26 +0000143 // l-values.
144 Value *VisitDeclRefExpr(DeclRefExpr *E) {
145 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000146 return llvm::ConstantInt::get(VMContext, EC->getInitVal());
Chris Lattner2da04b32007-08-24 05:35:26 +0000147 return EmitLoadOfLValue(E);
148 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000149 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
150 return CGF.EmitObjCSelectorExpr(E);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000151 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000152 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
153 return CGF.EmitObjCProtocolExpr(E);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000154 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000155 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Daniel Dunbar55310df2008-08-27 06:57:25 +0000156 return EmitLoadOfLValue(E);
157 }
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000158 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000159 return EmitLoadOfLValue(E);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000160 }
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000161 Value *VisitObjCImplicitSetterGetterRefExpr(
162 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000163 return EmitLoadOfLValue(E);
164 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000165 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
166 return CGF.EmitObjCMessageExpr(E).getScalarVal();
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000167 }
168
Chris Lattner2da04b32007-08-24 05:35:26 +0000169 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000170 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Chris Lattner2da04b32007-08-24 05:35:26 +0000171 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000172 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattner084bc322008-10-26 23:53:12 +0000173 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
174 return EmitLoadOfLValue(E);
175 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000176 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000177 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
178 return EmitLValue(E).getAddress();
179 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000180
Chris Lattner6307f192008-08-10 01:53:14 +0000181 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel43fc86d2007-10-24 17:18:43 +0000182
183 Value *VisitInitListExpr(InitListExpr *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000184 bool Ignore = TestAndClearIgnoreResultAssign();
185 (void)Ignore;
186 assert (Ignore == false && "init list ignored");
Anders Carlssona297e7a2007-12-05 07:36:10 +0000187 unsigned NumInitElements = E->getNumInits();
Mike Stump4a3999f2009-09-09 13:00:44 +0000188
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000189 if (E->hadArrayRangeDesignator()) {
190 CGF.ErrorUnsupported(E, "GNU array range designator extension");
191 }
192
Mike Stump4a3999f2009-09-09 13:00:44 +0000193 const llvm::VectorType *VType =
Anders Carlsson6f2a10e2008-01-29 01:15:48 +0000194 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
Mike Stump4a3999f2009-09-09 13:00:44 +0000195
Anders Carlsson6f2a10e2008-01-29 01:15:48 +0000196 // We have a scalar in braces. Just use the first element.
Mike Stump4a3999f2009-09-09 13:00:44 +0000197 if (!VType)
Anders Carlsson6f2a10e2008-01-29 01:15:48 +0000198 return Visit(E->getInit(0));
Mike Stump4a3999f2009-09-09 13:00:44 +0000199
Anders Carlssona297e7a2007-12-05 07:36:10 +0000200 unsigned NumVectorElements = VType->getNumElements();
201 const llvm::Type *ElementType = VType->getElementType();
Anders Carlssona297e7a2007-12-05 07:36:10 +0000202
203 // Emit individual vector element stores.
Owen Anderson7ec07a52009-07-30 23:11:26 +0000204 llvm::Value *V = llvm::UndefValue::get(VType);
Mike Stump4a3999f2009-09-09 13:00:44 +0000205
Anders Carlssonaa5c9192007-12-18 02:45:33 +0000206 // Emit initializers
207 unsigned i;
208 for (i = 0; i < NumInitElements; ++i) {
Devang Patelb67e5962007-10-24 18:05:48 +0000209 Value *NewV = Visit(E->getInit(i));
Owen Anderson41a75022009-08-13 21:57:51 +0000210 Value *Idx =
211 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i);
Devang Patelb67e5962007-10-24 18:05:48 +0000212 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel43fc86d2007-10-24 17:18:43 +0000213 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000214
Anders Carlssona297e7a2007-12-05 07:36:10 +0000215 // Emit remaining default initializers
216 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
Owen Anderson41a75022009-08-13 21:57:51 +0000217 Value *Idx =
218 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i);
Owen Anderson0b75f232009-07-31 20:28:54 +0000219 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
Anders Carlssona297e7a2007-12-05 07:36:10 +0000220 V = Builder.CreateInsertElement(V, NewV, Idx);
221 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000222
Devang Patelb67e5962007-10-24 18:05:48 +0000223 return V;
Devang Patel43fc86d2007-10-24 17:18:43 +0000224 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000225
Douglas Gregor0202cb42009-01-29 17:44:32 +0000226 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
Owen Anderson0b75f232009-07-31 20:28:54 +0000227 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Douglas Gregor0202cb42009-01-29 17:44:32 +0000228 }
Eli Friedmanbc633be2009-04-20 03:54:15 +0000229 Value *VisitCastExpr(const CastExpr *E) {
230 // Make sure to evaluate VLA bounds now so that we have them for later.
231 if (E->getType()->isVariablyModifiedType())
232 CGF.EmitVLASize(E->getType());
233
Anders Carlsson15583e42009-08-24 18:12:39 +0000234 return EmitCastExpr(E->getSubExpr(), E->getType(), E->getCastKind());
Chris Lattner2da04b32007-08-24 05:35:26 +0000235 }
Anders Carlsson15583e42009-08-24 18:12:39 +0000236 Value *EmitCastExpr(const Expr *E, QualType T, CastExpr::CastKind Kind);
Chris Lattner2da04b32007-08-24 05:35:26 +0000237
238 Value *VisitCallExpr(const CallExpr *E) {
Anders Carlssond8b7ae22009-05-27 03:37:57 +0000239 if (E->getCallReturnType()->isReferenceType())
240 return EmitLoadOfLValue(E);
Mike Stump4a3999f2009-09-09 13:00:44 +0000241
Chris Lattner4647a212007-08-31 22:49:20 +0000242 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner2da04b32007-08-24 05:35:26 +0000243 }
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000244
Chris Lattner04a913b2007-08-31 22:09:40 +0000245 Value *VisitStmtExpr(const StmtExpr *E);
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000246
Mike Stump1db7d042009-02-28 09:07:16 +0000247 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
Mike Stump4a3999f2009-09-09 13:00:44 +0000248
Chris Lattner2da04b32007-08-24 05:35:26 +0000249 // Unary Operators.
250 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
251 Value *VisitUnaryPostDec(const UnaryOperator *E) {
252 return VisitPrePostIncDec(E, false, false);
253 }
254 Value *VisitUnaryPostInc(const UnaryOperator *E) {
255 return VisitPrePostIncDec(E, true, false);
256 }
257 Value *VisitUnaryPreDec(const UnaryOperator *E) {
258 return VisitPrePostIncDec(E, false, true);
259 }
260 Value *VisitUnaryPreInc(const UnaryOperator *E) {
261 return VisitPrePostIncDec(E, true, true);
262 }
263 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
264 return EmitLValue(E->getSubExpr()).getAddress();
265 }
266 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
267 Value *VisitUnaryPlus(const UnaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000268 // This differs from gcc, though, most likely due to a bug in gcc.
269 TestAndClearIgnoreResultAssign();
Chris Lattner2da04b32007-08-24 05:35:26 +0000270 return Visit(E->getSubExpr());
271 }
272 Value *VisitUnaryMinus (const UnaryOperator *E);
273 Value *VisitUnaryNot (const UnaryOperator *E);
274 Value *VisitUnaryLNot (const UnaryOperator *E);
Chris Lattner9f0ad962007-08-24 21:20:17 +0000275 Value *VisitUnaryReal (const UnaryOperator *E);
276 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner2da04b32007-08-24 05:35:26 +0000277 Value *VisitUnaryExtension(const UnaryOperator *E) {
278 return Visit(E->getSubExpr());
279 }
Anders Carlssona8dc3e62008-01-29 15:56:48 +0000280 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Mike Stump4a3999f2009-09-09 13:00:44 +0000281
Anders Carlssona5d077d2009-04-14 16:58:56 +0000282 // C++
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000283 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
284 return Visit(DAE->getExpr());
285 }
Anders Carlssona5d077d2009-04-14 16:58:56 +0000286 Value *VisitCXXThisExpr(CXXThisExpr *TE) {
287 return CGF.LoadCXXThis();
Mike Stump4a3999f2009-09-09 13:00:44 +0000288 }
289
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000290 Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson2262b302009-05-31 00:09:15 +0000291 return CGF.EmitCXXExprWithTemporaries(E).getScalarVal();
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000292 }
Anders Carlsson4a7b49b2009-05-31 01:40:14 +0000293 Value *VisitCXXNewExpr(const CXXNewExpr *E) {
294 return CGF.EmitCXXNewExpr(E);
295 }
Anders Carlsson81f0df92009-08-16 21:13:42 +0000296 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
297 CGF.EmitCXXDeleteExpr(E);
298 return 0;
299 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000300
Douglas Gregorad8a3362009-09-04 17:36:40 +0000301 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
302 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +0000303 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +0000304 // operator (), and the result of such a call has type void. The only
305 // effect is the evaluation of the postfix-expression before the dot or
306 // arrow.
307 CGF.EmitScalarExpr(E->getBase());
308 return 0;
309 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000310
Chris Lattner2da04b32007-08-24 05:35:26 +0000311 // Binary Operators.
Chris Lattner2da04b32007-08-24 05:35:26 +0000312 Value *EmitMul(const BinOpInfo &Ops) {
Mike Stumpd3e38852009-04-02 18:15:54 +0000313 if (CGF.getContext().getLangOptions().OverflowChecking
314 && Ops.Ty->isSignedIntegerType())
Mike Stump0c61b732009-04-01 20:28:16 +0000315 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner94dfae22009-06-17 06:36:24 +0000316 if (Ops.LHS->getType()->isFPOrFPVector())
317 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
Chris Lattner2da04b32007-08-24 05:35:26 +0000318 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
319 }
Mike Stump0c61b732009-04-01 20:28:16 +0000320 /// Create a binary op that checks for overflow.
321 /// Currently only supports +, - and *.
322 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
Chris Lattner2da04b32007-08-24 05:35:26 +0000323 Value *EmitDiv(const BinOpInfo &Ops);
324 Value *EmitRem(const BinOpInfo &Ops);
325 Value *EmitAdd(const BinOpInfo &Ops);
326 Value *EmitSub(const BinOpInfo &Ops);
327 Value *EmitShl(const BinOpInfo &Ops);
328 Value *EmitShr(const BinOpInfo &Ops);
329 Value *EmitAnd(const BinOpInfo &Ops) {
330 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
331 }
332 Value *EmitXor(const BinOpInfo &Ops) {
333 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
334 }
335 Value *EmitOr (const BinOpInfo &Ops) {
336 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
337 }
338
Chris Lattner3d966d62007-08-24 21:00:35 +0000339 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattnerb6334692007-08-26 21:41:21 +0000340 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner3d966d62007-08-24 21:00:35 +0000341 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
342
343 // Binary operators and binary compound assignment operators.
344#define HANDLEBINOP(OP) \
Chris Lattnerb6334692007-08-26 21:41:21 +0000345 Value *VisitBin ## OP(const BinaryOperator *E) { \
346 return Emit ## OP(EmitBinOps(E)); \
347 } \
348 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
349 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner3d966d62007-08-24 21:00:35 +0000350 }
351 HANDLEBINOP(Mul);
352 HANDLEBINOP(Div);
353 HANDLEBINOP(Rem);
354 HANDLEBINOP(Add);
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +0000355 HANDLEBINOP(Sub);
Chris Lattner3d966d62007-08-24 21:00:35 +0000356 HANDLEBINOP(Shl);
357 HANDLEBINOP(Shr);
358 HANDLEBINOP(And);
359 HANDLEBINOP(Xor);
360 HANDLEBINOP(Or);
361#undef HANDLEBINOP
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +0000362
Chris Lattner2da04b32007-08-24 05:35:26 +0000363 // Comparisons.
364 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
365 unsigned SICmpOpc, unsigned FCmpOpc);
366#define VISITCOMP(CODE, UI, SI, FP) \
367 Value *VisitBin##CODE(const BinaryOperator *E) { \
368 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
369 llvm::FCmpInst::FP); }
370 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
371 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
372 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
373 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
374 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
375 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
376#undef VISITCOMP
Mike Stump4a3999f2009-09-09 13:00:44 +0000377
Chris Lattner2da04b32007-08-24 05:35:26 +0000378 Value *VisitBinAssign (const BinaryOperator *E);
379
380 Value *VisitBinLAnd (const BinaryOperator *E);
381 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner2da04b32007-08-24 05:35:26 +0000382 Value *VisitBinComma (const BinaryOperator *E);
383
384 // Other Operators.
Mike Stumpab3afd82009-02-12 18:29:15 +0000385 Value *VisitBlockExpr(const BlockExpr *BE);
Chris Lattner2da04b32007-08-24 05:35:26 +0000386 Value *VisitConditionalOperator(const ConditionalOperator *CO);
387 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000388 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner2da04b32007-08-24 05:35:26 +0000389 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
390 return CGF.EmitObjCStringLiteral(E);
391 }
392};
393} // end anonymous namespace.
394
395//===----------------------------------------------------------------------===//
396// Utilities
397//===----------------------------------------------------------------------===//
398
Chris Lattnere0044382007-08-26 16:42:57 +0000399/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner2e928882007-08-26 17:25:57 +0000400/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnere0044382007-08-26 16:42:57 +0000401Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
402 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
Mike Stump4a3999f2009-09-09 13:00:44 +0000403
Chris Lattnere0044382007-08-26 16:42:57 +0000404 if (SrcType->isRealFloatingType()) {
405 // Compare against 0.0 for fp scalars.
Owen Anderson0b75f232009-07-31 20:28:54 +0000406 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnere0044382007-08-26 16:42:57 +0000407 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
408 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000409
Anders Carlssonf48123b2009-08-09 18:26:27 +0000410 if (SrcType->isMemberPointerType()) {
411 // FIXME: This is ABI specific.
Mike Stump4a3999f2009-09-09 13:00:44 +0000412
Anders Carlssonf48123b2009-08-09 18:26:27 +0000413 // Compare against -1.
414 llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType());
415 return Builder.CreateICmpNE(Src, NegativeOne, "tobool");
416 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000417
Daniel Dunbaref957f32008-08-25 10:38:11 +0000418 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
Chris Lattnere0044382007-08-26 16:42:57 +0000419 "Unknown scalar type to convert");
Mike Stump4a3999f2009-09-09 13:00:44 +0000420
Chris Lattnere0044382007-08-26 16:42:57 +0000421 // Because of the type rules of C, we often end up computing a logical value,
422 // then zero extending it to int, then wanting it as a logical value again.
423 // Optimize this common case.
424 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
Owen Anderson41a75022009-08-13 21:57:51 +0000425 if (ZI->getOperand(0)->getType() ==
426 llvm::Type::getInt1Ty(CGF.getLLVMContext())) {
Chris Lattnere0044382007-08-26 16:42:57 +0000427 Value *Result = ZI->getOperand(0);
Eli Friedman7031d732008-01-29 18:13:51 +0000428 // If there aren't any more uses, zap the instruction to save space.
429 // Note that there can be more uses, for example if this
430 // is the result of an assignment.
431 if (ZI->use_empty())
432 ZI->eraseFromParent();
Chris Lattnere0044382007-08-26 16:42:57 +0000433 return Result;
434 }
435 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000436
Chris Lattnere0044382007-08-26 16:42:57 +0000437 // Compare against an integer or pointer null.
Owen Anderson0b75f232009-07-31 20:28:54 +0000438 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnere0044382007-08-26 16:42:57 +0000439 return Builder.CreateICmpNE(Src, Zero, "tobool");
440}
441
Chris Lattner3474c202007-08-26 06:48:56 +0000442/// EmitScalarConversion - Emit a conversion from the specified type to the
443/// specified destination type, both of which are LLVM scalar types.
Chris Lattner42e6b812007-08-26 16:34:22 +0000444Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
445 QualType DstType) {
Chris Lattner0f398c42008-07-26 22:37:01 +0000446 SrcType = CGF.getContext().getCanonicalType(SrcType);
447 DstType = CGF.getContext().getCanonicalType(DstType);
Chris Lattner3474c202007-08-26 06:48:56 +0000448 if (SrcType == DstType) return Src;
Mike Stump4a3999f2009-09-09 13:00:44 +0000449
Chris Lattner08c611e2007-08-26 07:21:11 +0000450 if (DstType->isVoidType()) return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000451
Owen Anderson41a75022009-08-13 21:57:51 +0000452 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Chris Lattner3474c202007-08-26 06:48:56 +0000453
454 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc141c1b2007-08-26 16:52:28 +0000455 if (DstType->isBooleanType())
456 return EmitConversionToBool(Src, SrcType);
Mike Stump4a3999f2009-09-09 13:00:44 +0000457
Chris Lattner3474c202007-08-26 06:48:56 +0000458 const llvm::Type *DstTy = ConvertType(DstType);
459
460 // Ignore conversions like int -> uint.
461 if (Src->getType() == DstTy)
462 return Src;
463
Mike Stump4a3999f2009-09-09 13:00:44 +0000464 // Handle pointer conversions next: pointers can only be converted to/from
465 // other pointers and integers. Check for pointer types in terms of LLVM, as
466 // some native types (like Obj-C id) may map to a pointer type.
Daniel Dunbar427f8732008-08-25 09:51:32 +0000467 if (isa<llvm::PointerType>(DstTy)) {
Chris Lattner3474c202007-08-26 06:48:56 +0000468 // The source value may be an integer, or a pointer.
Anders Carlsson12f5a252009-09-12 04:57:16 +0000469 if (isa<llvm::PointerType>(Src->getType()))
Chris Lattner3474c202007-08-26 06:48:56 +0000470 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson12f5a252009-09-12 04:57:16 +0000471
Chris Lattner3474c202007-08-26 06:48:56 +0000472 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
Eli Friedman42d2a3a2009-03-04 04:02:35 +0000473 // First, convert to the correct width so that we control the kind of
474 // extension.
Mike Stump4a3999f2009-09-09 13:00:44 +0000475 const llvm::Type *MiddleTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000476 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Eli Friedman42d2a3a2009-03-04 04:02:35 +0000477 bool InputSigned = SrcType->isSignedIntegerType();
478 llvm::Value* IntResult =
479 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
480 // Then, cast to pointer.
481 return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
Chris Lattner3474c202007-08-26 06:48:56 +0000482 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000483
Daniel Dunbar427f8732008-08-25 09:51:32 +0000484 if (isa<llvm::PointerType>(Src->getType())) {
Chris Lattner3474c202007-08-26 06:48:56 +0000485 // Must be an ptr to int cast.
486 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlssone89b84a2007-10-31 23:18:02 +0000487 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner3474c202007-08-26 06:48:56 +0000488 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000489
Nate Begemance4d7fc2008-04-18 23:10:10 +0000490 // A scalar can be splatted to an extended vector of the same element type
Nate Begeman5ec4b312009-08-10 23:49:36 +0000491 if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000492 // Cast the scalar to element type
493 QualType EltTy = DstType->getAsExtVectorType()->getElementType();
494 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
495
496 // Insert the element in element zero of an undef vector
Owen Anderson7ec07a52009-07-30 23:11:26 +0000497 llvm::Value *UnV = llvm::UndefValue::get(DstTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000498 llvm::Value *Idx =
499 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000500 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
501
502 // Splat the element across to all elements
503 llvm::SmallVector<llvm::Constant*, 16> Args;
504 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
505 for (unsigned i = 0; i < NumElements; i++)
Owen Anderson41a75022009-08-13 21:57:51 +0000506 Args.push_back(llvm::ConstantInt::get(
507 llvm::Type::getInt32Ty(VMContext), 0));
Mike Stump4a3999f2009-09-09 13:00:44 +0000508
Owen Anderson3cc120a2009-07-28 21:22:35 +0000509 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000510 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
511 return Yay;
512 }
Nate Begeman330aaa72007-12-30 02:59:45 +0000513
Chris Lattner6cba8e92008-02-02 04:51:41 +0000514 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlssona297e7a2007-12-05 07:36:10 +0000515 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner6cba8e92008-02-02 04:51:41 +0000516 isa<llvm::VectorType>(DstTy))
Anders Carlssona297e7a2007-12-05 07:36:10 +0000517 return Builder.CreateBitCast(Src, DstTy, "conv");
Mike Stump4a3999f2009-09-09 13:00:44 +0000518
Chris Lattner3474c202007-08-26 06:48:56 +0000519 // Finally, we have the arithmetic types: real int/float.
520 if (isa<llvm::IntegerType>(Src->getType())) {
521 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlssonc9d41e72007-12-26 18:20:19 +0000522 if (isa<llvm::IntegerType>(DstTy))
523 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
524 else if (InputSigned)
525 return Builder.CreateSIToFP(Src, DstTy, "conv");
526 else
527 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner3474c202007-08-26 06:48:56 +0000528 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000529
Chris Lattner3474c202007-08-26 06:48:56 +0000530 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
531 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlssonc9d41e72007-12-26 18:20:19 +0000532 if (DstType->isSignedIntegerType())
533 return Builder.CreateFPToSI(Src, DstTy, "conv");
534 else
535 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner3474c202007-08-26 06:48:56 +0000536 }
537
538 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlssonc9d41e72007-12-26 18:20:19 +0000539 if (DstTy->getTypeID() < Src->getType()->getTypeID())
540 return Builder.CreateFPTrunc(Src, DstTy, "conv");
541 else
542 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner3474c202007-08-26 06:48:56 +0000543}
544
Mike Stump4a3999f2009-09-09 13:00:44 +0000545/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
546/// type to the specified destination type, where the destination type is an
547/// LLVM scalar type.
Chris Lattner42e6b812007-08-26 16:34:22 +0000548Value *ScalarExprEmitter::
549EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
550 QualType SrcTy, QualType DstTy) {
Chris Lattnerc141c1b2007-08-26 16:52:28 +0000551 // Get the source element type.
Chris Lattner0f398c42008-07-26 22:37:01 +0000552 SrcTy = SrcTy->getAsComplexType()->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000553
Chris Lattnerc141c1b2007-08-26 16:52:28 +0000554 // Handle conversions to bool first, they are special: comparisons against 0.
555 if (DstTy->isBooleanType()) {
556 // Complex != 0 -> (Real != 0) | (Imag != 0)
557 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
558 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
559 return Builder.CreateOr(Src.first, Src.second, "tobool");
560 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000561
Chris Lattner42e6b812007-08-26 16:34:22 +0000562 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
563 // the imaginary part of the complex value is discarded and the value of the
564 // real part is converted according to the conversion rules for the
Mike Stump4a3999f2009-09-09 13:00:44 +0000565 // corresponding real type.
Chris Lattner42e6b812007-08-26 16:34:22 +0000566 return EmitScalarConversion(Src.first, SrcTy, DstTy);
567}
568
569
Chris Lattner2da04b32007-08-24 05:35:26 +0000570//===----------------------------------------------------------------------===//
571// Visitor Methods
572//===----------------------------------------------------------------------===//
573
574Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000575 CGF.ErrorUnsupported(E, "scalar expression");
Chris Lattner2da04b32007-08-24 05:35:26 +0000576 if (E->getType()->isVoidType())
577 return 0;
Owen Anderson7ec07a52009-07-30 23:11:26 +0000578 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Chris Lattner2da04b32007-08-24 05:35:26 +0000579}
580
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000581Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
582 llvm::SmallVector<llvm::Constant*, 32> indices;
583 for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
584 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
585 }
586 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
587 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000588 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000589 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
590}
591
Chris Lattner2da04b32007-08-24 05:35:26 +0000592Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000593 TestAndClearIgnoreResultAssign();
594
Chris Lattner2da04b32007-08-24 05:35:26 +0000595 // Emit subscript expressions in rvalue context's. For most cases, this just
596 // loads the lvalue formed by the subscript expr. However, we have to be
597 // careful, because the base of a vector subscript is occasionally an rvalue,
598 // so we can't get it as an lvalue.
599 if (!E->getBase()->getType()->isVectorType())
600 return EmitLoadOfLValue(E);
Mike Stump4a3999f2009-09-09 13:00:44 +0000601
Chris Lattner2da04b32007-08-24 05:35:26 +0000602 // Handle the vector case. The base must be a vector, the index must be an
603 // integer value.
604 Value *Base = Visit(E->getBase());
605 Value *Idx = Visit(E->getIdx());
Eli Friedmane381f7e2009-03-28 02:45:41 +0000606 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
Owen Anderson41a75022009-08-13 21:57:51 +0000607 Idx = Builder.CreateIntCast(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +0000608 llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Owen Anderson41a75022009-08-13 21:57:51 +0000609 IdxSigned,
Eli Friedman754d5ac2009-03-28 03:27:06 +0000610 "vecidxcast");
Chris Lattner2da04b32007-08-24 05:35:26 +0000611 return Builder.CreateExtractElement(Base, Idx, "vecext");
612}
613
Chris Lattner2da04b32007-08-24 05:35:26 +0000614// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
615// have to handle a more broad range of conversions than explicit casts, as they
616// handle things like function to ptr-to-function decay etc.
Anders Carlsson15583e42009-08-24 18:12:39 +0000617Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy,
618 CastExpr::CastKind Kind) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000619 if (!DestTy->isVoidType())
620 TestAndClearIgnoreResultAssign();
Mike Stump4a3999f2009-09-09 13:00:44 +0000621
Anders Carlsson3df53bc2009-08-24 18:26:39 +0000622 switch (Kind) {
623 default:
624 break;
Anders Carlssonf1ae6d42009-09-01 20:52:42 +0000625 case CastExpr::CK_BitCast: {
626 Value *Src = Visit(const_cast<Expr*>(E));
627 return Builder.CreateBitCast(Src, ConvertType(DestTy));
628 }
Anders Carlssonc3277092009-08-24 18:37:17 +0000629 case CastExpr::CK_ArrayToPointerDecay: {
630 assert(E->getType()->isArrayType() &&
631 "Array to pointer decay must have array source type!");
Mike Stump4a3999f2009-09-09 13:00:44 +0000632
Anders Carlssonc3277092009-08-24 18:37:17 +0000633 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
634
635 // Note that VLA pointers are always decayed, so we don't need to do
636 // anything here.
637 if (!E->getType()->isVariableArrayType()) {
638 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
639 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
640 ->getElementType()) &&
641 "Expected pointer to array");
642 V = Builder.CreateStructGEP(V, 0, "arraydecay");
643 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000644
Anders Carlssonc3277092009-08-24 18:37:17 +0000645 // The resultant pointer type can be implicitly casted to other pointer
646 // types as well (e.g. void*) and can be implicitly converted to integer.
647 const llvm::Type *DestLTy = ConvertType(DestTy);
648 if (V->getType() != DestLTy) {
649 if (isa<llvm::PointerType>(DestLTy))
650 V = Builder.CreateBitCast(V, DestLTy, "ptrconv");
651 else {
652 assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay");
653 V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv");
654 }
655 }
656 return V;
Mike Stump4a3999f2009-09-09 13:00:44 +0000657 }
Anders Carlsson3df53bc2009-08-24 18:26:39 +0000658 case CastExpr::CK_NullToMemberPointer:
659 return CGF.CGM.EmitNullConstant(DestTy);
Anders Carlsson12f5a252009-09-12 04:57:16 +0000660
661 case CastExpr::CK_DerivedToBase: {
662 const RecordType *DerivedClassTy =
663 E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
664 CXXRecordDecl *DerivedClassDecl =
665 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
666
667 const RecordType *BaseClassTy =
668 DestTy->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
669 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
670
671 Value *Src = Visit(const_cast<Expr*>(E));
672 return CGF.GetAddressCXXOfBaseClass(Src, DerivedClassDecl, BaseClassDecl,
673 /*NullCheckValue=*/true);
674 }
675
Anders Carlsson3df53bc2009-08-24 18:26:39 +0000676 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000677
Chris Lattner5de3b172007-08-26 07:26:12 +0000678 // Handle cases where the source is an non-complex type.
Mike Stump4a3999f2009-09-09 13:00:44 +0000679
Chris Lattnerdf53e202008-02-16 23:55:16 +0000680 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner3474c202007-08-26 06:48:56 +0000681 Value *Src = Visit(const_cast<Expr*>(E));
682
Chris Lattner3474c202007-08-26 06:48:56 +0000683 // Use EmitScalarConversion to perform the conversion.
684 return EmitScalarConversion(Src, E->getType(), DestTy);
685 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000686
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000687 if (E->getType()->isAnyComplexType()) {
Chris Lattnerdf53e202008-02-16 23:55:16 +0000688 // Handle cases where the source is a complex type.
Mike Stumpdf0fe272009-05-29 15:46:01 +0000689 bool IgnoreImag = true;
690 bool IgnoreImagAssign = true;
691 bool IgnoreReal = IgnoreResultAssign;
692 bool IgnoreRealAssign = IgnoreResultAssign;
693 if (DestTy->isBooleanType())
694 IgnoreImagAssign = IgnoreImag = false;
695 else if (DestTy->isVoidType()) {
696 IgnoreReal = IgnoreImag = false;
697 IgnoreRealAssign = IgnoreImagAssign = true;
698 }
699 CodeGenFunction::ComplexPairTy V
700 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign,
701 IgnoreImagAssign);
702 return EmitComplexToScalarConversion(V, E->getType(), DestTy);
Chris Lattnerdf53e202008-02-16 23:55:16 +0000703 }
Chris Lattner46c71612007-08-26 07:16:41 +0000704
Chris Lattnerdf53e202008-02-16 23:55:16 +0000705 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
706 // evaluate the result and return.
Mike Stumpdf0fe272009-05-29 15:46:01 +0000707 CGF.EmitAggExpr(E, 0, false, true);
Chris Lattnerdf53e202008-02-16 23:55:16 +0000708 return 0;
Chris Lattner2da04b32007-08-24 05:35:26 +0000709}
710
Chris Lattner04a913b2007-08-31 22:09:40 +0000711Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner7e800972008-07-26 20:23:23 +0000712 return CGF.EmitCompoundStmt(*E->getSubStmt(),
713 !E->getType()->isVoidType()).getScalarVal();
Chris Lattner04a913b2007-08-31 22:09:40 +0000714}
715
Mike Stump1db7d042009-02-28 09:07:16 +0000716Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
717 return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp");
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000718}
Chris Lattner04a913b2007-08-31 22:09:40 +0000719
Chris Lattner2da04b32007-08-24 05:35:26 +0000720//===----------------------------------------------------------------------===//
721// Unary Operators
722//===----------------------------------------------------------------------===//
723
724Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner100198f2007-08-24 16:24:49 +0000725 bool isInc, bool isPre) {
Chris Lattner2da04b32007-08-24 05:35:26 +0000726 LValue LV = EmitLValue(E->getSubExpr());
Eli Friedman7b471672009-03-23 03:00:06 +0000727 QualType ValTy = E->getSubExpr()->getType();
728 Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal();
Owen Anderson41a75022009-08-13 21:57:51 +0000729
730 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump4a3999f2009-09-09 13:00:44 +0000731
Chris Lattner2da04b32007-08-24 05:35:26 +0000732 int AmountVal = isInc ? 1 : -1;
Eli Friedmane381f7e2009-03-28 02:45:41 +0000733
734 if (ValTy->isPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000735 ValTy->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedmane381f7e2009-03-28 02:45:41 +0000736 // The amount of the addition/subtraction needs to account for the VLA size
737 CGF.ErrorUnsupported(E, "VLA pointer inc/dec");
738 }
739
Chris Lattner2da04b32007-08-24 05:35:26 +0000740 Value *NextVal;
Mike Stump4a3999f2009-09-09 13:00:44 +0000741 if (const llvm::PointerType *PT =
Chris Lattnerc2a0b972009-03-18 04:25:13 +0000742 dyn_cast<llvm::PointerType>(InVal->getType())) {
Owen Anderson170229f2009-07-14 23:10:40 +0000743 llvm::Constant *Inc =
Owen Anderson41a75022009-08-13 21:57:51 +0000744 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
Chris Lattnerc2a0b972009-03-18 04:25:13 +0000745 if (!isa<llvm::FunctionType>(PT->getElementType())) {
Fariborz Jahanianc3443a32009-07-16 22:04:59 +0000746 QualType PTEE = ValTy->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000747 if (const ObjCInterfaceType *OIT =
Fariborz Jahanianc3443a32009-07-16 22:04:59 +0000748 dyn_cast<ObjCInterfaceType>(PTEE)) {
749 // Handle interface types, which are not represented with a concrete type.
750 int size = CGF.getContext().getTypeSize(OIT) / 8;
751 if (!isInc)
752 size = -size;
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000753 Inc = llvm::ConstantInt::get(Inc->getType(), size);
Mike Stump4a3999f2009-09-09 13:00:44 +0000754 const llvm::Type *i8Ty =
Owen Anderson41a75022009-08-13 21:57:51 +0000755 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Fariborz Jahanianc3443a32009-07-16 22:04:59 +0000756 InVal = Builder.CreateBitCast(InVal, i8Ty);
757 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
758 llvm::Value *lhs = LV.getAddress();
Owen Anderson9793f0e2009-07-29 22:16:19 +0000759 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
Mike Stump4a3999f2009-09-09 13:00:44 +0000760 LV = LValue::MakeAddr(lhs, ValTy.getCVRQualifiers(),
Fariborz Jahanianc3443a32009-07-16 22:04:59 +0000761 CGF.getContext().getObjCGCAttrKind(ValTy));
Mike Stump658fe022009-07-30 22:28:39 +0000762 } else
Dan Gohman43b44842009-08-12 00:33:55 +0000763 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
Chris Lattnerc2a0b972009-03-18 04:25:13 +0000764 } else {
Owen Anderson170229f2009-07-14 23:10:40 +0000765 const llvm::Type *i8Ty =
Owen Anderson41a75022009-08-13 21:57:51 +0000766 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Chris Lattnerc2a0b972009-03-18 04:25:13 +0000767 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
768 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
769 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
770 }
Owen Anderson41a75022009-08-13 21:57:51 +0000771 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
Chris Lattner746b2132009-02-11 07:40:06 +0000772 // Bool++ is an interesting case, due to promotion rules, we get:
773 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
774 // Bool = ((int)Bool+1) != 0
775 // An interesting aspect of this is that increment is always true.
776 // Decrement does not have this property.
Owen Andersonfe4e3472009-07-31 17:39:36 +0000777 NextVal = llvm::ConstantInt::getTrue(VMContext);
Chris Lattner94dfae22009-06-17 06:36:24 +0000778 } else if (isa<llvm::IntegerType>(InVal->getType())) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000779 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Dan Gohmanb3210392009-08-12 01:16:29 +0000780
781 // Signed integer overflow is undefined behavior.
782 if (ValTy->isSignedIntegerType())
783 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
784 else
785 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattnera01d8982007-08-26 05:10:16 +0000786 } else {
787 // Add the inc/dec to the real part.
Owen Anderson41a75022009-08-13 21:57:51 +0000788 if (InVal->getType() == llvm::Type::getFloatTy(VMContext))
Mike Stump4a3999f2009-09-09 13:00:44 +0000789 NextVal =
790 llvm::ConstantFP::get(VMContext,
Owen Andersone05f2ed2009-07-27 21:00:51 +0000791 llvm::APFloat(static_cast<float>(AmountVal)));
Owen Anderson41a75022009-08-13 21:57:51 +0000792 else if (InVal->getType() == llvm::Type::getDoubleTy(VMContext))
Mike Stump4a3999f2009-09-09 13:00:44 +0000793 NextVal =
Owen Andersone05f2ed2009-07-27 21:00:51 +0000794 llvm::ConstantFP::get(VMContext,
795 llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerff2367c2008-04-20 00:50:39 +0000796 else {
797 llvm::APFloat F(static_cast<float>(AmountVal));
Dale Johannesenc48814b2008-10-09 23:02:32 +0000798 bool ignored;
799 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
800 &ignored);
Owen Andersone05f2ed2009-07-27 21:00:51 +0000801 NextVal = llvm::ConstantFP::get(VMContext, F);
Chris Lattnerd3d8aca2007-09-13 06:19:18 +0000802 }
Chris Lattner94dfae22009-06-17 06:36:24 +0000803 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
Chris Lattnera01d8982007-08-26 05:10:16 +0000804 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000805
Chris Lattner2da04b32007-08-24 05:35:26 +0000806 // Store the updated result through the lvalue.
Eli Friedman7b471672009-03-23 03:00:06 +0000807 if (LV.isBitfield())
808 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy,
809 &NextVal);
810 else
811 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
Chris Lattner2da04b32007-08-24 05:35:26 +0000812
813 // If this is a postinc, return the value read from memory, otherwise use the
814 // updated value.
815 return isPre ? NextVal : InVal;
816}
817
818
819Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000820 TestAndClearIgnoreResultAssign();
Chris Lattner2da04b32007-08-24 05:35:26 +0000821 Value *Op = Visit(E->getSubExpr());
Chris Lattner94dfae22009-06-17 06:36:24 +0000822 if (Op->getType()->isFPOrFPVector())
823 return Builder.CreateFNeg(Op, "neg");
Chris Lattner2da04b32007-08-24 05:35:26 +0000824 return Builder.CreateNeg(Op, "neg");
825}
826
827Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000828 TestAndClearIgnoreResultAssign();
Chris Lattner2da04b32007-08-24 05:35:26 +0000829 Value *Op = Visit(E->getSubExpr());
830 return Builder.CreateNot(Op, "neg");
831}
832
833Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
834 // Compare operand to zero.
835 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000836
Chris Lattner2da04b32007-08-24 05:35:26 +0000837 // Invert value.
838 // TODO: Could dynamically modify easy computations here. For example, if
839 // the operand is an icmp ne, turn into icmp eq.
840 BoolVal = Builder.CreateNot(BoolVal, "lnot");
Mike Stump4a3999f2009-09-09 13:00:44 +0000841
Anders Carlsson775640d2009-05-19 18:44:53 +0000842 // ZExt result to the expr type.
843 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
Chris Lattner2da04b32007-08-24 05:35:26 +0000844}
845
Sebastian Redl6f282892008-11-11 17:56:53 +0000846/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
847/// argument of the sizeof expression as an integer.
848Value *
849ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
Sebastian Redl6f282892008-11-11 17:56:53 +0000850 QualType TypeToSize = E->getTypeOfArgument();
Eli Friedman2aa38fe2009-01-24 22:19:05 +0000851 if (E->isSizeOf()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000852 if (const VariableArrayType *VAT =
Eli Friedman2aa38fe2009-01-24 22:19:05 +0000853 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
854 if (E->isArgumentType()) {
855 // sizeof(type) - make sure to emit the VLA size.
856 CGF.EmitVLASize(TypeToSize);
Eli Friedman3253e182009-04-20 03:21:44 +0000857 } else {
858 // C99 6.5.3.4p2: If the argument is an expression of type
859 // VLA, it is evaluated.
860 CGF.EmitAnyExpr(E->getArgumentExpr());
Eli Friedman2aa38fe2009-01-24 22:19:05 +0000861 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000862
Anders Carlsson31f86492009-02-05 19:43:10 +0000863 return CGF.GetVLASize(VAT);
Anders Carlsson76dbc042008-12-21 03:33:21 +0000864 }
Anders Carlsson30032882008-12-12 07:38:43 +0000865 }
Eli Friedman2aa38fe2009-01-24 22:19:05 +0000866
Mike Stump4a3999f2009-09-09 13:00:44 +0000867 // If this isn't sizeof(vla), the result must be constant; use the constant
868 // folding logic so we don't have to duplicate it here.
Eli Friedman2aa38fe2009-01-24 22:19:05 +0000869 Expr::EvalResult Result;
870 E->Evaluate(Result, CGF.getContext());
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000871 return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
Chris Lattner2da04b32007-08-24 05:35:26 +0000872}
873
Chris Lattner9f0ad962007-08-24 21:20:17 +0000874Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
875 Expr *Op = E->getSubExpr();
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000876 if (Op->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +0000877 return CGF.EmitComplexExpr(Op, false, true, false, true).first;
Chris Lattner9f0ad962007-08-24 21:20:17 +0000878 return Visit(Op);
879}
880Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
881 Expr *Op = E->getSubExpr();
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000882 if (Op->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +0000883 return CGF.EmitComplexExpr(Op, true, false, true, false).second;
Mike Stump4a3999f2009-09-09 13:00:44 +0000884
Mike Stumpdf0fe272009-05-29 15:46:01 +0000885 // __imag on a scalar returns zero. Emit the subexpr to ensure side
886 // effects are evaluated, but not the actual value.
887 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid)
888 CGF.EmitLValue(Op);
889 else
890 CGF.EmitScalarExpr(Op, true);
Owen Anderson0b75f232009-07-31 20:28:54 +0000891 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner9f0ad962007-08-24 21:20:17 +0000892}
893
Mike Stump11289f42009-09-09 15:08:12 +0000894Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) {
Eli Friedman988a16b2009-02-27 06:44:11 +0000895 Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
Eli Friedman8549e5d2009-01-24 22:38:55 +0000896 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedman988a16b2009-02-27 06:44:11 +0000897 return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
Anders Carlssona8dc3e62008-01-29 15:56:48 +0000898}
Chris Lattner9f0ad962007-08-24 21:20:17 +0000899
Chris Lattner2da04b32007-08-24 05:35:26 +0000900//===----------------------------------------------------------------------===//
901// Binary Operators
902//===----------------------------------------------------------------------===//
903
904BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000905 TestAndClearIgnoreResultAssign();
Chris Lattner2da04b32007-08-24 05:35:26 +0000906 BinOpInfo Result;
907 Result.LHS = Visit(E->getLHS());
908 Result.RHS = Visit(E->getRHS());
Chris Lattner3d966d62007-08-24 21:00:35 +0000909 Result.Ty = E->getType();
Chris Lattner2da04b32007-08-24 05:35:26 +0000910 Result.E = E;
911 return Result;
912}
913
Chris Lattnerb6334692007-08-26 21:41:21 +0000914Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner3d966d62007-08-24 21:00:35 +0000915 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000916 bool Ignore = TestAndClearIgnoreResultAssign();
Chris Lattner3d966d62007-08-24 21:00:35 +0000917 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
918
919 BinOpInfo OpInfo;
920
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000921 if (E->getComputationResultType()->isAnyComplexType()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000922 // This needs to go through the complex expression emitter, but it's a tad
923 // complicated to do that... I'm leaving it out for now. (Note that we do
924 // actually need the imaginary part of the RHS for multiplication and
925 // division.)
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000926 CGF.ErrorUnsupported(E, "complex compound assignment");
Owen Anderson7ec07a52009-07-30 23:11:26 +0000927 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000928 }
929
Mike Stumpc63428b2009-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 Friedman8b7b1b12009-03-28 01:22:36 +0000935 // Load/convert the LHS.
Chris Lattner3d966d62007-08-24 21:00:35 +0000936 LValue LHSLV = EmitLValue(E->getLHS());
937 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000938 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
939 E->getComputationLHSType());
Mike Stump4a3999f2009-09-09 13:00:44 +0000940
Chris Lattner3d966d62007-08-24 21:00:35 +0000941 // Expand the binary operator.
942 Value *Result = (this->*Func)(OpInfo);
Mike Stump4a3999f2009-09-09 13:00:44 +0000943
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +0000944 // Convert the result back to the LHS type.
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000945 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
946
Mike Stump4a3999f2009-09-09 13:00:44 +0000947 // Store the result value into the LHS lvalue. Bit-fields are handled
948 // specially because the result is altered by the store, i.e., [C99 6.5.16p1]
949 // 'An assignment expression has the value of the left operand after the
950 // assignment...'.
Mike Stumpdf0fe272009-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 Dunbar9b1335e2008-11-19 09:36:46 +0000959 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
Mike Stumpdf0fe272009-05-29 15:46:01 +0000960 if (Ignore)
961 return 0;
962 return EmitLoadOfLValue(LHSLV, E->getType());
Chris Lattner3d966d62007-08-24 21:00:35 +0000963}
964
965
Chris Lattner2da04b32007-08-24 05:35:26 +0000966Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begeman628028b2007-12-30 01:28:16 +0000967 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner2da04b32007-08-24 05:35:26 +0000968 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner3d966d62007-08-24 21:00:35 +0000969 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner2da04b32007-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 Lattner3d966d62007-08-24 21:00:35 +0000977 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner2da04b32007-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 Stump0c61b732009-04-01 20:28:16 +0000983Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
984 unsigned IID;
985 unsigned OpID = 0;
Mike Stump40968592009-04-02 01:03:55 +0000986
Mike Stumpd3e38852009-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 Dunbard92123f2009-04-08 16:23:09 +00001005 IID = 0;
Mike Stump0c61b732009-04-01 20:28:16 +00001006 }
Mike Stumpd3e38852009-04-02 18:15:54 +00001007 OpID <<= 1;
1008 OpID |= 1;
1009
Mike Stump0c61b732009-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:
Mike Stump4a3999f2009-09-09 13:00:44 +00001032 // long long *__overflow_handler)(long long a, long long b, char op,
Mike Stump0c61b732009-04-01 20:28:16 +00001033 // char width)
1034 std::vector<const llvm::Type*> handerArgTypes;
Owen Anderson41a75022009-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 Stump0c61b732009-04-01 20:28:16 +00001041 llvm::Value *handlerFunction =
1042 CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
Owen Anderson9793f0e2009-07-29 22:16:19 +00001043 llvm::PointerType::getUnqual(handlerTy));
Mike Stump0c61b732009-04-01 20:28:16 +00001044 handlerFunction = Builder.CreateLoad(handlerFunction);
1045
1046 llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
Owen Anderson41a75022009-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),
Mike Stump4a3999f2009-09-09 13:00:44 +00001050 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
Mike Stump0c61b732009-04-01 20:28:16 +00001051 cast<llvm::IntegerType>(opTy)->getBitWidth()));
1052
1053 handlerResult = Builder.CreateTrunc(handlerResult, opTy);
1054
1055 Builder.CreateBr(continueBB);
Mike Stump4a3999f2009-09-09 13:00:44 +00001056
Mike Stump0c61b732009-04-01 20:28:16 +00001057 // 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 Lattner2da04b32007-08-24 05:35:26 +00001067
1068Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Steve Naroff6b712a72009-07-14 18:25:06 +00001069 if (!Ops.Ty->isAnyPointerType()) {
Chris Lattner94dfae22009-06-17 06:36:24 +00001070 if (CGF.getContext().getLangOptions().OverflowChecking &&
1071 Ops.Ty->isSignedIntegerType())
Mike Stump0c61b732009-04-01 20:28:16 +00001072 return EmitOverflowCheckedBinOp(Ops);
Mike Stump4a3999f2009-09-09 13:00:44 +00001073
Chris Lattner94dfae22009-06-17 06:36:24 +00001074 if (Ops.LHS->getType()->isFPOrFPVector())
1075 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add");
Dan Gohmanb3210392009-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 Lattner2da04b32007-08-24 05:35:26 +00001081 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Mike Stump0c61b732009-04-01 20:28:16 +00001082 }
Eli Friedmane381f7e2009-03-28 02:45:41 +00001083
Steve Naroff7cae42b2009-07-10 23:34:53 +00001084 if (Ops.Ty->isPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001085 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedmane381f7e2009-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 Lattner20455f22008-01-03 06:36:51 +00001089 Value *Ptr, *Idx;
1090 Expr *IdxExp;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001091 const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>();
Mike Stump4a3999f2009-09-09 13:00:44 +00001092 const ObjCObjectPointerType *OPT =
Steve Naroff7cae42b2009-07-10 23:34:53 +00001093 Ops.E->getLHS()->getType()->getAsObjCObjectPointerType();
1094 if (PT || OPT) {
Chris Lattner20455f22008-01-03 06:36:51 +00001095 Ptr = Ops.LHS;
1096 Idx = Ops.RHS;
1097 IdxExp = Ops.E->getRHS();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001098 } else { // int + pointer
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001099 PT = Ops.E->getRHS()->getType()->getAs<PointerType>();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001100 OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType();
1101 assert((PT || OPT) && "Invalid add expr");
Chris Lattner20455f22008-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 Gupta47425152009-04-24 02:40:57 +00001108 if (Width < CGF.LLVMPointerWidth) {
Chris Lattner20455f22008-01-03 06:36:51 +00001109 // Zero or sign extend the pointer value based on whether the index is
1110 // signed or not.
Owen Anderson41a75022009-08-13 21:57:51 +00001111 const llvm::Type *IdxType =
1112 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Chris Lattner0f398c42008-07-26 22:37:01 +00001113 if (IdxExp->getType()->isSignedIntegerType())
Chris Lattner20455f22008-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 Naroff7cae42b2009-07-10 23:34:53 +00001118 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001119 // Handle interface types, which are not represented with a concrete type.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001120 if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001121 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001122 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001123 CGF.getContext().getTypeSize(OIT) / 8);
1124 Idx = Builder.CreateMul(Idx, InterfaceSize);
Owen Anderson41a75022009-08-13 21:57:51 +00001125 const llvm::Type *i8Ty =
1126 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001127 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
1128 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
1129 return Builder.CreateBitCast(Res, Ptr->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001130 }
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001131
Mike Stump4a3999f2009-09-09 13:00:44 +00001132 // Explicitly handle GNU void* and function pointer arithmetic extensions. The
1133 // GNU void* casts amount to no-ops since our void* type is i8*, but this is
1134 // future proof.
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001135 if (ElementType->isVoidType() || ElementType->isFunctionType()) {
Owen Anderson41a75022009-08-13 21:57:51 +00001136 const llvm::Type *i8Ty =
1137 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001138 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001139 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001140 return Builder.CreateBitCast(Res, Ptr->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001141 }
1142
Dan Gohman43b44842009-08-12 00:33:55 +00001143 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr");
Chris Lattner2da04b32007-08-24 05:35:26 +00001144}
1145
1146Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
Mike Stump0c61b732009-04-01 20:28:16 +00001147 if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
Mike Stumpd3e38852009-04-02 18:15:54 +00001148 if (CGF.getContext().getLangOptions().OverflowChecking
1149 && Ops.Ty->isSignedIntegerType())
Mike Stump0c61b732009-04-01 20:28:16 +00001150 return EmitOverflowCheckedBinOp(Ops);
Chris Lattner94dfae22009-06-17 06:36:24 +00001151
1152 if (Ops.LHS->getType()->isFPOrFPVector())
1153 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub");
Chris Lattner2da04b32007-08-24 05:35:26 +00001154 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
Mike Stump0c61b732009-04-01 20:28:16 +00001155 }
Chris Lattner3d966d62007-08-24 21:00:35 +00001156
Steve Naroff7cae42b2009-07-10 23:34:53 +00001157 if (Ops.E->getLHS()->getType()->isPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001158 Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) {
Eli Friedmane381f7e2009-03-28 02:45:41 +00001159 // The amount of the addition needs to account for the VLA size for
1160 // ptr-int
1161 // The amount of the division needs to account for the VLA size for
1162 // ptr-ptr.
1163 CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
1164 }
1165
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001166 const QualType LHSType = Ops.E->getLHS()->getType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00001167 const QualType LHSElementType = LHSType->getPointeeType();
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001168 if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
1169 // pointer - int
1170 Value *Idx = Ops.RHS;
1171 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001172 if (Width < CGF.LLVMPointerWidth) {
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001173 // Zero or sign extend the pointer value based on whether the index is
1174 // signed or not.
Owen Anderson41a75022009-08-13 21:57:51 +00001175 const llvm::Type *IdxType =
1176 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001177 if (Ops.E->getRHS()->getType()->isSignedIntegerType())
1178 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
1179 else
1180 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
1181 }
1182 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001183
Mike Stump4a3999f2009-09-09 13:00:44 +00001184 // Handle interface types, which are not represented with a concrete type.
1185 if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001186 dyn_cast<ObjCInterfaceType>(LHSElementType)) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001187 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001188 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001189 CGF.getContext().getTypeSize(OIT) / 8);
1190 Idx = Builder.CreateMul(Idx, InterfaceSize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001191 const llvm::Type *i8Ty =
Owen Anderson41a75022009-08-13 21:57:51 +00001192 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001193 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1194 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
1195 return Builder.CreateBitCast(Res, Ops.LHS->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001196 }
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001197
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001198 // Explicitly handle GNU void* and function pointer arithmetic
Mike Stump4a3999f2009-09-09 13:00:44 +00001199 // extensions. The GNU void* casts amount to no-ops since our void* type is
1200 // i8*, but this is future proof.
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001201 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001202 const llvm::Type *i8Ty =
Owen Anderson41a75022009-08-13 21:57:51 +00001203 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar42a8cd32009-01-23 18:51:09 +00001204 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
1205 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
1206 return Builder.CreateBitCast(Res, Ops.LHS->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001207 }
1208
Dan Gohman43b44842009-08-12 00:33:55 +00001209 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr");
Daniel Dunbar5aa55d52008-08-05 00:47:03 +00001210 } else {
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001211 // pointer - pointer
1212 Value *LHS = Ops.LHS;
1213 Value *RHS = Ops.RHS;
Mike Stump4a3999f2009-09-09 13:00:44 +00001214
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001215 uint64_t ElementSize;
Daniel Dunbar5aa55d52008-08-05 00:47:03 +00001216
Chris Lattner60dcdc72009-02-11 07:21:43 +00001217 // Handle GCC extension for pointer arithmetic on void* and function pointer
1218 // types.
1219 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001220 ElementSize = 1;
1221 } else {
1222 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
1223 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001224
Daniel Dunbarbfb1cd72008-08-06 02:00:38 +00001225 const llvm::Type *ResultType = ConvertType(Ops.Ty);
1226 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
1227 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1228 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Mike Stump4a3999f2009-09-09 13:00:44 +00001229
Chris Lattner60dcdc72009-02-11 07:21:43 +00001230 // Optimize out the shift for element size of 1.
1231 if (ElementSize == 1)
1232 return BytesBetween;
Dan Gohman1cebe562009-08-11 22:40:09 +00001233
1234 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
Mike Stump4a3999f2009-09-09 13:00:44 +00001235 // pointer difference in C is only defined in the case where both operands
1236 // are pointing to elements of an array.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001237 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
Dan Gohman1cebe562009-08-11 22:40:09 +00001238 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
Chris Lattner2da04b32007-08-24 05:35:26 +00001239 }
Chris Lattner2da04b32007-08-24 05:35:26 +00001240}
1241
1242Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
1243 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1244 // RHS to the same size as the LHS.
1245 Value *RHS = Ops.RHS;
1246 if (Ops.LHS->getType() != RHS->getType())
1247 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
Mike Stump4a3999f2009-09-09 13:00:44 +00001248
Chris Lattner2da04b32007-08-24 05:35:26 +00001249 return Builder.CreateShl(Ops.LHS, RHS, "shl");
1250}
1251
1252Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
1253 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
1254 // RHS to the same size as the LHS.
1255 Value *RHS = Ops.RHS;
1256 if (Ops.LHS->getType() != RHS->getType())
1257 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
Mike Stump4a3999f2009-09-09 13:00:44 +00001258
Chris Lattner3d966d62007-08-24 21:00:35 +00001259 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner2da04b32007-08-24 05:35:26 +00001260 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
1261 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
1262}
1263
1264Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
1265 unsigned SICmpOpc, unsigned FCmpOpc) {
Mike Stumpdf0fe272009-05-29 15:46:01 +00001266 TestAndClearIgnoreResultAssign();
Chris Lattner42e6b812007-08-26 16:34:22 +00001267 Value *Result;
Chris Lattner2da04b32007-08-24 05:35:26 +00001268 QualType LHSTy = E->getLHS()->getType();
Chris Lattner2a7deb62009-07-08 01:08:03 +00001269 if (!LHSTy->isAnyComplexType()) {
Chris Lattner2da04b32007-08-24 05:35:26 +00001270 Value *LHS = Visit(E->getLHS());
1271 Value *RHS = Visit(E->getRHS());
Mike Stump4a3999f2009-09-09 13:00:44 +00001272
Eli Friedman5ac69052009-07-22 06:07:16 +00001273 if (LHS->getType()->isFPOrFPVector()) {
Nate Begemanfe79ca22008-07-25 20:16:05 +00001274 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
Chris Lattner2da04b32007-08-24 05:35:26 +00001275 LHS, RHS, "cmp");
Eli Friedman3c285242008-05-29 15:09:15 +00001276 } else if (LHSTy->isSignedIntegerType()) {
1277 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
Chris Lattner2da04b32007-08-24 05:35:26 +00001278 LHS, RHS, "cmp");
1279 } else {
Eli Friedman3c285242008-05-29 15:09:15 +00001280 // Unsigned integers and pointers.
1281 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
Chris Lattner2da04b32007-08-24 05:35:26 +00001282 LHS, RHS, "cmp");
1283 }
Chris Lattner2a7deb62009-07-08 01:08:03 +00001284
1285 // If this is a vector comparison, sign extend the result to the appropriate
1286 // vector integer type and return it (don't convert to bool).
1287 if (LHSTy->isVectorType())
1288 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
Mike Stump4a3999f2009-09-09 13:00:44 +00001289
Chris Lattner2da04b32007-08-24 05:35:26 +00001290 } else {
1291 // Complex Comparison: can only be an equality comparison.
1292 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
1293 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
Mike Stump4a3999f2009-09-09 13:00:44 +00001294
Chris Lattner0f398c42008-07-26 22:37:01 +00001295 QualType CETy = LHSTy->getAsComplexType()->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001296
Chris Lattner42e6b812007-08-26 16:34:22 +00001297 Value *ResultR, *ResultI;
Chris Lattner2da04b32007-08-24 05:35:26 +00001298 if (CETy->isRealFloatingType()) {
1299 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1300 LHS.first, RHS.first, "cmp.r");
1301 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
1302 LHS.second, RHS.second, "cmp.i");
1303 } else {
1304 // Complex comparisons can only be equality comparisons. As such, signed
1305 // and unsigned opcodes are the same.
1306 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1307 LHS.first, RHS.first, "cmp.r");
1308 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
1309 LHS.second, RHS.second, "cmp.i");
1310 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001311
Chris Lattner2da04b32007-08-24 05:35:26 +00001312 if (E->getOpcode() == BinaryOperator::EQ) {
1313 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
1314 } else {
1315 assert(E->getOpcode() == BinaryOperator::NE &&
1316 "Complex comparison other than == or != ?");
1317 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
1318 }
1319 }
Nuno Lopesa0abe622009-01-11 23:22:37 +00001320
1321 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
Chris Lattner2da04b32007-08-24 05:35:26 +00001322}
1323
1324Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +00001325 bool Ignore = TestAndClearIgnoreResultAssign();
1326
1327 // __block variables need to have the rhs evaluated first, plus this should
1328 // improve codegen just a little.
Chris Lattner2da04b32007-08-24 05:35:26 +00001329 Value *RHS = Visit(E->getRHS());
Mike Stumpaed08f92009-05-21 21:05:15 +00001330 LValue LHS = EmitLValue(E->getLHS());
Mike Stump4a3999f2009-09-09 13:00:44 +00001331
Daniel Dunbar9b1335e2008-11-19 09:36:46 +00001332 // Store the value into the LHS. Bit-fields are handled specially
Daniel Dunbar7689f6b2008-11-19 11:54:05 +00001333 // because the result is altered by the store, i.e., [C99 6.5.16p1]
1334 // 'An assignment expression has the value of the left operand after
Eli Friedmane381f7e2009-03-28 02:45:41 +00001335 // the assignment...'.
Mike Stumpdf0fe272009-05-29 15:46:01 +00001336 if (LHS.isBitfield()) {
1337 if (!LHS.isVolatileQualified()) {
1338 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
1339 &RHS);
1340 return RHS;
1341 } else
1342 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType());
1343 } else
Daniel Dunbar9b1335e2008-11-19 09:36:46 +00001344 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
Mike Stumpdf0fe272009-05-29 15:46:01 +00001345 if (Ignore)
1346 return 0;
1347 return EmitLoadOfLValue(LHS, E->getType());
Chris Lattner2da04b32007-08-24 05:35:26 +00001348}
1349
1350Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
Chris Lattner8b084582008-11-12 08:26:50 +00001351 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
1352 // If we have 1 && X, just emit X without inserting the control flow.
1353 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1354 if (Cond == 1) { // If we have 1 && X, just emit X.
Chris Lattner5b1964b2008-11-11 07:41:27 +00001355 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1356 // ZExt result to int.
1357 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
1358 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001359
Chris Lattner8b084582008-11-12 08:26:50 +00001360 // 0 && RHS: If it is safe, just elide the RHS, and return 0.
1361 if (!CGF.ContainsLabel(E->getRHS()))
Owen Anderson0b75f232009-07-31 20:28:54 +00001362 return llvm::Constant::getNullValue(CGF.LLVMIntTy);
Chris Lattner5b1964b2008-11-11 07:41:27 +00001363 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001364
Daniel Dunbara612e792008-11-13 01:38:36 +00001365 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
1366 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
Chris Lattner8b084582008-11-12 08:26:50 +00001367
Chris Lattner35710d182008-11-12 08:38:24 +00001368 // Branch on the LHS first. If it is false, go to the failure (cont) block.
1369 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
1370
1371 // Any edges into the ContBlock are now from an (indeterminate number of)
1372 // edges from this first condition. All of these values will be false. Start
1373 // setting up the PHI node in the Cont Block for this.
Owen Anderson41a75022009-08-13 21:57:51 +00001374 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1375 "", ContBlock);
Chris Lattner35710d182008-11-12 08:38:24 +00001376 PN->reserveOperandSpace(2); // Normal case, two inputs.
1377 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1378 PI != PE; ++PI)
Owen Andersonfe4e3472009-07-31 17:39:36 +00001379 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
Mike Stump4a3999f2009-09-09 13:00:44 +00001380
Anders Carlssonf47a3de2009-06-04 02:53:13 +00001381 CGF.PushConditionalTempDestruction();
Chris Lattner2da04b32007-08-24 05:35:26 +00001382 CGF.EmitBlock(RHSBlock);
1383 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
Anders Carlssonf47a3de2009-06-04 02:53:13 +00001384 CGF.PopConditionalTempDestruction();
Mike Stump4a3999f2009-09-09 13:00:44 +00001385
Chris Lattner2da04b32007-08-24 05:35:26 +00001386 // Reaquire the RHS block, as there may be subblocks inserted.
1387 RHSBlock = Builder.GetInsertBlock();
Chris Lattner35710d182008-11-12 08:38:24 +00001388
1389 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1390 // into the phi node for the edge with the value of RHSCond.
Chris Lattner2da04b32007-08-24 05:35:26 +00001391 CGF.EmitBlock(ContBlock);
Chris Lattner2da04b32007-08-24 05:35:26 +00001392 PN->addIncoming(RHSCond, RHSBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001393
Chris Lattner2da04b32007-08-24 05:35:26 +00001394 // ZExt result to int.
1395 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
1396}
1397
1398Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
Chris Lattner8b084582008-11-12 08:26:50 +00001399 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
1400 // If we have 0 || X, just emit X without inserting the control flow.
1401 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
1402 if (Cond == -1) { // If we have 0 || X, just emit X.
Chris Lattner5b1964b2008-11-11 07:41:27 +00001403 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1404 // ZExt result to int.
1405 return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
1406 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001407
Eli Friedmane9184352008-12-02 16:02:46 +00001408 // 1 || RHS: If it is safe, just elide the RHS, and return 1.
Chris Lattner8b084582008-11-12 08:26:50 +00001409 if (!CGF.ContainsLabel(E->getRHS()))
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001410 return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
Chris Lattner5b1964b2008-11-11 07:41:27 +00001411 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001412
Daniel Dunbara612e792008-11-13 01:38:36 +00001413 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
1414 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
Mike Stump4a3999f2009-09-09 13:00:44 +00001415
Chris Lattner35710d182008-11-12 08:38:24 +00001416 // Branch on the LHS first. If it is true, go to the success (cont) block.
1417 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
1418
1419 // Any edges into the ContBlock are now from an (indeterminate number of)
1420 // edges from this first condition. All of these values will be true. Start
1421 // setting up the PHI node in the Cont Block for this.
Owen Anderson41a75022009-08-13 21:57:51 +00001422 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
1423 "", ContBlock);
Chris Lattner35710d182008-11-12 08:38:24 +00001424 PN->reserveOperandSpace(2); // Normal case, two inputs.
1425 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
1426 PI != PE; ++PI)
Owen Andersonfe4e3472009-07-31 17:39:36 +00001427 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
Chris Lattner35710d182008-11-12 08:38:24 +00001428
Anders Carlssonf47a3de2009-06-04 02:53:13 +00001429 CGF.PushConditionalTempDestruction();
1430
Chris Lattner35710d182008-11-12 08:38:24 +00001431 // Emit the RHS condition as a bool value.
Chris Lattner2da04b32007-08-24 05:35:26 +00001432 CGF.EmitBlock(RHSBlock);
1433 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
Mike Stump4a3999f2009-09-09 13:00:44 +00001434
Anders Carlssonf47a3de2009-06-04 02:53:13 +00001435 CGF.PopConditionalTempDestruction();
Mike Stump4a3999f2009-09-09 13:00:44 +00001436
Chris Lattner2da04b32007-08-24 05:35:26 +00001437 // Reaquire the RHS block, as there may be subblocks inserted.
1438 RHSBlock = Builder.GetInsertBlock();
Mike Stump4a3999f2009-09-09 13:00:44 +00001439
Chris Lattner35710d182008-11-12 08:38:24 +00001440 // Emit an unconditional branch from this block to ContBlock. Insert an entry
1441 // into the phi node for the edge with the value of RHSCond.
1442 CGF.EmitBlock(ContBlock);
Chris Lattner2da04b32007-08-24 05:35:26 +00001443 PN->addIncoming(RHSCond, RHSBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001444
Chris Lattner2da04b32007-08-24 05:35:26 +00001445 // ZExt result to int.
1446 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1447}
1448
1449Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1450 CGF.EmitStmt(E->getLHS());
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00001451 CGF.EnsureInsertPoint();
Chris Lattner2da04b32007-08-24 05:35:26 +00001452 return Visit(E->getRHS());
1453}
1454
1455//===----------------------------------------------------------------------===//
1456// Other Operators
1457//===----------------------------------------------------------------------===//
1458
Chris Lattner3fd91f832008-11-12 08:55:54 +00001459/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
1460/// expression is cheap enough and side-effect-free enough to evaluate
1461/// unconditionally instead of conditionally. This is used to convert control
1462/// flow into selects in some cases.
1463static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) {
1464 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
1465 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +00001466
Chris Lattner3fd91f832008-11-12 08:55:54 +00001467 // TODO: Allow anything we can constant fold to an integer or fp constant.
1468 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
1469 isa<FloatingLiteral>(E))
1470 return true;
Mike Stump4a3999f2009-09-09 13:00:44 +00001471
Chris Lattner3fd91f832008-11-12 08:55:54 +00001472 // Non-volatile automatic variables too, to get "cond ? X : Y" where
1473 // X and Y are local variables.
1474 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1475 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1476 if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified())
1477 return true;
Mike Stump4a3999f2009-09-09 13:00:44 +00001478
Chris Lattner3fd91f832008-11-12 08:55:54 +00001479 return false;
1480}
1481
1482
Chris Lattner2da04b32007-08-24 05:35:26 +00001483Value *ScalarExprEmitter::
1484VisitConditionalOperator(const ConditionalOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +00001485 TestAndClearIgnoreResultAssign();
Chris Lattnercd439292008-11-12 08:04:58 +00001486 // If the condition constant folds and can be elided, try to avoid emitting
1487 // the condition and the dead arm.
1488 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
Chris Lattnerd53e2332008-11-11 18:56:45 +00001489 Expr *Live = E->getLHS(), *Dead = E->getRHS();
Chris Lattnercd439292008-11-12 08:04:58 +00001490 if (Cond == -1)
Chris Lattnerd53e2332008-11-11 18:56:45 +00001491 std::swap(Live, Dead);
Mike Stump4a3999f2009-09-09 13:00:44 +00001492
Chris Lattnercd439292008-11-12 08:04:58 +00001493 // If the dead side doesn't have labels we need, and if the Live side isn't
1494 // the gnu missing ?: extension (which we could handle, but don't bother
1495 // to), just emit the Live part.
1496 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part
1497 Live) // Live part isn't missing.
1498 return Visit(Live);
Chris Lattnerd53e2332008-11-11 18:56:45 +00001499 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001500
1501
Chris Lattner3fd91f832008-11-12 08:55:54 +00001502 // If this is a really simple expression (like x ? 4 : 5), emit this as a
1503 // select instead of as control flow. We can only do this if it is cheap and
Chris Lattner9ce8a532008-11-16 06:16:27 +00001504 // safe to evaluate the LHS and RHS unconditionally.
Chris Lattner3fd91f832008-11-12 08:55:54 +00001505 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) &&
1506 isCheapEnoughToEvaluateUnconditionally(E->getRHS())) {
1507 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
1508 llvm::Value *LHS = Visit(E->getLHS());
1509 llvm::Value *RHS = Visit(E->getRHS());
1510 return Builder.CreateSelect(CondV, LHS, RHS, "cond");
1511 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001512
1513
Daniel Dunbard2a53a72008-11-12 10:13:37 +00001514 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1515 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
Daniel Dunbara612e792008-11-13 01:38:36 +00001516 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner51e71182008-11-12 08:08:13 +00001517 Value *CondVal = 0;
Chris Lattnercd439292008-11-12 08:04:58 +00001518
Mike Stump4a3999f2009-09-09 13:00:44 +00001519 // If we don't have the GNU missing condition extension, emit a branch on bool
1520 // the normal way.
Chris Lattnercd7bc142009-02-13 23:35:32 +00001521 if (E->getLHS()) {
1522 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
1523 // the branch on bool.
1524 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
1525 } else {
1526 // Otherwise, for the ?: extension, evaluate the conditional and then
1527 // convert it to bool the hard way. We do this explicitly because we need
1528 // the unconverted value for the missing middle value of the ?:.
Chris Lattner51e71182008-11-12 08:08:13 +00001529 CondVal = CGF.EmitScalarExpr(E->getCond());
Mike Stump4a3999f2009-09-09 13:00:44 +00001530
Chris Lattnercd7bc142009-02-13 23:35:32 +00001531 // In some cases, EmitScalarConversion will delete the "CondVal" expression
1532 // if there are no extra uses (an optimization). Inhibit this by making an
1533 // extra dead use, because we're going to add a use of CondVal later. We
1534 // don't use the builder for this, because we don't want it to get optimized
1535 // away. This leaves dead code, but the ?: extension isn't common.
1536 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
1537 Builder.GetInsertBlock());
Mike Stump4a3999f2009-09-09 13:00:44 +00001538
Chris Lattner51e71182008-11-12 08:08:13 +00001539 Value *CondBoolVal =
1540 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1541 CGF.getContext().BoolTy);
1542 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner51e71182008-11-12 08:08:13 +00001543 }
Anders Carlsson43c52cd2009-06-04 03:00:32 +00001544
1545 CGF.PushConditionalTempDestruction();
Chris Lattner2da04b32007-08-24 05:35:26 +00001546 CGF.EmitBlock(LHSBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001547
Chris Lattner2da04b32007-08-24 05:35:26 +00001548 // Handle the GNU extension for missing LHS.
Chris Lattner2ab40a62007-11-26 01:40:58 +00001549 Value *LHS;
1550 if (E->getLHS())
Eli Friedmand5a48382008-05-16 20:38:39 +00001551 LHS = Visit(E->getLHS());
Chris Lattner2ab40a62007-11-26 01:40:58 +00001552 else // Perform promotions, to handle cases like "short ?: int"
1553 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001554
Anders Carlsson43c52cd2009-06-04 03:00:32 +00001555 CGF.PopConditionalTempDestruction();
Chris Lattner2da04b32007-08-24 05:35:26 +00001556 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbarc56e6762008-11-11 09:41:28 +00001557 CGF.EmitBranch(ContBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001558
Anders Carlsson43c52cd2009-06-04 03:00:32 +00001559 CGF.PushConditionalTempDestruction();
Chris Lattner2da04b32007-08-24 05:35:26 +00001560 CGF.EmitBlock(RHSBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001561
Eli Friedmand5a48382008-05-16 20:38:39 +00001562 Value *RHS = Visit(E->getRHS());
Anders Carlsson43c52cd2009-06-04 03:00:32 +00001563 CGF.PopConditionalTempDestruction();
Chris Lattner2da04b32007-08-24 05:35:26 +00001564 RHSBlock = Builder.GetInsertBlock();
Daniel Dunbarc56e6762008-11-11 09:41:28 +00001565 CGF.EmitBranch(ContBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001566
Chris Lattner2da04b32007-08-24 05:35:26 +00001567 CGF.EmitBlock(ContBlock);
Mike Stump4a3999f2009-09-09 13:00:44 +00001568
Nuno Lopes7bd6e582008-06-04 19:15:45 +00001569 if (!LHS || !RHS) {
Chris Lattnerb6a7b582007-11-30 17:56:23 +00001570 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1571 return 0;
1572 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001573
Chris Lattner2da04b32007-08-24 05:35:26 +00001574 // Create a PHI node for the real part.
1575 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1576 PN->reserveOperandSpace(2);
1577 PN->addIncoming(LHS, LHSBlock);
1578 PN->addIncoming(RHS, RHSBlock);
1579 return PN;
1580}
1581
1582Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001583 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattner2da04b32007-08-24 05:35:26 +00001584}
1585
Chris Lattnerb6a7b582007-11-30 17:56:23 +00001586Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Eli Friedmanddea0ad2009-01-20 17:46:04 +00001587 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson13abd7e2008-11-04 05:30:00 +00001588 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
1589
1590 // If EmitVAArg fails, we fall back to the LLVM instruction.
Mike Stump4a3999f2009-09-09 13:00:44 +00001591 if (!ArgPtr)
Anders Carlsson13abd7e2008-11-04 05:30:00 +00001592 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1593
Mike Stumpdf0fe272009-05-29 15:46:01 +00001594 // FIXME Volatility.
Anders Carlsson13abd7e2008-11-04 05:30:00 +00001595 return Builder.CreateLoad(ArgPtr);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001596}
1597
Mike Stumpab3afd82009-02-12 18:29:15 +00001598Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001599 return CGF.BuildBlockLiteralTmp(BE);
Mike Stumpab3afd82009-02-12 18:29:15 +00001600}
1601
Chris Lattner2da04b32007-08-24 05:35:26 +00001602//===----------------------------------------------------------------------===//
1603// Entry Point into this File
1604//===----------------------------------------------------------------------===//
1605
Mike Stump4a3999f2009-09-09 13:00:44 +00001606/// EmitScalarExpr - Emit the computation of the specified expression of scalar
1607/// type, ignoring the result.
Mike Stumpdf0fe272009-05-29 15:46:01 +00001608Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
Chris Lattner2da04b32007-08-24 05:35:26 +00001609 assert(E && !hasAggregateLLVMType(E->getType()) &&
1610 "Invalid scalar expression to emit");
Mike Stump4a3999f2009-09-09 13:00:44 +00001611
Mike Stumpdf0fe272009-05-29 15:46:01 +00001612 return ScalarExprEmitter(*this, IgnoreResultAssign)
1613 .Visit(const_cast<Expr*>(E));
Chris Lattner2da04b32007-08-24 05:35:26 +00001614}
Chris Lattner3474c202007-08-26 06:48:56 +00001615
1616/// EmitScalarConversion - Emit a conversion from the specified type to the
1617/// specified destination type, both of which are LLVM scalar types.
Chris Lattner42e6b812007-08-26 16:34:22 +00001618Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1619 QualType DstTy) {
Chris Lattner3474c202007-08-26 06:48:56 +00001620 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1621 "Invalid scalar expression to emit");
1622 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1623}
Chris Lattner42e6b812007-08-26 16:34:22 +00001624
Mike Stump4a3999f2009-09-09 13:00:44 +00001625/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
1626/// type to the specified destination type, where the destination type is an
1627/// LLVM scalar type.
Chris Lattner42e6b812007-08-26 16:34:22 +00001628Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1629 QualType SrcTy,
1630 QualType DstTy) {
Chris Lattnerf3bc75a2008-04-04 16:54:41 +00001631 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattner42e6b812007-08-26 16:34:22 +00001632 "Invalid complex -> scalar conversion");
1633 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1634 DstTy);
1635}
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001636
1637Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1638 assert(V1->getType() == V2->getType() &&
1639 "Vector operands must be of the same type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001640 unsigned NumElements =
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001641 cast<llvm::VectorType>(V1->getType())->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +00001642
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001643 va_list va;
1644 va_start(va, V2);
Mike Stump4a3999f2009-09-09 13:00:44 +00001645
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001646 llvm::SmallVector<llvm::Constant*, 16> Args;
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001647 for (unsigned i = 0; i < NumElements; i++) {
1648 int n = va_arg(va, int);
Mike Stump4a3999f2009-09-09 13:00:44 +00001649 assert(n >= 0 && n < (int)NumElements * 2 &&
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001650 "Vector shuffle index out of bounds!");
Owen Anderson41a75022009-08-13 21:57:51 +00001651 Args.push_back(llvm::ConstantInt::get(
1652 llvm::Type::getInt32Ty(VMContext), n));
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001653 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001654
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001655 const char *Name = va_arg(va, const char *);
1656 va_end(va);
Mike Stump4a3999f2009-09-09 13:00:44 +00001657
Owen Anderson3cc120a2009-07-28 21:22:35 +00001658 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
Mike Stump4a3999f2009-09-09 13:00:44 +00001659
Anders Carlssonb9eb82c2007-12-10 19:35:18 +00001660 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1661}
1662
Mike Stump4a3999f2009-09-09 13:00:44 +00001663llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Chris Lattner62843782008-07-26 20:15:14 +00001664 unsigned NumVals, bool isSplat) {
Anders Carlssonf5f65442007-12-15 21:23:30 +00001665 llvm::Value *Vec
Owen Anderson7ec07a52009-07-30 23:11:26 +00001666 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
Mike Stump4a3999f2009-09-09 13:00:44 +00001667
Chris Lattner62843782008-07-26 20:15:14 +00001668 for (unsigned i = 0, e = NumVals; i != e; ++i) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001669 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Owen Anderson41a75022009-08-13 21:57:51 +00001670 llvm::Value *Idx = llvm::ConstantInt::get(
1671 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman330aaa72007-12-30 02:59:45 +00001672 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlssonf5f65442007-12-15 21:23:30 +00001673 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001674
1675 return Vec;
Anders Carlssonf5f65442007-12-15 21:23:30 +00001676}