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