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