Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 1 | //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGCall.h" |
Devang Patel | d3a6b0f | 2011-03-04 18:54:42 +0000 | [diff] [blame] | 17 | #include "CGDebugInfo.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 19 | #include "CGOpenMPRuntime.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "CGRecordLayout.h" |
| 21 | #include "CodeGenModule.h" |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 22 | #include "TargetInfo.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 24 | #include "clang/AST/Attr.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Hashing.h" |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/Intrinsics.h" |
| 31 | #include "llvm/IR/LLVMContext.h" |
| 32 | #include "llvm/IR/MDBuilder.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ConvertUTF.h" |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/SanitizerStats.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 36 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace CodeGen; |
| 39 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 40 | //===--------------------------------------------------------------------===// |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 41 | // Miscellaneous Helper Methods |
| 42 | //===--------------------------------------------------------------------===// |
| 43 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 44 | llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { |
| 45 | unsigned addressSpace = |
| 46 | cast<llvm::PointerType>(value->getType())->getAddressSpace(); |
| 47 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 48 | llvm::PointerType *destType = Int8PtrTy; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 49 | if (addressSpace) |
| 50 | destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace); |
| 51 | |
| 52 | if (value->getType() == destType) return value; |
| 53 | return Builder.CreateBitCast(value, destType); |
| 54 | } |
| 55 | |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 56 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 57 | /// block. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 58 | Address CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, CharUnits Align, |
| 59 | const Twine &Name) { |
| 60 | auto Alloca = CreateTempAlloca(Ty, Name); |
| 61 | Alloca->setAlignment(Align.getQuantity()); |
| 62 | return Address(Alloca, Align); |
| 63 | } |
| 64 | |
| 65 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 66 | /// block. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 67 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | const Twine &Name) { |
Chris Lattner | 4764022 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 69 | if (!Builder.isNamePreserving()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 70 | return new llvm::AllocaInst(Ty, nullptr, "", AllocaInsertPt); |
| 71 | return new llvm::AllocaInst(Ty, nullptr, Name, AllocaInsertPt); |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 73 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 74 | /// CreateDefaultAlignTempAlloca - This creates an alloca with the |
| 75 | /// default alignment of the corresponding LLVM type, which is *not* |
| 76 | /// guaranteed to be related in any way to the expected alignment of |
| 77 | /// an AST type that might have been lowered to Ty. |
| 78 | Address CodeGenFunction::CreateDefaultAlignTempAlloca(llvm::Type *Ty, |
| 79 | const Twine &Name) { |
| 80 | CharUnits Align = |
| 81 | CharUnits::fromQuantity(CGM.getDataLayout().getABITypeAlignment(Ty)); |
| 82 | return CreateTempAlloca(Ty, Align, Name); |
| 83 | } |
| 84 | |
| 85 | void CodeGenFunction::InitTempAlloca(Address Var, llvm::Value *Init) { |
| 86 | assert(isa<llvm::AllocaInst>(Var.getPointer())); |
| 87 | auto *Store = new llvm::StoreInst(Init, Var.getPointer()); |
| 88 | Store->setAlignment(Var.getAlignment().getQuantity()); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 89 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 90 | Block->getInstList().insertAfter(AllocaInsertPt->getIterator(), Store); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 93 | Address CodeGenFunction::CreateIRTemp(QualType Ty, const Twine &Name) { |
Daniel Dunbar | d004918 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 94 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 95 | return CreateTempAlloca(ConvertType(Ty), Align, Name); |
Daniel Dunbar | d004918 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 98 | Address CodeGenFunction::CreateMemTemp(QualType Ty, const Twine &Name) { |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 99 | // FIXME: Should we prefer the preferred type alignment here? |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 100 | return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name); |
| 101 | } |
| 102 | |
| 103 | Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align, |
| 104 | const Twine &Name) { |
| 105 | return CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name); |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 108 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 109 | /// expression and compare the result against zero, returning an Int1Ty value. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 110 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 111 | PGO.setCurrentStmt(E); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 112 | if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 113 | llvm::Value *MemPtr = EmitScalarExpr(E); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 114 | return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT); |
Eli Friedman | 68396b1 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 115 | } |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 116 | |
| 117 | QualType BoolTy = getContext().BoolTy; |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 118 | SourceLocation Loc = E->getExprLoc(); |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 119 | if (!E->getType()->isAnyComplexType()) |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 120 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 121 | |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 122 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(), BoolTy, |
| 123 | Loc); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 124 | } |
| 125 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 126 | /// EmitIgnoredExpr - Emit code to compute the specified expression, |
| 127 | /// ignoring the result. |
| 128 | void CodeGenFunction::EmitIgnoredExpr(const Expr *E) { |
| 129 | if (E->isRValue()) |
| 130 | return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true); |
| 131 | |
| 132 | // Just emit it as an l-value and drop the result. |
| 133 | EmitLValue(E); |
| 134 | } |
| 135 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 136 | /// EmitAnyExpr - Emit code to compute the specified expression which |
| 137 | /// can have any type. The result is returned as an RValue struct. |
| 138 | /// If this is an aggregate expression, AggSlot indicates where the |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 139 | /// result should be returned. |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 140 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, |
| 141 | AggValueSlot aggSlot, |
| 142 | bool ignoreResult) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 143 | switch (getEvaluationKind(E->getType())) { |
| 144 | case TEK_Scalar: |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 145 | return RValue::get(EmitScalarExpr(E, ignoreResult)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 146 | case TEK_Complex: |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 147 | return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 148 | case TEK_Aggregate: |
| 149 | if (!ignoreResult && aggSlot.isIgnored()) |
| 150 | aggSlot = CreateAggTemp(E->getType(), "agg-temp"); |
| 151 | EmitAggExpr(E, aggSlot); |
| 152 | return aggSlot.asRValue(); |
| 153 | } |
| 154 | llvm_unreachable("bad evaluation kind"); |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 157 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 158 | /// always be accessible even if no aggregate location is provided. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 159 | RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) { |
| 160 | AggValueSlot AggSlot = AggValueSlot::ignored(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 161 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 162 | if (hasAggregateEvaluationKind(E->getType())) |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 163 | AggSlot = CreateAggTemp(E->getType(), "agg.tmp"); |
| 164 | return EmitAnyExpr(E, AggSlot); |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 167 | /// EmitAnyExprToMem - Evaluate an expression into a given memory |
| 168 | /// location. |
| 169 | void CodeGenFunction::EmitAnyExprToMem(const Expr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 170 | Address Location, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 171 | Qualifiers Quals, |
| 172 | bool IsInit) { |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 173 | // FIXME: This function should take an LValue as an argument. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 174 | switch (getEvaluationKind(E->getType())) { |
| 175 | case TEK_Complex: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 176 | EmitComplexExprIntoLValue(E, MakeAddrLValue(Location, E->getType()), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 177 | /*isInit*/ false); |
| 178 | return; |
| 179 | |
| 180 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 181 | EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 182 | AggValueSlot::IsDestructed_t(IsInit), |
John McCall | a8a39bc | 2011-08-26 05:38:08 +0000 | [diff] [blame] | 183 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 184 | AggValueSlot::IsAliased_t(!IsInit))); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | case TEK_Scalar: { |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 189 | RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 190 | LValue LV = MakeAddrLValue(Location, E->getType()); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 191 | EmitStoreThroughLValue(RV, LV); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 192 | return; |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 193 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 194 | } |
| 195 | llvm_unreachable("bad evaluation kind"); |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 198 | static void |
| 199 | pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 200 | const Expr *E, Address ReferenceTemporary) { |
Rafael Espindola | b9d75ca | 2012-10-27 00:43:14 +0000 | [diff] [blame] | 201 | // Objective-C++ ARC: |
| 202 | // If we are binding a reference to a temporary that has ownership, we |
| 203 | // need to perform retain/release operations on the temporary. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 204 | // |
| 205 | // FIXME: This should be looking at E, not M. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 206 | if (auto Lifetime = M->getType().getObjCLifetime()) { |
| 207 | switch (Lifetime) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 208 | case Qualifiers::OCL_None: |
| 209 | case Qualifiers::OCL_ExplicitNone: |
| 210 | // Carry on to normal cleanup handling. |
| 211 | break; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 212 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 213 | case Qualifiers::OCL_Autoreleasing: |
| 214 | // Nothing to do; cleaned up by an autorelease pool. |
| 215 | return; |
| 216 | |
| 217 | case Qualifiers::OCL_Strong: |
| 218 | case Qualifiers::OCL_Weak: |
| 219 | switch (StorageDuration Duration = M->getStorageDuration()) { |
| 220 | case SD_Static: |
| 221 | // Note: we intentionally do not register a cleanup to release |
| 222 | // the object on program termination. |
| 223 | return; |
| 224 | |
| 225 | case SD_Thread: |
| 226 | // FIXME: We should probably register a cleanup in this case. |
| 227 | return; |
| 228 | |
| 229 | case SD_Automatic: |
| 230 | case SD_FullExpression: |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 231 | CodeGenFunction::Destroyer *Destroy; |
| 232 | CleanupKind CleanupKind; |
| 233 | if (Lifetime == Qualifiers::OCL_Strong) { |
| 234 | const ValueDecl *VD = M->getExtendingDecl(); |
| 235 | bool Precise = |
| 236 | VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>(); |
| 237 | CleanupKind = CGF.getARCCleanupKind(); |
| 238 | Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise |
| 239 | : &CodeGenFunction::destroyARCStrongImprecise; |
| 240 | } else { |
| 241 | // __weak objects always get EH cleanups; otherwise, exceptions |
| 242 | // could cause really nasty crashes instead of mere leaks. |
| 243 | CleanupKind = NormalAndEHCleanup; |
| 244 | Destroy = &CodeGenFunction::destroyARCWeak; |
| 245 | } |
| 246 | if (Duration == SD_FullExpression) |
| 247 | CGF.pushDestroy(CleanupKind, ReferenceTemporary, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 248 | M->getType(), *Destroy, |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 249 | CleanupKind & EHCleanup); |
| 250 | else |
| 251 | CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 252 | M->getType(), |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 253 | *Destroy, CleanupKind & EHCleanup); |
| 254 | return; |
| 255 | |
| 256 | case SD_Dynamic: |
| 257 | llvm_unreachable("temporary cannot have dynamic storage duration"); |
| 258 | } |
| 259 | llvm_unreachable("unknown storage duration"); |
| 260 | } |
| 261 | } |
| 262 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 263 | CXXDestructorDecl *ReferenceTemporaryDtor = nullptr; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 264 | if (const RecordType *RT = |
| 265 | E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
| 266 | // Get the destructor for the reference temporary. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 267 | auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 268 | if (!ClassDecl->hasTrivialDestructor()) |
| 269 | ReferenceTemporaryDtor = ClassDecl->getDestructor(); |
| 270 | } |
| 271 | |
| 272 | if (!ReferenceTemporaryDtor) |
| 273 | return; |
| 274 | |
| 275 | // Call the destructor for the temporary. |
| 276 | switch (M->getStorageDuration()) { |
| 277 | case SD_Static: |
| 278 | case SD_Thread: { |
| 279 | llvm::Constant *CleanupFn; |
| 280 | llvm::Constant *CleanupArg; |
| 281 | if (E->getType()->isArrayType()) { |
| 282 | CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 283 | ReferenceTemporary, E->getType(), |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 284 | CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions, |
| 285 | dyn_cast_or_null<VarDecl>(M->getExtendingDecl())); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 286 | CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
| 287 | } else { |
Rafael Espindola | 1ac0ec8 | 2014-09-11 15:42:06 +0000 | [diff] [blame] | 288 | CleanupFn = CGF.CGM.getAddrOfCXXStructor(ReferenceTemporaryDtor, |
| 289 | StructorType::Complete); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 290 | CleanupArg = cast<llvm::Constant>(ReferenceTemporary.getPointer()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 291 | } |
| 292 | CGF.CGM.getCXXABI().registerGlobalDtor( |
| 293 | CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg); |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | case SD_FullExpression: |
| 298 | CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(), |
| 299 | CodeGenFunction::destroyCXXObject, |
| 300 | CGF.getLangOpts().Exceptions); |
| 301 | break; |
| 302 | |
| 303 | case SD_Automatic: |
| 304 | CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup, |
| 305 | ReferenceTemporary, E->getType(), |
| 306 | CodeGenFunction::destroyCXXObject, |
| 307 | CGF.getLangOpts().Exceptions); |
| 308 | break; |
| 309 | |
| 310 | case SD_Dynamic: |
| 311 | llvm_unreachable("temporary cannot have dynamic storage duration"); |
| 312 | } |
| 313 | } |
| 314 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 315 | static Address |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 316 | createReferenceTemporary(CodeGenFunction &CGF, |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 317 | const MaterializeTemporaryExpr *M, const Expr *Inner) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 318 | switch (M->getStorageDuration()) { |
| 319 | case SD_FullExpression: |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 320 | case SD_Automatic: { |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 321 | // If we have a constant temporary array or record try to promote it into a |
| 322 | // constant global under the same rules a normal constant would've been |
| 323 | // promoted. This is easier on the optimizer and generally emits fewer |
| 324 | // instructions. |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 325 | QualType Ty = Inner->getType(); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 326 | if (CGF.CGM.getCodeGenOpts().MergeAllConstants && |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 327 | (Ty->isArrayType() || Ty->isRecordType()) && |
| 328 | CGF.CGM.isTypeConstant(Ty, true)) |
| 329 | if (llvm::Constant *Init = CGF.CGM.EmitConstantExpr(Inner, Ty, &CGF)) { |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 330 | auto *GV = new llvm::GlobalVariable( |
| 331 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
| 332 | llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 333 | CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty); |
| 334 | GV->setAlignment(alignment.getQuantity()); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 335 | // FIXME: Should we put the new global into a COMDAT? |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 336 | return Address(GV, alignment); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 337 | } |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 338 | return CGF.CreateMemTemp(Ty, "ref.tmp"); |
| 339 | } |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 340 | case SD_Thread: |
| 341 | case SD_Static: |
Hans Wennborg | f9d865b | 2015-03-17 16:38:58 +0000 | [diff] [blame] | 342 | return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 343 | |
| 344 | case SD_Dynamic: |
| 345 | llvm_unreachable("temporary can't have dynamic storage duration"); |
| 346 | } |
| 347 | llvm_unreachable("unknown storage duration"); |
| 348 | } |
| 349 | |
Saleem Abdulrasool | 8925dc0 | 2014-10-24 19:54:32 +0000 | [diff] [blame] | 350 | LValue CodeGenFunction:: |
| 351 | EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) { |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 352 | const Expr *E = M->GetTemporaryExpr(); |
Richard Smith | 7c5d4dc | 2013-06-11 02:41:00 +0000 | [diff] [blame] | 353 | |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 354 | // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so |
| 355 | // as that will cause the lifetime adjustment to be lost for ARC |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 356 | auto ownership = M->getType().getObjCLifetime(); |
| 357 | if (ownership != Qualifiers::OCL_None && |
| 358 | ownership != Qualifiers::OCL_ExplicitNone) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 359 | Address Object = createReferenceTemporary(*this, M, E); |
| 360 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) { |
| 361 | Object = Address(llvm::ConstantExpr::getBitCast(Var, |
| 362 | ConvertTypeForMem(E->getType()) |
| 363 | ->getPointerTo(Object.getAddressSpace())), |
| 364 | Object.getAlignment()); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 365 | // We should not have emitted the initializer for this temporary as a |
| 366 | // constant. |
| 367 | assert(!Var->hasInitializer()); |
| 368 | Var->setInitializer(CGM.EmitNullConstant(E->getType())); |
| 369 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 370 | LValue RefTempDst = MakeAddrLValue(Object, M->getType(), |
| 371 | AlignmentSource::Decl); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 372 | |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 373 | switch (getEvaluationKind(E->getType())) { |
| 374 | default: llvm_unreachable("expected scalar or aggregate expression"); |
| 375 | case TEK_Scalar: |
| 376 | EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false); |
| 377 | break; |
| 378 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 379 | EmitAggExpr(E, AggValueSlot::forAddr(Object, |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 380 | E->getType().getQualifiers(), |
| 381 | AggValueSlot::IsDestructed, |
| 382 | AggValueSlot::DoesNotNeedGCBarriers, |
| 383 | AggValueSlot::IsNotAliased)); |
| 384 | break; |
| 385 | } |
| 386 | } |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 387 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 388 | pushTemporaryCleanup(*this, M, E, Object); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 389 | return RefTempDst; |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 392 | SmallVector<const Expr *, 2> CommaLHSs; |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 393 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 394 | E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
| 395 | |
Saleem Abdulrasool | 8925dc0 | 2014-10-24 19:54:32 +0000 | [diff] [blame] | 396 | for (const auto &Ignored : CommaLHSs) |
| 397 | EmitIgnoredExpr(Ignored); |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 398 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 399 | if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 400 | if (opaque->getType()->isRecordType()) { |
| 401 | assert(Adjustments.empty()); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 402 | return EmitOpaqueValueLValue(opaque); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 406 | // Create and initialize the reference temporary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 407 | Address Object = createReferenceTemporary(*this, M, E); |
| 408 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) { |
| 409 | Object = Address(llvm::ConstantExpr::getBitCast( |
| 410 | Var, ConvertTypeForMem(E->getType())->getPointerTo()), |
| 411 | Object.getAlignment()); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 412 | // If the temporary is a global and has a constant initializer or is a |
| 413 | // constant temporary that we promoted to a global, we may have already |
| 414 | // initialized it. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 415 | if (!Var->hasInitializer()) { |
| 416 | Var->setInitializer(CGM.EmitNullConstant(E->getType())); |
| 417 | EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); |
| 418 | } |
| 419 | } else { |
| 420 | EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); |
| 421 | } |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 422 | pushTemporaryCleanup(*this, M, E, Object); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 423 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 424 | // Perform derived-to-base casts and/or field accesses, to get from the |
| 425 | // temporary object we created (and, potentially, for which we extended |
| 426 | // the lifetime) to the subobject we're binding the reference to. |
| 427 | for (unsigned I = Adjustments.size(); I != 0; --I) { |
| 428 | SubobjectAdjustment &Adjustment = Adjustments[I-1]; |
| 429 | switch (Adjustment.Kind) { |
| 430 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 431 | Object = |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 432 | GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass, |
| 433 | Adjustment.DerivedToBase.BasePath->path_begin(), |
| 434 | Adjustment.DerivedToBase.BasePath->path_end(), |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 435 | /*NullCheckValue=*/ false, E->getExprLoc()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 436 | break; |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 437 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 438 | case SubobjectAdjustment::FieldAdjustment: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 439 | LValue LV = MakeAddrLValue(Object, E->getType(), |
| 440 | AlignmentSource::Decl); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 441 | LV = EmitLValueForField(LV, Adjustment.Field); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 442 | assert(LV.isSimple() && |
| 443 | "materialized temporary field is not a simple lvalue"); |
| 444 | Object = LV.getAddress(); |
| 445 | break; |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 448 | case SubobjectAdjustment::MemberPointerAdjustment: { |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 449 | llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 450 | Object = EmitCXXMemberDataPointerAddress(E, Object, Ptr, |
| 451 | Adjustment.Ptr.MPT); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | } |
Anders Carlsson | 7d4c083 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 455 | } |
Eli Friedman | c21cb44 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 456 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 457 | return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | RValue |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 461 | CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) { |
| 462 | // Emit the expression as an lvalue. |
| 463 | LValue LV = EmitLValue(E); |
| 464 | assert(LV.isSimple()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 465 | llvm::Value *Value = LV.getPointer(); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 466 | |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 467 | if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 468 | // C++11 [dcl.ref]p5 (as amended by core issue 453): |
| 469 | // If a glvalue to which a reference is directly bound designates neither |
| 470 | // an existing object or function of an appropriate type nor a region of |
| 471 | // storage of suitable size and alignment to contain an object of the |
| 472 | // reference's type, the behavior is undefined. |
| 473 | QualType Ty = E->getType(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 474 | EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 475 | } |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 477 | return RValue::get(Value); |
Anders Carlsson | 6f5a015 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 481 | /// getAccessedFieldNo - Given an encoded value and a result number, return the |
| 482 | /// input field number being accessed. |
| 483 | unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 484 | const llvm::Constant *Elts) { |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 485 | return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx)) |
| 486 | ->getZExtValue(); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 489 | /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h. |
| 490 | static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, |
| 491 | llvm::Value *High) { |
| 492 | llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL); |
| 493 | llvm::Value *K47 = Builder.getInt64(47); |
| 494 | llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul); |
| 495 | llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0); |
| 496 | llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul); |
| 497 | llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0); |
| 498 | return Builder.CreateMul(B1, KMul); |
| 499 | } |
| 500 | |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 501 | bool CodeGenFunction::sanitizePerformTypeCheck() const { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 502 | return SanOpts.has(SanitizerKind::Null) | |
| 503 | SanOpts.has(SanitizerKind::Alignment) | |
| 504 | SanOpts.has(SanitizerKind::ObjectSize) | |
| 505 | SanOpts.has(SanitizerKind::Vptr); |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 508 | void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 509 | llvm::Value *Ptr, QualType Ty, |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 510 | CharUnits Alignment, bool SkipNullCheck) { |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 511 | if (!sanitizePerformTypeCheck()) |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 512 | return; |
| 513 | |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 514 | // Don't check pointers outside the default address space. The null check |
| 515 | // isn't correct, the object-size check isn't supported by LLVM, and we can't |
| 516 | // communicate the addresses to the runtime handler for the vptr check. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 517 | if (Ptr->getType()->getPointerAddressSpace()) |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 518 | return; |
| 519 | |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 520 | SanitizerScope SanScope(this); |
| 521 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 522 | SmallVector<std::pair<llvm::Value *, SanitizerMask>, 3> Checks; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 523 | llvm::BasicBlock *Done = nullptr; |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 524 | |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 525 | bool AllowNullPointers = TCK == TCK_DowncastPointer || TCK == TCK_Upcast || |
| 526 | TCK == TCK_UpcastToVirtualBase; |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 527 | if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) && |
| 528 | !SkipNullCheck) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 529 | // The glvalue must not be an empty glvalue. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 530 | llvm::Value *IsNonNull = Builder.CreateIsNotNull(Ptr); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 531 | |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 532 | if (AllowNullPointers) { |
| 533 | // When performing pointer casts, it's OK if the value is null. |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 534 | // Skip the remaining checks in that case. |
| 535 | Done = createBasicBlock("null"); |
| 536 | llvm::BasicBlock *Rest = createBasicBlock("not.null"); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 537 | Builder.CreateCondBr(IsNonNull, Rest, Done); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 538 | EmitBlock(Rest); |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 539 | } else { |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 540 | Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null)); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 541 | } |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | bc3be65 | 2010-04-10 18:34:14 +0000 | [diff] [blame] | 543 | |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 544 | if (SanOpts.has(SanitizerKind::ObjectSize) && !Ty->isIncompleteType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 545 | uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity(); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 546 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 547 | // The glvalue must refer to a large enough storage region. |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 548 | // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 549 | // to check this. |
Matt Arsenault | 2f15263 | 2013-10-07 19:00:18 +0000 | [diff] [blame] | 550 | // FIXME: Get object address space |
| 551 | llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy }; |
| 552 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 553 | llvm::Value *Min = Builder.getFalse(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 554 | llvm::Value *CastAddr = Builder.CreateBitCast(Ptr, Int8PtrTy); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 555 | llvm::Value *LargeEnough = |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 556 | Builder.CreateICmpUGE(Builder.CreateCall(F, {CastAddr, Min}), |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 557 | llvm::ConstantInt::get(IntPtrTy, Size)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 558 | Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize)); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 559 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 560 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 561 | uint64_t AlignVal = 0; |
| 562 | |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 563 | if (SanOpts.has(SanitizerKind::Alignment)) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 564 | AlignVal = Alignment.getQuantity(); |
| 565 | if (!Ty->isIncompleteType() && !AlignVal) |
| 566 | AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 567 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 568 | // The glvalue must be suitably aligned. |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 569 | if (AlignVal) { |
| 570 | llvm::Value *Align = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 571 | Builder.CreateAnd(Builder.CreatePtrToInt(Ptr, IntPtrTy), |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 572 | llvm::ConstantInt::get(IntPtrTy, AlignVal - 1)); |
| 573 | llvm::Value *Aligned = |
| 574 | Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 575 | Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment)); |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 576 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 579 | if (Checks.size() > 0) { |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 580 | llvm::Constant *StaticData[] = { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 581 | EmitCheckSourceLocation(Loc), |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 582 | EmitCheckTypeDescriptor(Ty), |
| 583 | llvm::ConstantInt::get(SizeTy, AlignVal), |
| 584 | llvm::ConstantInt::get(Int8Ty, TCK) |
| 585 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 586 | EmitCheck(Checks, "type_mismatch", StaticData, Ptr); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 587 | } |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 588 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 589 | // If possible, check that the vptr indicates that there is a subobject of |
| 590 | // type Ty at offset zero within this object. |
Richard Smith | be024a8 | 2012-12-18 00:22:45 +0000 | [diff] [blame] | 591 | // |
| 592 | // C++11 [basic.life]p5,6: |
| 593 | // [For storage which does not refer to an object within its lifetime] |
| 594 | // The program has undefined behavior if: |
| 595 | // -- the [pointer or glvalue] is used to access a non-static data member |
Richard Smith | 8b731ea | 2012-12-18 03:04:38 +0000 | [diff] [blame] | 596 | // or call a non-static member function |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 597 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 598 | if (SanOpts.has(SanitizerKind::Vptr) && |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 599 | (TCK == TCK_MemberAccess || TCK == TCK_MemberCall || |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 600 | TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference || |
| 601 | TCK == TCK_UpcastToVirtualBase) && |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 602 | RD && RD->hasDefinition() && RD->isDynamicClass()) { |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 603 | // Compute a hash of the mangled name of the type. |
| 604 | // |
| 605 | // FIXME: This is not guaranteed to be deterministic! Move to a |
| 606 | // fingerprinting mechanism once LLVM provides one. For the time |
| 607 | // being the implementation happens to be deterministic. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 608 | SmallString<64> MangledName; |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 609 | llvm::raw_svector_ostream Out(MangledName); |
| 610 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(), |
| 611 | Out); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 612 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 613 | // Blacklist based on the mangled type. |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 614 | if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType( |
| 615 | Out.str())) { |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 616 | llvm::hash_code TypeHash = hash_value(Out.str()); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 617 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 618 | // Load the vptr, and compute hash_16_bytes(TypeHash, vptr). |
| 619 | llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash); |
| 620 | llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 621 | Address VPtrAddr(Builder.CreateBitCast(Ptr, VPtrTy), getPointerAlign()); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 622 | llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr); |
| 623 | llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 624 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 625 | llvm::Value *Hash = emitHash16Bytes(Builder, Low, High); |
| 626 | Hash = Builder.CreateTrunc(Hash, IntPtrTy); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 627 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 628 | // Look the hash up in our cache. |
| 629 | const int CacheSize = 128; |
| 630 | llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize); |
| 631 | llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable, |
| 632 | "__ubsan_vptr_type_cache"); |
| 633 | llvm::Value *Slot = Builder.CreateAnd(Hash, |
| 634 | llvm::ConstantInt::get(IntPtrTy, |
| 635 | CacheSize-1)); |
| 636 | llvm::Value *Indices[] = { Builder.getInt32(0), Slot }; |
| 637 | llvm::Value *CacheVal = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 638 | Builder.CreateAlignedLoad(Builder.CreateInBoundsGEP(Cache, Indices), |
| 639 | getPointerAlign()); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 640 | |
| 641 | // If the hash isn't in the cache, call a runtime handler to perform the |
| 642 | // hard work of checking whether the vptr is for an object of the right |
| 643 | // type. This will either fill in the cache and return, or produce a |
| 644 | // diagnostic. |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 645 | llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 646 | llvm::Constant *StaticData[] = { |
| 647 | EmitCheckSourceLocation(Loc), |
| 648 | EmitCheckTypeDescriptor(Ty), |
| 649 | CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()), |
| 650 | llvm::ConstantInt::get(Int8Ty, TCK) |
| 651 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 652 | llvm::Value *DynamicData[] = { Ptr, Hash }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 653 | EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr), |
| 654 | "dynamic_type_cache_miss", StaticData, DynamicData); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 655 | } |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 656 | } |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 657 | |
| 658 | if (Done) { |
| 659 | Builder.CreateBr(Done); |
| 660 | EmitBlock(Done); |
| 661 | } |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 663 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 664 | /// Determine whether this expression refers to a flexible array member in a |
| 665 | /// struct. We disable array bounds checks for such members. |
| 666 | static bool isFlexibleArrayMemberExpr(const Expr *E) { |
| 667 | // For compatibility with existing code, we treat arrays of length 0 or |
| 668 | // 1 as flexible array members. |
| 669 | const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 670 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 671 | if (CAT->getSize().ugt(1)) |
| 672 | return false; |
| 673 | } else if (!isa<IncompleteArrayType>(AT)) |
| 674 | return false; |
| 675 | |
| 676 | E = E->IgnoreParens(); |
| 677 | |
| 678 | // A flexible array member must be the last member in the class. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 679 | if (const auto *ME = dyn_cast<MemberExpr>(E)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 680 | // FIXME: If the base type of the member expr is not FD->getParent(), |
| 681 | // this should not be treated as a flexible array member access. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 682 | if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 683 | RecordDecl::field_iterator FI( |
| 684 | DeclContext::decl_iterator(const_cast<FieldDecl *>(FD))); |
| 685 | return ++FI == FD->getParent()->field_end(); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | /// If Base is known to point to the start of an array, return the length of |
| 693 | /// that array. Return 0 if the length cannot be determined. |
Benjamin Kramer | 36f89cc | 2013-03-09 15:15:22 +0000 | [diff] [blame] | 694 | static llvm::Value *getArrayIndexingBound( |
| 695 | CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 696 | // For the vector indexing extension, the bound is the number of elements. |
| 697 | if (const VectorType *VT = Base->getType()->getAs<VectorType>()) { |
| 698 | IndexedType = Base->getType(); |
| 699 | return CGF.Builder.getInt32(VT->getNumElements()); |
| 700 | } |
| 701 | |
| 702 | Base = Base->IgnoreParens(); |
| 703 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 704 | if (const auto *CE = dyn_cast<CastExpr>(Base)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 705 | if (CE->getCastKind() == CK_ArrayToPointerDecay && |
| 706 | !isFlexibleArrayMemberExpr(CE->getSubExpr())) { |
| 707 | IndexedType = CE->getSubExpr()->getType(); |
| 708 | const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 709 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 710 | return CGF.Builder.getInt(CAT->getSize()); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 711 | else if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 712 | return CGF.getVLASize(VAT).first; |
| 713 | } |
| 714 | } |
| 715 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 716 | return nullptr; |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base, |
| 720 | llvm::Value *Index, QualType IndexType, |
| 721 | bool Accessed) { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 722 | assert(SanOpts.has(SanitizerKind::ArrayBounds) && |
Richard Smith | 6b53e22 | 2013-10-22 22:51:04 +0000 | [diff] [blame] | 723 | "should not be called unless adding bounds checks"); |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 724 | SanitizerScope SanScope(this); |
Richard Smith | 2847b22 | 2013-02-24 01:56:24 +0000 | [diff] [blame] | 725 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 726 | QualType IndexedType; |
| 727 | llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType); |
| 728 | if (!Bound) |
| 729 | return; |
| 730 | |
| 731 | bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType(); |
| 732 | llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned); |
| 733 | llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false); |
| 734 | |
| 735 | llvm::Constant *StaticData[] = { |
| 736 | EmitCheckSourceLocation(E->getExprLoc()), |
| 737 | EmitCheckTypeDescriptor(IndexedType), |
| 738 | EmitCheckTypeDescriptor(IndexType) |
| 739 | }; |
| 740 | llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal) |
| 741 | : Builder.CreateICmpULE(IndexVal, BoundVal); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 742 | EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds), "out_of_bounds", |
| 743 | StaticData, Index); |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 747 | CodeGenFunction::ComplexPairTy CodeGenFunction:: |
| 748 | EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 749 | bool isInc, bool isPre) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 750 | ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 752 | llvm::Value *NextVal; |
| 753 | if (isa<llvm::IntegerType>(InVal.first->getType())) { |
| 754 | uint64_t AmountVal = isInc ? 1 : -1; |
| 755 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 757 | // Add the inc/dec to the real part. |
| 758 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 759 | } else { |
| 760 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
| 761 | llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); |
| 762 | if (!isInc) |
| 763 | FVal.changeSign(); |
| 764 | NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 766 | // Add the inc/dec to the real part. |
| 767 | NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 768 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 770 | ComplexPairTy IncVal(NextVal, InVal.second); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 772 | // Store the updated result through the lvalue. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 773 | EmitStoreOfComplex(IncVal, LV, /*init*/ false); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 775 | // If this is a postinc, return the value read from memory, otherwise use the |
| 776 | // updated value. |
| 777 | return isPre ? IncVal : InVal; |
| 778 | } |
| 779 | |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 780 | void CodeGenModule::EmitExplicitCastExprType(const ExplicitCastExpr *E, |
| 781 | CodeGenFunction *CGF) { |
| 782 | // Bind VLAs in the cast type. |
| 783 | if (CGF && E->getType()->isVariablyModifiedType()) |
| 784 | CGF->EmitVariablyModifiedType(E->getType()); |
| 785 | |
| 786 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 787 | DI->EmitExplicitCastType(E->getType()); |
| 788 | } |
| 789 | |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 790 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 791 | // LValue Expression Emission |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 792 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 793 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 794 | /// EmitPointerWithAlignment - Given an expression of pointer type, try to |
| 795 | /// derive a more accurate bound on the alignment of the pointer. |
| 796 | Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E, |
| 797 | AlignmentSource *Source) { |
| 798 | // We allow this with ObjC object pointers because of fragile ABIs. |
| 799 | assert(E->getType()->isPointerType() || |
| 800 | E->getType()->isObjCObjectPointerType()); |
| 801 | E = E->IgnoreParens(); |
| 802 | |
| 803 | // Casts: |
| 804 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 805 | if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE)) |
| 806 | CGM.EmitExplicitCastExprType(ECE, this); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 807 | |
| 808 | switch (CE->getCastKind()) { |
| 809 | // Non-converting casts (but not C's implicit conversion from void*). |
| 810 | case CK_BitCast: |
| 811 | case CK_NoOp: |
| 812 | if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) { |
| 813 | if (PtrTy->getPointeeType()->isVoidType()) |
| 814 | break; |
| 815 | |
| 816 | AlignmentSource InnerSource; |
| 817 | Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), &InnerSource); |
| 818 | if (Source) *Source = InnerSource; |
| 819 | |
| 820 | // If this is an explicit bitcast, and the source l-value is |
| 821 | // opaque, honor the alignment of the casted-to type. |
| 822 | if (isa<ExplicitCastExpr>(CE) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 823 | InnerSource != AlignmentSource::Decl) { |
| 824 | Addr = Address(Addr.getPointer(), |
| 825 | getNaturalPointeeTypeAlignment(E->getType(), Source)); |
| 826 | } |
| 827 | |
Peter Collingbourne | 574975e | 2016-01-14 02:49:48 +0000 | [diff] [blame] | 828 | if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) && |
| 829 | CE->getCastKind() == CK_BitCast) { |
Peter Collingbourne | ee381ff | 2015-09-09 00:01:31 +0000 | [diff] [blame] | 830 | if (auto PT = E->getType()->getAs<PointerType>()) |
| 831 | EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(), |
| 832 | /*MayBeNull=*/true, |
| 833 | CodeGenFunction::CFITCK_UnrelatedCast, |
| 834 | CE->getLocStart()); |
| 835 | } |
| 836 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 837 | return Builder.CreateBitCast(Addr, ConvertType(E->getType())); |
| 838 | } |
| 839 | break; |
| 840 | |
| 841 | // Array-to-pointer decay. |
| 842 | case CK_ArrayToPointerDecay: |
| 843 | return EmitArrayToPointerDecay(CE->getSubExpr(), Source); |
| 844 | |
| 845 | // Derived-to-base conversions. |
| 846 | case CK_UncheckedDerivedToBase: |
| 847 | case CK_DerivedToBase: { |
| 848 | Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), Source); |
| 849 | auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl(); |
| 850 | return GetAddressOfBaseClass(Addr, Derived, |
| 851 | CE->path_begin(), CE->path_end(), |
| 852 | ShouldNullCheckClassCastValue(CE), |
| 853 | CE->getExprLoc()); |
| 854 | } |
| 855 | |
| 856 | // TODO: Is there any reason to treat base-to-derived conversions |
| 857 | // specially? |
| 858 | default: |
| 859 | break; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // Unary &. |
| 864 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 865 | if (UO->getOpcode() == UO_AddrOf) { |
| 866 | LValue LV = EmitLValue(UO->getSubExpr()); |
| 867 | if (Source) *Source = LV.getAlignmentSource(); |
| 868 | return LV.getAddress(); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // TODO: conditional operators, comma. |
| 873 | |
| 874 | // Otherwise, use the alignment of the type. |
| 875 | CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), Source); |
| 876 | return Address(EmitScalarExpr(E), Align); |
| 877 | } |
| 878 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 879 | RValue CodeGenFunction::GetUndefRValue(QualType Ty) { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 880 | if (Ty->isVoidType()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 881 | return RValue::get(nullptr); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 882 | |
| 883 | switch (getEvaluationKind(Ty)) { |
| 884 | case TEK_Complex: { |
| 885 | llvm::Type *EltTy = |
| 886 | ConvertType(Ty->castAs<ComplexType>()->getElementType()); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 887 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 888 | return RValue::getComplex(std::make_pair(U, U)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 889 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 891 | // If this is a use of an undefined aggregate type, the aggregate must have an |
| 892 | // identifiable address. Just because the contents of the value are undefined |
| 893 | // doesn't mean that the address can't be taken and compared. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 894 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 895 | Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp"); |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 896 | return RValue::getAggregate(DestPtr); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 897 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 898 | |
| 899 | case TEK_Scalar: |
| 900 | return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); |
| 901 | } |
| 902 | llvm_unreachable("bad evaluation kind"); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 905 | RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, |
| 906 | const char *Name) { |
| 907 | ErrorUnsupported(E, Name); |
| 908 | return GetUndefRValue(E->getType()); |
| 909 | } |
| 910 | |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 911 | LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, |
| 912 | const char *Name) { |
| 913 | ErrorUnsupported(E, Name); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 914 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 915 | return MakeAddrLValue(Address(llvm::UndefValue::get(Ty), CharUnits::One()), |
| 916 | E->getType()); |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 919 | LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 920 | LValue LV; |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 921 | if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 922 | LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true); |
| 923 | else |
| 924 | LV = EmitLValue(E); |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 925 | if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 926 | EmitTypeCheck(TCK, E->getExprLoc(), LV.getPointer(), |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 927 | E->getType(), LV.getAlignment()); |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 928 | return LV; |
| 929 | } |
| 930 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 931 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 932 | /// of the expression. |
| 933 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 934 | /// This can return one of two things: a simple address or a bitfield reference. |
| 935 | /// In either case, the LLVM Value* in the LValue structure is guaranteed to be |
| 936 | /// an LLVM pointer type. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 937 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 938 | /// If this returns a bitfield reference, nothing about the pointee type of the |
| 939 | /// LLVM value is known: For example, it may not be a pointer to an integer. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 940 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 941 | /// If this returns a normal address, and if the lvalue's C type is fixed size, |
| 942 | /// this method guarantees that the returned pointer type will point to an LLVM |
| 943 | /// type of the same size of the lvalue's type. If the lvalue has a variable |
| 944 | /// length type, this is not possible. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 945 | /// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 946 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 947 | ApplyDebugLocation DL(*this, E); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 948 | switch (E->getStmtClass()) { |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 949 | default: return EmitUnsupportedLValue(E, "l-value expression"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 950 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 951 | case Expr::ObjCPropertyRefExprClass: |
| 952 | llvm_unreachable("cannot emit a property reference directly"); |
| 953 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 954 | case Expr::ObjCSelectorExprClass: |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 955 | return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); |
Fariborz Jahanian | 531c16f | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 956 | case Expr::ObjCIsaExprClass: |
| 957 | return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 958 | case Expr::BinaryOperatorClass: |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 959 | return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); |
David Majnemer | ce27e42 | 2015-02-14 01:48:17 +0000 | [diff] [blame] | 960 | case Expr::CompoundAssignOperatorClass: { |
| 961 | QualType Ty = E->getType(); |
| 962 | if (const AtomicType *AT = Ty->getAs<AtomicType>()) |
| 963 | Ty = AT->getValueType(); |
| 964 | if (!Ty->isAnyComplexType()) |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 965 | return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
| 966 | return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
David Majnemer | ce27e42 | 2015-02-14 01:48:17 +0000 | [diff] [blame] | 967 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 968 | case Expr::CallExprClass: |
Anders Carlsson | c82555f | 2009-09-01 21:18:52 +0000 | [diff] [blame] | 969 | case Expr::CXXMemberCallExprClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 970 | case Expr::CXXOperatorCallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 971 | case Expr::UserDefinedLiteralClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 972 | return EmitCallExprLValue(cast<CallExpr>(E)); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 973 | case Expr::VAArgExprClass: |
| 974 | return EmitVAArgExprLValue(cast<VAArgExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 975 | case Expr::DeclRefExprClass: |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 976 | return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Eric Christopher | d98e424 | 2011-09-08 17:15:04 +0000 | [diff] [blame] | 977 | case Expr::ParenExprClass: |
| 978 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 979 | case Expr::GenericSelectionExprClass: |
| 980 | return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr()); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 981 | case Expr::PredefinedExprClass: |
| 982 | return EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 983 | case Expr::StringLiteralClass: |
| 984 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 985 | case Expr::ObjCEncodeExprClass: |
| 986 | return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 987 | case Expr::PseudoObjectExprClass: |
| 988 | return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 989 | case Expr::InitListExprClass: |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 990 | return EmitInitListLValue(cast<InitListExpr>(E)); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 991 | case Expr::CXXTemporaryObjectExprClass: |
| 992 | case Expr::CXXConstructExprClass: |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 993 | return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); |
| 994 | case Expr::CXXBindTemporaryExprClass: |
| 995 | return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 996 | case Expr::CXXUuidofExprClass: |
| 997 | return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E)); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 998 | case Expr::LambdaExprClass: |
| 999 | return EmitLambdaLValue(cast<LambdaExpr>(E)); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1000 | |
| 1001 | case Expr::ExprWithCleanupsClass: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1002 | const auto *cleanups = cast<ExprWithCleanups>(E); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1003 | enterFullExpression(cleanups); |
| 1004 | RunCleanupsScope Scope(*this); |
| 1005 | return EmitLValue(cleanups->getSubExpr()); |
| 1006 | } |
| 1007 | |
Anders Carlsson | 52ce3bb | 2009-11-14 01:51:50 +0000 | [diff] [blame] | 1008 | case Expr::CXXDefaultArgExprClass: |
| 1009 | return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1010 | case Expr::CXXDefaultInitExprClass: { |
| 1011 | CXXDefaultInitExprScope Scope(*this); |
| 1012 | return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr()); |
| 1013 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1014 | case Expr::CXXTypeidExprClass: |
| 1015 | return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1016 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 1017 | case Expr::ObjCMessageExprClass: |
| 1018 | return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1019 | case Expr::ObjCIvarRefExprClass: |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1020 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 1021 | case Expr::StmtExprClass: |
| 1022 | return EmitStmtExprLValue(cast<StmtExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1023 | case Expr::UnaryOperatorClass: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1024 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 1025 | case Expr::ArraySubscriptExprClass: |
| 1026 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 1027 | case Expr::OMPArraySectionExprClass: |
| 1028 | return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E)); |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1029 | case Expr::ExtVectorElementExprClass: |
| 1030 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1031 | case Expr::MemberExprClass: |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1032 | return EmitMemberExpr(cast<MemberExpr>(E)); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1033 | case Expr::CompoundLiteralExprClass: |
| 1034 | return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 1035 | case Expr::ConditionalOperatorClass: |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1036 | return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1037 | case Expr::BinaryConditionalOperatorClass: |
| 1038 | return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E)); |
Chris Lattner | 053441f | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1039 | case Expr::ChooseExprClass: |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 1040 | return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr()); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1041 | case Expr::OpaqueValueExprClass: |
| 1042 | return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E)); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1043 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 1044 | return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); |
Chris Lattner | 63d06ab | 2009-03-18 04:02:57 +0000 | [diff] [blame] | 1045 | case Expr::ImplicitCastExprClass: |
| 1046 | case Expr::CStyleCastExprClass: |
| 1047 | case Expr::CXXFunctionalCastExprClass: |
| 1048 | case Expr::CXXStaticCastExprClass: |
| 1049 | case Expr::CXXDynamicCastExprClass: |
| 1050 | case Expr::CXXReinterpretCastExprClass: |
| 1051 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1052 | case Expr::ObjCBridgedCastExprClass: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1053 | return EmitCastLValue(cast<CastExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1055 | case Expr::MaterializeTemporaryExprClass: |
| 1056 | return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1060 | /// Given an object of the given canonical type, can we safely copy a |
| 1061 | /// value out of it based on its initializer? |
| 1062 | static bool isConstantEmittableObjectType(QualType type) { |
| 1063 | assert(type.isCanonical()); |
| 1064 | assert(!type->isReferenceType()); |
| 1065 | |
| 1066 | // Must be const-qualified but non-volatile. |
| 1067 | Qualifiers qs = type.getLocalQualifiers(); |
| 1068 | if (!qs.hasConst() || qs.hasVolatile()) return false; |
| 1069 | |
| 1070 | // Otherwise, all object types satisfy this except C++ classes with |
| 1071 | // mutable subobjects or non-trivial copy/destroy behavior. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1072 | if (const auto *RT = dyn_cast<RecordType>(type)) |
| 1073 | if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1074 | if (RD->hasMutableFields() || !RD->isTrivial()) |
| 1075 | return false; |
| 1076 | |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | /// Can we constant-emit a load of a reference to a variable of the |
| 1081 | /// given type? This is different from predicates like |
| 1082 | /// Decl::isUsableInConstantExpressions because we do want it to apply |
| 1083 | /// in situations that don't necessarily satisfy the language's rules |
| 1084 | /// for this (e.g. C++'s ODR-use rules). For example, we want to able |
| 1085 | /// to do this with const float variables even if those variables |
| 1086 | /// aren't marked 'constexpr'. |
| 1087 | enum ConstantEmissionKind { |
| 1088 | CEK_None, |
| 1089 | CEK_AsReferenceOnly, |
| 1090 | CEK_AsValueOrReference, |
| 1091 | CEK_AsValueOnly |
| 1092 | }; |
| 1093 | static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) { |
| 1094 | type = type.getCanonicalType(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1095 | if (const auto *ref = dyn_cast<ReferenceType>(type)) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1096 | if (isConstantEmittableObjectType(ref->getPointeeType())) |
| 1097 | return CEK_AsValueOrReference; |
| 1098 | return CEK_AsReferenceOnly; |
| 1099 | } |
| 1100 | if (isConstantEmittableObjectType(type)) |
| 1101 | return CEK_AsValueOnly; |
| 1102 | return CEK_None; |
| 1103 | } |
| 1104 | |
| 1105 | /// Try to emit a reference to the given value without producing it as |
| 1106 | /// an l-value. This is actually more than an optimization: we can't |
| 1107 | /// produce an l-value for variables that we never actually captured |
| 1108 | /// in a block or lambda, which means const int variables or constexpr |
| 1109 | /// literals or similar. |
| 1110 | CodeGenFunction::ConstantEmission |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1111 | CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { |
| 1112 | ValueDecl *value = refExpr->getDecl(); |
| 1113 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1114 | // The value needs to be an enum constant or a constant variable. |
| 1115 | ConstantEmissionKind CEK; |
| 1116 | if (isa<ParmVarDecl>(value)) { |
| 1117 | CEK = CEK_None; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1118 | } else if (auto *var = dyn_cast<VarDecl>(value)) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1119 | CEK = checkVarTypeForConstantEmission(var->getType()); |
| 1120 | } else if (isa<EnumConstantDecl>(value)) { |
| 1121 | CEK = CEK_AsValueOnly; |
| 1122 | } else { |
| 1123 | CEK = CEK_None; |
| 1124 | } |
| 1125 | if (CEK == CEK_None) return ConstantEmission(); |
| 1126 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1127 | Expr::EvalResult result; |
| 1128 | bool resultIsReference; |
| 1129 | QualType resultType; |
| 1130 | |
| 1131 | // It's best to evaluate all the way as an r-value if that's permitted. |
| 1132 | if (CEK != CEK_AsReferenceOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1133 | refExpr->EvaluateAsRValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1134 | resultIsReference = false; |
| 1135 | resultType = refExpr->getType(); |
| 1136 | |
| 1137 | // Otherwise, try to evaluate as an l-value. |
| 1138 | } else if (CEK != CEK_AsValueOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1139 | refExpr->EvaluateAsLValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1140 | resultIsReference = true; |
| 1141 | resultType = value->getType(); |
| 1142 | |
| 1143 | // Failure. |
| 1144 | } else { |
| 1145 | return ConstantEmission(); |
| 1146 | } |
| 1147 | |
| 1148 | // In any case, if the initializer has side-effects, abandon ship. |
| 1149 | if (result.HasSideEffects) |
| 1150 | return ConstantEmission(); |
| 1151 | |
| 1152 | // Emit as a constant. |
| 1153 | llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this); |
| 1154 | |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 1155 | // Make sure we emit a debug reference to the global variable. |
| 1156 | // This should probably fire even for |
| 1157 | if (isa<VarDecl>(value)) { |
| 1158 | if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value))) |
| 1159 | EmitDeclRefExprDbgValue(refExpr, C); |
| 1160 | } else { |
| 1161 | assert(isa<EnumConstantDecl>(value)); |
| 1162 | EmitDeclRefExprDbgValue(refExpr, C); |
| 1163 | } |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1164 | |
| 1165 | // If we emitted a reference constant, we need to dereference that. |
| 1166 | if (resultIsReference) |
| 1167 | return ConstantEmission::forReference(C); |
| 1168 | |
| 1169 | return ConstantEmission::forValue(C); |
| 1170 | } |
| 1171 | |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1172 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, |
| 1173 | SourceLocation Loc) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1174 | return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1175 | lvalue.getType(), Loc, lvalue.getAlignmentSource(), |
| 1176 | lvalue.getTBAAInfo(), |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1177 | lvalue.getTBAABaseType(), lvalue.getTBAAOffset(), |
| 1178 | lvalue.isNontemporal()); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1181 | static bool hasBooleanRepresentation(QualType Ty) { |
| 1182 | if (Ty->isBooleanType()) |
| 1183 | return true; |
| 1184 | |
| 1185 | if (const EnumType *ET = Ty->getAs<EnumType>()) |
| 1186 | return ET->getDecl()->getIntegerType()->isBooleanType(); |
| 1187 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 1188 | if (const AtomicType *AT = Ty->getAs<AtomicType>()) |
| 1189 | return hasBooleanRepresentation(AT->getValueType()); |
| 1190 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
| 1193 | |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1194 | static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, |
| 1195 | llvm::APInt &Min, llvm::APInt &End, |
| 1196 | bool StrictEnums) { |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1197 | const EnumType *ET = Ty->getAs<EnumType>(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1198 | bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums && |
| 1199 | ET && !ET->getDecl()->isFixed(); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1200 | bool IsBool = hasBooleanRepresentation(Ty); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1201 | if (!IsBool && !IsRegularCPlusPlusEnum) |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1202 | return false; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1203 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1204 | if (IsBool) { |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1205 | Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0); |
| 1206 | End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1207 | } else { |
| 1208 | const EnumDecl *ED = ET->getDecl(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1209 | llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType()); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1210 | unsigned Bitwidth = LTy->getScalarSizeInBits(); |
| 1211 | unsigned NumNegativeBits = ED->getNumNegativeBits(); |
| 1212 | unsigned NumPositiveBits = ED->getNumPositiveBits(); |
| 1213 | |
| 1214 | if (NumNegativeBits) { |
| 1215 | unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); |
| 1216 | assert(NumBits <= Bitwidth); |
| 1217 | End = llvm::APInt(Bitwidth, 1) << (NumBits - 1); |
| 1218 | Min = -End; |
| 1219 | } else { |
| 1220 | assert(NumPositiveBits <= Bitwidth); |
| 1221 | End = llvm::APInt(Bitwidth, 1) << NumPositiveBits; |
| 1222 | Min = llvm::APInt(Bitwidth, 0); |
| 1223 | } |
| 1224 | } |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { |
| 1229 | llvm::APInt Min, End; |
| 1230 | if (!getRangeForType(*this, Ty, Min, End, |
| 1231 | CGM.getCodeGenOpts().StrictEnums)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1232 | return nullptr; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1233 | |
Duncan Sands | c720e78 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 1234 | llvm::MDBuilder MDHelper(getLLVMContext()); |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 1235 | return MDHelper.createRange(Min, End); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1238 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, |
| 1239 | QualType Ty, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1240 | SourceLocation Loc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1241 | AlignmentSource AlignSource, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1242 | llvm::MDNode *TBAAInfo, |
| 1243 | QualType TBAABaseType, |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1244 | uint64_t TBAAOffset, |
| 1245 | bool isNontemporal) { |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1246 | // For better performance, handle vector loads differently. |
| 1247 | if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1248 | const llvm::Type *EltTy = Addr.getElementType(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1249 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1250 | const auto *VTy = cast<llvm::VectorType>(EltTy); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1251 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1252 | // Handle vectors of size 3 like size 4 for better performance. |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1253 | if (VTy->getNumElements() == 3) { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1254 | |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1255 | // Bitcast to vec4 type. |
| 1256 | llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(), |
| 1257 | 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1258 | Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4"); |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1259 | // Now load value. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1260 | llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4"); |
Richard Smith | f0480fc | 2012-12-13 05:41:48 +0000 | [diff] [blame] | 1261 | |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1262 | // Shuffle vector to get vec3. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1263 | V = Builder.CreateShuffleVector(V, llvm::UndefValue::get(vec4Ty), |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 1264 | {0, 1, 2}, "extractVec"); |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1265 | return EmitFromMemory(V, Ty); |
| 1266 | } |
| 1267 | } |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1268 | |
| 1269 | // Atomic operations have to be done on integral types. |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 1270 | if (Ty->isAtomicType() || typeIsSuitableForInlineAtomic(Ty, Volatile)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1271 | LValue lvalue = |
| 1272 | LValue::MakeAddr(Addr, Ty, getContext(), AlignSource, TBAAInfo); |
David Majnemer | eeaec26 | 2015-02-14 02:18:14 +0000 | [diff] [blame] | 1273 | return EmitAtomicLoad(lvalue, Loc).getScalarVal(); |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1274 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1275 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1276 | llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile); |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1277 | if (isNontemporal) { |
| 1278 | llvm::MDNode *Node = llvm::MDNode::get( |
| 1279 | Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1))); |
| 1280 | Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); |
| 1281 | } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1282 | if (TBAAInfo) { |
| 1283 | llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, |
| 1284 | TBAAOffset); |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 1285 | if (TBAAPath) |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1286 | CGM.DecorateInstructionWithTBAA(Load, TBAAPath, |
| 1287 | false /*ConvertTypeToTag*/); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1288 | } |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1289 | |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 1290 | bool NeedsBoolCheck = |
| 1291 | SanOpts.has(SanitizerKind::Bool) && hasBooleanRepresentation(Ty); |
| 1292 | bool NeedsEnumCheck = |
| 1293 | SanOpts.has(SanitizerKind::Enum) && Ty->getAs<EnumType>(); |
| 1294 | if (NeedsBoolCheck || NeedsEnumCheck) { |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 1295 | SanitizerScope SanScope(this); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1296 | llvm::APInt Min, End; |
| 1297 | if (getRangeForType(*this, Ty, Min, End, true)) { |
| 1298 | --End; |
| 1299 | llvm::Value *Check; |
| 1300 | if (!Min) |
| 1301 | Check = Builder.CreateICmpULE( |
| 1302 | Load, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1303 | else { |
| 1304 | llvm::Value *Upper = Builder.CreateICmpSLE( |
| 1305 | Load, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1306 | llvm::Value *Lower = Builder.CreateICmpSGE( |
| 1307 | Load, llvm::ConstantInt::get(getLLVMContext(), Min)); |
| 1308 | Check = Builder.CreateAnd(Upper, Lower); |
| 1309 | } |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1310 | llvm::Constant *StaticArgs[] = { |
| 1311 | EmitCheckSourceLocation(Loc), |
| 1312 | EmitCheckTypeDescriptor(Ty) |
| 1313 | }; |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 1314 | SanitizerMask Kind = NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 1315 | EmitCheck(std::make_pair(Check, Kind), "load_invalid_value", StaticArgs, |
| 1316 | EmitCheckValue(Load)); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } else if (CGM.getCodeGenOpts().OptimizationLevel > 0) |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1319 | if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) |
| 1320 | Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1321 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1322 | return EmitFromMemory(Load, Ty); |
NAKAMURA Takumi | 2681efc | 2012-03-24 14:43:42 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1325 | llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { |
| 1326 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1327 | if (hasBooleanRepresentation(Ty)) { |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1328 | // This should really always be an i1, but sometimes it's already |
| 1329 | // an i8, and it's awkward to track those cases down. |
| 1330 | if (Value->getType()->isIntegerTy(1)) |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1331 | return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool"); |
| 1332 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1333 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | return Value; |
| 1337 | } |
| 1338 | |
| 1339 | llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { |
| 1340 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1341 | if (hasBooleanRepresentation(Ty)) { |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1342 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1343 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1344 | return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); |
| 1345 | } |
| 1346 | |
| 1347 | return Value; |
| 1348 | } |
| 1349 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1350 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, |
| 1351 | bool Volatile, QualType Ty, |
| 1352 | AlignmentSource AlignSource, |
| 1353 | llvm::MDNode *TBAAInfo, |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1354 | bool isInit, QualType TBAABaseType, |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1355 | uint64_t TBAAOffset, |
| 1356 | bool isNontemporal) { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1357 | |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1358 | // Handle vectors differently to get better performance. |
| 1359 | if (Ty->isVectorType()) { |
| 1360 | llvm::Type *SrcTy = Value->getType(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1361 | auto *VecTy = cast<llvm::VectorType>(SrcTy); |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1362 | // Handle vec3 special. |
| 1363 | if (VecTy->getNumElements() == 3) { |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1364 | // Our source is a vec3, do a shuffle vector to make it a vec4. |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 1365 | llvm::Constant *Mask[] = {Builder.getInt32(0), Builder.getInt32(1), |
| 1366 | Builder.getInt32(2), |
| 1367 | llvm::UndefValue::get(Builder.getInt32Ty())}; |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1368 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1369 | Value = Builder.CreateShuffleVector(Value, |
| 1370 | llvm::UndefValue::get(VecTy), |
| 1371 | MaskV, "extractVec"); |
| 1372 | SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4); |
| 1373 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1374 | if (Addr.getElementType() != SrcTy) { |
| 1375 | Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp"); |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1378 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1379 | Value = EmitToMemory(Value, Ty); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1380 | |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 1381 | if (Ty->isAtomicType() || |
| 1382 | (!isInit && typeIsSuitableForInlineAtomic(Ty, Volatile))) { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1383 | EmitAtomicStore(RValue::get(Value), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1384 | LValue::MakeAddr(Addr, Ty, getContext(), |
| 1385 | AlignSource, TBAAInfo), |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1386 | isInit); |
| 1387 | return; |
| 1388 | } |
| 1389 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1390 | llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1391 | if (isNontemporal) { |
| 1392 | llvm::MDNode *Node = |
| 1393 | llvm::MDNode::get(Store->getContext(), |
| 1394 | llvm::ConstantAsMetadata::get(Builder.getInt32(1))); |
| 1395 | Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); |
| 1396 | } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1397 | if (TBAAInfo) { |
| 1398 | llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, |
| 1399 | TBAAOffset); |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 1400 | if (TBAAPath) |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1401 | CGM.DecorateInstructionWithTBAA(Store, TBAAPath, |
| 1402 | false /*ConvertTypeToTag*/); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1403 | } |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1406 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1407 | bool isInit) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1408 | EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1409 | lvalue.getType(), lvalue.getAlignmentSource(), |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1410 | lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(), |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1411 | lvalue.getTBAAOffset(), lvalue.isNontemporal()); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1414 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this |
| 1415 | /// method emits the address of the lvalue, then loads the result as an rvalue, |
| 1416 | /// returning the rvalue. |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1417 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) { |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1418 | if (LV.isObjCWeak()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1419 | // load of a __weak object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1420 | Address AddrWeakObj = LV.getAddress(); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1421 | return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, |
| 1422 | AddrWeakObj)); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1423 | } |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 1424 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1425 | // In MRC mode, we do a load+autorelease. |
| 1426 | if (!getLangOpts().ObjCAutoRefCount) { |
| 1427 | return RValue::get(EmitARCLoadWeak(LV.getAddress())); |
| 1428 | } |
| 1429 | |
| 1430 | // In ARC mode, we load retained and then consume the value. |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 1431 | llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress()); |
| 1432 | Object = EmitObjCConsumeObject(LV.getType(), Object); |
| 1433 | return RValue::get(Object); |
| 1434 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1435 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1436 | if (LV.isSimple()) { |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 1437 | assert(!LV.getType()->isFunctionType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1438 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1439 | // Everything needs a load. |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1440 | return RValue::get(EmitLoadOfScalar(LV, Loc)); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1441 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1442 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1443 | if (LV.isVectorElt()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1444 | llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(), |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1445 | LV.isVolatileQualified()); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1446 | return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(), |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1447 | "vecext")); |
| 1448 | } |
Chris Lattner | 73ab9b3 | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 1449 | |
| 1450 | // If this is a reference to a subset of the elements of a vector, either |
| 1451 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1452 | if (LV.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1453 | return EmitLoadOfExtVectorElementLValue(LV); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1454 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1455 | // Global Register variables always invoke intrinsics |
| 1456 | if (LV.isGlobalReg()) |
| 1457 | return EmitLoadOfGlobalRegLValue(LV); |
| 1458 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1459 | assert(LV.isBitField() && "Unknown LValue type!"); |
| 1460 | return EmitLoadOfBitfieldLValue(LV); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1463 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1464 | const CGBitFieldInfo &Info = LV.getBitFieldInfo(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1465 | |
Daniel Dunbar | 3447a02 | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 1466 | // Get the output type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1467 | llvm::Type *ResLTy = ConvertType(LV.getType()); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1468 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1469 | Address Ptr = LV.getBitFieldAddress(); |
| 1470 | llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1471 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1472 | if (Info.IsSigned) { |
David Greene | c5ff624 | 2013-01-15 23:13:47 +0000 | [diff] [blame] | 1473 | assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1474 | unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size; |
| 1475 | if (HighBits) |
| 1476 | Val = Builder.CreateShl(Val, HighBits, "bf.shl"); |
| 1477 | if (Info.Offset + HighBits) |
| 1478 | Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr"); |
| 1479 | } else { |
| 1480 | if (Info.Offset) |
| 1481 | Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr"); |
Eli Bendersky | 03b913d | 2012-12-18 22:22:16 +0000 | [diff] [blame] | 1482 | if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize) |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1483 | Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1484 | Info.Size), |
| 1485 | "bf.clear"); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1486 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1487 | Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast"); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1488 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1489 | return RValue::get(Val); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1492 | // If this is a reference to a subset of the elements of a vector, create an |
| 1493 | // appropriate shufflevector. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1494 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1495 | llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(), |
| 1496 | LV.isVolatileQualified()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1497 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1498 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1499 | |
| 1500 | // If the result of the expression is a non-vector type, we must be extracting |
| 1501 | // a single element. Just codegen as an extractelement. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1502 | const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1503 | if (!ExprVT) { |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1504 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Michael J. Spencer | dd59775 | 2014-05-31 00:22:12 +0000 | [diff] [blame] | 1505 | llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1506 | return RValue::get(Builder.CreateExtractElement(Vec, Elt)); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1507 | } |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Always use shuffle vector to try to retain the original program structure |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1510 | unsigned NumResultElts = ExprVT->getNumElements(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1512 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1513 | for (unsigned i = 0; i != NumResultElts; ++i) |
| 1514 | Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1516 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1517 | Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1518 | MaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1519 | return RValue::get(Vec); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1522 | /// @brief Generates lvalue for partial ext_vector access. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1523 | Address CodeGenFunction::EmitExtVectorElementLValue(LValue LV) { |
| 1524 | Address VectorAddress = LV.getExtVectorAddress(); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1525 | const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); |
| 1526 | QualType EQT = ExprVT->getElementType(); |
| 1527 | llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1528 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1529 | Address CastToPointerElement = |
| 1530 | Builder.CreateElementBitCast(VectorAddress, VectorElementTy, |
| 1531 | "conv.ptr.element"); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1532 | |
| 1533 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
| 1534 | unsigned ix = getAccessedFieldNo(0, Elts); |
| 1535 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1536 | Address VectorBasePtrPlusIx = |
| 1537 | Builder.CreateConstInBoundsGEP(CastToPointerElement, ix, |
| 1538 | getContext().getTypeSizeInChars(EQT), |
| 1539 | "vector.elt"); |
| 1540 | |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1541 | return VectorBasePtrPlusIx; |
| 1542 | } |
| 1543 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1544 | /// @brief Load of global gamed gegisters are always calls to intrinsics. |
| 1545 | RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) { |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1546 | assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) && |
| 1547 | "Bad type for register variable"); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1548 | llvm::MDNode *RegName = cast<llvm::MDNode>( |
| 1549 | cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata()); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1550 | |
| 1551 | // We accept integer and pointer types only |
| 1552 | llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType()); |
| 1553 | llvm::Type *Ty = OrigTy; |
| 1554 | if (OrigTy->isPointerTy()) |
| 1555 | Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); |
| 1556 | llvm::Type *Types[] = { Ty }; |
| 1557 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1558 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1559 | llvm::Value *Call = Builder.CreateCall( |
| 1560 | F, llvm::MetadataAsValue::get(Ty->getContext(), RegName)); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1561 | if (OrigTy->isPointerTy()) |
| 1562 | Call = Builder.CreateIntToPtr(Call, OrigTy); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1563 | return RValue::get(Call); |
| 1564 | } |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1565 | |
Chris Lattner | 9369a56 | 2007-06-29 16:31:29 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1567 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 1568 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 1569 | /// is 'Ty'. |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1570 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1571 | bool isInit) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1572 | if (!Dst.isSimple()) { |
| 1573 | if (Dst.isVectorElt()) { |
| 1574 | // Read/modify/write the vector, inserting the new element. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1575 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(), |
| 1576 | Dst.isVolatileQualified()); |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1577 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1578 | Dst.getVectorIdx(), "vecins"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1579 | Builder.CreateStore(Vec, Dst.getVectorAddress(), |
| 1580 | Dst.isVolatileQualified()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1581 | return; |
| 1582 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1583 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1584 | // If this is an update of extended vector elements, insert them as |
| 1585 | // appropriate. |
| 1586 | if (Dst.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1587 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1588 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1589 | if (Dst.isGlobalReg()) |
| 1590 | return EmitStoreThroughGlobalRegLValue(Src, Dst); |
| 1591 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1592 | assert(Dst.isBitField() && "Unknown LValue type"); |
| 1593 | return EmitStoreThroughBitfieldLValue(Src, Dst); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1594 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1595 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1596 | // There's special magic for assigning into an ARC-qualified l-value. |
| 1597 | if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) { |
| 1598 | switch (Lifetime) { |
| 1599 | case Qualifiers::OCL_None: |
| 1600 | llvm_unreachable("present but none"); |
| 1601 | |
| 1602 | case Qualifiers::OCL_ExplicitNone: |
| 1603 | // nothing special |
| 1604 | break; |
| 1605 | |
| 1606 | case Qualifiers::OCL_Strong: |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1607 | EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1608 | return; |
| 1609 | |
| 1610 | case Qualifiers::OCL_Weak: |
| 1611 | EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true); |
| 1612 | return; |
| 1613 | |
| 1614 | case Qualifiers::OCL_Autoreleasing: |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1615 | Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(), |
| 1616 | Src.getScalarVal())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1617 | // fall into the normal path |
| 1618 | break; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1622 | if (Dst.isObjCWeak() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1623 | // load of a __weak object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1624 | Address LvalueDst = Dst.getAddress(); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1625 | llvm::Value *src = Src.getScalarVal(); |
Mike Stump | ca5ae66 | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 1626 | CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1627 | return; |
| 1628 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1629 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1630 | if (Dst.isObjCStrong() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1631 | // load of a __strong object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1632 | Address LvalueDst = Dst.getAddress(); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1633 | llvm::Value *src = Src.getScalarVal(); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1634 | if (Dst.isObjCIvar()) { |
| 1635 | assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1636 | llvm::Type *ResultType = IntPtrTy; |
| 1637 | Address dst = EmitPointerWithAlignment(Dst.getBaseIvarExp()); |
| 1638 | llvm::Value *RHS = dst.getPointer(); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1639 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1640 | llvm::Value *LHS = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1641 | Builder.CreatePtrToInt(LvalueDst.getPointer(), ResultType, |
| 1642 | "sub.ptr.lhs.cast"); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1643 | llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); |
Fariborz Jahanian | 1f9ed58 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 1644 | CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1645 | BytesBetween); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1646 | } else if (Dst.isGlobalObjCRef()) { |
| 1647 | CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, |
| 1648 | Dst.isThreadLocalRef()); |
| 1649 | } |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 1650 | else |
| 1651 | CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1652 | return; |
| 1653 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | 6278e6a | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 1655 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1656 | EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1659 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1660 | llvm::Value **Result) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1661 | const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1662 | llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1663 | Address Ptr = Dst.getBitFieldAddress(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1664 | |
Daniel Dunbar | 67aba79 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 1665 | // Get the source value, truncated to the width of the bit-field. |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1666 | llvm::Value *SrcVal = Src.getScalarVal(); |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1667 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1668 | // Cast the source to the storage type and shift it into place. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1669 | SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(), |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1670 | /*IsSigned=*/false); |
| 1671 | llvm::Value *MaskedVal = SrcVal; |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1672 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1673 | // See if there are other bits in the bitfield's storage we'll need to load |
| 1674 | // and mask together with source before storing. |
| 1675 | if (Info.StorageSize != Info.Size) { |
| 1676 | assert(Info.StorageSize > Info.Size && "Invalid bitfield size."); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1677 | llvm::Value *Val = |
| 1678 | Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load"); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Mask the source value as needed. |
| 1681 | if (!hasBooleanRepresentation(Dst.getType())) |
| 1682 | SrcVal = Builder.CreateAnd(SrcVal, |
| 1683 | llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1684 | Info.Size), |
| 1685 | "bf.value"); |
| 1686 | MaskedVal = SrcVal; |
| 1687 | if (Info.Offset) |
| 1688 | SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl"); |
| 1689 | |
| 1690 | // Mask out the original value. |
| 1691 | Val = Builder.CreateAnd(Val, |
| 1692 | ~llvm::APInt::getBitsSet(Info.StorageSize, |
| 1693 | Info.Offset, |
| 1694 | Info.Offset + Info.Size), |
| 1695 | "bf.clear"); |
| 1696 | |
| 1697 | // Or together the unchanged values and the source value. |
| 1698 | SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set"); |
| 1699 | } else { |
| 1700 | assert(Info.Offset == 0); |
| 1701 | } |
| 1702 | |
| 1703 | // Write the new value back out. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1704 | Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified()); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1705 | |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1706 | // Return the new value of the bit-field, if requested. |
| 1707 | if (Result) { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1708 | llvm::Value *ResultVal = MaskedVal; |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1709 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1710 | // Sign extend the value if needed. |
| 1711 | if (Info.IsSigned) { |
| 1712 | assert(Info.Size <= Info.StorageSize); |
| 1713 | unsigned HighBits = Info.StorageSize - Info.Size; |
| 1714 | if (HighBits) { |
| 1715 | ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl"); |
| 1716 | ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr"); |
| 1717 | } |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1720 | ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned, |
| 1721 | "bf.result.cast"); |
Eli Friedman | 39b685e | 2012-12-19 00:26:58 +0000 | [diff] [blame] | 1722 | *Result = EmitFromMemory(ResultVal, Dst.getType()); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1723 | } |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1726 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1727 | LValue Dst) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1728 | // This access turns into a read/modify/write of the vector. Load the input |
| 1729 | // value now. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1730 | llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddress(), |
| 1731 | Dst.isVolatileQualified()); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1732 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1734 | llvm::Value *SrcVal = Src.getScalarVal(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1735 | |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1736 | if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1737 | unsigned NumSrcElts = VTy->getNumElements(); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1738 | unsigned NumDstElts = |
| 1739 | cast<llvm::VectorType>(Vec->getType())->getNumElements(); |
| 1740 | if (NumDstElts == NumSrcElts) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1741 | // Use shuffle vector is the src and destination are the same number of |
| 1742 | // elements and restore the vector mask since it is on the side it will be |
| 1743 | // stored. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1744 | SmallVector<llvm::Constant*, 4> Mask(NumDstElts); |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1745 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1746 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1748 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1749 | Vec = Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1750 | llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1751 | MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1752 | } else if (NumDstElts > NumSrcElts) { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1753 | // Extended the source vector to the same length and then shuffle it |
| 1754 | // into the destination. |
| 1755 | // FIXME: since we're shuffling with undef, can we just use the indices |
| 1756 | // into that? This could be simpler. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1757 | SmallVector<llvm::Constant*, 4> ExtMask; |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1758 | for (unsigned i = 0; i != NumSrcElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1759 | ExtMask.push_back(Builder.getInt32(i)); |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1760 | ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1761 | llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1762 | llvm::Value *ExtSrcVal = |
Daniel Dunbar | 3d926cb | 2009-02-17 18:31:04 +0000 | [diff] [blame] | 1763 | Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1764 | llvm::UndefValue::get(SrcVal->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1765 | ExtMaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1766 | // build identity |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1767 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1768 | for (unsigned i = 0; i != NumDstElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1769 | Mask.push_back(Builder.getInt32(i)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1770 | |
Joey Gouly | cf4143b | 2013-11-21 17:09:05 +0000 | [diff] [blame] | 1771 | // When the vector size is odd and .odd or .hi is used, the last element |
| 1772 | // of the Elts constant array will be one past the size of the vector. |
| 1773 | // Ignore the last element here, if it is greater than the mask size. |
| 1774 | if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size()) |
| 1775 | NumSrcElts--; |
| 1776 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1777 | // modify when what gets shuffled in |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1778 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1779 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1780 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1781 | Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1782 | } else { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1783 | // We should never shorten the vector |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1784 | llvm_unreachable("unexpected shorten vector length"); |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } else { |
| 1787 | // If the Src is a scalar (not a vector) it must be updating one element. |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1788 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Michael J. Spencer | dd59775 | 2014-05-31 00:22:12 +0000 | [diff] [blame] | 1789 | llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1790 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1791 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1792 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1793 | Builder.CreateStore(Vec, Dst.getExtVectorAddress(), |
| 1794 | Dst.isVolatileQualified()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1797 | /// @brief Store of global named registers are always calls to intrinsics. |
| 1798 | void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) { |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1799 | assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) && |
| 1800 | "Bad type for register variable"); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1801 | llvm::MDNode *RegName = cast<llvm::MDNode>( |
| 1802 | cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata()); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1803 | assert(RegName && "Register LValue is not metadata"); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1804 | |
| 1805 | // We accept integer and pointer types only |
| 1806 | llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType()); |
| 1807 | llvm::Type *Ty = OrigTy; |
| 1808 | if (OrigTy->isPointerTy()) |
| 1809 | Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); |
| 1810 | llvm::Type *Types[] = { Ty }; |
| 1811 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1812 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types); |
| 1813 | llvm::Value *Value = Src.getScalarVal(); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1814 | if (OrigTy->isPointerTy()) |
| 1815 | Value = Builder.CreatePtrToInt(Value, Ty); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 1816 | Builder.CreateCall( |
| 1817 | F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value}); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Eric Christopher | c9e2a68 | 2014-05-20 17:10:39 +0000 | [diff] [blame] | 1820 | // setObjCGCLValueClass - sets class of the lvalue for the purpose of |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1821 | // generating write-barries API. It is currently a global, ivar, |
| 1822 | // or neither. |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1823 | static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1824 | LValue &LV, |
| 1825 | bool IsMemberAccess=false) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1826 | if (Ctx.getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1827 | return; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1828 | |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1829 | if (isa<ObjCIvarRefExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1830 | QualType ExpTy = E->getType(); |
| 1831 | if (IsMemberAccess && ExpTy->isPointerType()) { |
| 1832 | // If ivar is a structure pointer, assigning to field of |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1833 | // this struct follows gcc's behavior and makes it a non-ivar |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1834 | // writer-barrier conservatively. |
| 1835 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 1836 | if (ExpTy->isRecordType()) { |
| 1837 | LV.setObjCIvar(false); |
| 1838 | return; |
| 1839 | } |
| 1840 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1841 | LV.setObjCIvar(true); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1842 | auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E)); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1843 | LV.setBaseIvarExp(Exp->getBase()); |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1844 | LV.setObjCArray(E->getType()->isArrayType()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1845 | return; |
| 1846 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1847 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1848 | if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) { |
| 1849 | if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1850 | if (VD->hasGlobalStorage()) { |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1851 | LV.setGlobalObjCRef(true); |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1852 | LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1853 | } |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1854 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1855 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1856 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1857 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1858 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1859 | if (const auto *Exp = dyn_cast<UnaryOperator>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1860 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1861 | return; |
| 1862 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1863 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1864 | if (const auto *Exp = dyn_cast<ParenExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1865 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1866 | if (LV.isObjCIvar()) { |
| 1867 | // If cast is to a structure pointer, follow gcc's behavior and make it |
| 1868 | // a non-ivar write-barrier. |
| 1869 | QualType ExpTy = E->getType(); |
| 1870 | if (ExpTy->isPointerType()) |
| 1871 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 1872 | if (ExpTy->isRecordType()) |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1873 | LV.setObjCIvar(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1874 | } |
| 1875 | return; |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 1876 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1877 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1878 | if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1879 | setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV); |
| 1880 | return; |
| 1881 | } |
| 1882 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1883 | if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1884 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1885 | return; |
| 1886 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1887 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1888 | if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1889 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1890 | return; |
| 1891 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1892 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1893 | if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1894 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1895 | return; |
| 1896 | } |
| 1897 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1898 | if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) { |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1899 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1900 | if (LV.isObjCIvar() && !LV.isObjCArray()) |
| 1901 | // Using array syntax to assigning to what an ivar points to is not |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1902 | // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1903 | LV.setObjCIvar(false); |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1904 | else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1905 | // Using array syntax to assigning to what global points to is not |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 1906 | // same as assigning to the global itself. {id *G;} G[i] = 0; |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1907 | LV.setGlobalObjCRef(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1908 | return; |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1909 | } |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1910 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1911 | if (const auto *Exp = dyn_cast<MemberExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1912 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true); |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 1913 | // We don't know if member is an 'ivar', but this flag is looked at |
| 1914 | // only in the context of LV.isObjCIvar(). |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 1915 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1916 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1920 | static llvm::Value * |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 1921 | EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1922 | llvm::Value *V, llvm::Type *IRType, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1923 | StringRef Name = StringRef()) { |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1924 | unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 1925 | return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1928 | static LValue EmitThreadPrivateVarDeclLValue( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1929 | CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, |
| 1930 | llvm::Type *RealVarTy, SourceLocation Loc) { |
| 1931 | Addr = CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc); |
| 1932 | Addr = CGF.Builder.CreateElementBitCast(Addr, RealVarTy); |
| 1933 | return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); |
| 1934 | } |
| 1935 | |
| 1936 | Address CodeGenFunction::EmitLoadOfReference(Address Addr, |
| 1937 | const ReferenceType *RefTy, |
| 1938 | AlignmentSource *Source) { |
| 1939 | llvm::Value *Ptr = Builder.CreateLoad(Addr); |
| 1940 | return Address(Ptr, getNaturalTypeAlignment(RefTy->getPointeeType(), |
| 1941 | Source, /*forPointee*/ true)); |
| 1942 | |
| 1943 | } |
| 1944 | |
| 1945 | LValue CodeGenFunction::EmitLoadOfReferenceLValue(Address RefAddr, |
| 1946 | const ReferenceType *RefTy) { |
| 1947 | AlignmentSource Source; |
| 1948 | Address Addr = EmitLoadOfReference(RefAddr, RefTy, &Source); |
| 1949 | return MakeAddrLValue(Addr, RefTy->getPointeeType(), Source); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1952 | Address CodeGenFunction::EmitLoadOfPointer(Address Ptr, |
| 1953 | const PointerType *PtrTy, |
| 1954 | AlignmentSource *Source) { |
| 1955 | llvm::Value *Addr = Builder.CreateLoad(Ptr); |
| 1956 | return Address(Addr, getNaturalTypeAlignment(PtrTy->getPointeeType(), Source, |
| 1957 | /*forPointeeType=*/true)); |
| 1958 | } |
| 1959 | |
| 1960 | LValue CodeGenFunction::EmitLoadOfPointerLValue(Address PtrAddr, |
| 1961 | const PointerType *PtrTy) { |
| 1962 | AlignmentSource Source; |
| 1963 | Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &Source); |
| 1964 | return MakeAddrLValue(Addr, PtrTy->getPointeeType(), Source); |
| 1965 | } |
| 1966 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1967 | static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, |
| 1968 | const Expr *E, const VarDecl *VD) { |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 1969 | QualType T = E->getType(); |
| 1970 | |
| 1971 | // If it's thread_local, emit a call to its wrapper function instead. |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1972 | if (VD->getTLSKind() == VarDecl::TLS_Dynamic && |
| 1973 | CGF.CGM.getCXXABI().usesThreadWrapperFunction()) |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 1974 | return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T); |
| 1975 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1976 | llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1977 | llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); |
| 1978 | V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1979 | CharUnits Alignment = CGF.getContext().getDeclAlign(VD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1980 | Address Addr(V, Alignment); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1981 | LValue LV; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1982 | // Emit reference to the private copy of the variable if it is an OpenMP |
| 1983 | // threadprivate variable. |
| 1984 | if (CGF.getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1985 | return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1986 | E->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1987 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) { |
| 1988 | LV = CGF.EmitLoadOfReferenceLValue(Addr, RefTy); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1989 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1990 | LV = CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 1991 | } |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 1992 | setObjCGCLValueClass(CGF.getContext(), E, LV); |
| 1993 | return LV; |
| 1994 | } |
| 1995 | |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1996 | static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 1997 | const Expr *E, const FunctionDecl *FD) { |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 1998 | llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 1999 | if (!FD->hasPrototype()) { |
| 2000 | if (const FunctionProtoType *Proto = |
| 2001 | FD->getType()->getAs<FunctionProtoType>()) { |
| 2002 | // Ugly case: for a K&R-style definition, the type of the definition |
| 2003 | // isn't the same as the type of a use. Correct for this with a |
| 2004 | // bitcast. |
| 2005 | QualType NoProtoType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2006 | CGF.getContext().getFunctionNoProtoType(Proto->getReturnType()); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2007 | NoProtoType = CGF.getContext().getPointerType(NoProtoType); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 2008 | V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType)); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2011 | CharUnits Alignment = CGF.getContext().getDeclAlign(FD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2012 | return CGF.MakeAddrLValue(V, E->getType(), Alignment, AlignmentSource::Decl); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Ben Langmuir | 3b4c30b | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2015 | static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, |
| 2016 | llvm::Value *ThisValue) { |
| 2017 | QualType TagType = CGF.getContext().getTagDeclType(FD->getParent()); |
| 2018 | LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType); |
| 2019 | return CGF.EmitLValueForField(LV, FD); |
| 2020 | } |
| 2021 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2022 | /// Named Registers are named metadata pointing to the register name |
| 2023 | /// which will be read from/written to as an argument to the intrinsic |
| 2024 | /// @llvm.read/write_register. |
| 2025 | /// So far, only the name is being passed down, but other options such as |
| 2026 | /// register type, allocation type or even optimization options could be |
| 2027 | /// passed down via the metadata node. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2028 | static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM) { |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2029 | SmallString<64> Name("llvm.named.register."); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2030 | AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>(); |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2031 | assert(Asm->getLabel().size() < 64-Name.size() && |
| 2032 | "Register name too big"); |
| 2033 | Name.append(Asm->getLabel()); |
Renato Golin | 156a853 | 2014-05-19 22:36:19 +0000 | [diff] [blame] | 2034 | llvm::NamedMDNode *M = |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2035 | CGM.getModule().getOrInsertNamedMetadata(Name); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2036 | if (M->getNumOperands() == 0) { |
| 2037 | llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(), |
| 2038 | Asm->getLabel()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2039 | llvm::Metadata *Ops[] = {Str}; |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2040 | M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
| 2041 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2042 | |
| 2043 | CharUnits Alignment = CGM.getContext().getDeclAlign(VD); |
| 2044 | |
| 2045 | llvm::Value *Ptr = |
| 2046 | llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0)); |
| 2047 | return LValue::MakeGlobalReg(Address(Ptr, Alignment), VD->getType()); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 2050 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 2051 | const NamedDecl *ND = E->getDecl(); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2052 | QualType T = E->getType(); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2053 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2054 | if (const auto *VD = dyn_cast<VarDecl>(ND)) { |
| 2055 | // Global Named registers access via intrinsics only |
| 2056 | if (VD->getStorageClass() == SC_Register && |
| 2057 | VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2058 | return EmitGlobalNamedRegister(VD, CGM); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2059 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2060 | // A DeclRefExpr for a reference initialized by a constant expression can |
| 2061 | // appear without being odr-used. Directly emit the constant initializer. |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2062 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2063 | if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() && |
| 2064 | VD->isUsableInConstantExpressions(getContext()) && |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2065 | VD->checkInitIsICE() && |
| 2066 | // Do not emit if it is private OpenMP variable. |
| 2067 | !(E->refersToEnclosingVariableOrCapture() && CapturedStmtInfo && |
| 2068 | LocalDeclMap.count(VD))) { |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2069 | llvm::Constant *Val = |
| 2070 | CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this); |
| 2071 | assert(Val && "failed to emit reference constant expression"); |
| 2072 | // FIXME: Eventually we will want to emit vector element references. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2073 | |
| 2074 | // Should we be using the alignment of the constant pointer we emitted? |
| 2075 | CharUnits Alignment = getNaturalTypeAlignment(E->getType(), nullptr, |
| 2076 | /*pointee*/ true); |
| 2077 | |
| 2078 | return MakeAddrLValue(Address(Val, Alignment), T, AlignmentSource::Decl); |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2079 | } |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Check for captured variables. |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 2082 | if (E->refersToEnclosingVariableOrCapture()) { |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2083 | if (auto *FD = LambdaCaptureFields.lookup(VD)) |
| 2084 | return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue); |
| 2085 | else if (CapturedStmtInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2086 | auto it = LocalDeclMap.find(VD); |
| 2087 | if (it != LocalDeclMap.end()) { |
| 2088 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) { |
| 2089 | return EmitLoadOfReferenceLValue(it->second, RefTy); |
Alexey Bataev | caacd53 | 2015-09-04 11:26:21 +0000 | [diff] [blame] | 2090 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2091 | return MakeAddrLValue(it->second, T); |
Alexey Bataev | caacd53 | 2015-09-04 11:26:21 +0000 | [diff] [blame] | 2092 | } |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2093 | LValue CapLVal = |
| 2094 | EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD), |
| 2095 | CapturedStmtInfo->getContextValue()); |
| 2096 | return MakeAddrLValue( |
| 2097 | Address(CapLVal.getPointer(), getContext().getDeclAlign(VD)), |
| 2098 | CapLVal.getType(), AlignmentSource::Decl); |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2099 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2100 | |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2101 | assert(isa<BlockDecl>(CurCodeDecl)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2102 | Address addr = GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>()); |
| 2103 | return MakeAddrLValue(addr, T, AlignmentSource::Decl); |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2104 | } |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Eli Friedman | 5720e34 | 2012-01-21 04:52:58 +0000 | [diff] [blame] | 2107 | // FIXME: We should be able to assert this for FunctionDecls as well! |
| 2108 | // FIXME: We should be able to assert this for all DeclRefExprs, not just |
| 2109 | // those with a valid source location. |
| 2110 | assert((ND->isUsed(false) || !isa<VarDecl>(ND) || |
| 2111 | !E->getLocation().isValid()) && |
| 2112 | "Should not use decl without marking it used!"); |
| 2113 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2114 | if (ND->hasAttr<WeakRefAttr>()) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2115 | const auto *VD = cast<ValueDecl>(ND); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2116 | ConstantAddress Aliasee = CGM.GetWeakRefReference(VD); |
| 2117 | return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2120 | if (const auto *VD = dyn_cast<VarDecl>(ND)) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 2121 | // Check if this is a global variable. |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2122 | if (VD->hasLinkage() || VD->isStaticDataMember()) |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2123 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Anders Carlsson | 6eee972 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 2124 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2125 | Address addr = Address::invalid(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2126 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2127 | // The variable should generally be present in the local decl map. |
| 2128 | auto iter = LocalDeclMap.find(VD); |
| 2129 | if (iter != LocalDeclMap.end()) { |
| 2130 | addr = iter->second; |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 2131 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2132 | // Otherwise, it might be static local we haven't emitted yet for |
| 2133 | // some reason; most likely, because it's in an outer function. |
| 2134 | } else if (VD->isStaticLocal()) { |
| 2135 | addr = Address(CGM.getOrCreateStaticVarDecl( |
| 2136 | *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false)), |
| 2137 | getContext().getDeclAlign(VD)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2138 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2139 | // No other cases for now. |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2140 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2141 | llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?"); |
| 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | // Check for OpenMP threadprivate variables. |
| 2146 | if (getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { |
| 2147 | return EmitThreadPrivateVarDeclLValue( |
| 2148 | *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()), |
| 2149 | E->getExprLoc()); |
| 2150 | } |
| 2151 | |
| 2152 | // Drill into block byref variables. |
| 2153 | bool isBlockByref = VD->hasAttr<BlocksAttr>(); |
| 2154 | if (isBlockByref) { |
| 2155 | addr = emitBlockByrefAddress(addr, VD); |
| 2156 | } |
| 2157 | |
| 2158 | // Drill into reference types. |
| 2159 | LValue LV; |
| 2160 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) { |
| 2161 | LV = EmitLoadOfReferenceLValue(addr, RefTy); |
| 2162 | } else { |
| 2163 | LV = MakeAddrLValue(addr, T, AlignmentSource::Decl); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2164 | } |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2165 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2166 | bool isLocalStorage = VD->hasLocalStorage(); |
| 2167 | |
| 2168 | bool NonGCable = isLocalStorage && |
| 2169 | !VD->getType()->isReferenceType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2170 | !isBlockByref; |
Fariborz Jahanian | 44a41d1 | 2010-11-19 18:17:09 +0000 | [diff] [blame] | 2171 | if (NonGCable) { |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2172 | LV.getQuals().removeObjCGCAttr(); |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2173 | LV.setNonGC(true); |
| 2174 | } |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2175 | |
| 2176 | bool isImpreciseLifetime = |
| 2177 | (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>()); |
| 2178 | if (isImpreciseLifetime) |
| 2179 | LV.setARCPreciseLifetime(ARCImpreciseLifetime); |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2180 | setObjCGCLValueClass(getContext(), E, LV); |
Fariborz Jahanian | 003e830 | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 2181 | return LV; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2182 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2183 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2184 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2185 | return EmitFunctionDeclLValue(*this, E, FD); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2186 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2187 | llvm_unreachable("Unhandled DeclRefExpr"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 2188 | } |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 2189 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2190 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 2191 | // __extension__ doesn't affect lvalue-ness. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2192 | if (E->getOpcode() == UO_Extension) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2193 | return EmitLValue(E->getSubExpr()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2194 | |
Chris Lattner | 0f398c4 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 2195 | QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2196 | switch (E->getOpcode()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2197 | default: llvm_unreachable("Unknown unary operator lvalue!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2198 | case UO_Deref: { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2199 | QualType T = E->getSubExpr()->getType()->getPointeeType(); |
| 2200 | assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2202 | AlignmentSource AlignSource; |
| 2203 | Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &AlignSource); |
| 2204 | LValue LV = MakeAddrLValue(Addr, T, AlignSource); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2205 | LV.getQuals().setAddressSpace(ExprTy.getAddressSpace()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2207 | // We should not generate __weak write barrier on indirect reference |
| 2208 | // of a pointer to object; as in void foo (__weak id *param); *param = 0; |
| 2209 | // But, we continue to generate __strong write barrier on indirect write |
| 2210 | // into a pointer to object. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2211 | if (getLangOpts().ObjC1 && |
| 2212 | getLangOpts().getGC() != LangOptions::NonGC && |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2213 | LV.isObjCWeak()) |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2214 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2215 | return LV; |
| 2216 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2217 | case UO_Real: |
| 2218 | case UO_Imag: { |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2219 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2220 | assert(LV.isSimple() && "real/imag on non-ordinary l-value"); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2221 | |
Richard Smith | 0b6b8e4 | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 2222 | // __real is valid on scalars. This is a faster way of testing that. |
| 2223 | // __imag can only produce an rvalue on scalars. |
| 2224 | if (E->getOpcode() == UO_Real && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2225 | !LV.getAddress().getElementType()->isStructTy()) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2226 | assert(E->getSubExpr()->getType()->isArithmeticType()); |
| 2227 | return LV; |
| 2228 | } |
| 2229 | |
| 2230 | assert(E->getSubExpr()->getType()->isAnyComplexType()); |
| 2231 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2232 | Address Component = |
| 2233 | (E->getOpcode() == UO_Real |
| 2234 | ? emitAddrOfRealComponent(LV.getAddress(), LV.getType()) |
| 2235 | : emitAddrOfImagComponent(LV.getAddress(), LV.getType())); |
| 2236 | return MakeAddrLValue(Component, ExprTy, LV.getAlignmentSource()); |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2237 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2238 | case UO_PreInc: |
| 2239 | case UO_PreDec: { |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 2240 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2241 | bool isInc = E->getOpcode() == UO_PreInc; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2242 | |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 2243 | if (E->getType()->isAnyComplexType()) |
| 2244 | EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 2245 | else |
| 2246 | EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 2247 | return LV; |
| 2248 | } |
Eli Friedman | a72bf0f | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 2249 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 2252 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 2253 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2254 | E->getType(), AlignmentSource::Decl); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2257 | LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 2258 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2259 | E->getType(), AlignmentSource::Decl); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2262 | LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2263 | auto SL = E->getFunctionName(); |
| 2264 | assert(SL != nullptr && "No StringLiteral name in PredefinedExpr"); |
| 2265 | StringRef FnName = CurFn->getName(); |
| 2266 | if (FnName.startswith("\01")) |
| 2267 | FnName = FnName.substr(1); |
| 2268 | StringRef NameItems[] = { |
| 2269 | PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName}; |
| 2270 | std::string GVName = llvm::join(NameItems, NameItems + 2, "."); |
Fariborz Jahanian | 68e7938 | 2014-11-14 23:55:27 +0000 | [diff] [blame] | 2271 | if (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2272 | auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str()); |
| 2273 | return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); |
Fariborz Jahanian | 68e7938 | 2014-11-14 23:55:27 +0000 | [diff] [blame] | 2274 | } |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2275 | auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2276 | return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2279 | /// Emit a type description suitable for use by a runtime sanitizer library. The |
| 2280 | /// format of a type descriptor is |
| 2281 | /// |
| 2282 | /// \code |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2283 | /// { i16 TypeKind, i16 TypeInfo } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2284 | /// \endcode |
| 2285 | /// |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2286 | /// followed by an array of i8 containing the type name. TypeKind is 0 for an |
| 2287 | /// integer, 1 for a floating point value, and -1 for anything else. |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2288 | llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) { |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2289 | // Only emit each type's descriptor once. |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2290 | if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T)) |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2291 | return C; |
| 2292 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2293 | uint16_t TypeKind = -1; |
| 2294 | uint16_t TypeInfo = 0; |
Mike Stump | 9a4e012 | 2009-12-15 00:59:40 +0000 | [diff] [blame] | 2295 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2296 | if (T->isIntegerType()) { |
| 2297 | TypeKind = 0; |
| 2298 | TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) | |
Aaron Ballman | f505d55 | 2012-11-30 21:44:01 +0000 | [diff] [blame] | 2299 | (T->isSignedIntegerType() ? 1 : 0); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2300 | } else if (T->isFloatingType()) { |
| 2301 | TypeKind = 1; |
| 2302 | TypeInfo = getContext().getTypeSize(T); |
| 2303 | } |
| 2304 | |
| 2305 | // Format the type name as if for a diagnostic, including quotes and |
| 2306 | // optionally an 'aka'. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2307 | SmallString<32> Buffer; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2308 | CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype, |
| 2309 | (intptr_t)T.getAsOpaquePtr(), |
Craig Topper | 3aa4fb3 | 2014-06-12 05:32:35 +0000 | [diff] [blame] | 2310 | StringRef(), StringRef(), None, Buffer, |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2311 | None); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2312 | |
| 2313 | llvm::Constant *Components[] = { |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2314 | Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo), |
| 2315 | llvm::ConstantDataArray::getString(getLLVMContext(), Buffer) |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2316 | }; |
| 2317 | llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components); |
| 2318 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2319 | auto *GV = new llvm::GlobalVariable( |
| 2320 | CGM.getModule(), Descriptor->getType(), |
| 2321 | /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2322 | GV->setUnnamedAddr(true); |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 2323 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV); |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2324 | |
| 2325 | // Remember the descriptor for this type. |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2326 | CGM.setTypeDescriptorInMap(T, GV); |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2327 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2328 | return GV; |
| 2329 | } |
| 2330 | |
| 2331 | llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) { |
| 2332 | llvm::Type *TargetTy = IntPtrTy; |
| 2333 | |
Richard Smith | 48366f7 | 2013-03-22 00:47:07 +0000 | [diff] [blame] | 2334 | // Floating-point types which fit into intptr_t are bitcast to integers |
| 2335 | // and then passed directly (after zero-extension, if necessary). |
| 2336 | if (V->getType()->isFloatingPointTy()) { |
| 2337 | unsigned Bits = V->getType()->getPrimitiveSizeInBits(); |
| 2338 | if (Bits <= TargetTy->getIntegerBitWidth()) |
| 2339 | V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(), |
| 2340 | Bits)); |
| 2341 | } |
| 2342 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2343 | // Integers which fit in intptr_t are zero-extended and passed directly. |
| 2344 | if (V->getType()->isIntegerTy() && |
| 2345 | V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth()) |
| 2346 | return Builder.CreateZExt(V, TargetTy); |
| 2347 | |
| 2348 | // Pointers are passed directly, everything else is passed by address. |
| 2349 | if (!V->getType()->isPointerTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2350 | Address Ptr = CreateDefaultAlignTempAlloca(V->getType()); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2351 | Builder.CreateStore(V, Ptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2352 | V = Ptr.getPointer(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2353 | } |
| 2354 | return Builder.CreatePtrToInt(V, TargetTy); |
| 2355 | } |
| 2356 | |
| 2357 | /// \brief Emit a representation of a SourceLocation for passing to a handler |
| 2358 | /// in a sanitizer runtime library. The format for this data is: |
| 2359 | /// \code |
| 2360 | /// struct SourceLocation { |
| 2361 | /// const char *Filename; |
| 2362 | /// int32_t Line, Column; |
| 2363 | /// }; |
| 2364 | /// \endcode |
| 2365 | /// For an invalid SourceLocation, the Filename pointer is null. |
| 2366 | llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) { |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2367 | llvm::Constant *Filename; |
| 2368 | int Line, Column; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2369 | |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2370 | PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc); |
| 2371 | if (PLoc.isValid()) { |
| 2372 | auto FilenameGV = CGM.GetAddrOfConstantCString(PLoc.getFilename(), ".src"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2373 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal( |
| 2374 | cast<llvm::GlobalVariable>(FilenameGV.getPointer())); |
| 2375 | Filename = FilenameGV.getPointer(); |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2376 | Line = PLoc.getLine(); |
| 2377 | Column = PLoc.getColumn(); |
| 2378 | } else { |
| 2379 | Filename = llvm::Constant::getNullValue(Int8PtrTy); |
| 2380 | Line = Column = 0; |
| 2381 | } |
| 2382 | |
| 2383 | llvm::Constant *Data[] = {Filename, Builder.getInt32(Line), |
| 2384 | Builder.getInt32(Column)}; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2385 | |
| 2386 | return llvm::ConstantStruct::getAnon(Data); |
| 2387 | } |
| 2388 | |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2389 | namespace { |
| 2390 | /// \brief Specify under what conditions this check can be recovered |
| 2391 | enum class CheckRecoverableKind { |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2392 | /// Always terminate program execution if this check fails. |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2393 | Unrecoverable, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2394 | /// Check supports recovering, runtime has both fatal (noreturn) and |
| 2395 | /// non-fatal handlers for this check. |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2396 | Recoverable, |
| 2397 | /// Runtime conditionally aborts, always need to support recovery. |
| 2398 | AlwaysRecoverable |
| 2399 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2400 | } |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2401 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 2402 | static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind) { |
| 2403 | assert(llvm::countPopulation(Kind) == 1); |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2404 | switch (Kind) { |
| 2405 | case SanitizerKind::Vptr: |
| 2406 | return CheckRecoverableKind::AlwaysRecoverable; |
| 2407 | case SanitizerKind::Return: |
| 2408 | case SanitizerKind::Unreachable: |
| 2409 | return CheckRecoverableKind::Unrecoverable; |
| 2410 | default: |
| 2411 | return CheckRecoverableKind::Recoverable; |
| 2412 | } |
| 2413 | } |
| 2414 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2415 | static void emitCheckHandlerCall(CodeGenFunction &CGF, |
| 2416 | llvm::FunctionType *FnType, |
| 2417 | ArrayRef<llvm::Value *> FnArgs, |
| 2418 | StringRef CheckName, |
| 2419 | CheckRecoverableKind RecoverKind, bool IsFatal, |
| 2420 | llvm::BasicBlock *ContBB) { |
| 2421 | assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable); |
| 2422 | bool NeedsAbortSuffix = |
| 2423 | IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable; |
| 2424 | std::string FnName = ("__ubsan_handle_" + CheckName + |
| 2425 | (NeedsAbortSuffix ? "_abort" : "")).str(); |
| 2426 | bool MayReturn = |
| 2427 | !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable; |
| 2428 | |
| 2429 | llvm::AttrBuilder B; |
| 2430 | if (!MayReturn) { |
| 2431 | B.addAttribute(llvm::Attribute::NoReturn) |
| 2432 | .addAttribute(llvm::Attribute::NoUnwind); |
| 2433 | } |
| 2434 | B.addAttribute(llvm::Attribute::UWTable); |
| 2435 | |
| 2436 | llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction( |
| 2437 | FnType, FnName, |
| 2438 | llvm::AttributeSet::get(CGF.getLLVMContext(), |
| 2439 | llvm::AttributeSet::FunctionIndex, B)); |
| 2440 | llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs); |
| 2441 | if (!MayReturn) { |
| 2442 | HandlerCall->setDoesNotReturn(); |
| 2443 | CGF.Builder.CreateUnreachable(); |
| 2444 | } else { |
| 2445 | CGF.Builder.CreateBr(ContBB); |
| 2446 | } |
| 2447 | } |
| 2448 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2449 | void CodeGenFunction::EmitCheck( |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 2450 | ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked, |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2451 | StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs, |
| 2452 | ArrayRef<llvm::Value *> DynamicArgs) { |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 2453 | assert(IsSanitizerScope); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2454 | assert(Checked.size() > 0); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2455 | |
| 2456 | llvm::Value *FatalCond = nullptr; |
| 2457 | llvm::Value *RecoverableCond = nullptr; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2458 | llvm::Value *TrapCond = nullptr; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2459 | for (int i = 0, n = Checked.size(); i < n; ++i) { |
| 2460 | llvm::Value *Check = Checked[i].first; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2461 | // -fsanitize-trap= overrides -fsanitize-recover=. |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2462 | llvm::Value *&Cond = |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2463 | CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second) |
| 2464 | ? TrapCond |
| 2465 | : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second) |
| 2466 | ? RecoverableCond |
| 2467 | : FatalCond; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2468 | Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check; |
| 2469 | } |
| 2470 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2471 | if (TrapCond) |
| 2472 | EmitTrapCheck(TrapCond); |
| 2473 | if (!FatalCond && !RecoverableCond) |
| 2474 | return; |
| 2475 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2476 | llvm::Value *JointCond; |
| 2477 | if (FatalCond && RecoverableCond) |
| 2478 | JointCond = Builder.CreateAnd(FatalCond, RecoverableCond); |
| 2479 | else |
| 2480 | JointCond = FatalCond ? FatalCond : RecoverableCond; |
| 2481 | assert(JointCond); |
| 2482 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2483 | CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2484 | // In cross-DSO CFI mode this code is used to generate __cfi_check_fail, which |
| 2485 | // includes all checks, even those that are not in SanOpts. |
| 2486 | assert(CGM.getCodeGenOpts().SanitizeCfiCrossDso || |
| 2487 | SanOpts.has(Checked[0].second)); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2488 | #ifndef NDEBUG |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2489 | for (int i = 1, n = Checked.size(); i < n; ++i) { |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2490 | assert(RecoverKind == getRecoverableKind(Checked[i].second) && |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2491 | "All recoverable kinds in a single check must be same!"); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2492 | assert(CGM.getCodeGenOpts().SanitizeCfiCrossDso || |
| 2493 | SanOpts.has(Checked[i].second)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2494 | } |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2495 | #endif |
Chad Rosier | ae229d5 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 2496 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2497 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2498 | llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName); |
| 2499 | llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers); |
Will Dietz | ddd282a | 2012-12-15 01:39:14 +0000 | [diff] [blame] | 2500 | // Give hint that we very much don't expect to execute the handler |
| 2501 | // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp |
| 2502 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 2503 | llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); |
| 2504 | Branch->setMetadata(llvm::LLVMContext::MD_prof, Node); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2505 | EmitBlock(Handlers); |
Will Dietz | ddd282a | 2012-12-15 01:39:14 +0000 | [diff] [blame] | 2506 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2507 | // Handler functions take an i8* pointing to the (handler-specific) static |
| 2508 | // information block, followed by a sequence of intptr_t arguments |
| 2509 | // representing operand values. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2510 | SmallVector<llvm::Value *, 4> Args; |
| 2511 | SmallVector<llvm::Type *, 4> ArgTypes; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2512 | Args.reserve(DynamicArgs.size() + 1); |
| 2513 | ArgTypes.reserve(DynamicArgs.size() + 1); |
| 2514 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2515 | // Emit handler arguments and create handler function type. |
| 2516 | if (!StaticArgs.empty()) { |
| 2517 | llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); |
| 2518 | auto *InfoPtr = |
| 2519 | new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, |
| 2520 | llvm::GlobalVariable::PrivateLinkage, Info); |
| 2521 | InfoPtr->setUnnamedAddr(true); |
| 2522 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); |
| 2523 | Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy)); |
| 2524 | ArgTypes.push_back(Int8PtrTy); |
| 2525 | } |
| 2526 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2527 | for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) { |
| 2528 | Args.push_back(EmitCheckValue(DynamicArgs[i])); |
| 2529 | ArgTypes.push_back(IntPtrTy); |
| 2530 | } |
| 2531 | |
| 2532 | llvm::FunctionType *FnType = |
| 2533 | llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false); |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2534 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2535 | if (!FatalCond || !RecoverableCond) { |
| 2536 | // Simple case: we need to generate a single handler call, either |
| 2537 | // fatal, or non-fatal. |
| 2538 | emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, |
| 2539 | (FatalCond != nullptr), Cont); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2540 | } else { |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2541 | // Emit two handler calls: first one for set of unrecoverable checks, |
| 2542 | // another one for recoverable. |
| 2543 | llvm::BasicBlock *NonFatalHandlerBB = |
| 2544 | createBasicBlock("non_fatal." + CheckName); |
| 2545 | llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName); |
| 2546 | Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB); |
| 2547 | EmitBlock(FatalHandlerBB); |
| 2548 | emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, true, |
| 2549 | NonFatalHandlerBB); |
| 2550 | EmitBlock(NonFatalHandlerBB); |
| 2551 | emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, false, |
| 2552 | Cont); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2553 | } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2554 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2555 | EmitBlock(Cont); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2558 | void CodeGenFunction::EmitCfiSlowPathCheck( |
| 2559 | SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId, |
| 2560 | llvm::Value *Ptr, ArrayRef<llvm::Constant *> StaticArgs) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2561 | llvm::BasicBlock *Cont = createBasicBlock("cfi.cont"); |
| 2562 | |
| 2563 | llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath"); |
| 2564 | llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB); |
| 2565 | |
| 2566 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 2567 | llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); |
| 2568 | BI->setMetadata(llvm::LLVMContext::MD_prof, Node); |
| 2569 | |
| 2570 | EmitBlock(CheckBB); |
| 2571 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2572 | bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind); |
| 2573 | |
| 2574 | llvm::CallInst *CheckCall; |
| 2575 | if (WithDiag) { |
| 2576 | llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); |
| 2577 | auto *InfoPtr = |
| 2578 | new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, |
| 2579 | llvm::GlobalVariable::PrivateLinkage, Info); |
| 2580 | InfoPtr->setUnnamedAddr(true); |
| 2581 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); |
| 2582 | |
| 2583 | llvm::Constant *SlowPathDiagFn = CGM.getModule().getOrInsertFunction( |
| 2584 | "__cfi_slowpath_diag", |
| 2585 | llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, |
| 2586 | false)); |
| 2587 | CheckCall = Builder.CreateCall( |
| 2588 | SlowPathDiagFn, |
| 2589 | {TypeId, Ptr, Builder.CreateBitCast(InfoPtr, Int8PtrTy)}); |
| 2590 | } else { |
| 2591 | llvm::Constant *SlowPathFn = CGM.getModule().getOrInsertFunction( |
| 2592 | "__cfi_slowpath", |
| 2593 | llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false)); |
| 2594 | CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr}); |
| 2595 | } |
| 2596 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2597 | CheckCall->setDoesNotThrow(); |
| 2598 | |
| 2599 | EmitBlock(Cont); |
| 2600 | } |
| 2601 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2602 | // This function is basically a switch over the CFI failure kind, which is |
| 2603 | // extracted from CFICheckFailData (1st function argument). Each case is either |
| 2604 | // llvm.trap or a call to one of the two runtime handlers, based on |
| 2605 | // -fsanitize-trap and -fsanitize-recover settings. Default case (invalid |
| 2606 | // failure kind) traps, but this should really never happen. CFICheckFailData |
| 2607 | // can be nullptr if the calling module has -fsanitize-trap behavior for this |
| 2608 | // check kind; in this case __cfi_check_fail traps as well. |
| 2609 | void CodeGenFunction::EmitCfiCheckFail() { |
| 2610 | SanitizerScope SanScope(this); |
| 2611 | FunctionArgList Args; |
| 2612 | ImplicitParamDecl ArgData(getContext(), nullptr, SourceLocation(), nullptr, |
| 2613 | getContext().VoidPtrTy); |
| 2614 | ImplicitParamDecl ArgAddr(getContext(), nullptr, SourceLocation(), nullptr, |
| 2615 | getContext().VoidPtrTy); |
| 2616 | Args.push_back(&ArgData); |
| 2617 | Args.push_back(&ArgAddr); |
| 2618 | |
| 2619 | const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 2620 | getContext().VoidTy, Args, FunctionType::ExtInfo(), /*variadic=*/false); |
| 2621 | |
| 2622 | llvm::Function *F = llvm::Function::Create( |
| 2623 | llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false), |
| 2624 | llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule()); |
| 2625 | F->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 2626 | |
| 2627 | StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args, |
| 2628 | SourceLocation()); |
| 2629 | |
| 2630 | llvm::Value *Data = |
| 2631 | EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false, |
| 2632 | CGM.getContext().VoidPtrTy, ArgData.getLocation()); |
| 2633 | llvm::Value *Addr = |
| 2634 | EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false, |
| 2635 | CGM.getContext().VoidPtrTy, ArgAddr.getLocation()); |
| 2636 | |
| 2637 | // Data == nullptr means the calling module has trap behaviour for this check. |
| 2638 | llvm::Value *DataIsNotNullPtr = |
| 2639 | Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy)); |
| 2640 | EmitTrapCheck(DataIsNotNullPtr); |
| 2641 | |
| 2642 | llvm::StructType *SourceLocationTy = |
| 2643 | llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty, nullptr); |
| 2644 | llvm::StructType *CfiCheckFailDataTy = |
| 2645 | llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy, nullptr); |
| 2646 | |
| 2647 | llvm::Value *V = Builder.CreateConstGEP2_32( |
| 2648 | CfiCheckFailDataTy, |
| 2649 | Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0, |
| 2650 | 0); |
| 2651 | Address CheckKindAddr(V, getIntAlign()); |
| 2652 | llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr); |
| 2653 | |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2654 | llvm::Value *AllVtables = llvm::MetadataAsValue::get( |
| 2655 | CGM.getLLVMContext(), |
| 2656 | llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); |
| 2657 | llvm::Value *ValidVtable = Builder.CreateZExt( |
| 2658 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::bitset_test), |
| 2659 | {Addr, AllVtables}), |
| 2660 | IntPtrTy); |
| 2661 | |
Evgeniy Stepanov | 4d3b087 | 2016-01-25 23:45:37 +0000 | [diff] [blame] | 2662 | const std::pair<int, SanitizerMask> CheckKinds[] = { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2663 | {CFITCK_VCall, SanitizerKind::CFIVCall}, |
| 2664 | {CFITCK_NVCall, SanitizerKind::CFINVCall}, |
| 2665 | {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast}, |
| 2666 | {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast}, |
| 2667 | {CFITCK_ICall, SanitizerKind::CFIICall}}; |
| 2668 | |
| 2669 | SmallVector<std::pair<llvm::Value *, SanitizerMask>, 5> Checks; |
| 2670 | for (auto CheckKindMaskPair : CheckKinds) { |
| 2671 | int Kind = CheckKindMaskPair.first; |
| 2672 | SanitizerMask Mask = CheckKindMaskPair.second; |
| 2673 | llvm::Value *Cond = |
| 2674 | Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind)); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2675 | EmitCheck(std::make_pair(Cond, Mask), "cfi_check_fail", {}, |
| 2676 | {Data, Addr, ValidVtable}); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | FinishFunction(); |
| 2680 | // The only reference to this function will be created during LTO link. |
| 2681 | // Make sure it survives until then. |
| 2682 | CGM.addUsedGlobal(F); |
| 2683 | } |
| 2684 | |
Chad Rosier | ae229d5 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 2685 | void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) { |
Richard Smith | de67068 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2686 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 2687 | |
| 2688 | // If we're optimizing, collapse all calls to trap down to just one per |
| 2689 | // function to save on code size. |
| 2690 | if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) { |
| 2691 | TrapBB = createBasicBlock("trap"); |
| 2692 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2693 | EmitBlock(TrapBB); |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2694 | llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); |
Richard Smith | de67068 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2695 | TrapCall->setDoesNotReturn(); |
| 2696 | TrapCall->setDoesNotThrow(); |
| 2697 | Builder.CreateUnreachable(); |
| 2698 | } else { |
| 2699 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2700 | } |
| 2701 | |
| 2702 | EmitBlock(Cont); |
| 2703 | } |
| 2704 | |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2705 | llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) { |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 2706 | llvm::CallInst *TrapCall = Builder.CreateCall(CGM.getIntrinsic(IntrID)); |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2707 | |
| 2708 | if (!CGM.getCodeGenOpts().TrapFuncName.empty()) |
| 2709 | TrapCall->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 2710 | "trap-func-name", |
| 2711 | CGM.getCodeGenOpts().TrapFuncName); |
| 2712 | |
| 2713 | return TrapCall; |
| 2714 | } |
| 2715 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2716 | Address CodeGenFunction::EmitArrayToPointerDecay(const Expr *E, |
| 2717 | AlignmentSource *AlignSource) { |
| 2718 | assert(E->getType()->isArrayType() && |
| 2719 | "Array to pointer decay must have array source type!"); |
| 2720 | |
| 2721 | // Expressions of array type can't be bitfields or vector elements. |
| 2722 | LValue LV = EmitLValue(E); |
| 2723 | Address Addr = LV.getAddress(); |
| 2724 | if (AlignSource) *AlignSource = LV.getAlignmentSource(); |
| 2725 | |
| 2726 | // If the array type was an incomplete type, we need to make sure |
| 2727 | // the decay ends up being the right type. |
| 2728 | llvm::Type *NewTy = ConvertType(E->getType()); |
| 2729 | Addr = Builder.CreateElementBitCast(Addr, NewTy); |
| 2730 | |
| 2731 | // Note that VLA pointers are always decayed, so we don't need to do |
| 2732 | // anything here. |
| 2733 | if (!E->getType()->isVariableArrayType()) { |
| 2734 | assert(isa<llvm::ArrayType>(Addr.getElementType()) && |
| 2735 | "Expected pointer to array"); |
| 2736 | Addr = Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), "arraydecay"); |
| 2737 | } |
| 2738 | |
| 2739 | QualType EltType = E->getType()->castAsArrayTypeUnsafe()->getElementType(); |
| 2740 | return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType)); |
| 2741 | } |
| 2742 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2743 | /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an |
| 2744 | /// array to pointer, return the array subexpression. |
| 2745 | static const Expr *isSimpleArrayDecayOperand(const Expr *E) { |
| 2746 | // If this isn't just an array->pointer decay, bail out. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2747 | const auto *CE = dyn_cast<CastExpr>(E); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2748 | if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 2749 | return nullptr; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2751 | // If this is a decay from variable width array, bail out. |
| 2752 | const Expr *SubExpr = CE->getSubExpr(); |
| 2753 | if (SubExpr->getType()->isVariableArrayType()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2754 | return nullptr; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2756 | return SubExpr; |
| 2757 | } |
| 2758 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2759 | static llvm::Value *emitArraySubscriptGEP(CodeGenFunction &CGF, |
| 2760 | llvm::Value *ptr, |
| 2761 | ArrayRef<llvm::Value*> indices, |
| 2762 | bool inbounds, |
| 2763 | const llvm::Twine &name = "arrayidx") { |
| 2764 | if (inbounds) { |
| 2765 | return CGF.Builder.CreateInBoundsGEP(ptr, indices, name); |
| 2766 | } else { |
| 2767 | return CGF.Builder.CreateGEP(ptr, indices, name); |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | static CharUnits getArrayElementAlign(CharUnits arrayAlign, |
| 2772 | llvm::Value *idx, |
| 2773 | CharUnits eltSize) { |
| 2774 | // If we have a constant index, we can use the exact offset of the |
| 2775 | // element we're accessing. |
| 2776 | if (auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) { |
| 2777 | CharUnits offset = constantIdx->getZExtValue() * eltSize; |
| 2778 | return arrayAlign.alignmentAtOffset(offset); |
| 2779 | |
| 2780 | // Otherwise, use the worst-case alignment for any element. |
| 2781 | } else { |
| 2782 | return arrayAlign.alignmentOfArrayElement(eltSize); |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | static QualType getFixedSizeElementType(const ASTContext &ctx, |
| 2787 | const VariableArrayType *vla) { |
| 2788 | QualType eltType; |
| 2789 | do { |
| 2790 | eltType = vla->getElementType(); |
| 2791 | } while ((vla = ctx.getAsVariableArrayType(eltType))); |
| 2792 | return eltType; |
| 2793 | } |
| 2794 | |
| 2795 | static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr, |
| 2796 | ArrayRef<llvm::Value*> indices, |
| 2797 | QualType eltType, bool inbounds, |
| 2798 | const llvm::Twine &name = "arrayidx") { |
| 2799 | // All the indices except that last must be zero. |
| 2800 | #ifndef NDEBUG |
| 2801 | for (auto idx : indices.drop_back()) |
| 2802 | assert(isa<llvm::ConstantInt>(idx) && |
| 2803 | cast<llvm::ConstantInt>(idx)->isZero()); |
| 2804 | #endif |
| 2805 | |
| 2806 | // Determine the element size of the statically-sized base. This is |
| 2807 | // the thing that the indices are expressed in terms of. |
| 2808 | if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) { |
| 2809 | eltType = getFixedSizeElementType(CGF.getContext(), vla); |
| 2810 | } |
| 2811 | |
| 2812 | // We can use that to compute the best alignment of the element. |
| 2813 | CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType); |
| 2814 | CharUnits eltAlign = |
| 2815 | getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize); |
| 2816 | |
| 2817 | llvm::Value *eltPtr = |
| 2818 | emitArraySubscriptGEP(CGF, addr.getPointer(), indices, inbounds, name); |
| 2819 | return Address(eltPtr, eltAlign); |
| 2820 | } |
| 2821 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 2822 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E, |
| 2823 | bool Accessed) { |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2824 | // The index must always be an integer, which is not an aggregate. Emit it. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2825 | llvm::Value *Idx = EmitScalarExpr(E->getIdx()); |
Eli Friedman | 07bbeca | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 2826 | QualType IdxTy = E->getIdx()->getType(); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2827 | bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType(); |
Eli Friedman | 07bbeca | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 2828 | |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 2829 | if (SanOpts.has(SanitizerKind::ArrayBounds)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 2830 | EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed); |
| 2831 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2832 | // If the base is a vector type, then we are forming a vector element lvalue |
| 2833 | // with this subscript. |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 2834 | if (E->getBase()->getType()->isVectorType() && |
| 2835 | !isa<ExtVectorElementExpr>(E->getBase())) { |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2836 | // Emit the vector as an lvalue to get its address. |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2837 | LValue LHS = EmitLValue(E->getBase()); |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2838 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 2839 | return LValue::MakeVectorElt(LHS.getAddress(), Idx, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2840 | E->getBase()->getType(), |
| 2841 | LHS.getAlignmentSource()); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 2842 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2843 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2844 | // All the other cases basically behave like simple offsetting. |
| 2845 | |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 2846 | // Extend or truncate the index type to 32 or 64-bits. |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 2847 | if (Idx->getType() != IntPtrTy) |
| 2848 | Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom"); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2849 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2850 | // Handle the extvector case we ignored above. |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 2851 | if (isa<ExtVectorElementExpr>(E->getBase())) { |
| 2852 | LValue LV = EmitLValue(E->getBase()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2853 | Address Addr = EmitExtVectorElementLValue(LV); |
| 2854 | |
| 2855 | QualType EltType = LV.getType()->castAs<VectorType>()->getElementType(); |
| 2856 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true); |
| 2857 | return MakeAddrLValue(Addr, EltType, LV.getAlignmentSource()); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 2858 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2859 | |
| 2860 | AlignmentSource AlignSource; |
| 2861 | Address Addr = Address::invalid(); |
| 2862 | if (const VariableArrayType *vla = |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 2863 | getContext().getAsVariableArrayType(E->getType())) { |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 2864 | // The base must be a pointer, which is not an aggregate. Emit |
| 2865 | // it. It needs to be emitted first in case it's what captures |
| 2866 | // the VLA bounds. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2867 | Addr = EmitPointerWithAlignment(E->getBase(), &AlignSource); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2868 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 2869 | // The element count here is the total number of non-VLA elements. |
| 2870 | llvm::Value *numElements = getVLASize(vla).first; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2871 | |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2872 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 2873 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 2874 | // signed-overflow, so we use the same semantics for our explicit |
| 2875 | // multiply. We suppress this if overflow is not undefined behavior. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2876 | if (getLangOpts().isSignedOverflowDefined()) { |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2877 | Idx = Builder.CreateMul(Idx, numElements); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2878 | } else { |
| 2879 | Idx = Builder.CreateNSWMul(Idx, numElements); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2880 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2881 | |
| 2882 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(), |
| 2883 | !getLangOpts().isSignedOverflowDefined()); |
| 2884 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2885 | } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ |
| 2886 | // Indexing over an interface, as in "NSString *P; P[4];" |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2887 | CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT); |
| 2888 | llvm::Value *InterfaceSizeVal = |
| 2889 | llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity());; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2890 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2891 | llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2892 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2893 | // Emit the base pointer. |
| 2894 | Addr = EmitPointerWithAlignment(E->getBase(), &AlignSource); |
| 2895 | |
| 2896 | // We don't necessarily build correct LLVM struct types for ObjC |
| 2897 | // interfaces, so we can't rely on GEP to do this scaling |
| 2898 | // correctly, so we need to cast to i8*. FIXME: is this actually |
| 2899 | // true? A lot of other things in the fragile ABI would break... |
| 2900 | llvm::Type *OrigBaseTy = Addr.getType(); |
| 2901 | Addr = Builder.CreateElementBitCast(Addr, Int8Ty); |
| 2902 | |
| 2903 | // Do the GEP. |
| 2904 | CharUnits EltAlign = |
| 2905 | getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize); |
| 2906 | llvm::Value *EltPtr = |
| 2907 | emitArraySubscriptGEP(*this, Addr.getPointer(), ScaledIdx, false); |
| 2908 | Addr = Address(EltPtr, EltAlign); |
| 2909 | |
| 2910 | // Cast back. |
| 2911 | Addr = Builder.CreateBitCast(Addr, OrigBaseTy); |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 2912 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 2913 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 2914 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 2915 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 2916 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 2917 | assert(Array->getType()->isArrayType() && |
| 2918 | "Array to pointer decay must have array source type!"); |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 2919 | LValue ArrayLV; |
| 2920 | // For simple multidimensional array indexing, set the 'accessed' flag for |
| 2921 | // better bounds-checking of the base expression. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2922 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 2923 | ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); |
| 2924 | else |
| 2925 | ArrayLV = EmitLValue(Array); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2926 | |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 2927 | // Propagate the alignment from the array itself to the result. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2928 | Addr = emitArraySubscriptGEP(*this, ArrayLV.getAddress(), |
| 2929 | {CGM.getSize(CharUnits::Zero()), Idx}, |
| 2930 | E->getType(), |
| 2931 | !getLangOpts().isSignedOverflowDefined()); |
| 2932 | AlignSource = ArrayLV.getAlignmentSource(); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2933 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2934 | // The base must be a pointer; emit it with an estimate of its alignment. |
| 2935 | Addr = EmitPointerWithAlignment(E->getBase(), &AlignSource); |
| 2936 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(), |
| 2937 | !getLangOpts().isSignedOverflowDefined()); |
Anders Carlsson | 3d312f8 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 2938 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2939 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2940 | LValue LV = MakeAddrLValue(Addr, E->getType(), AlignSource); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2941 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2942 | // TODO: Preserve/extend path TBAA metadata? |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2943 | |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2944 | if (getLangOpts().ObjC1 && |
| 2945 | getLangOpts().getGC() != LangOptions::NonGC) { |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2946 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2947 | setObjCGCLValueClass(getContext(), E, LV); |
| 2948 | } |
Fariborz Jahanian | a9fecf3 | 2009-02-21 23:37:19 +0000 | [diff] [blame] | 2949 | return LV; |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2952 | static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, |
| 2953 | AlignmentSource &AlignSource, |
| 2954 | QualType BaseTy, QualType ElTy, |
| 2955 | bool IsLowerBound) { |
| 2956 | LValue BaseLVal; |
| 2957 | if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts())) { |
| 2958 | BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound); |
| 2959 | if (BaseTy->isArrayType()) { |
| 2960 | Address Addr = BaseLVal.getAddress(); |
| 2961 | AlignSource = BaseLVal.getAlignmentSource(); |
| 2962 | |
| 2963 | // If the array type was an incomplete type, we need to make sure |
| 2964 | // the decay ends up being the right type. |
| 2965 | llvm::Type *NewTy = CGF.ConvertType(BaseTy); |
| 2966 | Addr = CGF.Builder.CreateElementBitCast(Addr, NewTy); |
| 2967 | |
| 2968 | // Note that VLA pointers are always decayed, so we don't need to do |
| 2969 | // anything here. |
| 2970 | if (!BaseTy->isVariableArrayType()) { |
| 2971 | assert(isa<llvm::ArrayType>(Addr.getElementType()) && |
| 2972 | "Expected pointer to array"); |
| 2973 | Addr = CGF.Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), |
| 2974 | "arraydecay"); |
| 2975 | } |
| 2976 | |
| 2977 | return CGF.Builder.CreateElementBitCast(Addr, |
| 2978 | CGF.ConvertTypeForMem(ElTy)); |
| 2979 | } |
| 2980 | CharUnits Align = CGF.getNaturalTypeAlignment(ElTy, &AlignSource); |
| 2981 | return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()), Align); |
| 2982 | } |
| 2983 | return CGF.EmitPointerWithAlignment(Base, &AlignSource); |
| 2984 | } |
| 2985 | |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2986 | LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, |
| 2987 | bool IsLowerBound) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2988 | QualType BaseTy; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2989 | if (auto *ASE = |
| 2990 | dyn_cast<OMPArraySectionExpr>(E->getBase()->IgnoreParenImpCasts())) |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2991 | BaseTy = OMPArraySectionExpr::getBaseOriginalType(ASE); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2992 | else |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2993 | BaseTy = E->getBase()->getType(); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2994 | QualType ResultExprTy; |
| 2995 | if (auto *AT = getContext().getAsArrayType(BaseTy)) |
| 2996 | ResultExprTy = AT->getElementType(); |
| 2997 | else |
| 2998 | ResultExprTy = BaseTy->getPointeeType(); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2999 | llvm::Value *Idx = nullptr; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3000 | if (IsLowerBound || (!IsLowerBound && E->getColonLoc().isInvalid())) { |
| 3001 | // Requesting lower bound or upper bound, but without provided length and |
| 3002 | // without ':' symbol for the default length -> length = 1. |
| 3003 | // Idx = LowerBound ?: 0; |
| 3004 | if (auto *LowerBound = E->getLowerBound()) { |
| 3005 | Idx = Builder.CreateIntCast( |
| 3006 | EmitScalarExpr(LowerBound), IntPtrTy, |
| 3007 | LowerBound->getType()->hasSignedIntegerRepresentation()); |
| 3008 | } else |
| 3009 | Idx = llvm::ConstantInt::getNullValue(IntPtrTy); |
| 3010 | } else { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3011 | // Try to emit length or lower bound as constant. If this is possible, 1 |
| 3012 | // is subtracted from constant length or lower bound. Otherwise, emit LLVM |
| 3013 | // IR (LB + Len) - 1. |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3014 | auto &C = CGM.getContext(); |
| 3015 | auto *Length = E->getLength(); |
| 3016 | llvm::APSInt ConstLength; |
| 3017 | if (Length) { |
| 3018 | // Idx = LowerBound + Length - 1; |
| 3019 | if (Length->isIntegerConstantExpr(ConstLength, C)) { |
| 3020 | ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); |
| 3021 | Length = nullptr; |
| 3022 | } |
| 3023 | auto *LowerBound = E->getLowerBound(); |
| 3024 | llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false); |
| 3025 | if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) { |
| 3026 | ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits); |
| 3027 | LowerBound = nullptr; |
| 3028 | } |
| 3029 | if (!Length) |
| 3030 | --ConstLength; |
| 3031 | else if (!LowerBound) |
| 3032 | --ConstLowerBound; |
| 3033 | |
| 3034 | if (Length || LowerBound) { |
| 3035 | auto *LowerBoundVal = |
| 3036 | LowerBound |
| 3037 | ? Builder.CreateIntCast( |
| 3038 | EmitScalarExpr(LowerBound), IntPtrTy, |
| 3039 | LowerBound->getType()->hasSignedIntegerRepresentation()) |
| 3040 | : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound); |
| 3041 | auto *LengthVal = |
| 3042 | Length |
| 3043 | ? Builder.CreateIntCast( |
| 3044 | EmitScalarExpr(Length), IntPtrTy, |
| 3045 | Length->getType()->hasSignedIntegerRepresentation()) |
| 3046 | : llvm::ConstantInt::get(IntPtrTy, ConstLength); |
| 3047 | Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len", |
| 3048 | /*HasNUW=*/false, |
| 3049 | !getLangOpts().isSignedOverflowDefined()); |
| 3050 | if (Length && LowerBound) { |
| 3051 | Idx = Builder.CreateSub( |
| 3052 | Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1", |
| 3053 | /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); |
| 3054 | } |
| 3055 | } else |
| 3056 | Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound); |
| 3057 | } else { |
| 3058 | // Idx = ArraySize - 1; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3059 | QualType ArrayTy = BaseTy->isPointerType() |
| 3060 | ? E->getBase()->IgnoreParenImpCasts()->getType() |
| 3061 | : BaseTy; |
| 3062 | if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3063 | Length = VAT->getSizeExpr(); |
| 3064 | if (Length->isIntegerConstantExpr(ConstLength, C)) |
| 3065 | Length = nullptr; |
| 3066 | } else { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3067 | auto *CAT = C.getAsConstantArrayType(ArrayTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3068 | ConstLength = CAT->getSize(); |
| 3069 | } |
| 3070 | if (Length) { |
| 3071 | auto *LengthVal = Builder.CreateIntCast( |
| 3072 | EmitScalarExpr(Length), IntPtrTy, |
| 3073 | Length->getType()->hasSignedIntegerRepresentation()); |
| 3074 | Idx = Builder.CreateSub( |
| 3075 | LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1", |
| 3076 | /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); |
| 3077 | } else { |
| 3078 | ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); |
| 3079 | --ConstLength; |
| 3080 | Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength); |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | assert(Idx); |
| 3085 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3086 | Address EltPtr = Address::invalid(); |
| 3087 | AlignmentSource AlignSource; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3088 | if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3089 | // The base must be a pointer, which is not an aggregate. Emit |
| 3090 | // it. It needs to be emitted first in case it's what captures |
| 3091 | // the VLA bounds. |
| 3092 | Address Base = |
| 3093 | emitOMPArraySectionBase(*this, E->getBase(), AlignSource, BaseTy, |
| 3094 | VLA->getElementType(), IsLowerBound); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3095 | // The element count here is the total number of non-VLA elements. |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3096 | llvm::Value *NumElements = getVLASize(VLA).first; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 3099 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 3100 | // signed-overflow, so we use the same semantics for our explicit |
| 3101 | // multiply. We suppress this if overflow is not undefined behavior. |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3102 | if (getLangOpts().isSignedOverflowDefined()) |
| 3103 | Idx = Builder.CreateMul(Idx, NumElements); |
| 3104 | else |
| 3105 | Idx = Builder.CreateNSWMul(Idx, NumElements); |
| 3106 | EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(), |
| 3107 | !getLangOpts().isSignedOverflowDefined()); |
| 3108 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 3109 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 3110 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 3111 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 3112 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 3113 | assert(Array->getType()->isArrayType() && |
| 3114 | "Array to pointer decay must have array source type!"); |
| 3115 | LValue ArrayLV; |
| 3116 | // For simple multidimensional array indexing, set the 'accessed' flag for |
| 3117 | // better bounds-checking of the base expression. |
| 3118 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) |
| 3119 | ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); |
| 3120 | else |
| 3121 | ArrayLV = EmitLValue(Array); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3122 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3123 | // Propagate the alignment from the array itself to the result. |
| 3124 | EltPtr = emitArraySubscriptGEP( |
| 3125 | *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx}, |
| 3126 | ResultExprTy, !getLangOpts().isSignedOverflowDefined()); |
| 3127 | AlignSource = ArrayLV.getAlignmentSource(); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3128 | } else { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3129 | Address Base = emitOMPArraySectionBase(*this, E->getBase(), AlignSource, |
| 3130 | BaseTy, ResultExprTy, IsLowerBound); |
| 3131 | EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy, |
| 3132 | !getLangOpts().isSignedOverflowDefined()); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3135 | return MakeAddrLValue(EltPtr, ResultExprTy, AlignSource); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3138 | LValue CodeGenFunction:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3139 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3140 | // Emit the base vector as an l-value. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3141 | LValue Base; |
| 3142 | |
| 3143 | // ExtVectorElementExpr's base can either be a vector or pointer to vector. |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3144 | if (E->isArrow()) { |
| 3145 | // If it is a pointer to a vector, emit the address and form an lvalue with |
| 3146 | // it. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3147 | AlignmentSource AlignSource; |
| 3148 | Address Ptr = EmitPointerWithAlignment(E->getBase(), &AlignSource); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3149 | const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3150 | Base = MakeAddrLValue(Ptr, PT->getPointeeType(), AlignSource); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3151 | Base.getQuals().removeObjCGCAttr(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3152 | } else if (E->getBase()->isGLValue()) { |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3153 | // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), |
| 3154 | // emit the base as an lvalue. |
| 3155 | assert(E->getBase()->getType()->isVectorType()); |
| 3156 | Base = EmitLValue(E->getBase()); |
| 3157 | } else { |
| 3158 | // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such. |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 3159 | assert(E->getBase()->getType()->isVectorType() && |
Daniel Dunbar | 5b90195 | 2010-01-04 18:02:28 +0000 | [diff] [blame] | 3160 | "Result must be a vector"); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3161 | llvm::Value *Vec = EmitScalarExpr(E->getBase()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | f0a9ba3 | 2009-12-23 21:33:41 +0000 | [diff] [blame] | 3163 | // Store the vector to memory (because LValue wants an address). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3164 | Address VecMem = CreateMemTemp(E->getBase()->getType()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3165 | Builder.CreateStore(Vec, VecMem); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3166 | Base = MakeAddrLValue(VecMem, E->getBase()->getType(), |
| 3167 | AlignmentSource::Decl); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3168 | } |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 3169 | |
| 3170 | QualType type = |
| 3171 | E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3172 | |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3173 | // Encode the element access list into a vector of unsigned indices. |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 3174 | SmallVector<uint32_t, 4> Indices; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3175 | E->getEncodedElementAccess(Indices); |
| 3176 | |
| 3177 | if (Base.isSimple()) { |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 3178 | llvm::Constant *CV = |
| 3179 | llvm::ConstantDataVector::get(getLLVMContext(), Indices); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 3180 | return LValue::MakeExtVectorElt(Base.getAddress(), CV, type, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3181 | Base.getAlignmentSource()); |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3182 | } |
| 3183 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 3184 | |
| 3185 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3186 | SmallVector<llvm::Constant *, 4> CElts; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 3188 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 3189 | CElts.push_back(BaseElts->getAggregateElement(Indices[i])); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 3190 | llvm::Constant *CV = llvm::ConstantVector::get(CElts); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3191 | return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type, |
| 3192 | Base.getAlignmentSource()); |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3195 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Devang Patel | d68df20 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 3196 | Expr *BaseExpr = E->getBase(); |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | 4e4186b | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 3198 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3199 | LValue BaseLV; |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3200 | if (E->isArrow()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3201 | AlignmentSource AlignSource; |
| 3202 | Address Addr = EmitPointerWithAlignment(BaseExpr, &AlignSource); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3203 | QualType PtrTy = BaseExpr->getType()->getPointeeType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3204 | EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Addr.getPointer(), PtrTy); |
| 3205 | BaseLV = MakeAddrLValue(Addr, PtrTy, AlignSource); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3206 | } else |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 3207 | BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess); |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3208 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 3209 | NamedDecl *ND = E->getMemberDecl(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3210 | if (auto *Field = dyn_cast<FieldDecl>(ND)) { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3211 | LValue LV = EmitLValueForField(BaseLV, Field); |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 3212 | setObjCGCLValueClass(getContext(), E, LV); |
| 3213 | return LV; |
| 3214 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3215 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3216 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
Anders Carlsson | 5bbdc9f | 2009-11-07 23:16:50 +0000 | [diff] [blame] | 3217 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 3218 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3219 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 3220 | return EmitFunctionDeclLValue(*this, E, FD); |
| 3221 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3222 | llvm_unreachable("Unhandled member declaration!"); |
Eli Friedman | a62f3e1 | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 3223 | } |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3224 | |
John McCall | dec348f7 | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 3225 | /// Given that we are currently emitting a lambda, emit an l-value for |
| 3226 | /// one of its members. |
| 3227 | LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) { |
| 3228 | assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda()); |
| 3229 | assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent()); |
| 3230 | QualType LambdaTagType = |
| 3231 | getContext().getTagDeclType(Field->getParent()); |
| 3232 | LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType); |
| 3233 | return EmitLValueForField(LambdaLV, Field); |
| 3234 | } |
| 3235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3236 | /// Drill down to the storage of a field without walking into |
| 3237 | /// reference types. |
| 3238 | /// |
| 3239 | /// The resulting address doesn't necessarily have the right type. |
| 3240 | static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, |
| 3241 | const FieldDecl *field) { |
| 3242 | const RecordDecl *rec = field->getParent(); |
| 3243 | |
| 3244 | unsigned idx = |
| 3245 | CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); |
| 3246 | |
| 3247 | CharUnits offset; |
| 3248 | // Adjust the alignment down to the given offset. |
| 3249 | // As a special case, if the LLVM field index is 0, we know that this |
| 3250 | // is zero. |
| 3251 | assert((idx != 0 || CGF.getContext().getASTRecordLayout(rec) |
| 3252 | .getFieldOffset(field->getFieldIndex()) == 0) && |
| 3253 | "LLVM field at index zero had non-zero offset?"); |
| 3254 | if (idx != 0) { |
| 3255 | auto &recLayout = CGF.getContext().getASTRecordLayout(rec); |
| 3256 | auto offsetInBits = recLayout.getFieldOffset(field->getFieldIndex()); |
| 3257 | offset = CGF.getContext().toCharUnitsFromBits(offsetInBits); |
| 3258 | } |
| 3259 | |
| 3260 | return CGF.Builder.CreateStructGEP(base, idx, offset, field->getName()); |
| 3261 | } |
| 3262 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3263 | LValue CodeGenFunction::EmitLValueForField(LValue base, |
| 3264 | const FieldDecl *field) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3265 | AlignmentSource fieldAlignSource = |
| 3266 | getFieldAlignmentSource(base.getAlignmentSource()); |
| 3267 | |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3268 | if (field->isBitField()) { |
| 3269 | const CGRecordLayout &RL = |
| 3270 | CGM.getTypes().getCGRecordLayout(field->getParent()); |
| 3271 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(field); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3272 | Address Addr = base.getAddress(); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3273 | unsigned Idx = RL.getLLVMFieldNo(field); |
| 3274 | if (Idx != 0) |
| 3275 | // For structs, we GEP to the field that the record layout suggests. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3276 | Addr = Builder.CreateStructGEP(Addr, Idx, Info.StorageOffset, |
| 3277 | field->getName()); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3278 | // Get the access type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3279 | llvm::Type *FieldIntTy = |
| 3280 | llvm::Type::getIntNTy(getLLVMContext(), Info.StorageSize); |
| 3281 | if (Addr.getElementType() != FieldIntTy) |
| 3282 | Addr = Builder.CreateElementBitCast(Addr, FieldIntTy); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3283 | |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3284 | QualType fieldType = |
| 3285 | field->getType().withCVRQualifiers(base.getVRQualifiers()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3286 | return LValue::MakeBitfield(Addr, Info, fieldType, fieldAlignSource); |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3287 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3288 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3289 | const RecordDecl *rec = field->getParent(); |
| 3290 | QualType type = field->getType(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3291 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3292 | bool mayAlias = rec->hasAttr<MayAliasAttr>(); |
| 3293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3294 | Address addr = base.getAddress(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3295 | unsigned cvr = base.getVRQualifiers(); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3296 | bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3297 | if (rec->isUnion()) { |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 3298 | // For unions, there is no pointer adjustment. |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3299 | assert(!type->isReferenceType() && "union has reference member"); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3300 | // TODO: handle path-aware TBAA for union. |
| 3301 | TBAAPath = false; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3302 | } else { |
| 3303 | // For structs, we GEP to the field that the record layout suggests. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3304 | addr = emitAddrOfFieldStorage(*this, addr, field); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3305 | |
| 3306 | // If this is a reference field, load the reference right now. |
| 3307 | if (const ReferenceType *refType = type->getAs<ReferenceType>()) { |
| 3308 | llvm::LoadInst *load = Builder.CreateLoad(addr, "ref"); |
| 3309 | if (cvr & Qualifiers::Volatile) load->setVolatile(true); |
| 3310 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3311 | // Loading the reference will disable path-aware TBAA. |
| 3312 | TBAAPath = false; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3313 | if (CGM.shouldUseTBAA()) { |
| 3314 | llvm::MDNode *tbaa; |
| 3315 | if (mayAlias) |
| 3316 | tbaa = CGM.getTBAAInfo(getContext().CharTy); |
| 3317 | else |
| 3318 | tbaa = CGM.getTBAAInfo(type); |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 3319 | if (tbaa) |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 3320 | CGM.DecorateInstructionWithTBAA(load, tbaa); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3323 | mayAlias = false; |
| 3324 | type = refType->getPointeeType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3325 | |
| 3326 | CharUnits alignment = |
| 3327 | getNaturalTypeAlignment(type, &fieldAlignSource, /*pointee*/ true); |
| 3328 | addr = Address(load, alignment); |
| 3329 | |
| 3330 | // Qualifiers on the struct don't apply to the referencee, and |
| 3331 | // we'll pick up CVR from the actual type later, so reset these |
| 3332 | // additional qualifiers now. |
| 3333 | cvr = 0; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3334 | } |
Devang Patel | ed93c3c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 3335 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 3337 | // Make sure that the address is pointing to the right type. This is critical |
| 3338 | // for both unions and structs. A union needs a bitcast, a struct element |
| 3339 | // will need a bitcast if the LLVM type laid out doesn't match the desired |
| 3340 | // type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3341 | addr = Builder.CreateElementBitCast(addr, |
| 3342 | CGM.getTypes().ConvertTypeForMem(type), |
| 3343 | field->getName()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3344 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3345 | if (field->hasAttr<AnnotateAttr>()) |
| 3346 | addr = EmitFieldAnnotations(field, addr); |
| 3347 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3348 | LValue LV = MakeAddrLValue(addr, type, fieldAlignSource); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3349 | LV.getQuals().addCVRQualifiers(cvr); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3350 | if (TBAAPath) { |
| 3351 | const ASTRecordLayout &Layout = |
| 3352 | getContext().getASTRecordLayout(field->getParent()); |
| 3353 | // Set the base type to be the base type of the base LValue and |
| 3354 | // update offset to be relative to the base type. |
Manman Ren | 0e52166 | 2013-04-27 00:39:37 +0000 | [diff] [blame] | 3355 | LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType()); |
| 3356 | LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() + |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3357 | Layout.getFieldOffset(field->getFieldIndex()) / |
| 3358 | getContext().getCharWidth()); |
| 3359 | } |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3360 | |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 3361 | // __weak attribute on a field is ignored. |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3362 | if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak) |
| 3363 | LV.getQuals().removeObjCGCAttr(); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3364 | |
| 3365 | // Fields of may_alias structs act like 'char' for TBAA purposes. |
| 3366 | // FIXME: this should get propagated down through anonymous structs |
| 3367 | // and unions. |
| 3368 | if (mayAlias && LV.getTBAAInfo()) |
| 3369 | LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy)); |
| 3370 | |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3371 | return LV; |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3374 | LValue |
| 3375 | CodeGenFunction::EmitLValueForFieldInitialization(LValue Base, |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3376 | const FieldDecl *Field) { |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3377 | QualType FieldType = Field->getType(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3378 | |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3379 | if (!FieldType->isReferenceType()) |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3380 | return EmitLValueForField(Base, Field); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3381 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3382 | Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3383 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3384 | // Make sure that the address is pointing to the right type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3385 | llvm::Type *llvmType = ConvertTypeForMem(FieldType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3386 | V = Builder.CreateElementBitCast(V, llvmType, Field->getName()); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3387 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3388 | // TODO: access-path TBAA? |
| 3389 | auto FieldAlignSource = getFieldAlignmentSource(Base.getAlignmentSource()); |
| 3390 | return MakeAddrLValue(V, FieldType, FieldAlignSource); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 3393 | LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ |
Richard Smith | 2d988f0 | 2011-11-22 22:48:32 +0000 | [diff] [blame] | 3394 | if (E->isFileScope()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3395 | ConstantAddress GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E); |
| 3396 | return MakeAddrLValue(GlobalPtr, E->getType(), AlignmentSource::Decl); |
Richard Smith | 2d988f0 | 2011-11-22 22:48:32 +0000 | [diff] [blame] | 3397 | } |
Fariborz Jahanian | 5d53fcd | 2012-06-07 18:15:55 +0000 | [diff] [blame] | 3398 | if (E->getType()->isVariablyModifiedType()) |
| 3399 | // make sure to emit the VLA size. |
| 3400 | EmitVariablyModifiedType(E->getType()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3402 | Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 3403 | const Expr *InitExpr = E->getInitializer(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3404 | LValue Result = MakeAddrLValue(DeclPtr, E->getType(), AlignmentSource::Decl); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 3405 | |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 3406 | EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), |
| 3407 | /*Init*/ true); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 3408 | |
| 3409 | return Result; |
| 3410 | } |
| 3411 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3412 | LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) { |
| 3413 | if (!E->isGLValue()) |
| 3414 | // Initializing an aggregate temporary in C++11: T{...}. |
| 3415 | return EmitAggExprToLValue(E); |
| 3416 | |
| 3417 | // An lvalue initializer list must be initializing a reference. |
| 3418 | assert(E->getNumInits() == 1 && "reference init with multiple values"); |
| 3419 | return EmitLValue(E->getInit(0)); |
| 3420 | } |
| 3421 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3422 | /// Emit the operand of a glvalue conditional operator. This is either a glvalue |
| 3423 | /// or a (possibly-parenthesized) throw-expression. If this is a throw, no |
| 3424 | /// LValue is returned and the current block has been terminated. |
| 3425 | static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF, |
| 3426 | const Expr *Operand) { |
| 3427 | if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) { |
| 3428 | CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false); |
| 3429 | return None; |
| 3430 | } |
| 3431 | |
| 3432 | return CGF.EmitLValue(Operand); |
| 3433 | } |
| 3434 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3435 | LValue CodeGenFunction:: |
| 3436 | EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { |
| 3437 | if (!expr->isGLValue()) { |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3438 | // ?: here should be an aggregate. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3439 | assert(hasAggregateEvaluationKind(expr->getType()) && |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3440 | "Unexpected conditional operator!"); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3441 | return EmitAggExprToLValue(expr); |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 3442 | } |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 3443 | |
Eli Friedman | 5995489 | 2012-01-25 05:04:17 +0000 | [diff] [blame] | 3444 | OpaqueValueMapping binding(*this, expr); |
| 3445 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3446 | const Expr *condExpr = expr->getCond(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 3447 | bool CondExprBool; |
| 3448 | if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3449 | const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 3450 | if (!CondExprBool) std::swap(live, dead); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3451 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3452 | if (!ContainsLabel(dead)) { |
Justin Bogner | ea278c3 | 2014-01-07 00:20:28 +0000 | [diff] [blame] | 3453 | // If the true case is live, we need to track its region. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3454 | if (CondExprBool) |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3455 | incrementProfileCounter(expr); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3456 | return EmitLValue(live); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3457 | } |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3460 | llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true"); |
| 3461 | llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false"); |
| 3462 | llvm::BasicBlock *contBlock = createBasicBlock("cond.end"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3463 | |
| 3464 | ConditionalEvaluation eval(*this); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3465 | EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, getProfileCount(expr)); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3466 | |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3467 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3468 | EmitBlock(lhsBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3469 | incrementProfileCounter(expr); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3470 | eval.begin(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3471 | Optional<LValue> lhs = |
| 3472 | EmitLValueOrThrowExpression(*this, expr->getTrueExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3473 | eval.end(*this); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3474 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3475 | if (lhs && !lhs->isSimple()) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3476 | return EmitUnsupportedLValue(expr, "conditional operator"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3477 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3478 | lhsBlock = Builder.GetInsertBlock(); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3479 | if (lhs) |
| 3480 | Builder.CreateBr(contBlock); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3481 | |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3482 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3483 | EmitBlock(rhsBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3484 | eval.begin(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3485 | Optional<LValue> rhs = |
| 3486 | EmitLValueOrThrowExpression(*this, expr->getFalseExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3487 | eval.end(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3488 | if (rhs && !rhs->isSimple()) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3489 | return EmitUnsupportedLValue(expr, "conditional operator"); |
| 3490 | rhsBlock = Builder.GetInsertBlock(); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3491 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3492 | EmitBlock(contBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3493 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3494 | if (lhs && rhs) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3495 | llvm::PHINode *phi = Builder.CreatePHI(lhs->getPointer()->getType(), |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3496 | 2, "cond-lvalue"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3497 | phi->addIncoming(lhs->getPointer(), lhsBlock); |
| 3498 | phi->addIncoming(rhs->getPointer(), rhsBlock); |
| 3499 | Address result(phi, std::min(lhs->getAlignment(), rhs->getAlignment())); |
| 3500 | AlignmentSource alignSource = |
| 3501 | std::max(lhs->getAlignmentSource(), rhs->getAlignmentSource()); |
| 3502 | return MakeAddrLValue(result, expr->getType(), alignSource); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3503 | } else { |
| 3504 | assert((lhs || rhs) && |
| 3505 | "both operands of glvalue conditional are throw-expressions?"); |
| 3506 | return lhs ? *lhs : *rhs; |
| 3507 | } |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3510 | /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference |
| 3511 | /// type. If the cast is to a reference, we can have the usual lvalue result, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3512 | /// otherwise if a cast is needed by the code generator in an lvalue context, |
| 3513 | /// then it must mean that we need the address of an aggregate in order to |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3514 | /// access one of its members. This can happen for all the reasons that casts |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3515 | /// are permitted with aggregate result, including noop aggregate casts, and |
| 3516 | /// cast from scalar to union. |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 3517 | LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3518 | switch (E->getCastKind()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3519 | case CK_ToVoid: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3520 | case CK_BitCast: |
| 3521 | case CK_ArrayToPointerDecay: |
| 3522 | case CK_FunctionToPointerDecay: |
| 3523 | case CK_NullToMemberPointer: |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3524 | case CK_NullToPointer: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3525 | case CK_IntegralToPointer: |
| 3526 | case CK_PointerToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3527 | case CK_PointerToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3528 | case CK_VectorSplat: |
| 3529 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3530 | case CK_BooleanToSignedIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3531 | case CK_IntegralToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3532 | case CK_IntegralToFloating: |
| 3533 | case CK_FloatingToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3534 | case CK_FloatingToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3535 | case CK_FloatingCast: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3536 | case CK_FloatingRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3537 | case CK_FloatingComplexToReal: |
| 3538 | case CK_FloatingComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3539 | case CK_FloatingComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3540 | case CK_FloatingComplexToIntegralComplex: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3541 | case CK_IntegralRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3542 | case CK_IntegralComplexToReal: |
| 3543 | case CK_IntegralComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3544 | case CK_IntegralComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3545 | case CK_IntegralComplexToFloatingComplex: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3546 | case CK_DerivedToBaseMemberPointer: |
| 3547 | case CK_BaseToDerivedMemberPointer: |
| 3548 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 3549 | case CK_ReinterpretMemberPointer: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3550 | case CK_AnyPointerToBlockPointerCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3551 | case CK_ARCProduceObject: |
| 3552 | case CK_ARCConsumeObject: |
| 3553 | case CK_ARCReclaimReturnedObject: |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3554 | case CK_ARCExtendBlockObject: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3555 | case CK_CopyAndAutoreleaseBlockObject: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 3556 | case CK_AddressSpaceConversion: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3557 | return EmitUnsupportedLValue(E, "unexpected cast lvalue"); |
| 3558 | |
| 3559 | case CK_Dependent: |
| 3560 | llvm_unreachable("dependent cast kind in IR gen!"); |
| 3561 | |
| 3562 | case CK_BuiltinFnToFnPtr: |
| 3563 | llvm_unreachable("builtin functions are handled elsewhere"); |
| 3564 | |
Eli Friedman | be4504d | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 3565 | // These are never l-values; just use the aggregate emission code. |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3566 | case CK_NonAtomicToAtomic: |
| 3567 | case CK_AtomicToNonAtomic: |
Eli Friedman | be4504d | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 3568 | return EmitAggExprToLValue(E); |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3569 | |
Anders Carlsson | 8a01a75 | 2011-04-11 02:03:26 +0000 | [diff] [blame] | 3570 | case CK_Dynamic: { |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3571 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3572 | Address V = LV.getAddress(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3573 | const auto *DCE = cast<CXXDynamicCastExpr>(E); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3574 | return MakeNaturalAlignAddrLValue(EmitDynamicCast(V, DCE), E->getType()); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3577 | case CK_ConstructorConversion: |
| 3578 | case CK_UserDefinedConversion: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3579 | case CK_CPointerToObjCPointerCast: |
| 3580 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3581 | case CK_NoOp: |
| 3582 | case CK_LValueToRValue: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 3583 | return EmitLValue(E->getSubExpr()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3584 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3585 | case CK_UncheckedDerivedToBase: |
| 3586 | case CK_DerivedToBase: { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3587 | const RecordType *DerivedClassTy = |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3588 | E->getSubExpr()->getType()->getAs<RecordType>(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3589 | auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3590 | |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3591 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3592 | Address This = LV.getAddress(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3593 | |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3594 | // Perform the derived-to-base conversion |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3595 | Address Base = GetAddressOfBaseClass( |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 3596 | This, DerivedClassDecl, E->path_begin(), E->path_end(), |
| 3597 | /*NullCheckValue=*/false, E->getExprLoc()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3598 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3599 | return MakeAddrLValue(Base, E->getType(), LV.getAlignmentSource()); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3600 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3601 | case CK_ToUnion: |
Daniel Dunbar | 9c4e465 | 2010-02-05 20:02:42 +0000 | [diff] [blame] | 3602 | return EmitAggExprToLValue(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3603 | case CK_BaseToDerived: { |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3604 | const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3605 | auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3606 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3607 | LValue LV = EmitLValue(E->getSubExpr()); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 3608 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3609 | // Perform the base-to-derived conversion |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3610 | Address Derived = |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3611 | GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3612 | E->path_begin(), E->path_end(), |
| 3613 | /*NullCheckValue=*/false); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3614 | |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3615 | // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is |
| 3616 | // performed and the object is not of the derived type. |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 3617 | if (sanitizePerformTypeCheck()) |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3618 | EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3619 | Derived.getPointer(), E->getType()); |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3620 | |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3621 | if (SanOpts.has(SanitizerKind::CFIDerivedCast)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3622 | EmitVTablePtrCheckForCast(E->getType(), Derived.getPointer(), |
| 3623 | /*MayBeNull=*/false, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 3624 | CFITCK_DerivedCast, E->getLocStart()); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3625 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3626 | return MakeAddrLValue(Derived, E->getType(), LV.getAlignmentSource()); |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3627 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3628 | case CK_LValueBitCast: { |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3629 | // This must be a reinterpret_cast (or c-style equivalent). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3630 | const auto *CE = cast<ExplicitCastExpr>(E); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3631 | |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 3632 | CGM.EmitExplicitCastExprType(CE, this); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 3633 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3634 | Address V = Builder.CreateBitCast(LV.getAddress(), |
| 3635 | ConvertType(CE->getTypeAsWritten())); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3636 | |
| 3637 | if (SanOpts.has(SanitizerKind::CFIUnrelatedCast)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3638 | EmitVTablePtrCheckForCast(E->getType(), V.getPointer(), |
| 3639 | /*MayBeNull=*/false, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 3640 | CFITCK_UnrelatedCast, E->getLocStart()); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3641 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3642 | return MakeAddrLValue(V, E->getType(), LV.getAlignmentSource()); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 3643 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3644 | case CK_ObjCObjectLValueCast: { |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3645 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3646 | Address V = Builder.CreateElementBitCast(LV.getAddress(), |
| 3647 | ConvertType(E->getType())); |
| 3648 | return MakeAddrLValue(V, E->getType(), LV.getAlignmentSource()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3649 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3650 | case CK_ZeroToOCLEvent: |
| 3651 | llvm_unreachable("NULL to OpenCL event lvalue cast is not valid"); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3652 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3653 | |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 3654 | llvm_unreachable("Unhandled lvalue cast kind?"); |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3657 | LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) { |
John McCall | 9a54961 | 2011-11-08 22:54:08 +0000 | [diff] [blame] | 3658 | assert(OpaqueValueMappingData::shouldBindAsLValue(e)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3659 | return getOpaqueLValueMapping(e); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3662 | RValue CodeGenFunction::EmitRValueForField(LValue LV, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3663 | const FieldDecl *FD, |
| 3664 | SourceLocation Loc) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3665 | QualType FT = FD->getType(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3666 | LValue FieldLV = EmitLValueForField(LV, FD); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3667 | switch (getEvaluationKind(FT)) { |
| 3668 | case TEK_Complex: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3669 | return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3670 | case TEK_Aggregate: |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3671 | return FieldLV.asAggregateRValue(); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3672 | case TEK_Scalar: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3673 | return EmitLoadOfLValue(FieldLV, Loc); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3674 | } |
| 3675 | llvm_unreachable("bad evaluation kind"); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3676 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 3678 | //===--------------------------------------------------------------------===// |
| 3679 | // Expression Emission |
| 3680 | //===--------------------------------------------------------------------===// |
| 3681 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3682 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 3683 | ReturnValueSlot ReturnValue) { |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 3684 | // Builtins never have block type. |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 3685 | if (E->getCallee()->getType()->isBlockPointerType()) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 3686 | return EmitBlockCallExpr(E, ReturnValue); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 3687 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3688 | if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E)) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 3689 | return EmitCXXMemberCallExpr(CE, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3690 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3691 | if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E)) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 3692 | return EmitCUDAKernelCallExpr(CE, ReturnValue); |
| 3693 | |
Douglas Gregor | e0e9630 | 2011-09-06 21:41:04 +0000 | [diff] [blame] | 3694 | const Decl *TargetDecl = E->getCalleeDecl(); |
| 3695 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) { |
| 3696 | if (unsigned builtinID = FD->getBuiltinID()) |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3697 | return EmitBuiltinExpr(FD, builtinID, E, ReturnValue); |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3700 | if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E)) |
Anders Carlsson | 4034a95 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 3701 | if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl)) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 3702 | return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3703 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3704 | if (const auto *PseudoDtor = |
| 3705 | dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3706 | QualType DestroyedType = PseudoDtor->getDestroyedType(); |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3707 | if (DestroyedType.hasStrongOrWeakObjCLifetime()) { |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 3708 | // Automatic Reference Counting: |
| 3709 | // If the pseudo-expression names a retainable object with weak or |
| 3710 | // strong lifetime, the object shall be released. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3711 | Expr *BaseExpr = PseudoDtor->getBase(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3712 | Address BaseValue = Address::invalid(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3713 | Qualifiers BaseQuals; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3714 | |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 3715 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3716 | if (PseudoDtor->isArrow()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3717 | BaseValue = EmitPointerWithAlignment(BaseExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3718 | const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>(); |
| 3719 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
| 3720 | } else { |
| 3721 | LValue BaseLV = EmitLValue(BaseExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3722 | BaseValue = BaseLV.getAddress(); |
| 3723 | QualType BaseTy = BaseExpr->getType(); |
| 3724 | BaseQuals = BaseTy.getQualifiers(); |
| 3725 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3726 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 3727 | switch (DestroyedType.getObjCLifetime()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3728 | case Qualifiers::OCL_None: |
| 3729 | case Qualifiers::OCL_ExplicitNone: |
| 3730 | case Qualifiers::OCL_Autoreleasing: |
| 3731 | break; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3732 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3733 | case Qualifiers::OCL_Strong: |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3734 | EmitARCRelease(Builder.CreateLoad(BaseValue, |
Benjamin Kramer | dd19c01 | 2011-06-18 10:34:00 +0000 | [diff] [blame] | 3735 | PseudoDtor->getDestroyedType().isVolatileQualified()), |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 3736 | ARCPreciseLifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3737 | break; |
| 3738 | |
| 3739 | case Qualifiers::OCL_Weak: |
| 3740 | EmitARCDestroyWeak(BaseValue); |
| 3741 | break; |
| 3742 | } |
| 3743 | } else { |
| 3744 | // C++ [expr.pseudo]p1: |
| 3745 | // The result shall only be used as the operand for the function call |
| 3746 | // operator (), and the result of such a call has type void. The only |
| 3747 | // effect is the evaluation of the postfix-expression before the dot or |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3748 | // arrow. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3749 | EmitScalarExpr(E->getCallee()); |
| 3750 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3751 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3752 | return RValue::get(nullptr); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3753 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3754 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 3755 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 3756 | return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue, |
| 3757 | TargetDecl); |
Chris Lattner | 9e47ead | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 3760 | LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 3761 | // Comma expressions just emit their LHS then their RHS as an l-value. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3762 | if (E->getOpcode() == BO_Comma) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 3763 | EmitIgnoredExpr(E->getLHS()); |
Eli Friedman | 5445f6e | 2009-12-07 20:18:11 +0000 | [diff] [blame] | 3764 | EnsureInsertPoint(); |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 3765 | return EmitLValue(E->getRHS()); |
| 3766 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3767 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3768 | if (E->getOpcode() == BO_PtrMemD || |
| 3769 | E->getOpcode() == BO_PtrMemI) |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 3770 | return EmitPointerToDataMemberBinaryExpr(E); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 3771 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 3772 | assert(E->getOpcode() == BO_Assign && "unexpected binary l-value"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3773 | |
| 3774 | // Note that in all of these cases, __block variables need the RHS |
| 3775 | // evaluated first just in case the variable gets moved by the RHS. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3776 | |
| 3777 | switch (getEvaluationKind(E->getType())) { |
| 3778 | case TEK_Scalar: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3779 | switch (E->getLHS()->getType().getObjCLifetime()) { |
| 3780 | case Qualifiers::OCL_Strong: |
| 3781 | return EmitARCStoreStrong(E, /*ignored*/ false).first; |
| 3782 | |
| 3783 | case Qualifiers::OCL_Autoreleasing: |
| 3784 | return EmitARCStoreAutoreleasing(E).first; |
| 3785 | |
| 3786 | // No reason to do any of these differently. |
| 3787 | case Qualifiers::OCL_None: |
| 3788 | case Qualifiers::OCL_ExplicitNone: |
| 3789 | case Qualifiers::OCL_Weak: |
| 3790 | break; |
| 3791 | } |
| 3792 | |
John McCall | d0a3001 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 3793 | RValue RV = EmitAnyExpr(E->getRHS()); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 3794 | LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 3795 | EmitStoreThroughLValue(RV, LV); |
Anders Carlsson | 0999aaf | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 3796 | return LV; |
| 3797 | } |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 3798 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3799 | case TEK_Complex: |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 3800 | return EmitComplexAssignmentLValue(E); |
| 3801 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3802 | case TEK_Aggregate: |
| 3803 | return EmitAggExprToLValue(E); |
| 3804 | } |
| 3805 | llvm_unreachable("bad evaluation kind"); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 3808 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 3809 | RValue RV = EmitCallExpr(E); |
Anders Carlsson | 4ae70ff | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 3810 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 3811 | if (!RV.isScalar()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3812 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
| 3813 | AlignmentSource::Decl); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3814 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 3815 | assert(E->getCallReturnType(getContext())->isReferenceType() && |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 3816 | "Can't have a scalar return unless the return type is a " |
| 3817 | "reference type!"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3818 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3819 | return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 3822 | LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { |
| 3823 | // FIXME: This shouldn't require another copy. |
Daniel Dunbar | d0bc7b9 | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 3824 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 3827 | LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 3828 | assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() |
| 3829 | && "binding l-value to type which needs a temporary"); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3830 | AggValueSlot Slot = CreateAggTemp(E->getType()); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3831 | EmitCXXConstructExpr(E, Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
| 3833 | AlignmentSource::Decl); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 3836 | LValue |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 3837 | CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3838 | return MakeNaturalAlignAddrLValue(EmitCXXTypeidExpr(E), E->getType()); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3841 | Address CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 3842 | return Builder.CreateElementBitCast(CGM.GetAddrOfUuidDescriptor(E), |
| 3843 | ConvertType(E->getType())); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3847 | return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType(), |
| 3848 | AlignmentSource::Decl); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 3851 | LValue |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 3852 | CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 3853 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 3854 | Slot.setExternallyDestructed(); |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 3855 | EmitAggExpr(E->getSubExpr(), Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3856 | EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddress()); |
| 3857 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
| 3858 | AlignmentSource::Decl); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 3861 | LValue |
| 3862 | CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) { |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 3863 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 3864 | EmitLambdaExpr(E, Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3865 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
| 3866 | AlignmentSource::Decl); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 3869 | LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 3870 | RValue RV = EmitObjCMessageExpr(E); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3871 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 3872 | if (!RV.isScalar()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3873 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
| 3874 | AlignmentSource::Decl); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3875 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3876 | assert(E->getMethodDecl()->getReturnType()->isReferenceType() && |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 3877 | "Can't have a scalar return unless the return type is a " |
| 3878 | "reference type!"); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3879 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3880 | return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3883 | LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3884 | Address V = |
| 3885 | CGM.getObjCRuntime().GetAddrOfSelector(*this, E->getSelector()); |
| 3886 | return MakeAddrLValue(V, E->getType(), AlignmentSource::Decl); |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3889 | llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 3890 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3891 | return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3894 | LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, |
| 3895 | llvm::Value *BaseValue, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 3896 | const ObjCIvarDecl *Ivar, |
| 3897 | unsigned CVRQualifiers) { |
Chris Lattner | c4688d2 | 2009-04-17 17:44:48 +0000 | [diff] [blame] | 3898 | return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, |
Daniel Dunbar | 9ebf951 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 3899 | Ivar, CVRQualifiers); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 3903 | // FIXME: A lot of the code below could be shared with EmitMemberExpr. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3904 | llvm::Value *BaseValue = nullptr; |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 3905 | const Expr *BaseExpr = E->getBase(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3906 | Qualifiers BaseQuals; |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3907 | QualType ObjectTy; |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 3908 | if (E->isArrow()) { |
| 3909 | BaseValue = EmitScalarExpr(BaseExpr); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3910 | ObjectTy = BaseExpr->getType()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3911 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 3912 | } else { |
| 3913 | LValue BaseLV = EmitLValue(BaseExpr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3914 | BaseValue = BaseLV.getPointer(); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3915 | ObjectTy = BaseExpr->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3916 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 3917 | } |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 3918 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3919 | LValue LV = |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3920 | EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), |
| 3921 | BaseQuals.getCVRQualifiers()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 3922 | setObjCGCLValueClass(getContext(), E, LV); |
| 3923 | return LV; |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 3926 | LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 3927 | // Can only get l-value for message expression returning aggregate type |
| 3928 | RValue RV = EmitAnyExprToTemp(E); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3929 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
| 3930 | AlignmentSource::Decl); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 3933 | RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 3934 | const CallExpr *E, ReturnValueSlot ReturnValue, |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 3935 | CGCalleeInfo CalleeInfo, llvm::Value *Chain) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3936 | // Get the actual function type. The callee type will always be a pointer to |
| 3937 | // function type or a block pointer type. |
| 3938 | assert(CalleeType->isFunctionPointerType() && |
Anders Carlsson | d8db853 | 2009-04-07 18:53:02 +0000 | [diff] [blame] | 3939 | "Call must have function pointer type!"); |
| 3940 | |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 3941 | // Preserve the non-canonical function type because things like exception |
| 3942 | // specifications disappear in the canonical type. That information is useful |
| 3943 | // to drive the generation of more accurate code for this call later on. |
| 3944 | const FunctionProtoType *NonCanonicalFTP = CalleeType->getAs<PointerType>() |
| 3945 | ->getPointeeType() |
| 3946 | ->getAs<FunctionProtoType>(); |
| 3947 | |
| 3948 | const Decl *TargetDecl = CalleeInfo.getCalleeDecl(); |
| 3949 | |
Eric Christopher | 2b2d56f | 2015-11-12 00:44:12 +0000 | [diff] [blame] | 3950 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
Eric Christopher | 39db726 | 2015-11-14 01:56:04 +0000 | [diff] [blame] | 3951 | // We can only guarantee that a function is called from the correct |
| 3952 | // context/function based on the appropriate target attributes, |
| 3953 | // so only check in the case where we have both always_inline and target |
| 3954 | // since otherwise we could be making a conditional call after a check for |
| 3955 | // the proper cpu features (and it won't cause code generation issues due to |
| 3956 | // function based code generation). |
Eric Christopher | 2b2d56f | 2015-11-12 00:44:12 +0000 | [diff] [blame] | 3957 | if (TargetDecl->hasAttr<AlwaysInlineAttr>() && |
| 3958 | TargetDecl->hasAttr<TargetAttr>()) |
| 3959 | checkTargetFeatures(E, FD); |
| 3960 | |
John McCall | 6fd4c23 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 3961 | CalleeType = getContext().getCanonicalType(CalleeType); |
| 3962 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3963 | const auto *FnType = |
| 3964 | cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType()); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 3965 | |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 3966 | if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) && |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 3967 | (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { |
| 3968 | if (llvm::Constant *PrefixSig = |
| 3969 | CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) { |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 3970 | SanitizerScope SanScope(this); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 3971 | llvm::Constant *FTRTTIConst = |
| 3972 | CGM.GetAddrOfRTTIDescriptor(QualType(FnType, 0), /*ForEH=*/true); |
| 3973 | llvm::Type *PrefixStructTyElems[] = { |
| 3974 | PrefixSig->getType(), |
| 3975 | FTRTTIConst->getType() |
| 3976 | }; |
| 3977 | llvm::StructType *PrefixStructTy = llvm::StructType::get( |
| 3978 | CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true); |
| 3979 | |
| 3980 | llvm::Value *CalleePrefixStruct = Builder.CreateBitCast( |
| 3981 | Callee, llvm::PointerType::getUnqual(PrefixStructTy)); |
| 3982 | llvm::Value *CalleeSigPtr = |
David Blaikie | 17ea266 | 2015-04-04 21:07:17 +0000 | [diff] [blame] | 3983 | Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3984 | llvm::Value *CalleeSig = |
| 3985 | Builder.CreateAlignedLoad(CalleeSigPtr, getIntAlign()); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 3986 | llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig); |
| 3987 | |
| 3988 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 3989 | llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck"); |
| 3990 | Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont); |
| 3991 | |
| 3992 | EmitBlock(TypeCheck); |
| 3993 | llvm::Value *CalleeRTTIPtr = |
David Blaikie | 17ea266 | 2015-04-04 21:07:17 +0000 | [diff] [blame] | 3994 | Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3995 | llvm::Value *CalleeRTTI = |
| 3996 | Builder.CreateAlignedLoad(CalleeRTTIPtr, getPointerAlign()); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 3997 | llvm::Value *CalleeRTTIMatch = |
| 3998 | Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst); |
| 3999 | llvm::Constant *StaticData[] = { |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 4000 | EmitCheckSourceLocation(E->getLocStart()), |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4001 | EmitCheckTypeDescriptor(CalleeType) |
| 4002 | }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 4003 | EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function), |
| 4004 | "function_type_mismatch", StaticData, Callee); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4005 | |
| 4006 | Builder.CreateBr(Cont); |
| 4007 | EmitBlock(Cont); |
| 4008 | } |
| 4009 | } |
| 4010 | |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4011 | // If we are checking indirect calls and this call is indirect, check that the |
| 4012 | // function pointer is a member of the bit set for the function type. |
| 4013 | if (SanOpts.has(SanitizerKind::CFIICall) && |
| 4014 | (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { |
| 4015 | SanitizerScope SanScope(this); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 4016 | EmitSanitizerStatReport(llvm::SanStat_CFI_ICall); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4017 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4018 | llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0)); |
| 4019 | llvm::Value *BitSetName = llvm::MetadataAsValue::get(getLLVMContext(), MD); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4020 | |
| 4021 | llvm::Value *CastedCallee = Builder.CreateBitCast(Callee, Int8PtrTy); |
| 4022 | llvm::Value *BitSetTest = |
| 4023 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::bitset_test), |
| 4024 | {CastedCallee, BitSetName}); |
| 4025 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4026 | auto TypeId = CGM.CreateCfiIdForTypeMetadata(MD); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 4027 | llvm::Constant *StaticData[] = { |
| 4028 | llvm::ConstantInt::get(Int8Ty, CFITCK_ICall), |
| 4029 | EmitCheckSourceLocation(E->getLocStart()), |
| 4030 | EmitCheckTypeDescriptor(QualType(FnType, 0)), |
| 4031 | }; |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4032 | if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && TypeId) { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 4033 | EmitCfiSlowPathCheck(SanitizerKind::CFIICall, BitSetTest, TypeId, |
| 4034 | CastedCallee, StaticData); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4035 | } else { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4036 | EmitCheck(std::make_pair(BitSetTest, SanitizerKind::CFIICall), |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4037 | "cfi_check_fail", StaticData, |
| 4038 | {CastedCallee, llvm::UndefValue::get(IntPtrTy)}); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4039 | } |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 4042 | CallArgList Args; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4043 | if (Chain) |
| 4044 | Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)), |
| 4045 | CGM.getContext().VoidPtrTy); |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 4046 | EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arguments(), |
| 4047 | E->getDirectCallee(), /*ParamsToSkip*/ 0); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 4048 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4049 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall( |
| 4050 | Args, FnType, /*isChainCall=*/Chain); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 4051 | |
| 4052 | // C99 6.5.2.2p6: |
| 4053 | // If the expression that denotes the called function has a type |
| 4054 | // that does not include a prototype, [the default argument |
| 4055 | // promotions are performed]. If the number of arguments does not |
| 4056 | // equal the number of parameters, the behavior is undefined. If |
| 4057 | // the function is defined with a type that includes a prototype, |
| 4058 | // and either the prototype ends with an ellipsis (, ...) or the |
| 4059 | // types of the arguments after promotion are not compatible with |
| 4060 | // the types of the parameters, the behavior is undefined. If the |
| 4061 | // function is defined with a type that does not include a |
| 4062 | // prototype, and the types of the arguments after promotion are |
| 4063 | // not compatible with those of the parameters after promotion, |
| 4064 | // the behavior is undefined [except in some trivial cases]. |
| 4065 | // That is, in the general case, we should assume that a call |
| 4066 | // through an unprototyped function type works like a *non-variadic* |
| 4067 | // call. The way we make this work is to cast to the exact type |
| 4068 | // of the promoted arguments. |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4069 | // |
| 4070 | // Chain calls use this same code path to add the invisible chain parameter |
| 4071 | // to the function type. |
| 4072 | if (isa<FunctionNoProtoType>(FnType) || Chain) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 4073 | llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 4074 | CalleeTy = CalleeTy->getPointerTo(); |
| 4075 | Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast"); |
| 4076 | } |
| 4077 | |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 4078 | return EmitCall(FnInfo, Callee, ReturnValue, Args, |
| 4079 | CGCalleeInfo(NonCanonicalFTP, TargetDecl)); |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 4080 | } |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 4081 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4082 | LValue CodeGenFunction:: |
| 4083 | EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4084 | Address BaseAddr = Address::invalid(); |
| 4085 | if (E->getOpcode() == BO_PtrMemI) { |
| 4086 | BaseAddr = EmitPointerWithAlignment(E->getLHS()); |
| 4087 | } else { |
| 4088 | BaseAddr = EmitLValue(E->getLHS()).getAddress(); |
| 4089 | } |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4090 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 4091 | llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); |
| 4092 | |
| 4093 | const MemberPointerType *MPT |
| 4094 | = E->getRHS()->getType()->getAs<MemberPointerType>(); |
| 4095 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4096 | AlignmentSource AlignSource; |
| 4097 | Address MemberAddr = |
| 4098 | EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT, |
| 4099 | &AlignSource); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 4100 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4101 | return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), AlignSource); |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 4102 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4103 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4104 | /// Given the address of a temporary variable, produce an r-value of |
| 4105 | /// its type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4106 | RValue CodeGenFunction::convertTempToRValue(Address addr, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4107 | QualType type, |
| 4108 | SourceLocation loc) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4109 | LValue lvalue = MakeAddrLValue(addr, type, AlignmentSource::Decl); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4110 | switch (getEvaluationKind(type)) { |
| 4111 | case TEK_Complex: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4112 | return RValue::getComplex(EmitLoadOfComplex(lvalue, loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4113 | case TEK_Aggregate: |
| 4114 | return lvalue.asAggregateRValue(); |
| 4115 | case TEK_Scalar: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4116 | return RValue::get(EmitLoadOfScalar(lvalue, loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4117 | } |
| 4118 | llvm_unreachable("bad evaluation kind"); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 4121 | void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) { |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4122 | assert(Val->getType()->isFPOrFPVectorTy()); |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 4123 | if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val)) |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4124 | return; |
| 4125 | |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 4126 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 4127 | llvm::MDNode *Node = MDHelper.createFPMath(Accuracy); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4128 | |
Duncan Sands | 6fc4619 | 2012-04-14 12:37:26 +0000 | [diff] [blame] | 4129 | cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4130 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4131 | |
| 4132 | namespace { |
| 4133 | struct LValueOrRValue { |
| 4134 | LValue LV; |
| 4135 | RValue RV; |
| 4136 | }; |
| 4137 | } |
| 4138 | |
| 4139 | static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, |
| 4140 | const PseudoObjectExpr *E, |
| 4141 | bool forLValue, |
| 4142 | AggValueSlot slot) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4143 | SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4144 | |
| 4145 | // Find the result expression, if any. |
| 4146 | const Expr *resultExpr = E->getResultExpr(); |
| 4147 | LValueOrRValue result; |
| 4148 | |
| 4149 | for (PseudoObjectExpr::const_semantics_iterator |
| 4150 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { |
| 4151 | const Expr *semantic = *i; |
| 4152 | |
| 4153 | // If this semantic expression is an opaque value, bind it |
| 4154 | // to the result of its source expression. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4155 | if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4156 | |
| 4157 | // If this is the result expression, we may need to evaluate |
| 4158 | // directly into the slot. |
| 4159 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; |
| 4160 | OVMA opaqueData; |
| 4161 | if (ov == resultExpr && ov->isRValue() && !forLValue && |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4162 | CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4163 | CGF.EmitAggExpr(ov->getSourceExpr(), slot); |
| 4164 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4165 | LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(), |
| 4166 | AlignmentSource::Decl); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4167 | opaqueData = OVMA::bind(CGF, ov, LV); |
| 4168 | result.RV = slot.asRValue(); |
| 4169 | |
| 4170 | // Otherwise, emit as normal. |
| 4171 | } else { |
| 4172 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); |
| 4173 | |
| 4174 | // If this is the result, also evaluate the result now. |
| 4175 | if (ov == resultExpr) { |
| 4176 | if (forLValue) |
| 4177 | result.LV = CGF.EmitLValue(ov); |
| 4178 | else |
| 4179 | result.RV = CGF.EmitAnyExpr(ov, slot); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | opaques.push_back(opaqueData); |
| 4184 | |
| 4185 | // Otherwise, if the expression is the result, evaluate it |
| 4186 | // and remember the result. |
| 4187 | } else if (semantic == resultExpr) { |
| 4188 | if (forLValue) |
| 4189 | result.LV = CGF.EmitLValue(semantic); |
| 4190 | else |
| 4191 | result.RV = CGF.EmitAnyExpr(semantic, slot); |
| 4192 | |
| 4193 | // Otherwise, evaluate the expression in an ignored context. |
| 4194 | } else { |
| 4195 | CGF.EmitIgnoredExpr(semantic); |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | // Unbind all the opaques now. |
| 4200 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) |
| 4201 | opaques[i].unbind(CGF); |
| 4202 | |
| 4203 | return result; |
| 4204 | } |
| 4205 | |
| 4206 | RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E, |
| 4207 | AggValueSlot slot) { |
| 4208 | return emitPseudoObjectExpr(*this, E, false, slot).RV; |
| 4209 | } |
| 4210 | |
| 4211 | LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) { |
| 4212 | return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV; |
| 4213 | } |