Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 1 | //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGCall.h" |
Devang Patel | d3a6b0f | 2011-03-04 18:54:42 +0000 | [diff] [blame] | 17 | #include "CGDebugInfo.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "CGRecordLayout.h" |
| 20 | #include "CodeGenModule.h" |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 21 | #include "TargetInfo.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 24 | #include "clang/Basic/ConvertUTF.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Hashing.h" |
| 27 | #include "llvm/DataLayout.h" |
Nick Lewycky | 7c6c6cc | 2011-07-07 03:54:51 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 29 | #include "llvm/LLVMContext.h" |
Chandler Carruth | cc8f2a6 | 2012-07-15 23:28:01 +0000 | [diff] [blame] | 30 | #include "llvm/MDBuilder.h" |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 34 | //===--------------------------------------------------------------------===// |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 35 | // Miscellaneous Helper Methods |
| 36 | //===--------------------------------------------------------------------===// |
| 37 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 38 | llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { |
| 39 | unsigned addressSpace = |
| 40 | cast<llvm::PointerType>(value->getType())->getAddressSpace(); |
| 41 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 42 | llvm::PointerType *destType = Int8PtrTy; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 43 | if (addressSpace) |
| 44 | destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace); |
| 45 | |
| 46 | if (value->getType() == destType) return value; |
| 47 | return Builder.CreateBitCast(value, destType); |
| 48 | } |
| 49 | |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 50 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 51 | /// block. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 52 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 53 | const Twine &Name) { |
Chris Lattner | 4764022 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 54 | if (!Builder.isNamePreserving()) |
Daniel Dunbar | b5aacc2 | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 55 | return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt); |
Devang Patel | dac79de | 2009-10-12 22:29:02 +0000 | [diff] [blame] | 56 | return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt); |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 58 | |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 59 | void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var, |
| 60 | llvm::Value *Init) { |
| 61 | llvm::StoreInst *Store = new llvm::StoreInst(Init, Var); |
| 62 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
| 63 | Block->getInstList().insertAfter(&*AllocaInsertPt, Store); |
| 64 | } |
| 65 | |
Chris Lattner | c401de9 | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 66 | llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | const Twine &Name) { |
Daniel Dunbar | d004918 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 68 | llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name); |
| 69 | // FIXME: Should we prefer the preferred type alignment here? |
| 70 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
| 71 | Alloc->setAlignment(Align.getQuantity()); |
| 72 | return Alloc; |
| 73 | } |
| 74 | |
Chris Lattner | c401de9 | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 75 | llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 76 | const Twine &Name) { |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 77 | llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name); |
| 78 | // FIXME: Should we prefer the preferred type alignment here? |
| 79 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
| 80 | Alloc->setAlignment(Align.getQuantity()); |
| 81 | return Alloc; |
| 82 | } |
| 83 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 84 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 85 | /// expression and compare the result against zero, returning an Int1Ty value. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 86 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 87 | if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 88 | llvm::Value *MemPtr = EmitScalarExpr(E); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 89 | return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT); |
Eli Friedman | 68396b1 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 90 | } |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 91 | |
| 92 | QualType BoolTy = getContext().BoolTy; |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 93 | if (!E->getType()->isAnyComplexType()) |
Chris Lattner | 268fcce | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 94 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 268fcce | 2007-08-26 16:46:58 +0000 | [diff] [blame] | 96 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 97 | } |
| 98 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 99 | /// EmitIgnoredExpr - Emit code to compute the specified expression, |
| 100 | /// ignoring the result. |
| 101 | void CodeGenFunction::EmitIgnoredExpr(const Expr *E) { |
| 102 | if (E->isRValue()) |
| 103 | return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true); |
| 104 | |
| 105 | // Just emit it as an l-value and drop the result. |
| 106 | EmitLValue(E); |
| 107 | } |
| 108 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 109 | /// EmitAnyExpr - Emit code to compute the specified expression which |
| 110 | /// can have any type. The result is returned as an RValue struct. |
| 111 | /// If this is an aggregate expression, AggSlot indicates where the |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 112 | /// result should be returned. |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 113 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, |
| 114 | AggValueSlot aggSlot, |
| 115 | bool ignoreResult) { |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 116 | if (!hasAggregateLLVMType(E->getType())) |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 117 | return RValue::get(EmitScalarExpr(E, ignoreResult)); |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 118 | else if (E->getType()->isAnyComplexType()) |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 119 | return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 120 | |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 121 | if (!ignoreResult && aggSlot.isIgnored()) |
| 122 | aggSlot = CreateAggTemp(E->getType(), "agg-temp"); |
| 123 | EmitAggExpr(E, aggSlot); |
| 124 | return aggSlot.asRValue(); |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 127 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 128 | /// always be accessible even if no aggregate location is provided. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 129 | RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) { |
| 130 | AggValueSlot AggSlot = AggValueSlot::ignored(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 131 | |
| 132 | if (hasAggregateLLVMType(E->getType()) && |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 133 | !E->getType()->isAnyComplexType()) |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 134 | AggSlot = CreateAggTemp(E->getType(), "agg.tmp"); |
| 135 | return EmitAnyExpr(E, AggSlot); |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 136 | } |
| 137 | |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 138 | /// EmitAnyExprToMem - Evaluate an expression into a given memory |
| 139 | /// location. |
| 140 | void CodeGenFunction::EmitAnyExprToMem(const Expr *E, |
| 141 | llvm::Value *Location, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 142 | Qualifiers Quals, |
| 143 | bool IsInit) { |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 144 | // FIXME: This function should take an LValue as an argument. |
| 145 | if (E->getType()->isAnyComplexType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 146 | EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile()); |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 147 | } else if (hasAggregateLLVMType(E->getType())) { |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 148 | CharUnits Alignment = getContext().getTypeAlignInChars(E->getType()); |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 149 | EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 150 | AggValueSlot::IsDestructed_t(IsInit), |
John McCall | a8a39bc | 2011-08-26 05:38:08 +0000 | [diff] [blame] | 151 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 152 | AggValueSlot::IsAliased_t(!IsInit))); |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 153 | } else { |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 154 | RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 155 | LValue LV = MakeAddrLValue(Location, E->getType()); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 156 | EmitStoreThroughLValue(RV, LV); |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 160 | static llvm::Value * |
Chris Lattner | 2451696 | 2011-07-20 04:59:57 +0000 | [diff] [blame] | 161 | CreateReferenceTemporary(CodeGenFunction &CGF, QualType Type, |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 162 | const NamedDecl *InitializedDecl) { |
| 163 | if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) { |
| 164 | if (VD->hasGlobalStorage()) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 165 | SmallString<256> Name; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 166 | llvm::raw_svector_ostream Out(Name); |
| 167 | CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out); |
| 168 | Out.flush(); |
| 169 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 170 | llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type); |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 171 | |
| 172 | // Create the reference temporary. |
| 173 | llvm::GlobalValue *RefTemp = |
| 174 | new llvm::GlobalVariable(CGF.CGM.getModule(), |
| 175 | RefTempTy, /*isConstant=*/false, |
| 176 | llvm::GlobalValue::InternalLinkage, |
| 177 | llvm::Constant::getNullValue(RefTempTy), |
| 178 | Name.str()); |
| 179 | return RefTemp; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return CGF.CreateMemTemp(Type, "ref.tmp"); |
| 184 | } |
| 185 | |
| 186 | static llvm::Value * |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 187 | EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E, |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 188 | llvm::Value *&ReferenceTemporary, |
| 189 | const CXXDestructorDecl *&ReferenceTemporaryDtor, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 190 | QualType &ObjCARCReferenceLifetimeType, |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 191 | const NamedDecl *InitializedDecl) { |
Rafael Espindola | b9d75ca | 2012-10-27 00:43:14 +0000 | [diff] [blame] | 192 | const MaterializeTemporaryExpr *M = NULL; |
Rafael Espindola | 9c006de | 2012-10-27 01:03:43 +0000 | [diff] [blame] | 193 | E = E->findMaterializedTemporary(M); |
Rafael Espindola | b9d75ca | 2012-10-27 00:43:14 +0000 | [diff] [blame] | 194 | // Objective-C++ ARC: |
| 195 | // If we are binding a reference to a temporary that has ownership, we |
| 196 | // need to perform retain/release operations on the temporary. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 197 | if (M && CGF.getLangOpts().ObjCAutoRefCount && |
Rafael Espindola | b9d75ca | 2012-10-27 00:43:14 +0000 | [diff] [blame] | 198 | M->getType()->isObjCLifetimeType() && |
| 199 | (M->getType().getObjCLifetime() == Qualifiers::OCL_Strong || |
| 200 | M->getType().getObjCLifetime() == Qualifiers::OCL_Weak || |
| 201 | M->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing)) |
| 202 | ObjCARCReferenceLifetimeType = M->getType(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 203 | |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 204 | if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E)) { |
| 205 | CGF.enterFullExpression(EWC); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 206 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 207 | |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 208 | return EmitExprForReferenceBinding(CGF, EWC->getSubExpr(), |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 209 | ReferenceTemporary, |
| 210 | ReferenceTemporaryDtor, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 211 | ObjCARCReferenceLifetimeType, |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 212 | InitializedDecl); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | RValue RV; |
Douglas Gregor | 9c399a2 | 2011-01-22 02:44:21 +0000 | [diff] [blame] | 216 | if (E->isGLValue()) { |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 217 | // Emit the expression as an lvalue. |
| 218 | LValue LV = CGF.EmitLValue(E); |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 220 | if (LV.isSimple()) |
| 221 | return LV.getAddress(); |
Anders Carlsson | 824e061 | 2010-02-04 17:32:58 +0000 | [diff] [blame] | 222 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 223 | // We have to load the lvalue. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 224 | RV = CGF.EmitLoadOfLValue(LV); |
Eli Friedman | c21cb44 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 225 | } else { |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 226 | if (!ObjCARCReferenceLifetimeType.isNull()) { |
| 227 | ReferenceTemporary = CreateReferenceTemporary(CGF, |
| 228 | ObjCARCReferenceLifetimeType, |
| 229 | InitializedDecl); |
| 230 | |
| 231 | |
| 232 | LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary, |
| 233 | ObjCARCReferenceLifetimeType); |
| 234 | |
| 235 | CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl), |
| 236 | RefTempDst, false); |
| 237 | |
| 238 | bool ExtendsLifeOfTemporary = false; |
| 239 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) { |
| 240 | if (Var->extendsLifetimeOfTemporary()) |
| 241 | ExtendsLifeOfTemporary = true; |
| 242 | } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) { |
| 243 | ExtendsLifeOfTemporary = true; |
| 244 | } |
| 245 | |
| 246 | if (!ExtendsLifeOfTemporary) { |
| 247 | // Since the lifetime of this temporary isn't going to be extended, |
| 248 | // we need to clean it up ourselves at the end of the full expression. |
| 249 | switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) { |
| 250 | case Qualifiers::OCL_None: |
| 251 | case Qualifiers::OCL_ExplicitNone: |
| 252 | case Qualifiers::OCL_Autoreleasing: |
| 253 | break; |
| 254 | |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 255 | case Qualifiers::OCL_Strong: { |
| 256 | assert(!ObjCARCReferenceLifetimeType->isArrayType()); |
| 257 | CleanupKind cleanupKind = CGF.getARCCleanupKind(); |
| 258 | CGF.pushDestroy(cleanupKind, |
| 259 | ReferenceTemporary, |
| 260 | ObjCARCReferenceLifetimeType, |
| 261 | CodeGenFunction::destroyARCStrongImprecise, |
| 262 | cleanupKind & EHCleanup); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 263 | break; |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 264 | } |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 265 | |
| 266 | case Qualifiers::OCL_Weak: |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 267 | assert(!ObjCARCReferenceLifetimeType->isArrayType()); |
| 268 | CGF.pushDestroy(NormalAndEHCleanup, |
| 269 | ReferenceTemporary, |
| 270 | ObjCARCReferenceLifetimeType, |
| 271 | CodeGenFunction::destroyARCWeak, |
| 272 | /*useEHCleanupForArray*/ true); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | |
| 276 | ObjCARCReferenceLifetimeType = QualType(); |
| 277 | } |
| 278 | |
| 279 | return ReferenceTemporary; |
| 280 | } |
Rafael Espindola | b413676 | 2012-10-27 00:40:06 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 282 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
Rafael Espindola | 9c006de | 2012-10-27 01:03:43 +0000 | [diff] [blame] | 283 | E = E->skipRValueSubobjectAdjustments(Adjustments); |
Rafael Espindola | b413676 | 2012-10-27 00:40:06 +0000 | [diff] [blame] | 284 | if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E)) |
| 285 | if (opaque->getType()->isRecordType()) |
| 286 | return CGF.EmitOpaqueValueLValue(opaque).getAddress(); |
Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 287 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 288 | // Create a reference temporary if necessary. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 289 | AggValueSlot AggSlot = AggValueSlot::ignored(); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 290 | if (CGF.hasAggregateLLVMType(E->getType()) && |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 291 | !E->getType()->isAnyComplexType()) { |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 292 | ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), |
| 293 | InitializedDecl); |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 294 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(E->getType()); |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 295 | AggValueSlot::IsDestructed_t isDestructed |
| 296 | = AggValueSlot::IsDestructed_t(InitializedDecl != 0); |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 297 | AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Alignment, |
| 298 | Qualifiers(), isDestructed, |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 299 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 300 | AggValueSlot::IsNotAliased); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 301 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 302 | |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 303 | if (InitializedDecl) { |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 304 | // Get the destructor for the reference temporary. |
| 305 | if (const RecordType *RT = E->getType()->getAs<RecordType>()) { |
| 306 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 307 | if (!ClassDecl->hasTrivialDestructor()) |
Douglas Gregor | bac7490 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 308 | ReferenceTemporaryDtor = ClassDecl->getDestructor(); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 312 | RV = CGF.EmitAnyExpr(E, AggSlot); |
| 313 | |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 314 | // Check if need to perform derived-to-base casts and/or field accesses, to |
| 315 | // get from the temporary object we created (and, potentially, for which we |
| 316 | // extended the lifetime) to the subobject we're binding the reference to. |
| 317 | if (!Adjustments.empty()) { |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 318 | llvm::Value *Object = RV.getAggregateAddr(); |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 319 | for (unsigned I = Adjustments.size(); I != 0; --I) { |
| 320 | SubobjectAdjustment &Adjustment = Adjustments[I-1]; |
| 321 | switch (Adjustment.Kind) { |
| 322 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 323 | Object = |
| 324 | CGF.GetAddressOfBaseClass(Object, |
| 325 | Adjustment.DerivedToBase.DerivedClass, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 326 | Adjustment.DerivedToBase.BasePath->path_begin(), |
| 327 | Adjustment.DerivedToBase.BasePath->path_end(), |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 328 | /*NullCheckValue=*/false); |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
| 331 | case SubobjectAdjustment::FieldAdjustment: { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 332 | LValue LV = CGF.MakeAddrLValue(Object, E->getType()); |
| 333 | LV = CGF.EmitLValueForField(LV, Adjustment.Field); |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 334 | if (LV.isSimple()) { |
| 335 | Object = LV.getAddress(); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | // For non-simple lvalues, we actually have to create a copy of |
| 340 | // the object we're binding to. |
Daniel Dunbar | e8b6cda | 2010-08-21 03:37:02 +0000 | [diff] [blame] | 341 | QualType T = Adjustment.Field->getType().getNonReferenceType() |
| 342 | .getUnqualifiedType(); |
Anders Carlsson | 3f48c60 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 343 | Object = CreateReferenceTemporary(CGF, T, InitializedDecl); |
Daniel Dunbar | e8b6cda | 2010-08-21 03:37:02 +0000 | [diff] [blame] | 344 | LValue TempLV = CGF.MakeAddrLValue(Object, |
| 345 | Adjustment.Field->getType()); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 346 | CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV); |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 349 | |
Eli Friedman | 13ffdd8 | 2012-06-15 23:51:06 +0000 | [diff] [blame] | 350 | case SubobjectAdjustment::MemberPointerAdjustment: { |
Rafael Espindola | e7b11f5 | 2012-10-27 00:36:38 +0000 | [diff] [blame] | 351 | llvm::Value *Ptr = CGF.EmitScalarExpr(Adjustment.Ptr.RHS); |
Eli Friedman | 13ffdd8 | 2012-06-15 23:51:06 +0000 | [diff] [blame] | 352 | Object = CGF.CGM.getCXXABI().EmitMemberDataPointerAddress( |
Rafael Espindola | e7b11f5 | 2012-10-27 00:36:38 +0000 | [diff] [blame] | 353 | CGF, Object, Ptr, Adjustment.Ptr.MPT); |
Eli Friedman | 13ffdd8 | 2012-06-15 23:51:06 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
Douglas Gregor | 7c38f15 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Eli Friedman | b606925 | 2011-03-16 22:34:09 +0000 | [diff] [blame] | 358 | |
| 359 | return Object; |
Anders Carlsson | 66413c2 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 360 | } |
Anders Carlsson | 7d4c083 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 361 | } |
Eli Friedman | c21cb44 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 362 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 363 | if (RV.isAggregate()) |
| 364 | return RV.getAggregateAddr(); |
Eli Friedman | c21cb44 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 365 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 366 | // Create a temporary variable that we can bind the reference to. |
Anders Carlsson | 18c205e | 2010-06-27 17:23:46 +0000 | [diff] [blame] | 367 | ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(), |
| 368 | InitializedDecl); |
| 369 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 370 | |
| 371 | unsigned Alignment = |
| 372 | CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity(); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 373 | if (RV.isScalar()) |
| 374 | CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary, |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 375 | /*Volatile=*/false, Alignment, E->getType()); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 376 | else |
| 377 | CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary, |
| 378 | /*Volatile=*/false); |
| 379 | return ReferenceTemporary; |
| 380 | } |
| 381 | |
| 382 | RValue |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 383 | CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E, |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 384 | const NamedDecl *InitializedDecl) { |
| 385 | llvm::Value *ReferenceTemporary = 0; |
| 386 | const CXXDestructorDecl *ReferenceTemporaryDtor = 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 387 | QualType ObjCARCReferenceLifetimeType; |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 388 | llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary, |
| 389 | ReferenceTemporaryDtor, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 390 | ObjCARCReferenceLifetimeType, |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 391 | InitializedDecl); |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 392 | if (SanitizePerformTypeCheck && !E->getType()->isFunctionType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 393 | // C++11 [dcl.ref]p5 (as amended by core issue 453): |
| 394 | // If a glvalue to which a reference is directly bound designates neither |
| 395 | // an existing object or function of an appropriate type nor a region of |
| 396 | // storage of suitable size and alignment to contain an object of the |
| 397 | // reference's type, the behavior is undefined. |
| 398 | QualType Ty = E->getType(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 399 | EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 400 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 401 | if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull()) |
Anders Carlsson | 3f48c60 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 402 | return RValue::get(Value); |
| 403 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 404 | // Make sure to call the destructor for the reference temporary. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 405 | const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl); |
| 406 | if (VD && VD->hasGlobalStorage()) { |
| 407 | if (ReferenceTemporaryDtor) { |
Anders Carlsson | 3f48c60 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 408 | llvm::Constant *DtorFn = |
| 409 | CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 410 | CGM.getCXXABI().registerGlobalDtor(*this, DtorFn, |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 411 | cast<llvm::Constant>(ReferenceTemporary)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 412 | } else { |
| 413 | assert(!ObjCARCReferenceLifetimeType.isNull()); |
| 414 | // Note: We intentionally do not register a global "destructor" to |
| 415 | // release the object. |
Anders Carlsson | 3f48c60 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 416 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 417 | |
| 418 | return RValue::get(Value); |
Anders Carlsson | 3f48c60 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 419 | } |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 420 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 421 | if (ReferenceTemporaryDtor) |
| 422 | PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary); |
| 423 | else { |
| 424 | switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) { |
| 425 | case Qualifiers::OCL_None: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 426 | llvm_unreachable( |
| 427 | "Not a reference temporary that needs to be deallocated"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 428 | case Qualifiers::OCL_ExplicitNone: |
| 429 | case Qualifiers::OCL_Autoreleasing: |
| 430 | // Nothing to do. |
| 431 | break; |
| 432 | |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 433 | case Qualifiers::OCL_Strong: { |
| 434 | bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>(); |
| 435 | CleanupKind cleanupKind = getARCCleanupKind(); |
Benjamin Kramer | ae2d344 | 2011-07-12 18:37:23 +0000 | [diff] [blame] | 436 | pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 437 | precise ? destroyARCStrongPrecise : destroyARCStrongImprecise, |
| 438 | cleanupKind & EHCleanup); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 439 | break; |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 440 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 441 | |
Benjamin Kramer | ae2d344 | 2011-07-12 18:37:23 +0000 | [diff] [blame] | 442 | case Qualifiers::OCL_Weak: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 443 | // __weak objects always get EH cleanups; otherwise, exceptions |
| 444 | // could cause really nasty crashes instead of mere leaks. |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 445 | pushDestroy(NormalAndEHCleanup, ReferenceTemporary, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 446 | ObjCARCReferenceLifetimeType, destroyARCWeak, true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 447 | break; |
| 448 | } |
Benjamin Kramer | ae2d344 | 2011-07-12 18:37:23 +0000 | [diff] [blame] | 449 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 452 | return RValue::get(Value); |
Anders Carlsson | 6f5a015 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 456 | /// getAccessedFieldNo - Given an encoded value and a result number, return the |
| 457 | /// input field number being accessed. |
| 458 | unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 459 | const llvm::Constant *Elts) { |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 460 | return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx)) |
| 461 | ->getZExtValue(); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 464 | /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h. |
| 465 | static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, |
| 466 | llvm::Value *High) { |
| 467 | llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL); |
| 468 | llvm::Value *K47 = Builder.getInt64(47); |
| 469 | llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul); |
| 470 | llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0); |
| 471 | llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul); |
| 472 | llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0); |
| 473 | return Builder.CreateMul(B1, KMul); |
| 474 | } |
| 475 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 476 | void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, |
| 477 | llvm::Value *Address, |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 478 | QualType Ty, CharUnits Alignment) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 479 | if (!SanitizePerformTypeCheck) |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 480 | return; |
| 481 | |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 482 | // Don't check pointers outside the default address space. The null check |
| 483 | // isn't correct, the object-size check isn't supported by LLVM, and we can't |
| 484 | // communicate the addresses to the runtime handler for the vptr check. |
| 485 | if (Address->getType()->getPointerAddressSpace()) |
| 486 | return; |
| 487 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 488 | llvm::Value *Cond = 0; |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 489 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 490 | if (getLangOpts().SanitizeNull) { |
| 491 | // The glvalue must not be an empty glvalue. |
| 492 | Cond = Builder.CreateICmpNE( |
| 493 | Address, llvm::Constant::getNullValue(Address->getType())); |
| 494 | } |
Chris Lattner | bc3be65 | 2010-04-10 18:34:14 +0000 | [diff] [blame] | 495 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 496 | if (getLangOpts().SanitizeObjectSize && !Ty->isIncompleteType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 497 | uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity(); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 498 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 499 | // The glvalue must refer to a large enough storage region. |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 500 | // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 501 | // to check this. |
| 502 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy); |
| 503 | llvm::Value *Min = Builder.getFalse(); |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 504 | llvm::Value *CastAddr = Builder.CreateBitCast(Address, Int8PtrTy); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 505 | llvm::Value *LargeEnough = |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 506 | Builder.CreateICmpUGE(Builder.CreateCall2(F, CastAddr, Min), |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 507 | llvm::ConstantInt::get(IntPtrTy, Size)); |
| 508 | Cond = Cond ? Builder.CreateAnd(Cond, LargeEnough) : LargeEnough; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 509 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 510 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 511 | uint64_t AlignVal = 0; |
| 512 | |
| 513 | if (getLangOpts().SanitizeAlignment) { |
| 514 | AlignVal = Alignment.getQuantity(); |
| 515 | if (!Ty->isIncompleteType() && !AlignVal) |
| 516 | AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 517 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 518 | // The glvalue must be suitably aligned. |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 519 | if (AlignVal) { |
| 520 | llvm::Value *Align = |
| 521 | Builder.CreateAnd(Builder.CreatePtrToInt(Address, IntPtrTy), |
| 522 | llvm::ConstantInt::get(IntPtrTy, AlignVal - 1)); |
| 523 | llvm::Value *Aligned = |
| 524 | Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)); |
| 525 | Cond = Cond ? Builder.CreateAnd(Cond, Aligned) : Aligned; |
| 526 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 529 | if (Cond) { |
| 530 | llvm::Constant *StaticData[] = { |
| 531 | EmitCheckSourceLocation(Loc), |
| 532 | EmitCheckTypeDescriptor(Ty), |
| 533 | llvm::ConstantInt::get(SizeTy, AlignVal), |
| 534 | llvm::ConstantInt::get(Int8Ty, TCK) |
| 535 | }; |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 536 | EmitCheck(Cond, "type_mismatch", StaticData, Address, CRK_Recoverable); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 537 | } |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 538 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 539 | // If possible, check that the vptr indicates that there is a subobject of |
| 540 | // type Ty at offset zero within this object. |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 541 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 542 | if (getLangOpts().SanitizeVptr && TCK != TCK_ConstructorCall && |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 543 | RD && RD->hasDefinition() && RD->isDynamicClass()) { |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 544 | // Compute a hash of the mangled name of the type. |
| 545 | // |
| 546 | // FIXME: This is not guaranteed to be deterministic! Move to a |
| 547 | // fingerprinting mechanism once LLVM provides one. For the time |
| 548 | // being the implementation happens to be deterministic. |
| 549 | llvm::SmallString<64> MangledName; |
| 550 | llvm::raw_svector_ostream Out(MangledName); |
| 551 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(), |
| 552 | Out); |
| 553 | llvm::hash_code TypeHash = hash_value(Out.str()); |
| 554 | |
| 555 | // Load the vptr, and compute hash_16_bytes(TypeHash, vptr). |
| 556 | llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash); |
| 557 | llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0); |
| 558 | llvm::Value *VPtrAddr = Builder.CreateBitCast(Address, VPtrTy); |
| 559 | llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr); |
| 560 | llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty); |
| 561 | |
| 562 | llvm::Value *Hash = emitHash16Bytes(Builder, Low, High); |
| 563 | Hash = Builder.CreateTrunc(Hash, IntPtrTy); |
| 564 | |
| 565 | // Look the hash up in our cache. |
| 566 | const int CacheSize = 128; |
| 567 | llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize); |
| 568 | llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable, |
| 569 | "__ubsan_vptr_type_cache"); |
| 570 | llvm::Value *Slot = Builder.CreateAnd(Hash, |
| 571 | llvm::ConstantInt::get(IntPtrTy, |
| 572 | CacheSize-1)); |
| 573 | llvm::Value *Indices[] = { Builder.getInt32(0), Slot }; |
| 574 | llvm::Value *CacheVal = |
| 575 | Builder.CreateLoad(Builder.CreateInBoundsGEP(Cache, Indices)); |
| 576 | |
| 577 | // If the hash isn't in the cache, call a runtime handler to perform the |
| 578 | // hard work of checking whether the vptr is for an object of the right |
| 579 | // type. This will either fill in the cache and return, or produce a |
| 580 | // diagnostic. |
| 581 | llvm::Constant *StaticData[] = { |
| 582 | EmitCheckSourceLocation(Loc), |
| 583 | EmitCheckTypeDescriptor(Ty), |
| 584 | CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()), |
| 585 | llvm::ConstantInt::get(Int8Ty, TCK) |
| 586 | }; |
| 587 | llvm::Value *DynamicData[] = { Address, Hash }; |
| 588 | EmitCheck(Builder.CreateICmpEQ(CacheVal, Hash), |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 589 | "dynamic_type_cache_miss", StaticData, DynamicData, |
| 590 | CRK_AlwaysRecoverable); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 591 | } |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 595 | CodeGenFunction::ComplexPairTy CodeGenFunction:: |
| 596 | EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 597 | bool isInc, bool isPre) { |
| 598 | ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(), |
| 599 | LV.isVolatileQualified()); |
| 600 | |
| 601 | llvm::Value *NextVal; |
| 602 | if (isa<llvm::IntegerType>(InVal.first->getType())) { |
| 603 | uint64_t AmountVal = isInc ? 1 : -1; |
| 604 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); |
| 605 | |
| 606 | // Add the inc/dec to the real part. |
| 607 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 608 | } else { |
| 609 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
| 610 | llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); |
| 611 | if (!isInc) |
| 612 | FVal.changeSign(); |
| 613 | NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); |
| 614 | |
| 615 | // Add the inc/dec to the real part. |
| 616 | NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 617 | } |
| 618 | |
| 619 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 620 | |
| 621 | // Store the updated result through the lvalue. |
| 622 | StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified()); |
| 623 | |
| 624 | // If this is a postinc, return the value read from memory, otherwise use the |
| 625 | // updated value. |
| 626 | return isPre ? IncVal : InVal; |
| 627 | } |
| 628 | |
| 629 | |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 630 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 631 | // LValue Expression Emission |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 632 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 633 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 634 | RValue CodeGenFunction::GetUndefRValue(QualType Ty) { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 635 | if (Ty->isVoidType()) |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 636 | return RValue::get(0); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 637 | |
| 638 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 639 | llvm::Type *EltTy = ConvertType(CTy->getElementType()); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 640 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 641 | return RValue::getComplex(std::make_pair(U, U)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 644 | // If this is a use of an undefined aggregate type, the aggregate must have an |
| 645 | // identifiable address. Just because the contents of the value are undefined |
| 646 | // doesn't mean that the address can't be taken and compared. |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 647 | if (hasAggregateLLVMType(Ty)) { |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 648 | llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp"); |
| 649 | return RValue::getAggregate(DestPtr); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 651 | |
| 652 | return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 655 | RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, |
| 656 | const char *Name) { |
| 657 | ErrorUnsupported(E, Name); |
| 658 | return GetUndefRValue(E->getType()); |
| 659 | } |
| 660 | |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 661 | LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, |
| 662 | const char *Name) { |
| 663 | ErrorUnsupported(E, Name); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 664 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 665 | return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType()); |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 668 | LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) { |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 669 | LValue LV = EmitLValue(E); |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 670 | if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 671 | EmitTypeCheck(TCK, E->getExprLoc(), LV.getAddress(), |
| 672 | E->getType(), LV.getAlignment()); |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 673 | return LV; |
| 674 | } |
| 675 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 676 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 677 | /// of the expression. |
| 678 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 679 | /// This can return one of two things: a simple address or a bitfield reference. |
| 680 | /// In either case, the LLVM Value* in the LValue structure is guaranteed to be |
| 681 | /// an LLVM pointer type. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 682 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 683 | /// If this returns a bitfield reference, nothing about the pointee type of the |
| 684 | /// LLVM value is known: For example, it may not be a pointer to an integer. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 685 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 686 | /// If this returns a normal address, and if the lvalue's C type is fixed size, |
| 687 | /// this method guarantees that the returned pointer type will point to an LLVM |
| 688 | /// type of the same size of the lvalue's type. If the lvalue has a variable |
| 689 | /// length type, this is not possible. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 690 | /// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 691 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
| 692 | switch (E->getStmtClass()) { |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 693 | default: return EmitUnsupportedLValue(E, "l-value expression"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 694 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 695 | case Expr::ObjCPropertyRefExprClass: |
| 696 | llvm_unreachable("cannot emit a property reference directly"); |
| 697 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 698 | case Expr::ObjCSelectorExprClass: |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 699 | return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); |
Fariborz Jahanian | 531c16f | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 700 | case Expr::ObjCIsaExprClass: |
| 701 | return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 702 | case Expr::BinaryOperatorClass: |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 703 | return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); |
Douglas Gregor | 914af21 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 704 | case Expr::CompoundAssignOperatorClass: |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 705 | if (!E->getType()->isAnyComplexType()) |
| 706 | return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
| 707 | return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 708 | case Expr::CallExprClass: |
Anders Carlsson | c82555f | 2009-09-01 21:18:52 +0000 | [diff] [blame] | 709 | case Expr::CXXMemberCallExprClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 710 | case Expr::CXXOperatorCallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 711 | case Expr::UserDefinedLiteralClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 712 | return EmitCallExprLValue(cast<CallExpr>(E)); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 713 | case Expr::VAArgExprClass: |
| 714 | return EmitVAArgExprLValue(cast<VAArgExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 715 | case Expr::DeclRefExprClass: |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 716 | return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Eric Christopher | d98e424 | 2011-09-08 17:15:04 +0000 | [diff] [blame] | 717 | case Expr::ParenExprClass: |
| 718 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 719 | case Expr::GenericSelectionExprClass: |
| 720 | return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr()); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 721 | case Expr::PredefinedExprClass: |
| 722 | return EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 723 | case Expr::StringLiteralClass: |
| 724 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 725 | case Expr::ObjCEncodeExprClass: |
| 726 | return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 727 | case Expr::PseudoObjectExprClass: |
| 728 | return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 729 | case Expr::InitListExprClass: |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 730 | return EmitInitListLValue(cast<InitListExpr>(E)); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 731 | case Expr::CXXTemporaryObjectExprClass: |
| 732 | case Expr::CXXConstructExprClass: |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 733 | return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); |
| 734 | case Expr::CXXBindTemporaryExprClass: |
| 735 | return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 736 | case Expr::CXXUuidofExprClass: |
| 737 | return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E)); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 738 | case Expr::LambdaExprClass: |
| 739 | return EmitLambdaLValue(cast<LambdaExpr>(E)); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 740 | |
| 741 | case Expr::ExprWithCleanupsClass: { |
| 742 | const ExprWithCleanups *cleanups = cast<ExprWithCleanups>(E); |
| 743 | enterFullExpression(cleanups); |
| 744 | RunCleanupsScope Scope(*this); |
| 745 | return EmitLValue(cleanups->getSubExpr()); |
| 746 | } |
| 747 | |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 748 | case Expr::CXXScalarValueInitExprClass: |
| 749 | return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E)); |
Anders Carlsson | 52ce3bb | 2009-11-14 01:51:50 +0000 | [diff] [blame] | 750 | case Expr::CXXDefaultArgExprClass: |
| 751 | return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 752 | case Expr::CXXTypeidExprClass: |
| 753 | return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 754 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 755 | case Expr::ObjCMessageExprClass: |
| 756 | return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 757 | case Expr::ObjCIvarRefExprClass: |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 758 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 759 | case Expr::StmtExprClass: |
| 760 | return EmitStmtExprLValue(cast<StmtExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 761 | case Expr::UnaryOperatorClass: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 762 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 763 | case Expr::ArraySubscriptExprClass: |
| 764 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 765 | case Expr::ExtVectorElementExprClass: |
| 766 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 767 | case Expr::MemberExprClass: |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 768 | return EmitMemberExpr(cast<MemberExpr>(E)); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 769 | case Expr::CompoundLiteralExprClass: |
| 770 | return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 771 | case Expr::ConditionalOperatorClass: |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 772 | return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 773 | case Expr::BinaryConditionalOperatorClass: |
| 774 | return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E)); |
Chris Lattner | 053441f | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 775 | case Expr::ChooseExprClass: |
Eli Friedman | e0a5b8b | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 776 | return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext())); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 777 | case Expr::OpaqueValueExprClass: |
| 778 | return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E)); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 779 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 780 | return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); |
Chris Lattner | 63d06ab | 2009-03-18 04:02:57 +0000 | [diff] [blame] | 781 | case Expr::ImplicitCastExprClass: |
| 782 | case Expr::CStyleCastExprClass: |
| 783 | case Expr::CXXFunctionalCastExprClass: |
| 784 | case Expr::CXXStaticCastExprClass: |
| 785 | case Expr::CXXDynamicCastExprClass: |
| 786 | case Expr::CXXReinterpretCastExprClass: |
| 787 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 788 | case Expr::ObjCBridgedCastExprClass: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 789 | return EmitCastLValue(cast<CastExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 791 | case Expr::MaterializeTemporaryExprClass: |
| 792 | return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 796 | /// Given an object of the given canonical type, can we safely copy a |
| 797 | /// value out of it based on its initializer? |
| 798 | static bool isConstantEmittableObjectType(QualType type) { |
| 799 | assert(type.isCanonical()); |
| 800 | assert(!type->isReferenceType()); |
| 801 | |
| 802 | // Must be const-qualified but non-volatile. |
| 803 | Qualifiers qs = type.getLocalQualifiers(); |
| 804 | if (!qs.hasConst() || qs.hasVolatile()) return false; |
| 805 | |
| 806 | // Otherwise, all object types satisfy this except C++ classes with |
| 807 | // mutable subobjects or non-trivial copy/destroy behavior. |
| 808 | if (const RecordType *RT = dyn_cast<RecordType>(type)) |
| 809 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 810 | if (RD->hasMutableFields() || !RD->isTrivial()) |
| 811 | return false; |
| 812 | |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | /// Can we constant-emit a load of a reference to a variable of the |
| 817 | /// given type? This is different from predicates like |
| 818 | /// Decl::isUsableInConstantExpressions because we do want it to apply |
| 819 | /// in situations that don't necessarily satisfy the language's rules |
| 820 | /// for this (e.g. C++'s ODR-use rules). For example, we want to able |
| 821 | /// to do this with const float variables even if those variables |
| 822 | /// aren't marked 'constexpr'. |
| 823 | enum ConstantEmissionKind { |
| 824 | CEK_None, |
| 825 | CEK_AsReferenceOnly, |
| 826 | CEK_AsValueOrReference, |
| 827 | CEK_AsValueOnly |
| 828 | }; |
| 829 | static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) { |
| 830 | type = type.getCanonicalType(); |
| 831 | if (const ReferenceType *ref = dyn_cast<ReferenceType>(type)) { |
| 832 | if (isConstantEmittableObjectType(ref->getPointeeType())) |
| 833 | return CEK_AsValueOrReference; |
| 834 | return CEK_AsReferenceOnly; |
| 835 | } |
| 836 | if (isConstantEmittableObjectType(type)) |
| 837 | return CEK_AsValueOnly; |
| 838 | return CEK_None; |
| 839 | } |
| 840 | |
| 841 | /// Try to emit a reference to the given value without producing it as |
| 842 | /// an l-value. This is actually more than an optimization: we can't |
| 843 | /// produce an l-value for variables that we never actually captured |
| 844 | /// in a block or lambda, which means const int variables or constexpr |
| 845 | /// literals or similar. |
| 846 | CodeGenFunction::ConstantEmission |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 847 | CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { |
| 848 | ValueDecl *value = refExpr->getDecl(); |
| 849 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 850 | // The value needs to be an enum constant or a constant variable. |
| 851 | ConstantEmissionKind CEK; |
| 852 | if (isa<ParmVarDecl>(value)) { |
| 853 | CEK = CEK_None; |
| 854 | } else if (VarDecl *var = dyn_cast<VarDecl>(value)) { |
| 855 | CEK = checkVarTypeForConstantEmission(var->getType()); |
| 856 | } else if (isa<EnumConstantDecl>(value)) { |
| 857 | CEK = CEK_AsValueOnly; |
| 858 | } else { |
| 859 | CEK = CEK_None; |
| 860 | } |
| 861 | if (CEK == CEK_None) return ConstantEmission(); |
| 862 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 863 | Expr::EvalResult result; |
| 864 | bool resultIsReference; |
| 865 | QualType resultType; |
| 866 | |
| 867 | // It's best to evaluate all the way as an r-value if that's permitted. |
| 868 | if (CEK != CEK_AsReferenceOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 869 | refExpr->EvaluateAsRValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 870 | resultIsReference = false; |
| 871 | resultType = refExpr->getType(); |
| 872 | |
| 873 | // Otherwise, try to evaluate as an l-value. |
| 874 | } else if (CEK != CEK_AsValueOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 875 | refExpr->EvaluateAsLValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 876 | resultIsReference = true; |
| 877 | resultType = value->getType(); |
| 878 | |
| 879 | // Failure. |
| 880 | } else { |
| 881 | return ConstantEmission(); |
| 882 | } |
| 883 | |
| 884 | // In any case, if the initializer has side-effects, abandon ship. |
| 885 | if (result.HasSideEffects) |
| 886 | return ConstantEmission(); |
| 887 | |
| 888 | // Emit as a constant. |
| 889 | llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this); |
| 890 | |
| 891 | // Make sure we emit a debug reference to the global variable. |
| 892 | // This should probably fire even for |
| 893 | if (isa<VarDecl>(value)) { |
| 894 | if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value))) |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 895 | EmitDeclRefExprDbgValue(refExpr, C); |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 896 | } else { |
| 897 | assert(isa<EnumConstantDecl>(value)); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 898 | EmitDeclRefExprDbgValue(refExpr, C); |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // If we emitted a reference constant, we need to dereference that. |
| 902 | if (resultIsReference) |
| 903 | return ConstantEmission::forReference(C); |
| 904 | |
| 905 | return ConstantEmission::forValue(C); |
| 906 | } |
| 907 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 908 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) { |
| 909 | return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 910 | lvalue.getAlignment().getQuantity(), |
| 911 | lvalue.getType(), lvalue.getTBAAInfo()); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 914 | static bool hasBooleanRepresentation(QualType Ty) { |
| 915 | if (Ty->isBooleanType()) |
| 916 | return true; |
| 917 | |
| 918 | if (const EnumType *ET = Ty->getAs<EnumType>()) |
| 919 | return ET->getDecl()->getIntegerType()->isBooleanType(); |
| 920 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 921 | if (const AtomicType *AT = Ty->getAs<AtomicType>()) |
| 922 | return hasBooleanRepresentation(AT->getValueType()); |
| 923 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 924 | return false; |
| 925 | } |
| 926 | |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 927 | static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, |
| 928 | llvm::APInt &Min, llvm::APInt &End, |
| 929 | bool StrictEnums) { |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 930 | const EnumType *ET = Ty->getAs<EnumType>(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 931 | bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums && |
| 932 | ET && !ET->getDecl()->isFixed(); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 933 | bool IsBool = hasBooleanRepresentation(Ty); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 934 | if (!IsBool && !IsRegularCPlusPlusEnum) |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 935 | return false; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 936 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 937 | if (IsBool) { |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 938 | Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0); |
| 939 | End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 940 | } else { |
| 941 | const EnumDecl *ED = ET->getDecl(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 942 | llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType()); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 943 | unsigned Bitwidth = LTy->getScalarSizeInBits(); |
| 944 | unsigned NumNegativeBits = ED->getNumNegativeBits(); |
| 945 | unsigned NumPositiveBits = ED->getNumPositiveBits(); |
| 946 | |
| 947 | if (NumNegativeBits) { |
| 948 | unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); |
| 949 | assert(NumBits <= Bitwidth); |
| 950 | End = llvm::APInt(Bitwidth, 1) << (NumBits - 1); |
| 951 | Min = -End; |
| 952 | } else { |
| 953 | assert(NumPositiveBits <= Bitwidth); |
| 954 | End = llvm::APInt(Bitwidth, 1) << NumPositiveBits; |
| 955 | Min = llvm::APInt(Bitwidth, 0); |
| 956 | } |
| 957 | } |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 958 | return true; |
| 959 | } |
| 960 | |
| 961 | llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { |
| 962 | llvm::APInt Min, End; |
| 963 | if (!getRangeForType(*this, Ty, Min, End, |
| 964 | CGM.getCodeGenOpts().StrictEnums)) |
| 965 | return 0; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 966 | |
Duncan Sands | c720e78 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 967 | llvm::MDBuilder MDHelper(getLLVMContext()); |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 968 | return MDHelper.createRange(Min, End); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 971 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 972 | unsigned Alignment, QualType Ty, |
| 973 | llvm::MDNode *TBAAInfo) { |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 974 | |
| 975 | // For better performance, handle vector loads differently. |
| 976 | if (Ty->isVectorType()) { |
| 977 | llvm::Value *V; |
| 978 | const llvm::Type *EltTy = |
| 979 | cast<llvm::PointerType>(Addr->getType())->getElementType(); |
| 980 | |
| 981 | const llvm::VectorType *VTy = cast<llvm::VectorType>(EltTy); |
| 982 | |
| 983 | // Handle vectors of size 3, like size 4 for better performance. |
| 984 | if (VTy->getNumElements() == 3) { |
| 985 | |
| 986 | // Bitcast to vec4 type. |
| 987 | llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(), |
| 988 | 4); |
| 989 | llvm::PointerType *ptVec4Ty = |
| 990 | llvm::PointerType::get(vec4Ty, |
| 991 | (cast<llvm::PointerType>( |
| 992 | Addr->getType()))->getAddressSpace()); |
| 993 | llvm::Value *Cast = Builder.CreateBitCast(Addr, ptVec4Ty, |
| 994 | "castToVec4"); |
| 995 | // Now load value. |
| 996 | llvm::Value *LoadVal = Builder.CreateLoad(Cast, Volatile, "loadVec4"); |
Richard Smith | f0480fc | 2012-12-13 05:41:48 +0000 | [diff] [blame] | 997 | |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 998 | // Shuffle vector to get vec3. |
Richard Smith | f0480fc | 2012-12-13 05:41:48 +0000 | [diff] [blame] | 999 | llvm::Constant *Mask[] = { |
| 1000 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 0), |
| 1001 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 1), |
| 1002 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 2) |
| 1003 | }; |
| 1004 | |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1005 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1006 | V = Builder.CreateShuffleVector(LoadVal, |
| 1007 | llvm::UndefValue::get(vec4Ty), |
| 1008 | MaskV, "extractVec"); |
| 1009 | return EmitFromMemory(V, Ty); |
| 1010 | } |
| 1011 | } |
| 1012 | |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1013 | llvm::LoadInst *Load = Builder.CreateLoad(Addr); |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 1014 | if (Volatile) |
| 1015 | Load->setVolatile(true); |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1016 | if (Alignment) |
| 1017 | Load->setAlignment(Alignment); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1018 | if (TBAAInfo) |
| 1019 | CGM.DecorateInstruction(Load, TBAAInfo); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1020 | // If this is an atomic type, all normal reads must be atomic |
| 1021 | if (Ty->isAtomicType()) |
| 1022 | Load->setAtomic(llvm::SequentiallyConsistent); |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1023 | |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame^] | 1024 | if ((getLangOpts().SanitizeBool && hasBooleanRepresentation(Ty)) || |
| 1025 | (getLangOpts().SanitizeEnum && Ty->getAs<EnumType>())) { |
| 1026 | llvm::APInt Min, End; |
| 1027 | if (getRangeForType(*this, Ty, Min, End, true)) { |
| 1028 | --End; |
| 1029 | llvm::Value *Check; |
| 1030 | if (!Min) |
| 1031 | Check = Builder.CreateICmpULE( |
| 1032 | Load, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1033 | else { |
| 1034 | llvm::Value *Upper = Builder.CreateICmpSLE( |
| 1035 | Load, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1036 | llvm::Value *Lower = Builder.CreateICmpSGE( |
| 1037 | Load, llvm::ConstantInt::get(getLLVMContext(), Min)); |
| 1038 | Check = Builder.CreateAnd(Upper, Lower); |
| 1039 | } |
| 1040 | // FIXME: Provide a SourceLocation. |
| 1041 | EmitCheck(Check, "load_invalid_value", EmitCheckTypeDescriptor(Ty), |
| 1042 | EmitCheckValue(Load), CRK_Recoverable); |
| 1043 | } |
| 1044 | } else if (CGM.getCodeGenOpts().OptimizationLevel > 0) |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1045 | if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) |
| 1046 | Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1047 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1048 | return EmitFromMemory(Load, Ty); |
NAKAMURA Takumi | 2681efc | 2012-03-24 14:43:42 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1051 | llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { |
| 1052 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1053 | if (hasBooleanRepresentation(Ty)) { |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1054 | // This should really always be an i1, but sometimes it's already |
| 1055 | // an i8, and it's awkward to track those cases down. |
| 1056 | if (Value->getType()->isIntegerTy(1)) |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1057 | return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool"); |
| 1058 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1059 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | return Value; |
| 1063 | } |
| 1064 | |
| 1065 | llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { |
| 1066 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1067 | if (hasBooleanRepresentation(Ty)) { |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1068 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1069 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1070 | return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); |
| 1071 | } |
| 1072 | |
| 1073 | return Value; |
| 1074 | } |
| 1075 | |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1076 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1077 | bool Volatile, unsigned Alignment, |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1078 | QualType Ty, |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1079 | llvm::MDNode *TBAAInfo, |
| 1080 | bool isInit) { |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Handle vectors differently to get better performance. |
| 1083 | if (Ty->isVectorType()) { |
| 1084 | llvm::Type *SrcTy = Value->getType(); |
| 1085 | llvm::VectorType *VecTy = cast<llvm::VectorType>(SrcTy); |
| 1086 | // Handle vec3 special. |
| 1087 | if (VecTy->getNumElements() == 3) { |
| 1088 | llvm::LLVMContext &VMContext = getLLVMContext(); |
| 1089 | |
| 1090 | // Our source is a vec3, do a shuffle vector to make it a vec4. |
| 1091 | llvm::SmallVector<llvm::Constant*, 4> Mask; |
| 1092 | Mask.push_back(llvm::ConstantInt::get( |
| 1093 | llvm::Type::getInt32Ty(VMContext), |
| 1094 | 0)); |
| 1095 | Mask.push_back(llvm::ConstantInt::get( |
| 1096 | llvm::Type::getInt32Ty(VMContext), |
| 1097 | 1)); |
| 1098 | Mask.push_back(llvm::ConstantInt::get( |
| 1099 | llvm::Type::getInt32Ty(VMContext), |
| 1100 | 2)); |
| 1101 | Mask.push_back(llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext))); |
| 1102 | |
| 1103 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1104 | Value = Builder.CreateShuffleVector(Value, |
| 1105 | llvm::UndefValue::get(VecTy), |
| 1106 | MaskV, "extractVec"); |
| 1107 | SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4); |
| 1108 | } |
| 1109 | llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType()); |
| 1110 | if (DstPtr->getElementType() != SrcTy) { |
| 1111 | llvm::Type *MemTy = |
| 1112 | llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace()); |
| 1113 | Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp"); |
| 1114 | } |
| 1115 | } |
| 1116 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1117 | Value = EmitToMemory(Value, Ty); |
Chris Lattner | 1a5f897 | 2011-07-10 03:38:35 +0000 | [diff] [blame] | 1118 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1119 | llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); |
| 1120 | if (Alignment) |
| 1121 | Store->setAlignment(Alignment); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1122 | if (TBAAInfo) |
| 1123 | CGM.DecorateInstruction(Store, TBAAInfo); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1124 | if (!isInit && Ty->isAtomicType()) |
| 1125 | Store->setAtomic(llvm::SequentiallyConsistent); |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1128 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, |
| 1129 | bool isInit) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1130 | EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1131 | lvalue.getAlignment().getQuantity(), lvalue.getType(), |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1132 | lvalue.getTBAAInfo(), isInit); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1135 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this |
| 1136 | /// method emits the address of the lvalue, then loads the result as an rvalue, |
| 1137 | /// returning the rvalue. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1138 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) { |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1139 | if (LV.isObjCWeak()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1140 | // load of a __weak object. |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1141 | llvm::Value *AddrWeakObj = LV.getAddress(); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1142 | return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, |
| 1143 | AddrWeakObj)); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1144 | } |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 1145 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1146 | llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress()); |
| 1147 | Object = EmitObjCConsumeObject(LV.getType(), Object); |
| 1148 | return RValue::get(Object); |
| 1149 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1151 | if (LV.isSimple()) { |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 1152 | assert(!LV.getType()->isFunctionType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1153 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1154 | // Everything needs a load. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1155 | return RValue::get(EmitLoadOfScalar(LV)); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1156 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1158 | if (LV.isVectorElt()) { |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1159 | llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(), |
| 1160 | LV.isVolatileQualified()); |
| 1161 | Load->setAlignment(LV.getAlignment().getQuantity()); |
| 1162 | return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(), |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1163 | "vecext")); |
| 1164 | } |
Chris Lattner | 73ab9b3 | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 1165 | |
| 1166 | // If this is a reference to a subset of the elements of a vector, either |
| 1167 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1168 | if (LV.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1169 | return EmitLoadOfExtVectorElementLValue(LV); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1170 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1171 | assert(LV.isBitField() && "Unknown LValue type!"); |
| 1172 | return EmitLoadOfBitfieldLValue(LV); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1175 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1176 | const CGBitFieldInfo &Info = LV.getBitFieldInfo(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1177 | |
Daniel Dunbar | 3447a02 | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 1178 | // Get the output type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1179 | llvm::Type *ResLTy = ConvertType(LV.getType()); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1180 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1181 | llvm::Value *Ptr = LV.getBitFieldAddr(); |
| 1182 | llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), |
| 1183 | "bf.load"); |
| 1184 | cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1185 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1186 | if (Info.IsSigned) { |
| 1187 | assert((Info.Offset + Info.Size) <= Info.StorageSize); |
| 1188 | unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size; |
| 1189 | if (HighBits) |
| 1190 | Val = Builder.CreateShl(Val, HighBits, "bf.shl"); |
| 1191 | if (Info.Offset + HighBits) |
| 1192 | Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr"); |
| 1193 | } else { |
| 1194 | if (Info.Offset) |
| 1195 | Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr"); |
| 1196 | if (Info.Offset + Info.Size < Info.StorageSize) |
| 1197 | Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1198 | Info.Size), |
| 1199 | "bf.clear"); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1200 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1201 | Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast"); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1202 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1203 | return RValue::get(Val); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1206 | // If this is a reference to a subset of the elements of a vector, create an |
| 1207 | // appropriate shufflevector. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1208 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1209 | llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(), |
| 1210 | LV.isVolatileQualified()); |
| 1211 | Load->setAlignment(LV.getAlignment().getQuantity()); |
| 1212 | llvm::Value *Vec = Load; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1213 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1214 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1215 | |
| 1216 | // If the result of the expression is a non-vector type, we must be extracting |
| 1217 | // a single element. Just codegen as an extractelement. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1218 | const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1219 | if (!ExprVT) { |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1220 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1221 | llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1222 | return RValue::get(Builder.CreateExtractElement(Vec, Elt)); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1223 | } |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1224 | |
| 1225 | // Always use shuffle vector to try to retain the original program structure |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1226 | unsigned NumResultElts = ExprVT->getNumElements(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1228 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1229 | for (unsigned i = 0; i != NumResultElts; ++i) |
| 1230 | Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1232 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1233 | Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1234 | MaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1235 | return RValue::get(Vec); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
Chris Lattner | 9369a56 | 2007-06-29 16:31:29 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1240 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 1241 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 1242 | /// is 'Ty'. |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1243 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1244 | if (!Dst.isSimple()) { |
| 1245 | if (Dst.isVectorElt()) { |
| 1246 | // Read/modify/write the vector, inserting the new element. |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1247 | llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(), |
| 1248 | Dst.isVolatileQualified()); |
| 1249 | Load->setAlignment(Dst.getAlignment().getQuantity()); |
| 1250 | llvm::Value *Vec = Load; |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1251 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1252 | Dst.getVectorIdx(), "vecins"); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1253 | llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(), |
| 1254 | Dst.isVolatileQualified()); |
| 1255 | Store->setAlignment(Dst.getAlignment().getQuantity()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1256 | return; |
| 1257 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1258 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1259 | // If this is an update of extended vector elements, insert them as |
| 1260 | // appropriate. |
| 1261 | if (Dst.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1262 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1263 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1264 | assert(Dst.isBitField() && "Unknown LValue type"); |
| 1265 | return EmitStoreThroughBitfieldLValue(Src, Dst); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1266 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1267 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1268 | // There's special magic for assigning into an ARC-qualified l-value. |
| 1269 | if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) { |
| 1270 | switch (Lifetime) { |
| 1271 | case Qualifiers::OCL_None: |
| 1272 | llvm_unreachable("present but none"); |
| 1273 | |
| 1274 | case Qualifiers::OCL_ExplicitNone: |
| 1275 | // nothing special |
| 1276 | break; |
| 1277 | |
| 1278 | case Qualifiers::OCL_Strong: |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1279 | EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1280 | return; |
| 1281 | |
| 1282 | case Qualifiers::OCL_Weak: |
| 1283 | EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true); |
| 1284 | return; |
| 1285 | |
| 1286 | case Qualifiers::OCL_Autoreleasing: |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1287 | Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(), |
| 1288 | Src.getScalarVal())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1289 | // fall into the normal path |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1294 | if (Dst.isObjCWeak() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1295 | // load of a __weak object. |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1296 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 1297 | llvm::Value *src = Src.getScalarVal(); |
Mike Stump | ca5ae66 | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 1298 | CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1299 | return; |
| 1300 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1301 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1302 | if (Dst.isObjCStrong() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1303 | // load of a __strong object. |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1304 | llvm::Value *LvalueDst = Dst.getAddress(); |
| 1305 | llvm::Value *src = Src.getScalarVal(); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1306 | if (Dst.isObjCIvar()) { |
| 1307 | assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1308 | llvm::Type *ResultType = ConvertType(getContext().LongTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1309 | llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp()); |
Fariborz Jahanian | 1f9ed58 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 1310 | llvm::Value *dst = RHS; |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1311 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 1312 | llvm::Value *LHS = |
| 1313 | Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast"); |
| 1314 | llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); |
Fariborz Jahanian | 1f9ed58 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 1315 | CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1316 | BytesBetween); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1317 | } else if (Dst.isGlobalObjCRef()) { |
| 1318 | CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, |
| 1319 | Dst.isThreadLocalRef()); |
| 1320 | } |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 1321 | else |
| 1322 | CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 6278e6a | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 1326 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1327 | EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1330 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1331 | llvm::Value **Result) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1332 | const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1333 | llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType()); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1334 | llvm::Value *Ptr = Dst.getBitFieldAddr(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1335 | |
Daniel Dunbar | 67aba79 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 1336 | // Get the source value, truncated to the width of the bit-field. |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1337 | llvm::Value *SrcVal = Src.getScalarVal(); |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1338 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1339 | // Cast the source to the storage type and shift it into place. |
| 1340 | SrcVal = Builder.CreateIntCast(SrcVal, |
| 1341 | Ptr->getType()->getPointerElementType(), |
| 1342 | /*IsSigned=*/false); |
| 1343 | llvm::Value *MaskedVal = SrcVal; |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1344 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1345 | // See if there are other bits in the bitfield's storage we'll need to load |
| 1346 | // and mask together with source before storing. |
| 1347 | if (Info.StorageSize != Info.Size) { |
| 1348 | assert(Info.StorageSize > Info.Size && "Invalid bitfield size."); |
| 1349 | llvm::Value *Val = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), |
| 1350 | "bf.load"); |
| 1351 | cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment); |
| 1352 | |
| 1353 | // Mask the source value as needed. |
| 1354 | if (!hasBooleanRepresentation(Dst.getType())) |
| 1355 | SrcVal = Builder.CreateAnd(SrcVal, |
| 1356 | llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1357 | Info.Size), |
| 1358 | "bf.value"); |
| 1359 | MaskedVal = SrcVal; |
| 1360 | if (Info.Offset) |
| 1361 | SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl"); |
| 1362 | |
| 1363 | // Mask out the original value. |
| 1364 | Val = Builder.CreateAnd(Val, |
| 1365 | ~llvm::APInt::getBitsSet(Info.StorageSize, |
| 1366 | Info.Offset, |
| 1367 | Info.Offset + Info.Size), |
| 1368 | "bf.clear"); |
| 1369 | |
| 1370 | // Or together the unchanged values and the source value. |
| 1371 | SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set"); |
| 1372 | } else { |
| 1373 | assert(Info.Offset == 0); |
| 1374 | } |
| 1375 | |
| 1376 | // Write the new value back out. |
| 1377 | llvm::StoreInst *Store = Builder.CreateStore(SrcVal, Ptr, |
| 1378 | Dst.isVolatileQualified()); |
| 1379 | Store->setAlignment(Info.StorageAlignment); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1380 | |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1381 | // Return the new value of the bit-field, if requested. |
| 1382 | if (Result) { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1383 | llvm::Value *ResultVal = MaskedVal; |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1384 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1385 | // Sign extend the value if needed. |
| 1386 | if (Info.IsSigned) { |
| 1387 | assert(Info.Size <= Info.StorageSize); |
| 1388 | unsigned HighBits = Info.StorageSize - Info.Size; |
| 1389 | if (HighBits) { |
| 1390 | ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl"); |
| 1391 | ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr"); |
| 1392 | } |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1395 | ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned, |
| 1396 | "bf.result.cast"); |
| 1397 | *Result = ResultVal; |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1398 | } |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1401 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1402 | LValue Dst) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1403 | // This access turns into a read/modify/write of the vector. Load the input |
| 1404 | // value now. |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1405 | llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(), |
| 1406 | Dst.isVolatileQualified()); |
| 1407 | Load->setAlignment(Dst.getAlignment().getQuantity()); |
| 1408 | llvm::Value *Vec = Load; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1409 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1411 | llvm::Value *SrcVal = Src.getScalarVal(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1412 | |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1413 | if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1414 | unsigned NumSrcElts = VTy->getNumElements(); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1415 | unsigned NumDstElts = |
| 1416 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 1417 | if (NumDstElts == NumSrcElts) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1418 | // Use shuffle vector is the src and destination are the same number of |
| 1419 | // elements and restore the vector mask since it is on the side it will be |
| 1420 | // stored. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1421 | SmallVector<llvm::Constant*, 4> Mask(NumDstElts); |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1422 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1423 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1425 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1426 | Vec = Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1427 | llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1428 | MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1429 | } else if (NumDstElts > NumSrcElts) { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1430 | // Extended the source vector to the same length and then shuffle it |
| 1431 | // into the destination. |
| 1432 | // FIXME: since we're shuffling with undef, can we just use the indices |
| 1433 | // into that? This could be simpler. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1434 | SmallVector<llvm::Constant*, 4> ExtMask; |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1435 | for (unsigned i = 0; i != NumSrcElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1436 | ExtMask.push_back(Builder.getInt32(i)); |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1437 | ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1438 | llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1439 | llvm::Value *ExtSrcVal = |
Daniel Dunbar | 3d926cb | 2009-02-17 18:31:04 +0000 | [diff] [blame] | 1440 | Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1441 | llvm::UndefValue::get(SrcVal->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1442 | ExtMaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1443 | // build identity |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1444 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1445 | for (unsigned i = 0; i != NumDstElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1446 | Mask.push_back(Builder.getInt32(i)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1447 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1448 | // modify when what gets shuffled in |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1449 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1450 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1451 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1452 | Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1453 | } else { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1454 | // We should never shorten the vector |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1455 | llvm_unreachable("unexpected shorten vector length"); |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1456 | } |
| 1457 | } else { |
| 1458 | // If the Src is a scalar (not a vector) it must be updating one element. |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1459 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1460 | llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1461 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1463 | |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1464 | llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(), |
| 1465 | Dst.isVolatileQualified()); |
| 1466 | Store->setAlignment(Dst.getAlignment().getQuantity()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1469 | // setObjCGCLValueClass - sets class of he lvalue for the purpose of |
| 1470 | // generating write-barries API. It is currently a global, ivar, |
| 1471 | // or neither. |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1472 | static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1473 | LValue &LV, |
| 1474 | bool IsMemberAccess=false) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1475 | if (Ctx.getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1478 | if (isa<ObjCIvarRefExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1479 | QualType ExpTy = E->getType(); |
| 1480 | if (IsMemberAccess && ExpTy->isPointerType()) { |
| 1481 | // If ivar is a structure pointer, assigning to field of |
| 1482 | // this struct follows gcc's behavior and makes it a non-ivar |
| 1483 | // writer-barrier conservatively. |
| 1484 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 1485 | if (ExpTy->isRecordType()) { |
| 1486 | LV.setObjCIvar(false); |
| 1487 | return; |
| 1488 | } |
| 1489 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1490 | LV.setObjCIvar(true); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1491 | ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E)); |
| 1492 | LV.setBaseIvarExp(Exp->getBase()); |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1493 | LV.setObjCArray(E->getType()->isArrayType()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1494 | return; |
| 1495 | } |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1496 | |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1497 | if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) { |
| 1498 | if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1499 | if (VD->hasGlobalStorage()) { |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1500 | LV.setGlobalObjCRef(true); |
| 1501 | LV.setThreadLocalRef(VD->isThreadSpecified()); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1502 | } |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1503 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1504 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1505 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1506 | } |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1509 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
| 1512 | |
| 1513 | if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1514 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1515 | if (LV.isObjCIvar()) { |
| 1516 | // If cast is to a structure pointer, follow gcc's behavior and make it |
| 1517 | // a non-ivar write-barrier. |
| 1518 | QualType ExpTy = E->getType(); |
| 1519 | if (ExpTy->isPointerType()) |
| 1520 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 1521 | if (ExpTy->isRecordType()) |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1522 | LV.setObjCIvar(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1523 | } |
| 1524 | return; |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1525 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1526 | |
| 1527 | if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) { |
| 1528 | setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1532 | if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1533 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1534 | return; |
| 1535 | } |
| 1536 | |
| 1537 | if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1538 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1539 | return; |
| 1540 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1543 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1544 | return; |
| 1545 | } |
| 1546 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1547 | if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) { |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1548 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV); |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1549 | if (LV.isObjCIvar() && !LV.isObjCArray()) |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1550 | // Using array syntax to assigning to what an ivar points to is not |
| 1551 | // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1552 | LV.setObjCIvar(false); |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1553 | else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) |
| 1554 | // Using array syntax to assigning to what global points to is not |
| 1555 | // same as assigning to the global itself. {id *G;} G[i] = 0; |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1556 | LV.setGlobalObjCRef(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1557 | return; |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1558 | } |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1559 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1560 | if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1561 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true); |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1562 | // We don't know if member is an 'ivar', but this flag is looked at |
| 1563 | // only in the context of LV.isObjCIvar(). |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1564 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1565 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1569 | static llvm::Value * |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 1570 | EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1571 | llvm::Value *V, llvm::Type *IRType, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1572 | StringRef Name = StringRef()) { |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1573 | unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 1574 | return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1577 | static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, |
| 1578 | const Expr *E, const VarDecl *VD) { |
Daniel Dunbar | 7e215ea | 2009-11-08 09:46:46 +0000 | [diff] [blame] | 1579 | assert((VD->hasExternalStorage() || VD->isFileVarDecl()) && |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1580 | "Var decl must have external storage or be a file var decl!"); |
| 1581 | |
| 1582 | llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1583 | llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); |
| 1584 | V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1585 | CharUnits Alignment = CGF.getContext().getDeclAlign(VD); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1586 | QualType T = E->getType(); |
| 1587 | LValue LV; |
| 1588 | if (VD->getType()->isReferenceType()) { |
| 1589 | llvm::LoadInst *LI = CGF.Builder.CreateLoad(V); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1590 | LI->setAlignment(Alignment.getQuantity()); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1591 | V = LI; |
| 1592 | LV = CGF.MakeNaturalAlignAddrLValue(V, T); |
| 1593 | } else { |
| 1594 | LV = CGF.MakeAddrLValue(V, E->getType(), Alignment); |
| 1595 | } |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1596 | setObjCGCLValueClass(CGF.getContext(), E, LV); |
| 1597 | return LV; |
| 1598 | } |
| 1599 | |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1600 | static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 1601 | const Expr *E, const FunctionDecl *FD) { |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 1602 | llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1603 | if (!FD->hasPrototype()) { |
| 1604 | if (const FunctionProtoType *Proto = |
| 1605 | FD->getType()->getAs<FunctionProtoType>()) { |
| 1606 | // Ugly case: for a K&R-style definition, the type of the definition |
| 1607 | // isn't the same as the type of a use. Correct for this with a |
| 1608 | // bitcast. |
| 1609 | QualType NoProtoType = |
| 1610 | CGF.getContext().getFunctionNoProtoType(Proto->getResultType()); |
| 1611 | NoProtoType = CGF.getContext().getPointerType(NoProtoType); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1612 | V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType)); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1615 | CharUnits Alignment = CGF.getContext().getDeclAlign(FD); |
Daniel Dunbar | 5c81637 | 2010-08-21 04:20:22 +0000 | [diff] [blame] | 1616 | return CGF.MakeAddrLValue(V, E->getType(), Alignment); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1619 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1620 | const NamedDecl *ND = E->getDecl(); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1621 | CharUnits Alignment = getContext().getDeclAlign(ND); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1622 | QualType T = E->getType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1623 | |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 1624 | // A DeclRefExpr for a reference initialized by a constant expression can |
| 1625 | // appear without being odr-used. Directly emit the constant initializer. |
| 1626 | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 1627 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1628 | if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() && |
| 1629 | VD->isUsableInConstantExpressions(getContext()) && |
| 1630 | VD->checkInitIsICE()) { |
| 1631 | llvm::Constant *Val = |
| 1632 | CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this); |
| 1633 | assert(Val && "failed to emit reference constant expression"); |
| 1634 | // FIXME: Eventually we will want to emit vector element references. |
| 1635 | return MakeAddrLValue(Val, T, Alignment); |
| 1636 | } |
| 1637 | } |
| 1638 | |
Eli Friedman | 5720e34 | 2012-01-21 04:52:58 +0000 | [diff] [blame] | 1639 | // FIXME: We should be able to assert this for FunctionDecls as well! |
| 1640 | // FIXME: We should be able to assert this for all DeclRefExprs, not just |
| 1641 | // those with a valid source location. |
| 1642 | assert((ND->isUsed(false) || !isa<VarDecl>(ND) || |
| 1643 | !E->getLocation().isValid()) && |
| 1644 | "Should not use decl without marking it used!"); |
| 1645 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1646 | if (ND->hasAttr<WeakRefAttr>()) { |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 1647 | const ValueDecl *VD = cast<ValueDecl>(ND); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1648 | llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD); |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 1649 | return MakeAddrLValue(Aliasee, T, Alignment); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1652 | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 1653 | // Check if this is a global variable. |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1654 | if (VD->hasExternalStorage() || VD->isFileVarDecl()) |
| 1655 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Anders Carlsson | 6eee972 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1656 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1657 | bool isBlockVariable = VD->hasAttr<BlocksAttr>(); |
| 1658 | |
Fariborz Jahanian | 44a41d1 | 2010-11-19 18:17:09 +0000 | [diff] [blame] | 1659 | bool NonGCable = VD->hasLocalStorage() && |
| 1660 | !VD->getType()->isReferenceType() && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1661 | !isBlockVariable; |
Anders Carlsson | 6eee972 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1662 | |
| 1663 | llvm::Value *V = LocalDeclMap[VD]; |
Fariborz Jahanian | 366a948 | 2010-09-07 23:26:17 +0000 | [diff] [blame] | 1664 | if (!V && VD->isStaticLocal()) |
Fariborz Jahanian | 4d55b2d | 2010-04-19 18:15:02 +0000 | [diff] [blame] | 1665 | V = CGM.getStaticLocalDeclAddress(VD); |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Use special handling for lambdas. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1668 | if (!V) { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1669 | if (FieldDecl *FD = LambdaCaptureFields.lookup(VD)) { |
| 1670 | QualType LambdaTagType = getContext().getTagDeclType(FD->getParent()); |
| 1671 | LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, |
| 1672 | LambdaTagType); |
| 1673 | return EmitLValueForField(LambdaLV, FD); |
| 1674 | } |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 1675 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1676 | assert(isa<BlockDecl>(CurCodeDecl) && E->refersToEnclosingLocal()); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1677 | return MakeAddrLValue(GetAddrOfBlockDecl(VD, isBlockVariable), |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 1678 | T, Alignment); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Anders Carlsson | 6eee972 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 1681 | assert(V && "DeclRefExpr not entered in LocalDeclMap?"); |
| 1682 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1683 | if (isBlockVariable) |
Fariborz Jahanian | 2f2fa72 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 1684 | V = BuildBlockByrefAddress(V, VD); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 1685 | |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1686 | LValue LV; |
| 1687 | if (VD->getType()->isReferenceType()) { |
| 1688 | llvm::LoadInst *LI = Builder.CreateLoad(V); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1689 | LI->setAlignment(Alignment.getQuantity()); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1690 | V = LI; |
| 1691 | LV = MakeNaturalAlignAddrLValue(V, T); |
| 1692 | } else { |
| 1693 | LV = MakeAddrLValue(V, T, Alignment); |
| 1694 | } |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1695 | |
Fariborz Jahanian | 44a41d1 | 2010-11-19 18:17:09 +0000 | [diff] [blame] | 1696 | if (NonGCable) { |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 1697 | LV.getQuals().removeObjCGCAttr(); |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 1698 | LV.setNonGC(true); |
| 1699 | } |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1700 | setObjCGCLValueClass(getContext(), E, LV); |
Fariborz Jahanian | 003e830 | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 1701 | return LV; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1702 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1703 | |
| 1704 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND)) |
| 1705 | return EmitFunctionDeclLValue(*this, E, fn); |
| 1706 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1707 | llvm_unreachable("Unhandled DeclRefExpr"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1708 | } |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1710 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 1711 | // __extension__ doesn't affect lvalue-ness. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1712 | if (E->getOpcode() == UO_Extension) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1713 | return EmitLValue(E->getSubExpr()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1714 | |
Chris Lattner | 0f398c4 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1715 | QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1716 | switch (E->getOpcode()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1717 | default: llvm_unreachable("Unknown unary operator lvalue!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1718 | case UO_Deref: { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1719 | QualType T = E->getSubExpr()->getType()->getPointeeType(); |
| 1720 | assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | 2415357a | 2011-12-19 21:16:08 +0000 | [diff] [blame] | 1722 | LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 1723 | LV.getQuals().setAddressSpace(ExprTy.getAddressSpace()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1725 | // We should not generate __weak write barrier on indirect reference |
| 1726 | // of a pointer to object; as in void foo (__weak id *param); *param = 0; |
| 1727 | // But, we continue to generate __strong write barrier on indirect write |
| 1728 | // into a pointer to object. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1729 | if (getLangOpts().ObjC1 && |
| 1730 | getLangOpts().getGC() != LangOptions::NonGC && |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1731 | LV.isObjCWeak()) |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 1732 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1733 | return LV; |
| 1734 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1735 | case UO_Real: |
| 1736 | case UO_Imag: { |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1737 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1738 | assert(LV.isSimple() && "real/imag on non-ordinary l-value"); |
| 1739 | llvm::Value *Addr = LV.getAddress(); |
| 1740 | |
Richard Smith | 0b6b8e4 | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 1741 | // __real is valid on scalars. This is a faster way of testing that. |
| 1742 | // __imag can only produce an rvalue on scalars. |
| 1743 | if (E->getOpcode() == UO_Real && |
| 1744 | !cast<llvm::PointerType>(Addr->getType()) |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1745 | ->getElementType()->isStructTy()) { |
| 1746 | assert(E->getSubExpr()->getType()->isArithmeticType()); |
| 1747 | return LV; |
| 1748 | } |
| 1749 | |
| 1750 | assert(E->getSubExpr()->getType()->isAnyComplexType()); |
| 1751 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1752 | unsigned Idx = E->getOpcode() == UO_Imag; |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 1753 | return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(), |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1754 | Idx, "idx"), |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 1755 | ExprTy); |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 1756 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1757 | case UO_PreInc: |
| 1758 | case UO_PreDec: { |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 1759 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1760 | bool isInc = E->getOpcode() == UO_PreInc; |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 1761 | |
| 1762 | if (E->getType()->isAnyComplexType()) |
| 1763 | EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 1764 | else |
| 1765 | EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 1766 | return LV; |
| 1767 | } |
Eli Friedman | a72bf0f | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 1768 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 1771 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1772 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E), |
| 1773 | E->getType()); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1776 | LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1777 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E), |
| 1778 | E->getType()); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1781 | static llvm::Constant* |
| 1782 | GetAddrOfConstantWideString(StringRef Str, |
| 1783 | const char *GlobalName, |
| 1784 | ASTContext &Context, |
| 1785 | QualType Ty, SourceLocation Loc, |
| 1786 | CodeGenModule &CGM) { |
| 1787 | |
| 1788 | StringLiteral *SL = StringLiteral::Create(Context, |
| 1789 | Str, |
| 1790 | StringLiteral::Wide, |
| 1791 | /*Pascal = */false, |
| 1792 | Ty, Loc); |
| 1793 | llvm::Constant *C = CGM.GetConstantArrayFromStringLiteral(SL); |
| 1794 | llvm::GlobalVariable *GV = |
| 1795 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
| 1796 | !CGM.getLangOpts().WritableStrings, |
| 1797 | llvm::GlobalValue::PrivateLinkage, |
| 1798 | C, GlobalName); |
| 1799 | const unsigned WideAlignment = |
| 1800 | Context.getTypeAlignInChars(Ty).getQuantity(); |
| 1801 | GV->setAlignment(WideAlignment); |
| 1802 | return GV; |
| 1803 | } |
| 1804 | |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1805 | static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source, |
| 1806 | SmallString<32>& Target) { |
| 1807 | Target.resize(CharByteWidth * (Source.size() + 1)); |
Richard Smith | 639b8d0 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1808 | char *ResultPtr = &Target[0]; |
| 1809 | const UTF8 *ErrorPtr; |
| 1810 | bool success = ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr); |
Matt Beaumont-Gay | 36af16af | 2012-07-03 03:55:58 +0000 | [diff] [blame] | 1811 | (void)success; |
Nico Weber | 4b18c3f | 2012-07-03 02:24:52 +0000 | [diff] [blame] | 1812 | assert(success); |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1813 | Target.resize(ResultPtr - &Target[0]); |
| 1814 | } |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1815 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1816 | LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { |
Daniel Dunbar | b351747 | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1817 | switch (E->getIdentType()) { |
| 1818 | default: |
| 1819 | return EmitUnsupportedLValue(E, "predefined expression"); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1820 | |
Daniel Dunbar | b351747 | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1821 | case PredefinedExpr::Func: |
| 1822 | case PredefinedExpr::Function: |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1823 | case PredefinedExpr::LFunction: |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1824 | case PredefinedExpr::PrettyFunction: { |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1825 | unsigned IdentType = E->getIdentType(); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1826 | std::string GlobalVarName; |
| 1827 | |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1828 | switch (IdentType) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1829 | default: llvm_unreachable("Invalid type"); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1830 | case PredefinedExpr::Func: |
| 1831 | GlobalVarName = "__func__."; |
| 1832 | break; |
| 1833 | case PredefinedExpr::Function: |
| 1834 | GlobalVarName = "__FUNCTION__."; |
| 1835 | break; |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1836 | case PredefinedExpr::LFunction: |
| 1837 | GlobalVarName = "L__FUNCTION__."; |
| 1838 | break; |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1839 | case PredefinedExpr::PrettyFunction: |
| 1840 | GlobalVarName = "__PRETTY_FUNCTION__."; |
| 1841 | break; |
| 1842 | } |
| 1843 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1844 | StringRef FnName = CurFn->getName(); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1845 | if (FnName.startswith("\01")) |
| 1846 | FnName = FnName.substr(1); |
| 1847 | GlobalVarName += FnName; |
| 1848 | |
| 1849 | const Decl *CurDecl = CurCodeDecl; |
| 1850 | if (CurDecl == 0) |
| 1851 | CurDecl = getContext().getTranslationUnitDecl(); |
| 1852 | |
| 1853 | std::string FunctionName = |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1854 | (isa<BlockDecl>(CurDecl) |
| 1855 | ? FnName.str() |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1856 | : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)IdentType, |
| 1857 | CurDecl)); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1858 | |
Nico Weber | 3a691a3 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 1859 | const Type* ElemType = E->getType()->getArrayElementTypeNoTypeQual(); |
| 1860 | llvm::Constant *C; |
| 1861 | if (ElemType->isWideCharType()) { |
| 1862 | SmallString<32> RawChars; |
| 1863 | ConvertUTF8ToWideString( |
| 1864 | getContext().getTypeSizeInChars(ElemType).getQuantity(), |
| 1865 | FunctionName, RawChars); |
| 1866 | C = GetAddrOfConstantWideString(RawChars, |
| 1867 | GlobalVarName.c_str(), |
| 1868 | getContext(), |
| 1869 | E->getType(), |
| 1870 | E->getLocation(), |
| 1871 | CGM); |
| 1872 | } else { |
| 1873 | C = CGM.GetAddrOfConstantCString(FunctionName, |
| 1874 | GlobalVarName.c_str(), |
| 1875 | 1); |
| 1876 | } |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1877 | return MakeAddrLValue(C, E->getType()); |
Daniel Dunbar | b1d94a9 | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 1878 | } |
Daniel Dunbar | b351747 | 2008-10-17 21:58:32 +0000 | [diff] [blame] | 1879 | } |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1882 | /// Emit a type description suitable for use by a runtime sanitizer library. The |
| 1883 | /// format of a type descriptor is |
| 1884 | /// |
| 1885 | /// \code |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 1886 | /// { i16 TypeKind, i16 TypeInfo } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1887 | /// \endcode |
| 1888 | /// |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 1889 | /// followed by an array of i8 containing the type name. TypeKind is 0 for an |
| 1890 | /// integer, 1 for a floating point value, and -1 for anything else. |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1891 | llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) { |
| 1892 | // FIXME: Only emit each type's descriptor once. |
| 1893 | uint16_t TypeKind = -1; |
| 1894 | uint16_t TypeInfo = 0; |
Mike Stump | 9a4e012 | 2009-12-15 00:59:40 +0000 | [diff] [blame] | 1895 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1896 | if (T->isIntegerType()) { |
| 1897 | TypeKind = 0; |
| 1898 | TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) | |
Aaron Ballman | f505d55 | 2012-11-30 21:44:01 +0000 | [diff] [blame] | 1899 | (T->isSignedIntegerType() ? 1 : 0); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1900 | } else if (T->isFloatingType()) { |
| 1901 | TypeKind = 1; |
| 1902 | TypeInfo = getContext().getTypeSize(T); |
| 1903 | } |
| 1904 | |
| 1905 | // Format the type name as if for a diagnostic, including quotes and |
| 1906 | // optionally an 'aka'. |
| 1907 | llvm::SmallString<32> Buffer; |
| 1908 | CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype, |
| 1909 | (intptr_t)T.getAsOpaquePtr(), |
| 1910 | 0, 0, 0, 0, 0, 0, Buffer, |
| 1911 | ArrayRef<intptr_t>()); |
| 1912 | |
| 1913 | llvm::Constant *Components[] = { |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 1914 | Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo), |
| 1915 | llvm::ConstantDataArray::getString(getLLVMContext(), Buffer) |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1916 | }; |
| 1917 | llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components); |
| 1918 | |
| 1919 | llvm::GlobalVariable *GV = |
| 1920 | new llvm::GlobalVariable(CGM.getModule(), Descriptor->getType(), |
| 1921 | /*isConstant=*/true, |
| 1922 | llvm::GlobalVariable::PrivateLinkage, |
| 1923 | Descriptor); |
| 1924 | GV->setUnnamedAddr(true); |
| 1925 | return GV; |
| 1926 | } |
| 1927 | |
| 1928 | llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) { |
| 1929 | llvm::Type *TargetTy = IntPtrTy; |
| 1930 | |
| 1931 | // Integers which fit in intptr_t are zero-extended and passed directly. |
| 1932 | if (V->getType()->isIntegerTy() && |
| 1933 | V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth()) |
| 1934 | return Builder.CreateZExt(V, TargetTy); |
| 1935 | |
| 1936 | // Pointers are passed directly, everything else is passed by address. |
| 1937 | if (!V->getType()->isPointerTy()) { |
| 1938 | llvm::Value *Ptr = Builder.CreateAlloca(V->getType()); |
| 1939 | Builder.CreateStore(V, Ptr); |
| 1940 | V = Ptr; |
| 1941 | } |
| 1942 | return Builder.CreatePtrToInt(V, TargetTy); |
| 1943 | } |
| 1944 | |
| 1945 | /// \brief Emit a representation of a SourceLocation for passing to a handler |
| 1946 | /// in a sanitizer runtime library. The format for this data is: |
| 1947 | /// \code |
| 1948 | /// struct SourceLocation { |
| 1949 | /// const char *Filename; |
| 1950 | /// int32_t Line, Column; |
| 1951 | /// }; |
| 1952 | /// \endcode |
| 1953 | /// For an invalid SourceLocation, the Filename pointer is null. |
| 1954 | llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) { |
| 1955 | PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc); |
| 1956 | |
| 1957 | llvm::Constant *Data[] = { |
| 1958 | // FIXME: Only emit each file name once. |
| 1959 | PLoc.isValid() ? cast<llvm::Constant>( |
| 1960 | Builder.CreateGlobalStringPtr(PLoc.getFilename())) |
| 1961 | : llvm::Constant::getNullValue(Int8PtrTy), |
| 1962 | Builder.getInt32(PLoc.getLine()), |
| 1963 | Builder.getInt32(PLoc.getColumn()) |
| 1964 | }; |
| 1965 | |
| 1966 | return llvm::ConstantStruct::getAnon(Data); |
| 1967 | } |
| 1968 | |
| 1969 | void CodeGenFunction::EmitCheck(llvm::Value *Checked, StringRef CheckName, |
| 1970 | llvm::ArrayRef<llvm::Constant *> StaticArgs, |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 1971 | llvm::ArrayRef<llvm::Value *> DynamicArgs, |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 1972 | CheckRecoverableKind RecoverKind) { |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1973 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 1974 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1975 | llvm::BasicBlock *Handler = createBasicBlock("handler." + CheckName); |
| 1976 | Builder.CreateCondBr(Checked, Cont, Handler); |
| 1977 | EmitBlock(Handler); |
| 1978 | |
| 1979 | llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); |
| 1980 | llvm::GlobalValue *InfoPtr = |
| 1981 | new llvm::GlobalVariable(CGM.getModule(), Info->getType(), true, |
| 1982 | llvm::GlobalVariable::PrivateLinkage, Info); |
| 1983 | InfoPtr->setUnnamedAddr(true); |
| 1984 | |
| 1985 | llvm::SmallVector<llvm::Value *, 4> Args; |
| 1986 | llvm::SmallVector<llvm::Type *, 4> ArgTypes; |
| 1987 | Args.reserve(DynamicArgs.size() + 1); |
| 1988 | ArgTypes.reserve(DynamicArgs.size() + 1); |
| 1989 | |
| 1990 | // Handler functions take an i8* pointing to the (handler-specific) static |
| 1991 | // information block, followed by a sequence of intptr_t arguments |
| 1992 | // representing operand values. |
| 1993 | Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy)); |
| 1994 | ArgTypes.push_back(Int8PtrTy); |
| 1995 | for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) { |
| 1996 | Args.push_back(EmitCheckValue(DynamicArgs[i])); |
| 1997 | ArgTypes.push_back(IntPtrTy); |
| 1998 | } |
| 1999 | |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2000 | bool Recover = (RecoverKind == CRK_AlwaysRecoverable) || |
| 2001 | ((RecoverKind == CRK_Recoverable) && |
| 2002 | CGM.getCodeGenOpts().SanitizeRecover); |
| 2003 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2004 | llvm::FunctionType *FnType = |
| 2005 | llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false); |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 2006 | llvm::AttrBuilder B; |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2007 | if (!Recover) { |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2008 | B.addAttribute(llvm::Attributes::NoReturn) |
| 2009 | .addAttribute(llvm::Attributes::NoUnwind); |
| 2010 | } |
| 2011 | B.addAttribute(llvm::Attributes::UWTable); |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Checks that have two variants use a suffix to differentiate them |
| 2014 | bool NeedsAbortSuffix = (RecoverKind != CRK_Unrecoverable) && |
| 2015 | !CGM.getCodeGenOpts().SanitizeRecover; |
Richard Smith | 78f6b03 | 2012-12-03 22:39:14 +0000 | [diff] [blame] | 2016 | std::string FunctionName = ("__ubsan_handle_" + CheckName + |
| 2017 | (NeedsAbortSuffix? "_abort" : "")).str(); |
| 2018 | llvm::Value *Fn = |
| 2019 | CGM.CreateRuntimeFunction(FnType, FunctionName, |
| 2020 | llvm::Attributes::get(getLLVMContext(), B)); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2021 | llvm::CallInst *HandlerCall = Builder.CreateCall(Fn, Args); |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2022 | if (Recover) { |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2023 | Builder.CreateBr(Cont); |
| 2024 | } else { |
| 2025 | HandlerCall->setDoesNotReturn(); |
| 2026 | HandlerCall->setDoesNotThrow(); |
| 2027 | Builder.CreateUnreachable(); |
| 2028 | } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2029 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2030 | EmitBlock(Cont); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Richard Smith | de67068 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2033 | void CodeGenFunction::EmitTrapvCheck(llvm::Value *Checked) { |
| 2034 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 2035 | |
| 2036 | // If we're optimizing, collapse all calls to trap down to just one per |
| 2037 | // function to save on code size. |
| 2038 | if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) { |
| 2039 | TrapBB = createBasicBlock("trap"); |
| 2040 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2041 | EmitBlock(TrapBB); |
| 2042 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap); |
| 2043 | llvm::CallInst *TrapCall = Builder.CreateCall(F); |
| 2044 | TrapCall->setDoesNotReturn(); |
| 2045 | TrapCall->setDoesNotThrow(); |
| 2046 | Builder.CreateUnreachable(); |
| 2047 | } else { |
| 2048 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2049 | } |
| 2050 | |
| 2051 | EmitBlock(Cont); |
| 2052 | } |
| 2053 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2054 | /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an |
| 2055 | /// array to pointer, return the array subexpression. |
| 2056 | static const Expr *isSimpleArrayDecayOperand(const Expr *E) { |
| 2057 | // If this isn't just an array->pointer decay, bail out. |
| 2058 | const CastExpr *CE = dyn_cast<CastExpr>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2059 | if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay) |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2060 | return 0; |
| 2061 | |
| 2062 | // If this is a decay from variable width array, bail out. |
| 2063 | const Expr *SubExpr = CE->getSubExpr(); |
| 2064 | if (SubExpr->getType()->isVariableArrayType()) |
| 2065 | return 0; |
| 2066 | |
| 2067 | return SubExpr; |
| 2068 | } |
| 2069 | |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 2070 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2071 | // The index must always be an integer, which is not an aggregate. Emit it. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2072 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Eli Friedman | 07bbeca | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 2073 | QualType IdxTy = E->getIdx()->getType(); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2074 | bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType(); |
Eli Friedman | 07bbeca | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2076 | // If the base is a vector type, then we are forming a vector element lvalue |
| 2077 | // with this subscript. |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2078 | if (E->getBase()->getType()->isVectorType()) { |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2079 | // Emit the vector as an lvalue to get its address. |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2080 | LValue LHS = EmitLValue(E->getBase()); |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2081 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 2082 | Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx"); |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2083 | return LValue::MakeVectorElt(LHS.getAddress(), Idx, |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 2084 | E->getBase()->getType(), LHS.getAlignment()); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2085 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2086 | |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2087 | // Extend or truncate the index type to 32 or 64-bits. |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 2088 | if (Idx->getType() != IntPtrTy) |
| 2089 | Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom"); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2090 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2091 | // We know that the pointer points to a type of the correct size, unless the |
| 2092 | // size is a VLA or Objective-C interface. |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2093 | llvm::Value *Address = 0; |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2094 | CharUnits ArrayAlignment; |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 2095 | if (const VariableArrayType *vla = |
Anders Carlsson | 3d312f8 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 2096 | getContext().getAsVariableArrayType(E->getType())) { |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 2097 | // The base must be a pointer, which is not an aggregate. Emit |
| 2098 | // it. It needs to be emitted first in case it's what captures |
| 2099 | // the VLA bounds. |
| 2100 | Address = EmitScalarExpr(E->getBase()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2101 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 2102 | // The element count here is the total number of non-VLA elements. |
| 2103 | llvm::Value *numElements = getVLASize(vla).first; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2104 | |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2105 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 2106 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 2107 | // signed-overflow, so we use the same semantics for our explicit |
| 2108 | // multiply. We suppress this if overflow is not undefined behavior. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2109 | if (getLangOpts().isSignedOverflowDefined()) { |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2110 | Idx = Builder.CreateMul(Idx, numElements); |
Chris Lattner | 2e72da94 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2111 | Address = Builder.CreateGEP(Address, Idx, "arrayidx"); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2112 | } else { |
| 2113 | Idx = Builder.CreateNSWMul(Idx, numElements); |
Chris Lattner | 2e72da94 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2114 | Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx"); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2115 | } |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2116 | } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ |
| 2117 | // Indexing over an interface, as in "NSString *P; P[4];" |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2118 | llvm::Value *InterfaceSize = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2119 | llvm::ConstantInt::get(Idx->getType(), |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 2120 | getContext().getTypeSizeInChars(OIT).getQuantity()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2121 | |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2122 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
| 2123 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2124 | // The base must be a pointer, which is not an aggregate. Emit it. |
| 2125 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 2126 | Address = EmitCastToVoidPtr(Base); |
| 2127 | Address = Builder.CreateGEP(Address, Idx, "arrayidx"); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2128 | Address = Builder.CreateBitCast(Address, Base->getType()); |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2129 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 2130 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 2131 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 2132 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 2133 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 2134 | assert(Array->getType()->isArrayType() && |
| 2135 | "Array to pointer decay must have array source type!"); |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2136 | LValue ArrayLV = EmitLValue(Array); |
| 2137 | llvm::Value *ArrayPtr = ArrayLV.getAddress(); |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2138 | llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0); |
| 2139 | llvm::Value *Args[] = { Zero, Idx }; |
| 2140 | |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2141 | // Propagate the alignment from the array itself to the result. |
| 2142 | ArrayAlignment = ArrayLV.getAlignment(); |
| 2143 | |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2144 | if (getLangOpts().isSignedOverflowDefined()) |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 2145 | Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx"); |
Chris Lattner | 2e72da94 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2146 | else |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 2147 | Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx"); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2148 | } else { |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2149 | // The base must be a pointer, which is not an aggregate. Emit it. |
| 2150 | llvm::Value *Base = EmitScalarExpr(E->getBase()); |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2151 | if (getLangOpts().isSignedOverflowDefined()) |
Chris Lattner | 2e72da94 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2152 | Address = Builder.CreateGEP(Base, Idx, "arrayidx"); |
| 2153 | else |
| 2154 | Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); |
Anders Carlsson | 3d312f8 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 2155 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2156 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2157 | QualType T = E->getBase()->getType()->getPointeeType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2158 | assert(!T.isNull() && |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2159 | "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2160 | |
Chris Lattner | 36bc4f4 | 2012-01-04 22:35:55 +0000 | [diff] [blame] | 2161 | |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2162 | // Limit the alignment to that of the result type. |
Chris Lattner | 36bc4f4 | 2012-01-04 22:35:55 +0000 | [diff] [blame] | 2163 | LValue LV; |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2164 | if (!ArrayAlignment.isZero()) { |
| 2165 | CharUnits Align = getContext().getTypeAlignInChars(T); |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2166 | ArrayAlignment = std::min(Align, ArrayAlignment); |
Chris Lattner | 36bc4f4 | 2012-01-04 22:35:55 +0000 | [diff] [blame] | 2167 | LV = MakeAddrLValue(Address, T, ArrayAlignment); |
| 2168 | } else { |
| 2169 | LV = MakeNaturalAlignAddrLValue(Address, T); |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2172 | LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2173 | |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2174 | if (getLangOpts().ObjC1 && |
| 2175 | getLangOpts().getGC() != LangOptions::NonGC) { |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2176 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2177 | setObjCGCLValueClass(getContext(), E, LV); |
| 2178 | } |
Fariborz Jahanian | a9fecf3 | 2009-02-21 23:37:19 +0000 | [diff] [blame] | 2179 | return LV; |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2182 | static |
NAKAMURA Takumi | ccca11a | 2012-01-25 08:58:21 +0000 | [diff] [blame] | 2183 | llvm::Constant *GenerateConstantVector(CGBuilderTy &Builder, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2184 | SmallVector<unsigned, 4> &Elts) { |
| 2185 | SmallVector<llvm::Constant*, 4> CElts; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2186 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 2187 | CElts.push_back(Builder.getInt32(Elts[i])); |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2188 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 2189 | return llvm::ConstantVector::get(CElts); |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 2192 | LValue CodeGenFunction:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2193 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 2194 | // Emit the base vector as an l-value. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2195 | LValue Base; |
| 2196 | |
| 2197 | // ExtVectorElementExpr's base can either be a vector or pointer to vector. |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2198 | if (E->isArrow()) { |
| 2199 | // If it is a pointer to a vector, emit the address and form an lvalue with |
| 2200 | // it. |
Chris Lattner | b8211f6 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 2201 | llvm::Value *Ptr = EmitScalarExpr(E->getBase()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2202 | const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2203 | Base = MakeAddrLValue(Ptr, PT->getPointeeType()); |
| 2204 | Base.getQuals().removeObjCGCAttr(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2205 | } else if (E->getBase()->isGLValue()) { |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2206 | // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), |
| 2207 | // emit the base as an lvalue. |
| 2208 | assert(E->getBase()->getType()->isVectorType()); |
| 2209 | Base = EmitLValue(E->getBase()); |
| 2210 | } else { |
| 2211 | // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such. |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 2212 | assert(E->getBase()->getType()->isVectorType() && |
Daniel Dunbar | 5b90195 | 2010-01-04 18:02:28 +0000 | [diff] [blame] | 2213 | "Result must be a vector"); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2214 | llvm::Value *Vec = EmitScalarExpr(E->getBase()); |
| 2215 | |
Chris Lattner | f0a9ba3 | 2009-12-23 21:33:41 +0000 | [diff] [blame] | 2216 | // Store the vector to memory (because LValue wants an address). |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 2217 | llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2218 | Builder.CreateStore(Vec, VecMem); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2219 | Base = MakeAddrLValue(VecMem, E->getBase()->getType()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2220 | } |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 2221 | |
| 2222 | QualType type = |
| 2223 | E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 2224 | |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2225 | // Encode the element access list into a vector of unsigned indices. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2226 | SmallVector<unsigned, 4> Indices; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2227 | E->getEncodedElementAccess(Indices); |
| 2228 | |
| 2229 | if (Base.isSimple()) { |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 2230 | llvm::Constant *CV = GenerateConstantVector(Builder, Indices); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 2231 | return LValue::MakeExtVectorElt(Base.getAddress(), CV, type, |
| 2232 | Base.getAlignment()); |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2233 | } |
| 2234 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 2235 | |
| 2236 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2237 | SmallVector<llvm::Constant *, 4> CElts; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 2239 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 2240 | CElts.push_back(BaseElts->getAggregateElement(Indices[i])); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 2241 | llvm::Constant *CV = llvm::ConstantVector::get(CElts); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 2242 | return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type, |
| 2243 | Base.getAlignment()); |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 2246 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Devang Patel | d68df20 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 2247 | Expr *BaseExpr = E->getBase(); |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | 4e4186b | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 2249 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2250 | LValue BaseLV; |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 2251 | if (E->isArrow()) { |
| 2252 | llvm::Value *Ptr = EmitScalarExpr(BaseExpr); |
| 2253 | QualType PtrTy = BaseExpr->getType()->getPointeeType(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2254 | EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Ptr, PtrTy); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 2255 | BaseLV = MakeNaturalAlignAddrLValue(Ptr, PtrTy); |
| 2256 | } else |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2257 | BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess); |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 2258 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2259 | NamedDecl *ND = E->getMemberDecl(); |
| 2260 | if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2261 | LValue LV = EmitLValueForField(BaseLV, Field); |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2262 | setObjCGCLValueClass(getContext(), E, LV); |
| 2263 | return LV; |
| 2264 | } |
| 2265 | |
Anders Carlsson | 5bbdc9f | 2009-11-07 23:16:50 +0000 | [diff] [blame] | 2266 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 2267 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2268 | |
| 2269 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 2270 | return EmitFunctionDeclLValue(*this, E, FD); |
| 2271 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2272 | llvm_unreachable("Unhandled member declaration!"); |
Eli Friedman | a62f3e1 | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 2273 | } |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 2274 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2275 | LValue CodeGenFunction::EmitLValueForField(LValue base, |
| 2276 | const FieldDecl *field) { |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 2277 | if (field->isBitField()) { |
| 2278 | const CGRecordLayout &RL = |
| 2279 | CGM.getTypes().getCGRecordLayout(field->getParent()); |
| 2280 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(field); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 2281 | llvm::Value *Addr = base.getAddress(); |
| 2282 | unsigned Idx = RL.getLLVMFieldNo(field); |
| 2283 | if (Idx != 0) |
| 2284 | // For structs, we GEP to the field that the record layout suggests. |
| 2285 | Addr = Builder.CreateStructGEP(Addr, Idx, field->getName()); |
| 2286 | // Get the access type. |
| 2287 | llvm::Type *PtrTy = llvm::Type::getIntNPtrTy( |
| 2288 | getLLVMContext(), Info.StorageSize, |
| 2289 | CGM.getContext().getTargetAddressSpace(base.getType())); |
| 2290 | if (Addr->getType() != PtrTy) |
| 2291 | Addr = Builder.CreateBitCast(Addr, PtrTy); |
| 2292 | |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 2293 | QualType fieldType = |
| 2294 | field->getType().withCVRQualifiers(base.getVRQualifiers()); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 2295 | return LValue::MakeBitfield(Addr, Info, fieldType, base.getAlignment()); |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 2296 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2297 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2298 | const RecordDecl *rec = field->getParent(); |
| 2299 | QualType type = field->getType(); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2300 | CharUnits alignment = getContext().getDeclAlign(field); |
Eli Friedman | 133e804 | 2008-05-29 11:33:25 +0000 | [diff] [blame] | 2301 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2302 | // FIXME: It should be impossible to have an LValue without alignment for a |
| 2303 | // complete type. |
| 2304 | if (!base.getAlignment().isZero()) |
| 2305 | alignment = std::min(alignment, base.getAlignment()); |
| 2306 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2307 | bool mayAlias = rec->hasAttr<MayAliasAttr>(); |
| 2308 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2309 | llvm::Value *addr = base.getAddress(); |
| 2310 | unsigned cvr = base.getVRQualifiers(); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2311 | if (rec->isUnion()) { |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 2312 | // For unions, there is no pointer adjustment. |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2313 | assert(!type->isReferenceType() && "union has reference member"); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2314 | } else { |
| 2315 | // For structs, we GEP to the field that the record layout suggests. |
| 2316 | unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 2317 | addr = Builder.CreateStructGEP(addr, idx, field->getName()); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2318 | |
| 2319 | // If this is a reference field, load the reference right now. |
| 2320 | if (const ReferenceType *refType = type->getAs<ReferenceType>()) { |
| 2321 | llvm::LoadInst *load = Builder.CreateLoad(addr, "ref"); |
| 2322 | if (cvr & Qualifiers::Volatile) load->setVolatile(true); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2323 | load->setAlignment(alignment.getQuantity()); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2324 | |
| 2325 | if (CGM.shouldUseTBAA()) { |
| 2326 | llvm::MDNode *tbaa; |
| 2327 | if (mayAlias) |
| 2328 | tbaa = CGM.getTBAAInfo(getContext().CharTy); |
| 2329 | else |
| 2330 | tbaa = CGM.getTBAAInfo(type); |
| 2331 | CGM.DecorateInstruction(load, tbaa); |
| 2332 | } |
| 2333 | |
| 2334 | addr = load; |
| 2335 | mayAlias = false; |
| 2336 | type = refType->getPointeeType(); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2337 | if (type->isIncompleteType()) |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2338 | alignment = CharUnits(); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2339 | else |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2340 | alignment = getContext().getTypeAlignInChars(type); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2341 | cvr = 0; // qualifiers don't recursively apply to referencee |
| 2342 | } |
Devang Patel | ed93c3c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 2343 | } |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Make sure that the address is pointing to the right type. This is critical |
| 2346 | // for both unions and structs. A union needs a bitcast, a struct element |
| 2347 | // will need a bitcast if the LLVM type laid out doesn't match the desired |
| 2348 | // type. |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 2349 | addr = EmitBitCastOfLValueToProperType(*this, addr, |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2350 | CGM.getTypes().ConvertTypeForMem(type), |
| 2351 | field->getName()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2352 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2353 | if (field->hasAttr<AnnotateAttr>()) |
| 2354 | addr = EmitFieldAnnotations(field, addr); |
| 2355 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2356 | LValue LV = MakeAddrLValue(addr, type, alignment); |
| 2357 | LV.getQuals().addCVRQualifiers(cvr); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2358 | |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 2359 | // __weak attribute on a field is ignored. |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2360 | if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak) |
| 2361 | LV.getQuals().removeObjCGCAttr(); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 2362 | |
| 2363 | // Fields of may_alias structs act like 'char' for TBAA purposes. |
| 2364 | // FIXME: this should get propagated down through anonymous structs |
| 2365 | // and unions. |
| 2366 | if (mayAlias && LV.getTBAAInfo()) |
| 2367 | LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy)); |
| 2368 | |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2369 | return LV; |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2372 | LValue |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2373 | CodeGenFunction::EmitLValueForFieldInitialization(LValue Base, |
| 2374 | const FieldDecl *Field) { |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2375 | QualType FieldType = Field->getType(); |
| 2376 | |
| 2377 | if (!FieldType->isReferenceType()) |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2378 | return EmitLValueForField(Base, Field); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2379 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 2380 | const CGRecordLayout &RL = |
| 2381 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
| 2382 | unsigned idx = RL.getLLVMFieldNo(Field); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2383 | llvm::Value *V = Builder.CreateStructGEP(Base.getAddress(), idx); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2384 | assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); |
| 2385 | |
Chris Lattner | d7c5935 | 2011-07-10 05:53:24 +0000 | [diff] [blame] | 2386 | // Make sure that the address is pointing to the right type. This is critical |
| 2387 | // for both unions and structs. A union needs a bitcast, a struct element |
| 2388 | // will need a bitcast if the LLVM type laid out doesn't match the desired |
| 2389 | // type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2390 | llvm::Type *llvmType = ConvertTypeForMem(FieldType); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2391 | V = EmitBitCastOfLValueToProperType(*this, V, llvmType, Field->getName()); |
| 2392 | |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2393 | CharUnits Alignment = getContext().getDeclAlign(Field); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2394 | |
| 2395 | // FIXME: It should be impossible to have an LValue without alignment for a |
| 2396 | // complete type. |
| 2397 | if (!Base.getAlignment().isZero()) |
| 2398 | Alignment = std::min(Alignment, Base.getAlignment()); |
| 2399 | |
Daniel Dunbar | 5c81637 | 2010-08-21 04:20:22 +0000 | [diff] [blame] | 2400 | return MakeAddrLValue(V, FieldType, Alignment); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 2403 | LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ |
Richard Smith | 2d988f0 | 2011-11-22 22:48:32 +0000 | [diff] [blame] | 2404 | if (E->isFileScope()) { |
| 2405 | llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E); |
| 2406 | return MakeAddrLValue(GlobalPtr, E->getType()); |
| 2407 | } |
Fariborz Jahanian | 5d53fcd | 2012-06-07 18:15:55 +0000 | [diff] [blame] | 2408 | if (E->getType()->isVariablyModifiedType()) |
| 2409 | // make sure to emit the VLA size. |
| 2410 | EmitVariablyModifiedType(E->getType()); |
Fariborz Jahanian | bbc5bbf | 2012-06-07 17:07:15 +0000 | [diff] [blame] | 2411 | |
Daniel Dunbar | 27bacaf | 2010-02-16 19:43:39 +0000 | [diff] [blame] | 2412 | llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 2413 | const Expr *InitExpr = E->getInitializer(); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2414 | LValue Result = MakeAddrLValue(DeclPtr, E->getType()); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 2415 | |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 2416 | EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), |
| 2417 | /*Init*/ true); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 2418 | |
| 2419 | return Result; |
| 2420 | } |
| 2421 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 2422 | LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) { |
| 2423 | if (!E->isGLValue()) |
| 2424 | // Initializing an aggregate temporary in C++11: T{...}. |
| 2425 | return EmitAggExprToLValue(E); |
| 2426 | |
| 2427 | // An lvalue initializer list must be initializing a reference. |
| 2428 | assert(E->getNumInits() == 1 && "reference init with multiple values"); |
| 2429 | return EmitLValue(E->getInit(0)); |
| 2430 | } |
| 2431 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2432 | LValue CodeGenFunction:: |
| 2433 | EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { |
| 2434 | if (!expr->isGLValue()) { |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2435 | // ?: here should be an aggregate. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2436 | assert((hasAggregateLLVMType(expr->getType()) && |
| 2437 | !expr->getType()->isAnyComplexType()) && |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2438 | "Unexpected conditional operator!"); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2439 | return EmitAggExprToLValue(expr); |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 2440 | } |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 2441 | |
Eli Friedman | 5995489 | 2012-01-25 05:04:17 +0000 | [diff] [blame] | 2442 | OpaqueValueMapping binding(*this, expr); |
| 2443 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2444 | const Expr *condExpr = expr->getCond(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2445 | bool CondExprBool; |
| 2446 | if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2447 | const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2448 | if (!CondExprBool) std::swap(live, dead); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2449 | |
| 2450 | if (!ContainsLabel(dead)) |
| 2451 | return EmitLValue(live); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2454 | llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true"); |
| 2455 | llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false"); |
| 2456 | llvm::BasicBlock *contBlock = createBasicBlock("cond.end"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2457 | |
| 2458 | ConditionalEvaluation eval(*this); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2459 | EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2462 | EmitBlock(lhsBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2463 | eval.begin(*this); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2464 | LValue lhs = EmitLValue(expr->getTrueExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2465 | eval.end(*this); |
| 2466 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2467 | if (!lhs.isSimple()) |
| 2468 | return EmitUnsupportedLValue(expr, "conditional operator"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2469 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2470 | lhsBlock = Builder.GetInsertBlock(); |
| 2471 | Builder.CreateBr(contBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2474 | EmitBlock(rhsBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2475 | eval.begin(*this); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2476 | LValue rhs = EmitLValue(expr->getFalseExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2477 | eval.end(*this); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2478 | if (!rhs.isSimple()) |
| 2479 | return EmitUnsupportedLValue(expr, "conditional operator"); |
| 2480 | rhsBlock = Builder.GetInsertBlock(); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2481 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2482 | EmitBlock(contBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2483 | |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2484 | llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2, |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 2485 | "cond-lvalue"); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2486 | phi->addIncoming(lhs.getAddress(), lhsBlock); |
| 2487 | phi->addIncoming(rhs.getAddress(), rhsBlock); |
| 2488 | return MakeAddrLValue(phi, expr->getType()); |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 2491 | /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference |
| 2492 | /// type. If the cast is to a reference, we can have the usual lvalue result, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2493 | /// otherwise if a cast is needed by the code generator in an lvalue context, |
| 2494 | /// then it must mean that we need the address of an aggregate in order to |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 2495 | /// access one of its members. This can happen for all the reasons that casts |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2496 | /// are permitted with aggregate result, including noop aggregate casts, and |
| 2497 | /// cast from scalar to union. |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 2498 | LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2499 | switch (E->getCastKind()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2500 | case CK_ToVoid: |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 2501 | return EmitUnsupportedLValue(E, "unexpected cast lvalue"); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2502 | |
| 2503 | case CK_Dependent: |
| 2504 | llvm_unreachable("dependent cast kind in IR gen!"); |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2505 | |
| 2506 | case CK_BuiltinFnToFnPtr: |
| 2507 | llvm_unreachable("builtin functions are handled elsewhere"); |
| 2508 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2509 | // These two casts are currently treated as no-ops, although they could |
| 2510 | // potentially be real operations depending on the target's ABI. |
| 2511 | case CK_NonAtomicToAtomic: |
| 2512 | case CK_AtomicToNonAtomic: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2513 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2514 | case CK_NoOp: |
Douglas Gregor | 21d3fca | 2011-01-27 23:22:05 +0000 | [diff] [blame] | 2515 | case CK_LValueToRValue: |
| 2516 | if (!E->getSubExpr()->Classify(getContext()).isPRValue() |
| 2517 | || E->getType()->isRecordType()) |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 2518 | return EmitLValue(E->getSubExpr()); |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 2519 | // Fall through to synthesize a temporary. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2520 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2521 | case CK_BitCast: |
| 2522 | case CK_ArrayToPointerDecay: |
| 2523 | case CK_FunctionToPointerDecay: |
| 2524 | case CK_NullToMemberPointer: |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2525 | case CK_NullToPointer: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2526 | case CK_IntegralToPointer: |
| 2527 | case CK_PointerToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2528 | case CK_PointerToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2529 | case CK_VectorSplat: |
| 2530 | case CK_IntegralCast: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2531 | case CK_IntegralToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2532 | case CK_IntegralToFloating: |
| 2533 | case CK_FloatingToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2534 | case CK_FloatingToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2535 | case CK_FloatingCast: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 2536 | case CK_FloatingRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2537 | case CK_FloatingComplexToReal: |
| 2538 | case CK_FloatingComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 2539 | case CK_FloatingComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2540 | case CK_FloatingComplexToIntegralComplex: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 2541 | case CK_IntegralRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2542 | case CK_IntegralComplexToReal: |
| 2543 | case CK_IntegralComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 2544 | case CK_IntegralComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2545 | case CK_IntegralComplexToFloatingComplex: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2546 | case CK_DerivedToBaseMemberPointer: |
| 2547 | case CK_BaseToDerivedMemberPointer: |
| 2548 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 2549 | case CK_ReinterpretMemberPointer: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2550 | case CK_AnyPointerToBlockPointerCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2551 | case CK_ARCProduceObject: |
| 2552 | case CK_ARCConsumeObject: |
| 2553 | case CK_ARCReclaimReturnedObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 2554 | case CK_ARCExtendBlockObject: |
| 2555 | case CK_CopyAndAutoreleaseBlockObject: { |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 2556 | // These casts only produce lvalues when we're binding a reference to a |
| 2557 | // temporary realized from a (converted) pure rvalue. Emit the expression |
| 2558 | // as a value, copy it into a temporary, and return an lvalue referring to |
| 2559 | // that temporary. |
| 2560 | llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp"); |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 2561 | EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2562 | return MakeAddrLValue(V, E->getType()); |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 2563 | } |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 2564 | |
Anders Carlsson | 8a01a75 | 2011-04-11 02:03:26 +0000 | [diff] [blame] | 2565 | case CK_Dynamic: { |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2566 | LValue LV = EmitLValue(E->getSubExpr()); |
| 2567 | llvm::Value *V = LV.getAddress(); |
| 2568 | const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2569 | return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType()); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2572 | case CK_ConstructorConversion: |
| 2573 | case CK_UserDefinedConversion: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2574 | case CK_CPointerToObjCPointerCast: |
| 2575 | case CK_BlockPointerToObjCPointerCast: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 2576 | return EmitLValue(E->getSubExpr()); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2577 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2578 | case CK_UncheckedDerivedToBase: |
| 2579 | case CK_DerivedToBase: { |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2580 | const RecordType *DerivedClassTy = |
| 2581 | E->getSubExpr()->getType()->getAs<RecordType>(); |
| 2582 | CXXRecordDecl *DerivedClassDecl = |
| 2583 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2584 | |
| 2585 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 2586 | llvm::Value *This = LV.getAddress(); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Perform the derived-to-base conversion |
| 2589 | llvm::Value *Base = |
Fariborz Jahanian | 64cda8b | 2010-06-17 23:00:29 +0000 | [diff] [blame] | 2590 | GetAddressOfBaseClass(This, DerivedClassDecl, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2591 | E->path_begin(), E->path_end(), |
| 2592 | /*NullCheckValue=*/false); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2593 | |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2594 | return MakeAddrLValue(Base, E->getType()); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2595 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2596 | case CK_ToUnion: |
Daniel Dunbar | 9c4e465 | 2010-02-05 20:02:42 +0000 | [diff] [blame] | 2597 | return EmitAggExprToLValue(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2598 | case CK_BaseToDerived: { |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 2599 | const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); |
| 2600 | CXXRecordDecl *DerivedClassDecl = |
| 2601 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
| 2602 | |
| 2603 | LValue LV = EmitLValue(E->getSubExpr()); |
| 2604 | |
| 2605 | // Perform the base-to-derived conversion |
| 2606 | llvm::Value *Derived = |
Anders Carlsson | 8a64c1c | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 2607 | GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2608 | E->path_begin(), E->path_end(), |
| 2609 | /*NullCheckValue=*/false); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 2610 | |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2611 | return MakeAddrLValue(Derived, E->getType()); |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 2612 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2613 | case CK_LValueBitCast: { |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 2614 | // This must be a reinterpret_cast (or c-style equivalent). |
| 2615 | const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 2616 | |
| 2617 | LValue LV = EmitLValue(E->getSubExpr()); |
| 2618 | llvm::Value *V = Builder.CreateBitCast(LV.getAddress(), |
| 2619 | ConvertType(CE->getTypeAsWritten())); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2620 | return MakeAddrLValue(V, E->getType()); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 2621 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2622 | case CK_ObjCObjectLValueCast: { |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2623 | LValue LV = EmitLValue(E->getSubExpr()); |
| 2624 | QualType ToType = getContext().getLValueReferenceType(E->getType()); |
| 2625 | llvm::Value *V = Builder.CreateBitCast(LV.getAddress(), |
| 2626 | ConvertType(ToType)); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2627 | return MakeAddrLValue(V, E->getType()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2628 | } |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 2629 | } |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 2630 | |
| 2631 | llvm_unreachable("Unhandled lvalue cast kind?"); |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Fariborz Jahanian | e4d94ce | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 2634 | LValue CodeGenFunction::EmitNullInitializationLValue( |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2635 | const CXXScalarValueInitExpr *E) { |
Fariborz Jahanian | e4d94ce | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 2636 | QualType Ty = E->getType(); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2637 | LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty); |
Anders Carlsson | c0964b6 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 2638 | EmitNullInitialization(LV.getAddress(), Ty); |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 2639 | return LV; |
Fariborz Jahanian | e4d94ce | 2009-10-20 23:29:04 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 2642 | LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) { |
John McCall | 9a54961 | 2011-11-08 22:54:08 +0000 | [diff] [blame] | 2643 | assert(OpaqueValueMappingData::shouldBindAsLValue(e)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2644 | return getOpaqueLValueMapping(e); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2647 | LValue CodeGenFunction::EmitMaterializeTemporaryExpr( |
| 2648 | const MaterializeTemporaryExpr *E) { |
John McCall | 17054bd6 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 2649 | RValue RV = EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); |
Douglas Gregor | d410c08 | 2011-06-21 18:20:46 +0000 | [diff] [blame] | 2650 | return MakeAddrLValue(RV.getScalarVal(), E->getType()); |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2653 | RValue CodeGenFunction::EmitRValueForField(LValue LV, |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2654 | const FieldDecl *FD) { |
| 2655 | QualType FT = FD->getType(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2656 | LValue FieldLV = EmitLValueForField(LV, FD); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2657 | if (FT->isAnyComplexType()) |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2658 | return RValue::getComplex( |
| 2659 | LoadComplexFromAddr(FieldLV.getAddress(), |
| 2660 | FieldLV.isVolatileQualified())); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2661 | else if (CodeGenFunction::hasAggregateLLVMType(FT)) |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2662 | return FieldLV.asAggregateRValue(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2663 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2664 | return EmitLoadOfLValue(FieldLV); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2665 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2666 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 2667 | //===--------------------------------------------------------------------===// |
| 2668 | // Expression Emission |
| 2669 | //===--------------------------------------------------------------------===// |
| 2670 | |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2671 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, |
| 2672 | ReturnValueSlot ReturnValue) { |
Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2673 | if (CGDebugInfo *DI = getDebugInfo()) |
| 2674 | DI->EmitLocation(Builder, E->getLocStart()); |
Devang Patel | d3a6b0f | 2011-03-04 18:54:42 +0000 | [diff] [blame] | 2675 | |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2676 | // Builtins never have block type. |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 2677 | if (E->getCallee()->getType()->isBlockPointerType()) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2678 | return EmitBlockCallExpr(E, ReturnValue); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 2679 | |
Anders Carlsson | e5fd6f2 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 2680 | if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E)) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2681 | return EmitCXXMemberCallExpr(CE, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2682 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 2683 | if (const CUDAKernelCallExpr *CE = dyn_cast<CUDAKernelCallExpr>(E)) |
| 2684 | return EmitCUDAKernelCallExpr(CE, ReturnValue); |
| 2685 | |
Douglas Gregor | e0e9630 | 2011-09-06 21:41:04 +0000 | [diff] [blame] | 2686 | const Decl *TargetDecl = E->getCalleeDecl(); |
| 2687 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) { |
| 2688 | if (unsigned builtinID = FD->getBuiltinID()) |
| 2689 | return EmitBuiltinExpr(FD, builtinID, E); |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2692 | if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E)) |
Anders Carlsson | 4034a95 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 2693 | if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl)) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 2694 | return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2695 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2696 | if (const CXXPseudoDestructorExpr *PseudoDtor |
| 2697 | = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) { |
| 2698 | QualType DestroyedType = PseudoDtor->getDestroyedType(); |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2699 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2700 | DestroyedType->isObjCLifetimeType() && |
| 2701 | (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong || |
| 2702 | DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) { |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 2703 | // Automatic Reference Counting: |
| 2704 | // If the pseudo-expression names a retainable object with weak or |
| 2705 | // strong lifetime, the object shall be released. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2706 | Expr *BaseExpr = PseudoDtor->getBase(); |
| 2707 | llvm::Value *BaseValue = NULL; |
| 2708 | Qualifiers BaseQuals; |
| 2709 | |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 2710 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2711 | if (PseudoDtor->isArrow()) { |
| 2712 | BaseValue = EmitScalarExpr(BaseExpr); |
| 2713 | const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>(); |
| 2714 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
| 2715 | } else { |
| 2716 | LValue BaseLV = EmitLValue(BaseExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2717 | BaseValue = BaseLV.getAddress(); |
| 2718 | QualType BaseTy = BaseExpr->getType(); |
| 2719 | BaseQuals = BaseTy.getQualifiers(); |
| 2720 | } |
| 2721 | |
| 2722 | switch (PseudoDtor->getDestroyedType().getObjCLifetime()) { |
| 2723 | case Qualifiers::OCL_None: |
| 2724 | case Qualifiers::OCL_ExplicitNone: |
| 2725 | case Qualifiers::OCL_Autoreleasing: |
| 2726 | break; |
| 2727 | |
| 2728 | case Qualifiers::OCL_Strong: |
| 2729 | EmitARCRelease(Builder.CreateLoad(BaseValue, |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 2730 | PseudoDtor->getDestroyedType().isVolatileQualified()), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2731 | /*precise*/ true); |
| 2732 | break; |
| 2733 | |
| 2734 | case Qualifiers::OCL_Weak: |
| 2735 | EmitARCDestroyWeak(BaseValue); |
| 2736 | break; |
| 2737 | } |
| 2738 | } else { |
| 2739 | // C++ [expr.pseudo]p1: |
| 2740 | // The result shall only be used as the operand for the function call |
| 2741 | // operator (), and the result of such a call has type void. The only |
| 2742 | // effect is the evaluation of the postfix-expression before the dot or |
| 2743 | // arrow. |
| 2744 | EmitScalarExpr(E->getCallee()); |
| 2745 | } |
| 2746 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2747 | return RValue::get(0); |
| 2748 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2750 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2751 | return EmitCall(E->getCallee()->getType(), Callee, ReturnValue, |
Anders Carlsson | 3a9463b | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 2752 | E->arg_begin(), E->arg_end(), TargetDecl); |
Chris Lattner | 9e47ead | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 2755 | LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 2756 | // Comma expressions just emit their LHS then their RHS as an l-value. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2757 | if (E->getOpcode() == BO_Comma) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2758 | EmitIgnoredExpr(E->getLHS()); |
Eli Friedman | 5445f6e | 2009-12-07 20:18:11 +0000 | [diff] [blame] | 2759 | EnsureInsertPoint(); |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 2760 | return EmitLValue(E->getRHS()); |
| 2761 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2762 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2763 | if (E->getOpcode() == BO_PtrMemD || |
| 2764 | E->getOpcode() == BO_PtrMemI) |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2765 | return EmitPointerToDataMemberBinaryExpr(E); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 2766 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2767 | assert(E->getOpcode() == BO_Assign && "unexpected binary l-value"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2768 | |
| 2769 | // Note that in all of these cases, __block variables need the RHS |
| 2770 | // evaluated first just in case the variable gets moved by the RHS. |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 2771 | |
Anders Carlsson | 0999aaf | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 2772 | if (!hasAggregateLLVMType(E->getType())) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2773 | switch (E->getLHS()->getType().getObjCLifetime()) { |
| 2774 | case Qualifiers::OCL_Strong: |
| 2775 | return EmitARCStoreStrong(E, /*ignored*/ false).first; |
| 2776 | |
| 2777 | case Qualifiers::OCL_Autoreleasing: |
| 2778 | return EmitARCStoreAutoreleasing(E).first; |
| 2779 | |
| 2780 | // No reason to do any of these differently. |
| 2781 | case Qualifiers::OCL_None: |
| 2782 | case Qualifiers::OCL_ExplicitNone: |
| 2783 | case Qualifiers::OCL_Weak: |
| 2784 | break; |
| 2785 | } |
| 2786 | |
John McCall | d0a3001 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 2787 | RValue RV = EmitAnyExpr(E->getRHS()); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2788 | LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2789 | EmitStoreThroughLValue(RV, LV); |
Anders Carlsson | 0999aaf | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 2790 | return LV; |
| 2791 | } |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 2792 | |
| 2793 | if (E->getType()->isAnyComplexType()) |
| 2794 | return EmitComplexAssignmentLValue(E); |
| 2795 | |
Daniel Dunbar | d0bc7b9 | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 2796 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 2799 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 2800 | RValue RV = EmitCallExpr(E); |
Anders Carlsson | 4ae70ff | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2802 | if (!RV.isScalar()) |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2803 | return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2804 | |
| 2805 | assert(E->getCallReturnType()->isReferenceType() && |
| 2806 | "Can't have a scalar return unless the return type is a " |
| 2807 | "reference type!"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2808 | |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2809 | return MakeAddrLValue(RV.getScalarVal(), E->getType()); |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 2812 | LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { |
| 2813 | // FIXME: This shouldn't require another copy. |
Daniel Dunbar | d0bc7b9 | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 2814 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 2817 | LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 2818 | assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() |
| 2819 | && "binding l-value to type which needs a temporary"); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 2820 | AggValueSlot Slot = CreateAggTemp(E->getType()); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2821 | EmitCXXConstructExpr(E, Slot); |
| 2822 | return MakeAddrLValue(Slot.getAddr(), E->getType()); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 2825 | LValue |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2826 | CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2827 | return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType()); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2830 | llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 2831 | return CGM.GetAddrOfUuidDescriptor(E); |
| 2832 | } |
| 2833 | |
| 2834 | LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) { |
| 2835 | return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType()); |
| 2836 | } |
| 2837 | |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2838 | LValue |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 2839 | CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 2840 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 2841 | Slot.setExternallyDestructed(); |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 2842 | EmitAggExpr(E->getSubExpr(), Slot); |
Peter Collingbourne | 702b284 | 2011-11-27 22:09:22 +0000 | [diff] [blame] | 2843 | EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr()); |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 2844 | return MakeAddrLValue(Slot.getAddr(), E->getType()); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 2847 | LValue |
| 2848 | CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) { |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 2849 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 2850 | EmitLambdaExpr(E, Slot); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 2851 | return MakeAddrLValue(Slot.getAddr(), E->getType()); |
| 2852 | } |
| 2853 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 2854 | LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 2855 | RValue RV = EmitObjCMessageExpr(E); |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 2856 | |
| 2857 | if (!RV.isScalar()) |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2858 | return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 2859 | |
| 2860 | assert(E->getMethodDecl()->getResultType()->isReferenceType() && |
| 2861 | "Can't have a scalar return unless the return type is a " |
| 2862 | "reference type!"); |
| 2863 | |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2864 | return MakeAddrLValue(RV.getScalarVal(), E->getType()); |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 2867 | LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { |
| 2868 | llvm::Value *V = |
| 2869 | CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2870 | return MakeAddrLValue(V, E->getType()); |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2873 | llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2874 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2875 | return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2878 | LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, |
| 2879 | llvm::Value *BaseValue, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2880 | const ObjCIvarDecl *Ivar, |
| 2881 | unsigned CVRQualifiers) { |
Chris Lattner | c4688d2 | 2009-04-17 17:44:48 +0000 | [diff] [blame] | 2882 | return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, |
Daniel Dunbar | 9ebf951 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2883 | Ivar, CVRQualifiers); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2887 | // FIXME: A lot of the code below could be shared with EmitMemberExpr. |
| 2888 | llvm::Value *BaseValue = 0; |
| 2889 | const Expr *BaseExpr = E->getBase(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2890 | Qualifiers BaseQuals; |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2891 | QualType ObjectTy; |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2892 | if (E->isArrow()) { |
| 2893 | BaseValue = EmitScalarExpr(BaseExpr); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2894 | ObjectTy = BaseExpr->getType()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2895 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2896 | } else { |
| 2897 | LValue BaseLV = EmitLValue(BaseExpr); |
| 2898 | // FIXME: this isn't right for bitfields. |
| 2899 | BaseValue = BaseLV.getAddress(); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 2900 | ObjectTy = BaseExpr->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2901 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 2902 | } |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 2903 | |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 2904 | LValue LV = |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2905 | EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), |
| 2906 | BaseQuals.getCVRQualifiers()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 2907 | setObjCGCLValueClass(getContext(), E, LV); |
| 2908 | return LV; |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2911 | LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2912 | // Can only get l-value for message expression returning aggregate type |
| 2913 | RValue RV = EmitAnyExprToTemp(E); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2914 | return MakeAddrLValue(RV.getAggregateAddr(), E->getType()); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 2917 | RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee, |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2918 | ReturnValueSlot ReturnValue, |
Anders Carlsson | 3a9463b | 2009-05-27 01:22:39 +0000 | [diff] [blame] | 2919 | CallExpr::const_arg_iterator ArgBeg, |
| 2920 | CallExpr::const_arg_iterator ArgEnd, |
| 2921 | const Decl *TargetDecl) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2922 | // Get the actual function type. The callee type will always be a pointer to |
| 2923 | // function type or a block pointer type. |
| 2924 | assert(CalleeType->isFunctionPointerType() && |
Anders Carlsson | d8db853 | 2009-04-07 18:53:02 +0000 | [diff] [blame] | 2925 | "Call must have function pointer type!"); |
| 2926 | |
John McCall | 6fd4c23 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 2927 | CalleeType = getContext().getCanonicalType(CalleeType); |
| 2928 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2929 | const FunctionType *FnType |
| 2930 | = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType()); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2931 | |
| 2932 | CallArgList Args; |
John McCall | 6fd4c23 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 2933 | EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2934 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2935 | const CGFunctionInfo &FnInfo = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2936 | CGM.getTypes().arrangeFreeFunctionCall(Args, FnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2937 | |
| 2938 | // C99 6.5.2.2p6: |
| 2939 | // If the expression that denotes the called function has a type |
| 2940 | // that does not include a prototype, [the default argument |
| 2941 | // promotions are performed]. If the number of arguments does not |
| 2942 | // equal the number of parameters, the behavior is undefined. If |
| 2943 | // the function is defined with a type that includes a prototype, |
| 2944 | // and either the prototype ends with an ellipsis (, ...) or the |
| 2945 | // types of the arguments after promotion are not compatible with |
| 2946 | // the types of the parameters, the behavior is undefined. If the |
| 2947 | // function is defined with a type that does not include a |
| 2948 | // prototype, and the types of the arguments after promotion are |
| 2949 | // not compatible with those of the parameters after promotion, |
| 2950 | // the behavior is undefined [except in some trivial cases]. |
| 2951 | // That is, in the general case, we should assume that a call |
| 2952 | // through an unprototyped function type works like a *non-variadic* |
| 2953 | // call. The way we make this work is to cast to the exact type |
| 2954 | // of the promoted arguments. |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 2955 | if (isa<FunctionNoProtoType>(FnType)) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2956 | llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2957 | CalleeTy = CalleeTy->getPointerTo(); |
| 2958 | Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast"); |
| 2959 | } |
| 2960 | |
| 2961 | return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl); |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 2962 | } |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2964 | LValue CodeGenFunction:: |
| 2965 | EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { |
Eli Friedman | 928a567 | 2009-11-18 05:01:17 +0000 | [diff] [blame] | 2966 | llvm::Value *BaseV; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2967 | if (E->getOpcode() == BO_PtrMemI) |
Eli Friedman | 928a567 | 2009-11-18 05:01:17 +0000 | [diff] [blame] | 2968 | BaseV = EmitScalarExpr(E->getLHS()); |
| 2969 | else |
| 2970 | BaseV = EmitLValue(E->getLHS()).getAddress(); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2971 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 2972 | llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); |
| 2973 | |
| 2974 | const MemberPointerType *MPT |
| 2975 | = E->getRHS()->getType()->getAs<MemberPointerType>(); |
| 2976 | |
| 2977 | llvm::Value *AddV = |
| 2978 | CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT); |
| 2979 | |
| 2980 | return MakeAddrLValue(AddV, MPT->getPointeeType()); |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 2981 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2982 | |
| 2983 | static void |
| 2984 | EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest, |
| 2985 | llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2, |
| 2986 | uint64_t Size, unsigned Align, llvm::AtomicOrdering Order) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2987 | llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add; |
| 2988 | llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0; |
| 2989 | |
| 2990 | switch (E->getOp()) { |
| 2991 | case AtomicExpr::AO__c11_atomic_init: |
| 2992 | llvm_unreachable("Already handled!"); |
| 2993 | |
| 2994 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 2995 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 2996 | case AtomicExpr::AO__atomic_compare_exchange: |
| 2997 | case AtomicExpr::AO__atomic_compare_exchange_n: { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2998 | // Note that cmpxchg only supports specifying one ordering and |
| 2999 | // doesn't support weak cmpxchg, at least at the moment. |
| 3000 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 3001 | LoadVal1->setAlignment(Align); |
| 3002 | llvm::LoadInst *LoadVal2 = CGF.Builder.CreateLoad(Val2); |
| 3003 | LoadVal2->setAlignment(Align); |
| 3004 | llvm::AtomicCmpXchgInst *CXI = |
| 3005 | CGF.Builder.CreateAtomicCmpXchg(Ptr, LoadVal1, LoadVal2, Order); |
| 3006 | CXI->setVolatile(E->isVolatile()); |
| 3007 | llvm::StoreInst *StoreVal1 = CGF.Builder.CreateStore(CXI, Val1); |
| 3008 | StoreVal1->setAlignment(Align); |
| 3009 | llvm::Value *Cmp = CGF.Builder.CreateICmpEQ(CXI, LoadVal1); |
| 3010 | CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType())); |
| 3011 | return; |
| 3012 | } |
| 3013 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3014 | case AtomicExpr::AO__c11_atomic_load: |
| 3015 | case AtomicExpr::AO__atomic_load_n: |
| 3016 | case AtomicExpr::AO__atomic_load: { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3017 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr); |
| 3018 | Load->setAtomic(Order); |
| 3019 | Load->setAlignment(Size); |
| 3020 | Load->setVolatile(E->isVolatile()); |
| 3021 | llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest); |
| 3022 | StoreDest->setAlignment(Align); |
| 3023 | return; |
| 3024 | } |
| 3025 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3026 | case AtomicExpr::AO__c11_atomic_store: |
| 3027 | case AtomicExpr::AO__atomic_store: |
| 3028 | case AtomicExpr::AO__atomic_store_n: { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3029 | assert(!Dest && "Store does not return a value"); |
| 3030 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 3031 | LoadVal1->setAlignment(Align); |
| 3032 | llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr); |
| 3033 | Store->setAtomic(Order); |
| 3034 | Store->setAlignment(Size); |
| 3035 | Store->setVolatile(E->isVolatile()); |
| 3036 | return; |
| 3037 | } |
| 3038 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3039 | case AtomicExpr::AO__c11_atomic_exchange: |
| 3040 | case AtomicExpr::AO__atomic_exchange_n: |
| 3041 | case AtomicExpr::AO__atomic_exchange: |
| 3042 | Op = llvm::AtomicRMWInst::Xchg; |
| 3043 | break; |
| 3044 | |
| 3045 | case AtomicExpr::AO__atomic_add_fetch: |
| 3046 | PostOp = llvm::Instruction::Add; |
| 3047 | // Fall through. |
| 3048 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 3049 | case AtomicExpr::AO__atomic_fetch_add: |
| 3050 | Op = llvm::AtomicRMWInst::Add; |
| 3051 | break; |
| 3052 | |
| 3053 | case AtomicExpr::AO__atomic_sub_fetch: |
| 3054 | PostOp = llvm::Instruction::Sub; |
| 3055 | // Fall through. |
| 3056 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 3057 | case AtomicExpr::AO__atomic_fetch_sub: |
| 3058 | Op = llvm::AtomicRMWInst::Sub; |
| 3059 | break; |
| 3060 | |
| 3061 | case AtomicExpr::AO__atomic_and_fetch: |
| 3062 | PostOp = llvm::Instruction::And; |
| 3063 | // Fall through. |
| 3064 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 3065 | case AtomicExpr::AO__atomic_fetch_and: |
| 3066 | Op = llvm::AtomicRMWInst::And; |
| 3067 | break; |
| 3068 | |
| 3069 | case AtomicExpr::AO__atomic_or_fetch: |
| 3070 | PostOp = llvm::Instruction::Or; |
| 3071 | // Fall through. |
| 3072 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 3073 | case AtomicExpr::AO__atomic_fetch_or: |
| 3074 | Op = llvm::AtomicRMWInst::Or; |
| 3075 | break; |
| 3076 | |
| 3077 | case AtomicExpr::AO__atomic_xor_fetch: |
| 3078 | PostOp = llvm::Instruction::Xor; |
| 3079 | // Fall through. |
| 3080 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 3081 | case AtomicExpr::AO__atomic_fetch_xor: |
| 3082 | Op = llvm::AtomicRMWInst::Xor; |
| 3083 | break; |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3084 | |
| 3085 | case AtomicExpr::AO__atomic_nand_fetch: |
| 3086 | PostOp = llvm::Instruction::And; |
| 3087 | // Fall through. |
| 3088 | case AtomicExpr::AO__atomic_fetch_nand: |
| 3089 | Op = llvm::AtomicRMWInst::Nand; |
| 3090 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3091 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3092 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3093 | llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1); |
| 3094 | LoadVal1->setAlignment(Align); |
| 3095 | llvm::AtomicRMWInst *RMWI = |
| 3096 | CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order); |
| 3097 | RMWI->setVolatile(E->isVolatile()); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3098 | |
| 3099 | // For __atomic_*_fetch operations, perform the operation again to |
| 3100 | // determine the value which was written. |
| 3101 | llvm::Value *Result = RMWI; |
| 3102 | if (PostOp) |
| 3103 | Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1); |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3104 | if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch) |
| 3105 | Result = CGF.Builder.CreateNot(Result); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3106 | llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Result, Dest); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3107 | StoreDest->setAlignment(Align); |
| 3108 | } |
| 3109 | |
| 3110 | // This function emits any expression (scalar, complex, or aggregate) |
| 3111 | // into a temporary alloca. |
| 3112 | static llvm::Value * |
| 3113 | EmitValToTemp(CodeGenFunction &CGF, Expr *E) { |
| 3114 | llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp"); |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 3115 | CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(), |
| 3116 | /*Init*/ true); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3117 | return DeclPtr; |
| 3118 | } |
| 3119 | |
| 3120 | static RValue ConvertTempToRValue(CodeGenFunction &CGF, QualType Ty, |
| 3121 | llvm::Value *Dest) { |
| 3122 | if (Ty->isAnyComplexType()) |
| 3123 | return RValue::getComplex(CGF.LoadComplexFromAddr(Dest, false)); |
| 3124 | if (CGF.hasAggregateLLVMType(Ty)) |
| 3125 | return RValue::getAggregate(Dest); |
| 3126 | return RValue::get(CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(Dest, Ty))); |
| 3127 | } |
| 3128 | |
| 3129 | RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) { |
| 3130 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3131 | QualType MemTy = AtomicTy; |
| 3132 | if (const AtomicType *AT = AtomicTy->getAs<AtomicType>()) |
| 3133 | MemTy = AT->getValueType(); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3134 | CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy); |
| 3135 | uint64_t Size = sizeChars.getQuantity(); |
| 3136 | CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy); |
| 3137 | unsigned Align = alignChars.getQuantity(); |
Benjamin Kramer | 37196de | 2012-11-17 17:30:55 +0000 | [diff] [blame] | 3138 | unsigned MaxInlineWidthInBits = |
| 3139 | getContext().getTargetInfo().getMaxAtomicInlineWidth(); |
| 3140 | bool UseLibcall = (Size != Align || |
| 3141 | getContext().toBits(sizeChars) > MaxInlineWidthInBits); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3142 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3143 | llvm::Value *Ptr, *Order, *OrderFail = 0, *Val1 = 0, *Val2 = 0; |
| 3144 | Ptr = EmitScalarExpr(E->getPtr()); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3145 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3146 | if (E->getOp() == AtomicExpr::AO__c11_atomic_init) { |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3147 | assert(!Dest && "Init does not return a value"); |
David Chisnall | eb9496e | 2012-04-11 17:24:05 +0000 | [diff] [blame] | 3148 | if (!hasAggregateLLVMType(E->getVal1()->getType())) { |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 3149 | QualType PointeeType |
| 3150 | = E->getPtr()->getType()->getAs<PointerType>()->getPointeeType(); |
| 3151 | EmitScalarInit(EmitScalarExpr(E->getVal1()), |
| 3152 | LValue::MakeAddr(Ptr, PointeeType, alignChars, |
| 3153 | getContext())); |
David Chisnall | eb9496e | 2012-04-11 17:24:05 +0000 | [diff] [blame] | 3154 | } else if (E->getType()->isAnyComplexType()) { |
| 3155 | EmitComplexExprIntoAddr(E->getVal1(), Ptr, E->isVolatile()); |
| 3156 | } else { |
| 3157 | AggValueSlot Slot = AggValueSlot::forAddr(Ptr, alignChars, |
| 3158 | AtomicTy.getQualifiers(), |
| 3159 | AggValueSlot::IsNotDestructed, |
| 3160 | AggValueSlot::DoesNotNeedGCBarriers, |
| 3161 | AggValueSlot::IsNotAliased); |
| 3162 | EmitAggExpr(E->getVal1(), Slot); |
| 3163 | } |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3164 | return RValue::get(0); |
| 3165 | } |
| 3166 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3167 | Order = EmitScalarExpr(E->getOrder()); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3168 | |
| 3169 | switch (E->getOp()) { |
| 3170 | case AtomicExpr::AO__c11_atomic_init: |
| 3171 | llvm_unreachable("Already handled!"); |
| 3172 | |
| 3173 | case AtomicExpr::AO__c11_atomic_load: |
| 3174 | case AtomicExpr::AO__atomic_load_n: |
| 3175 | break; |
| 3176 | |
| 3177 | case AtomicExpr::AO__atomic_load: |
| 3178 | Dest = EmitScalarExpr(E->getVal1()); |
| 3179 | break; |
| 3180 | |
| 3181 | case AtomicExpr::AO__atomic_store: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3182 | Val1 = EmitScalarExpr(E->getVal1()); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3183 | break; |
| 3184 | |
| 3185 | case AtomicExpr::AO__atomic_exchange: |
| 3186 | Val1 = EmitScalarExpr(E->getVal1()); |
| 3187 | Dest = EmitScalarExpr(E->getVal2()); |
| 3188 | break; |
| 3189 | |
| 3190 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 3191 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 3192 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 3193 | case AtomicExpr::AO__atomic_compare_exchange: |
| 3194 | Val1 = EmitScalarExpr(E->getVal1()); |
| 3195 | if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange) |
| 3196 | Val2 = EmitScalarExpr(E->getVal2()); |
| 3197 | else |
| 3198 | Val2 = EmitValToTemp(*this, E->getVal2()); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3199 | OrderFail = EmitScalarExpr(E->getOrderFail()); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3200 | // Evaluate and discard the 'weak' argument. |
| 3201 | if (E->getNumSubExprs() == 6) |
| 3202 | EmitScalarExpr(E->getWeak()); |
| 3203 | break; |
| 3204 | |
| 3205 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 3206 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3207 | if (MemTy->isPointerType()) { |
| 3208 | // For pointer arithmetic, we're required to do a bit of math: |
| 3209 | // adding 1 to an int* is not the same as adding 1 to a uintptr_t. |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 3210 | // ... but only for the C11 builtins. The GNU builtins expect the |
| 3211 | // user to multiply by sizeof(T). |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3212 | QualType Val1Ty = E->getVal1()->getType(); |
| 3213 | llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1()); |
| 3214 | CharUnits PointeeIncAmt = |
| 3215 | getContext().getTypeSizeInChars(MemTy->getPointeeType()); |
| 3216 | Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt)); |
| 3217 | Val1 = CreateMemTemp(Val1Ty, ".atomictmp"); |
| 3218 | EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty)); |
| 3219 | break; |
| 3220 | } |
| 3221 | // Fall through. |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 3222 | case AtomicExpr::AO__atomic_fetch_add: |
| 3223 | case AtomicExpr::AO__atomic_fetch_sub: |
| 3224 | case AtomicExpr::AO__atomic_add_fetch: |
| 3225 | case AtomicExpr::AO__atomic_sub_fetch: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3226 | case AtomicExpr::AO__c11_atomic_store: |
| 3227 | case AtomicExpr::AO__c11_atomic_exchange: |
| 3228 | case AtomicExpr::AO__atomic_store_n: |
| 3229 | case AtomicExpr::AO__atomic_exchange_n: |
| 3230 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 3231 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 3232 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 3233 | case AtomicExpr::AO__atomic_fetch_and: |
| 3234 | case AtomicExpr::AO__atomic_fetch_or: |
| 3235 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3236 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3237 | case AtomicExpr::AO__atomic_and_fetch: |
| 3238 | case AtomicExpr::AO__atomic_or_fetch: |
| 3239 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3240 | case AtomicExpr::AO__atomic_nand_fetch: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3241 | Val1 = EmitValToTemp(*this, E->getVal1()); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3242 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3245 | if (!E->getType()->isVoidType() && !Dest) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3246 | Dest = CreateMemTemp(E->getType(), ".atomicdst"); |
| 3247 | |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3248 | // Use a library call. See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary . |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3249 | if (UseLibcall) { |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3250 | |
| 3251 | llvm::SmallVector<QualType, 5> Params; |
| 3252 | CallArgList Args; |
| 3253 | // Size is always the first parameter |
| 3254 | Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)), |
| 3255 | getContext().getSizeType()); |
| 3256 | // Atomic address is always the second parameter |
| 3257 | Args.add(RValue::get(EmitCastToVoidPtr(Ptr)), |
| 3258 | getContext().VoidPtrTy); |
| 3259 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3260 | const char* LibCallName; |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3261 | QualType RetTy = getContext().VoidTy; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3262 | switch (E->getOp()) { |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3263 | // There is only one libcall for compare an exchange, because there is no |
| 3264 | // optimisation benefit possible from a libcall version of a weak compare |
| 3265 | // and exchange. |
| 3266 | // bool __atomic_compare_exchange(size_t size, void *obj, void *expected, |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3267 | // void *desired, int success, int failure) |
| 3268 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 3269 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 3270 | case AtomicExpr::AO__atomic_compare_exchange: |
| 3271 | case AtomicExpr::AO__atomic_compare_exchange_n: |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3272 | LibCallName = "__atomic_compare_exchange"; |
| 3273 | RetTy = getContext().BoolTy; |
| 3274 | Args.add(RValue::get(EmitCastToVoidPtr(Val1)), |
| 3275 | getContext().VoidPtrTy); |
| 3276 | Args.add(RValue::get(EmitCastToVoidPtr(Val2)), |
| 3277 | getContext().VoidPtrTy); |
| 3278 | Args.add(RValue::get(Order), |
| 3279 | getContext().IntTy); |
| 3280 | Order = OrderFail; |
| 3281 | break; |
| 3282 | // void __atomic_exchange(size_t size, void *mem, void *val, void *return, |
| 3283 | // int order) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3284 | case AtomicExpr::AO__c11_atomic_exchange: |
| 3285 | case AtomicExpr::AO__atomic_exchange_n: |
| 3286 | case AtomicExpr::AO__atomic_exchange: |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3287 | LibCallName = "__atomic_exchange"; |
| 3288 | Args.add(RValue::get(EmitCastToVoidPtr(Val1)), |
| 3289 | getContext().VoidPtrTy); |
| 3290 | Args.add(RValue::get(EmitCastToVoidPtr(Dest)), |
| 3291 | getContext().VoidPtrTy); |
| 3292 | break; |
| 3293 | // void __atomic_store(size_t size, void *mem, void *val, int order) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3294 | case AtomicExpr::AO__c11_atomic_store: |
| 3295 | case AtomicExpr::AO__atomic_store: |
| 3296 | case AtomicExpr::AO__atomic_store_n: |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3297 | LibCallName = "__atomic_store"; |
| 3298 | Args.add(RValue::get(EmitCastToVoidPtr(Val1)), |
| 3299 | getContext().VoidPtrTy); |
| 3300 | break; |
| 3301 | // void __atomic_load(size_t size, void *mem, void *return, int order) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3302 | case AtomicExpr::AO__c11_atomic_load: |
| 3303 | case AtomicExpr::AO__atomic_load: |
| 3304 | case AtomicExpr::AO__atomic_load_n: |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3305 | LibCallName = "__atomic_load"; |
| 3306 | Args.add(RValue::get(EmitCastToVoidPtr(Dest)), |
| 3307 | getContext().VoidPtrTy); |
| 3308 | break; |
| 3309 | #if 0 |
| 3310 | // These are only defined for 1-16 byte integers. It is not clear what |
| 3311 | // their semantics would be on anything else... |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3312 | case AtomicExpr::Add: LibCallName = "__atomic_fetch_add_generic"; break; |
| 3313 | case AtomicExpr::Sub: LibCallName = "__atomic_fetch_sub_generic"; break; |
| 3314 | case AtomicExpr::And: LibCallName = "__atomic_fetch_and_generic"; break; |
| 3315 | case AtomicExpr::Or: LibCallName = "__atomic_fetch_or_generic"; break; |
| 3316 | case AtomicExpr::Xor: LibCallName = "__atomic_fetch_xor_generic"; break; |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3317 | #endif |
| 3318 | default: return EmitUnsupportedRValue(E, "atomic library call"); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3319 | } |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3320 | // order is always the last parameter |
| 3321 | Args.add(RValue::get(Order), |
| 3322 | getContext().IntTy); |
| 3323 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3324 | const CGFunctionInfo &FuncInfo = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 3325 | CGM.getTypes().arrangeFreeFunctionCall(RetTy, Args, |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3326 | FunctionType::ExtInfo(), RequiredArgs::All); |
| 3327 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3328 | llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName); |
| 3329 | RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args); |
| 3330 | if (E->isCmpXChg()) |
| 3331 | return Res; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3332 | if (E->getType()->isVoidType()) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3333 | return RValue::get(0); |
| 3334 | return ConvertTempToRValue(*this, E->getType(), Dest); |
| 3335 | } |
David Chisnall | db365f3 | 2012-03-29 18:01:11 +0000 | [diff] [blame] | 3336 | |
Eli Friedman | fb9c49e | 2012-10-30 01:15:28 +0000 | [diff] [blame] | 3337 | bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store || |
| 3338 | E->getOp() == AtomicExpr::AO__atomic_store || |
| 3339 | E->getOp() == AtomicExpr::AO__atomic_store_n; |
| 3340 | bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load || |
| 3341 | E->getOp() == AtomicExpr::AO__atomic_load || |
| 3342 | E->getOp() == AtomicExpr::AO__atomic_load_n; |
| 3343 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3344 | llvm::Type *IPtrTy = |
| 3345 | llvm::IntegerType::get(getLLVMContext(), Size * 8)->getPointerTo(); |
| 3346 | llvm::Value *OrigDest = Dest; |
| 3347 | Ptr = Builder.CreateBitCast(Ptr, IPtrTy); |
| 3348 | if (Val1) Val1 = Builder.CreateBitCast(Val1, IPtrTy); |
| 3349 | if (Val2) Val2 = Builder.CreateBitCast(Val2, IPtrTy); |
| 3350 | if (Dest && !E->isCmpXChg()) Dest = Builder.CreateBitCast(Dest, IPtrTy); |
| 3351 | |
| 3352 | if (isa<llvm::ConstantInt>(Order)) { |
| 3353 | int ord = cast<llvm::ConstantInt>(Order)->getZExtValue(); |
| 3354 | switch (ord) { |
| 3355 | case 0: // memory_order_relaxed |
| 3356 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3357 | llvm::Monotonic); |
| 3358 | break; |
| 3359 | case 1: // memory_order_consume |
| 3360 | case 2: // memory_order_acquire |
Eli Friedman | fb9c49e | 2012-10-30 01:15:28 +0000 | [diff] [blame] | 3361 | if (IsStore) |
| 3362 | break; // Avoid crashing on code with undefined behavior |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3363 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3364 | llvm::Acquire); |
| 3365 | break; |
| 3366 | case 3: // memory_order_release |
Eli Friedman | fb9c49e | 2012-10-30 01:15:28 +0000 | [diff] [blame] | 3367 | if (IsLoad) |
| 3368 | break; // Avoid crashing on code with undefined behavior |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3369 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3370 | llvm::Release); |
| 3371 | break; |
| 3372 | case 4: // memory_order_acq_rel |
Eli Friedman | fb9c49e | 2012-10-30 01:15:28 +0000 | [diff] [blame] | 3373 | if (IsLoad || IsStore) |
| 3374 | break; // Avoid crashing on code with undefined behavior |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3375 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3376 | llvm::AcquireRelease); |
| 3377 | break; |
| 3378 | case 5: // memory_order_seq_cst |
| 3379 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3380 | llvm::SequentiallyConsistent); |
| 3381 | break; |
| 3382 | default: // invalid order |
| 3383 | // We should not ever get here normally, but it's hard to |
| 3384 | // enforce that in general. |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3385 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3386 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3387 | if (E->getType()->isVoidType()) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3388 | return RValue::get(0); |
| 3389 | return ConvertTempToRValue(*this, E->getType(), OrigDest); |
| 3390 | } |
| 3391 | |
| 3392 | // Long case, when Order isn't obviously constant. |
| 3393 | |
| 3394 | // Create all the relevant BB's |
Eli Friedman | c202556 | 2011-10-11 20:00:47 +0000 | [diff] [blame] | 3395 | llvm::BasicBlock *MonotonicBB = 0, *AcquireBB = 0, *ReleaseBB = 0, |
| 3396 | *AcqRelBB = 0, *SeqCstBB = 0; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3397 | MonotonicBB = createBasicBlock("monotonic", CurFn); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3398 | if (!IsStore) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3399 | AcquireBB = createBasicBlock("acquire", CurFn); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3400 | if (!IsLoad) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3401 | ReleaseBB = createBasicBlock("release", CurFn); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3402 | if (!IsLoad && !IsStore) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3403 | AcqRelBB = createBasicBlock("acqrel", CurFn); |
| 3404 | SeqCstBB = createBasicBlock("seqcst", CurFn); |
| 3405 | llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn); |
| 3406 | |
| 3407 | // Create the switch for the split |
| 3408 | // MonotonicBB is arbitrarily chosen as the default case; in practice, this |
| 3409 | // doesn't matter unless someone is crazy enough to use something that |
| 3410 | // doesn't fold to a constant for the ordering. |
| 3411 | Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false); |
| 3412 | llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB); |
| 3413 | |
| 3414 | // Emit all the different atomics |
| 3415 | Builder.SetInsertPoint(MonotonicBB); |
| 3416 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3417 | llvm::Monotonic); |
| 3418 | Builder.CreateBr(ContBB); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3419 | if (!IsStore) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3420 | Builder.SetInsertPoint(AcquireBB); |
| 3421 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3422 | llvm::Acquire); |
| 3423 | Builder.CreateBr(ContBB); |
| 3424 | SI->addCase(Builder.getInt32(1), AcquireBB); |
| 3425 | SI->addCase(Builder.getInt32(2), AcquireBB); |
| 3426 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3427 | if (!IsLoad) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3428 | Builder.SetInsertPoint(ReleaseBB); |
| 3429 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3430 | llvm::Release); |
| 3431 | Builder.CreateBr(ContBB); |
| 3432 | SI->addCase(Builder.getInt32(3), ReleaseBB); |
| 3433 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3434 | if (!IsLoad && !IsStore) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3435 | Builder.SetInsertPoint(AcqRelBB); |
| 3436 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3437 | llvm::AcquireRelease); |
| 3438 | Builder.CreateBr(ContBB); |
| 3439 | SI->addCase(Builder.getInt32(4), AcqRelBB); |
| 3440 | } |
| 3441 | Builder.SetInsertPoint(SeqCstBB); |
| 3442 | EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align, |
| 3443 | llvm::SequentiallyConsistent); |
| 3444 | Builder.CreateBr(ContBB); |
| 3445 | SI->addCase(Builder.getInt32(5), SeqCstBB); |
| 3446 | |
| 3447 | // Cleanup and return |
| 3448 | Builder.SetInsertPoint(ContBB); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3449 | if (E->getType()->isVoidType()) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3450 | return RValue::get(0); |
| 3451 | return ConvertTempToRValue(*this, E->getType(), OrigDest); |
| 3452 | } |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 3453 | |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 3454 | void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) { |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 3455 | assert(Val->getType()->isFPOrFPVectorTy()); |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 3456 | if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val)) |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 3457 | return; |
| 3458 | |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 3459 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 3460 | llvm::MDNode *Node = MDHelper.createFPMath(Accuracy); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 3461 | |
Duncan Sands | 6fc4619 | 2012-04-14 12:37:26 +0000 | [diff] [blame] | 3462 | cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 3463 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3464 | |
| 3465 | namespace { |
| 3466 | struct LValueOrRValue { |
| 3467 | LValue LV; |
| 3468 | RValue RV; |
| 3469 | }; |
| 3470 | } |
| 3471 | |
| 3472 | static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, |
| 3473 | const PseudoObjectExpr *E, |
| 3474 | bool forLValue, |
| 3475 | AggValueSlot slot) { |
| 3476 | llvm::SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; |
| 3477 | |
| 3478 | // Find the result expression, if any. |
| 3479 | const Expr *resultExpr = E->getResultExpr(); |
| 3480 | LValueOrRValue result; |
| 3481 | |
| 3482 | for (PseudoObjectExpr::const_semantics_iterator |
| 3483 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { |
| 3484 | const Expr *semantic = *i; |
| 3485 | |
| 3486 | // If this semantic expression is an opaque value, bind it |
| 3487 | // to the result of its source expression. |
| 3488 | if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { |
| 3489 | |
| 3490 | // If this is the result expression, we may need to evaluate |
| 3491 | // directly into the slot. |
| 3492 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; |
| 3493 | OVMA opaqueData; |
| 3494 | if (ov == resultExpr && ov->isRValue() && !forLValue && |
| 3495 | CodeGenFunction::hasAggregateLLVMType(ov->getType()) && |
| 3496 | !ov->getType()->isAnyComplexType()) { |
| 3497 | CGF.EmitAggExpr(ov->getSourceExpr(), slot); |
| 3498 | |
| 3499 | LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType()); |
| 3500 | opaqueData = OVMA::bind(CGF, ov, LV); |
| 3501 | result.RV = slot.asRValue(); |
| 3502 | |
| 3503 | // Otherwise, emit as normal. |
| 3504 | } else { |
| 3505 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); |
| 3506 | |
| 3507 | // If this is the result, also evaluate the result now. |
| 3508 | if (ov == resultExpr) { |
| 3509 | if (forLValue) |
| 3510 | result.LV = CGF.EmitLValue(ov); |
| 3511 | else |
| 3512 | result.RV = CGF.EmitAnyExpr(ov, slot); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | opaques.push_back(opaqueData); |
| 3517 | |
| 3518 | // Otherwise, if the expression is the result, evaluate it |
| 3519 | // and remember the result. |
| 3520 | } else if (semantic == resultExpr) { |
| 3521 | if (forLValue) |
| 3522 | result.LV = CGF.EmitLValue(semantic); |
| 3523 | else |
| 3524 | result.RV = CGF.EmitAnyExpr(semantic, slot); |
| 3525 | |
| 3526 | // Otherwise, evaluate the expression in an ignored context. |
| 3527 | } else { |
| 3528 | CGF.EmitIgnoredExpr(semantic); |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | // Unbind all the opaques now. |
| 3533 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) |
| 3534 | opaques[i].unbind(CGF); |
| 3535 | |
| 3536 | return result; |
| 3537 | } |
| 3538 | |
| 3539 | RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E, |
| 3540 | AggValueSlot slot) { |
| 3541 | return emitPseudoObjectExpr(*this, E, false, slot).RV; |
| 3542 | } |
| 3543 | |
| 3544 | LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) { |
| 3545 | return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV; |
| 3546 | } |