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 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CGCall.h" |
Tim Shen | 421119f | 2016-07-01 21:08:47 +0000 | [diff] [blame] | 16 | #include "CGCleanup.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" |
Tim Shen | 421119f | 2016-07-01 21:08:47 +0000 | [diff] [blame] | 21 | #include "CodeGenFunction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "CodeGenModule.h" |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 23 | #include "TargetInfo.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTContext.h" |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 25 | #include "clang/AST/Attr.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
Vedant Kumar | 4593a46 | 2016-12-09 23:48:18 +0000 | [diff] [blame] | 27 | #include "clang/AST/NSAPI.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Hashing.h" |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/MDBuilder.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ConvertUTF.h" |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Filipe Cabecinhas | ab731f7 | 2016-05-12 16:51:36 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Path.h" |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/SanitizerStats.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 39 | |
Filipe Cabecinhas | 84171bd | 2016-12-12 16:43:40 +0000 | [diff] [blame] | 40 | #include <string> |
| 41 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | using namespace CodeGen; |
| 44 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 45 | //===--------------------------------------------------------------------===// |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 46 | // Miscellaneous Helper Methods |
| 47 | //===--------------------------------------------------------------------===// |
| 48 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 49 | llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { |
| 50 | unsigned addressSpace = |
| 51 | cast<llvm::PointerType>(value->getType())->getAddressSpace(); |
| 52 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 53 | llvm::PointerType *destType = Int8PtrTy; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 54 | if (addressSpace) |
| 55 | destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace); |
| 56 | |
| 57 | if (value->getType() == destType) return value; |
| 58 | return Builder.CreateBitCast(value, destType); |
| 59 | } |
| 60 | |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 61 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 62 | /// block. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 63 | Address CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, CharUnits Align, |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 64 | const Twine &Name, |
| 65 | llvm::Value *ArraySize, |
| 66 | bool CastToDefaultAddrSpace) { |
| 67 | auto Alloca = CreateTempAlloca(Ty, Name, ArraySize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 68 | Alloca->setAlignment(Align.getQuantity()); |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 69 | llvm::Value *V = Alloca; |
| 70 | // Alloca always returns a pointer in alloca address space, which may |
| 71 | // be different from the type defined by the language. For example, |
| 72 | // in C++ the auto variables are in the default address space. Therefore |
| 73 | // cast alloca to the default address space when necessary. |
| 74 | if (CastToDefaultAddrSpace && getASTAllocaAddressSpace() != LangAS::Default) { |
| 75 | auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default); |
| 76 | V = getTargetHooks().performAddrSpaceCast( |
| 77 | *this, V, getASTAllocaAddressSpace(), LangAS::Default, |
| 78 | Ty->getPointerTo(DestAddrSpace), /*non-null*/ true); |
| 79 | } |
| 80 | |
| 81 | return Address(V, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 84 | /// CreateTempAlloca - This creates an alloca and inserts it into the entry |
| 85 | /// block if \p ArraySize is nullptr, otherwise inserts it at the current |
| 86 | /// insertion point of the builder. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 87 | llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 88 | const Twine &Name, |
| 89 | llvm::Value *ArraySize) { |
| 90 | if (ArraySize) |
| 91 | return Builder.CreateAlloca(Ty, ArraySize, Name); |
Matt Arsenault | 502ad60 | 2017-04-10 22:28:02 +0000 | [diff] [blame] | 92 | return new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(), |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 93 | ArraySize, Name, AllocaInsertPt); |
Chris Lattner | e9a6453 | 2007-06-22 21:44:33 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 95 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 96 | /// CreateDefaultAlignTempAlloca - This creates an alloca with the |
| 97 | /// default alignment of the corresponding LLVM type, which is *not* |
| 98 | /// guaranteed to be related in any way to the expected alignment of |
| 99 | /// an AST type that might have been lowered to Ty. |
| 100 | Address CodeGenFunction::CreateDefaultAlignTempAlloca(llvm::Type *Ty, |
| 101 | const Twine &Name) { |
| 102 | CharUnits Align = |
| 103 | CharUnits::fromQuantity(CGM.getDataLayout().getABITypeAlignment(Ty)); |
| 104 | return CreateTempAlloca(Ty, Align, Name); |
| 105 | } |
| 106 | |
| 107 | void CodeGenFunction::InitTempAlloca(Address Var, llvm::Value *Init) { |
| 108 | assert(isa<llvm::AllocaInst>(Var.getPointer())); |
| 109 | auto *Store = new llvm::StoreInst(Init, Var.getPointer()); |
| 110 | Store->setAlignment(Var.getAlignment().getQuantity()); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 111 | llvm::BasicBlock *Block = AllocaInsertPt->getParent(); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 112 | Block->getInstList().insertAfter(AllocaInsertPt->getIterator(), Store); |
John McCall | 2e6567a | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 113 | } |
| 114 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 115 | Address CodeGenFunction::CreateIRTemp(QualType Ty, const Twine &Name) { |
Daniel Dunbar | d004918 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 116 | CharUnits Align = getContext().getTypeAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 117 | return CreateTempAlloca(ConvertType(Ty), Align, Name); |
Daniel Dunbar | d004918 | 2010-02-16 19:44:13 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 120 | Address CodeGenFunction::CreateMemTemp(QualType Ty, const Twine &Name, |
| 121 | bool CastToDefaultAddrSpace) { |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 122 | // FIXME: Should we prefer the preferred type alignment here? |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 123 | return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name, |
| 124 | CastToDefaultAddrSpace); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align, |
Yaxun Liu | 84744c1 | 2017-06-19 17:03:41 +0000 | [diff] [blame^] | 128 | const Twine &Name, |
| 129 | bool CastToDefaultAddrSpace) { |
| 130 | return CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name, nullptr, |
| 131 | CastToDefaultAddrSpace); |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 134 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 135 | /// expression and compare the result against zero, returning an Int1Ty value. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 136 | llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 137 | PGO.setCurrentStmt(E); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 138 | if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 139 | llvm::Value *MemPtr = EmitScalarExpr(E); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 140 | return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT); |
Eli Friedman | 68396b1 | 2009-12-11 09:26:29 +0000 | [diff] [blame] | 141 | } |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 142 | |
| 143 | QualType BoolTy = getContext().BoolTy; |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 144 | SourceLocation Loc = E->getExprLoc(); |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 145 | if (!E->getType()->isAnyComplexType()) |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 146 | return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 147 | |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 148 | return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(), BoolTy, |
| 149 | Loc); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 150 | } |
| 151 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 152 | /// EmitIgnoredExpr - Emit code to compute the specified expression, |
| 153 | /// ignoring the result. |
| 154 | void CodeGenFunction::EmitIgnoredExpr(const Expr *E) { |
| 155 | if (E->isRValue()) |
| 156 | return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true); |
| 157 | |
| 158 | // Just emit it as an l-value and drop the result. |
| 159 | EmitLValue(E); |
| 160 | } |
| 161 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 162 | /// EmitAnyExpr - Emit code to compute the specified expression which |
| 163 | /// can have any type. The result is returned as an RValue struct. |
| 164 | /// If this is an aggregate expression, AggSlot indicates where the |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 165 | /// result should be returned. |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 166 | RValue CodeGenFunction::EmitAnyExpr(const Expr *E, |
| 167 | AggValueSlot aggSlot, |
| 168 | bool ignoreResult) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 169 | switch (getEvaluationKind(E->getType())) { |
| 170 | case TEK_Scalar: |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 171 | return RValue::get(EmitScalarExpr(E, ignoreResult)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 172 | case TEK_Complex: |
John McCall | 4e8ca4f | 2012-07-02 23:58:38 +0000 | [diff] [blame] | 173 | return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 174 | case TEK_Aggregate: |
| 175 | if (!ignoreResult && aggSlot.isIgnored()) |
| 176 | aggSlot = CreateAggTemp(E->getType(), "agg-temp"); |
| 177 | EmitAggExpr(E, aggSlot); |
| 178 | return aggSlot.asRValue(); |
| 179 | } |
| 180 | llvm_unreachable("bad evaluation kind"); |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 183 | /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will |
| 184 | /// always be accessible even if no aggregate location is provided. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 185 | RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) { |
| 186 | AggValueSlot AggSlot = AggValueSlot::ignored(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 187 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 188 | if (hasAggregateEvaluationKind(E->getType())) |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 189 | AggSlot = CreateAggTemp(E->getType(), "agg.tmp"); |
| 190 | return EmitAnyExpr(E, AggSlot); |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 191 | } |
| 192 | |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 193 | /// EmitAnyExprToMem - Evaluate an expression into a given memory |
| 194 | /// location. |
| 195 | void CodeGenFunction::EmitAnyExprToMem(const Expr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 196 | Address Location, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 197 | Qualifiers Quals, |
| 198 | bool IsInit) { |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 199 | // FIXME: This function should take an LValue as an argument. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 200 | switch (getEvaluationKind(E->getType())) { |
| 201 | case TEK_Complex: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 202 | EmitComplexExprIntoLValue(E, MakeAddrLValue(Location, E->getType()), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 203 | /*isInit*/ false); |
| 204 | return; |
| 205 | |
| 206 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 207 | EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 208 | AggValueSlot::IsDestructed_t(IsInit), |
John McCall | a8a39bc | 2011-08-26 05:38:08 +0000 | [diff] [blame] | 209 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 210 | AggValueSlot::IsAliased_t(!IsInit))); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
| 214 | case TEK_Scalar: { |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 215 | RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); |
Daniel Dunbar | f6fb7e2 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 216 | LValue LV = MakeAddrLValue(Location, E->getType()); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 217 | EmitStoreThroughLValue(RV, LV); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 218 | return; |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 219 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 220 | } |
| 221 | llvm_unreachable("bad evaluation kind"); |
John McCall | 2188696 | 2010-04-21 10:05:39 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 224 | static void |
| 225 | pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 226 | const Expr *E, Address ReferenceTemporary) { |
Rafael Espindola | b9d75ca | 2012-10-27 00:43:14 +0000 | [diff] [blame] | 227 | // Objective-C++ ARC: |
| 228 | // If we are binding a reference to a temporary that has ownership, we |
| 229 | // need to perform retain/release operations on the temporary. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 230 | // |
| 231 | // FIXME: This should be looking at E, not M. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 232 | if (auto Lifetime = M->getType().getObjCLifetime()) { |
| 233 | switch (Lifetime) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 234 | case Qualifiers::OCL_None: |
| 235 | case Qualifiers::OCL_ExplicitNone: |
| 236 | // Carry on to normal cleanup handling. |
| 237 | break; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 238 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 239 | case Qualifiers::OCL_Autoreleasing: |
| 240 | // Nothing to do; cleaned up by an autorelease pool. |
| 241 | return; |
| 242 | |
| 243 | case Qualifiers::OCL_Strong: |
| 244 | case Qualifiers::OCL_Weak: |
| 245 | switch (StorageDuration Duration = M->getStorageDuration()) { |
| 246 | case SD_Static: |
| 247 | // Note: we intentionally do not register a cleanup to release |
| 248 | // the object on program termination. |
| 249 | return; |
| 250 | |
| 251 | case SD_Thread: |
| 252 | // FIXME: We should probably register a cleanup in this case. |
| 253 | return; |
| 254 | |
| 255 | case SD_Automatic: |
| 256 | case SD_FullExpression: |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 257 | CodeGenFunction::Destroyer *Destroy; |
| 258 | CleanupKind CleanupKind; |
| 259 | if (Lifetime == Qualifiers::OCL_Strong) { |
| 260 | const ValueDecl *VD = M->getExtendingDecl(); |
| 261 | bool Precise = |
| 262 | VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>(); |
| 263 | CleanupKind = CGF.getARCCleanupKind(); |
| 264 | Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise |
| 265 | : &CodeGenFunction::destroyARCStrongImprecise; |
| 266 | } else { |
| 267 | // __weak objects always get EH cleanups; otherwise, exceptions |
| 268 | // could cause really nasty crashes instead of mere leaks. |
| 269 | CleanupKind = NormalAndEHCleanup; |
| 270 | Destroy = &CodeGenFunction::destroyARCWeak; |
| 271 | } |
| 272 | if (Duration == SD_FullExpression) |
| 273 | CGF.pushDestroy(CleanupKind, ReferenceTemporary, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 274 | M->getType(), *Destroy, |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 275 | CleanupKind & EHCleanup); |
| 276 | else |
| 277 | CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary, |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 278 | M->getType(), |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 279 | *Destroy, CleanupKind & EHCleanup); |
| 280 | return; |
| 281 | |
| 282 | case SD_Dynamic: |
| 283 | llvm_unreachable("temporary cannot have dynamic storage duration"); |
| 284 | } |
| 285 | llvm_unreachable("unknown storage duration"); |
| 286 | } |
| 287 | } |
| 288 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 289 | CXXDestructorDecl *ReferenceTemporaryDtor = nullptr; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 290 | if (const RecordType *RT = |
| 291 | E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { |
| 292 | // Get the destructor for the reference temporary. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 293 | auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 294 | if (!ClassDecl->hasTrivialDestructor()) |
| 295 | ReferenceTemporaryDtor = ClassDecl->getDestructor(); |
| 296 | } |
| 297 | |
| 298 | if (!ReferenceTemporaryDtor) |
| 299 | return; |
| 300 | |
| 301 | // Call the destructor for the temporary. |
| 302 | switch (M->getStorageDuration()) { |
| 303 | case SD_Static: |
| 304 | case SD_Thread: { |
| 305 | llvm::Constant *CleanupFn; |
| 306 | llvm::Constant *CleanupArg; |
| 307 | if (E->getType()->isArrayType()) { |
| 308 | CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 309 | ReferenceTemporary, E->getType(), |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 310 | CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions, |
| 311 | dyn_cast_or_null<VarDecl>(M->getExtendingDecl())); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 312 | CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
| 313 | } else { |
Rafael Espindola | 1ac0ec8 | 2014-09-11 15:42:06 +0000 | [diff] [blame] | 314 | CleanupFn = CGF.CGM.getAddrOfCXXStructor(ReferenceTemporaryDtor, |
| 315 | StructorType::Complete); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 316 | CleanupArg = cast<llvm::Constant>(ReferenceTemporary.getPointer()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 317 | } |
| 318 | CGF.CGM.getCXXABI().registerGlobalDtor( |
| 319 | CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg); |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | case SD_FullExpression: |
| 324 | CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(), |
| 325 | CodeGenFunction::destroyCXXObject, |
| 326 | CGF.getLangOpts().Exceptions); |
| 327 | break; |
| 328 | |
| 329 | case SD_Automatic: |
| 330 | CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup, |
| 331 | ReferenceTemporary, E->getType(), |
| 332 | CodeGenFunction::destroyCXXObject, |
| 333 | CGF.getLangOpts().Exceptions); |
| 334 | break; |
| 335 | |
| 336 | case SD_Dynamic: |
| 337 | llvm_unreachable("temporary cannot have dynamic storage duration"); |
| 338 | } |
| 339 | } |
| 340 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 341 | static Address |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 342 | createReferenceTemporary(CodeGenFunction &CGF, |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 343 | const MaterializeTemporaryExpr *M, const Expr *Inner) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 344 | switch (M->getStorageDuration()) { |
| 345 | case SD_FullExpression: |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 346 | case SD_Automatic: { |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 347 | // If we have a constant temporary array or record try to promote it into a |
| 348 | // constant global under the same rules a normal constant would've been |
| 349 | // promoted. This is easier on the optimizer and generally emits fewer |
| 350 | // instructions. |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 351 | QualType Ty = Inner->getType(); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 352 | if (CGF.CGM.getCodeGenOpts().MergeAllConstants && |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 353 | (Ty->isArrayType() || Ty->isRecordType()) && |
| 354 | CGF.CGM.isTypeConstant(Ty, true)) |
| 355 | if (llvm::Constant *Init = CGF.CGM.EmitConstantExpr(Inner, Ty, &CGF)) { |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 356 | auto *GV = new llvm::GlobalVariable( |
| 357 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
| 358 | llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 359 | CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty); |
| 360 | GV->setAlignment(alignment.getQuantity()); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 361 | // FIXME: Should we put the new global into a COMDAT? |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 362 | return Address(GV, alignment); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 363 | } |
Benjamin Kramer | f3e67de | 2015-04-09 22:50:07 +0000 | [diff] [blame] | 364 | return CGF.CreateMemTemp(Ty, "ref.tmp"); |
| 365 | } |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 366 | case SD_Thread: |
| 367 | case SD_Static: |
Hans Wennborg | f9d865b | 2015-03-17 16:38:58 +0000 | [diff] [blame] | 368 | return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 369 | |
| 370 | case SD_Dynamic: |
| 371 | llvm_unreachable("temporary can't have dynamic storage duration"); |
| 372 | } |
| 373 | llvm_unreachable("unknown storage duration"); |
| 374 | } |
| 375 | |
Saleem Abdulrasool | 8925dc0 | 2014-10-24 19:54:32 +0000 | [diff] [blame] | 376 | LValue CodeGenFunction:: |
| 377 | EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) { |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 378 | const Expr *E = M->GetTemporaryExpr(); |
Richard Smith | 7c5d4dc | 2013-06-11 02:41:00 +0000 | [diff] [blame] | 379 | |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 380 | // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so |
| 381 | // as that will cause the lifetime adjustment to be lost for ARC |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 382 | auto ownership = M->getType().getObjCLifetime(); |
| 383 | if (ownership != Qualifiers::OCL_None && |
| 384 | ownership != Qualifiers::OCL_ExplicitNone) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 385 | Address Object = createReferenceTemporary(*this, M, E); |
| 386 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) { |
| 387 | Object = Address(llvm::ConstantExpr::getBitCast(Var, |
| 388 | ConvertTypeForMem(E->getType()) |
| 389 | ->getPointerTo(Object.getAddressSpace())), |
| 390 | Object.getAlignment()); |
Akira Hatanaka | fdacb5c | 2016-05-13 01:21:23 +0000 | [diff] [blame] | 391 | |
| 392 | // createReferenceTemporary will promote the temporary to a global with a |
| 393 | // constant initializer if it can. It can only do this to a value of |
| 394 | // ARC-manageable type if the value is global and therefore "immune" to |
| 395 | // ref-counting operations. Therefore we have no need to emit either a |
| 396 | // dynamic initialization or a cleanup and we can just return the address |
| 397 | // of the temporary. |
| 398 | if (Var->hasInitializer()) |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 399 | return MakeAddrLValue(Object, M->getType(), |
| 400 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Akira Hatanaka | fdacb5c | 2016-05-13 01:21:23 +0000 | [diff] [blame] | 401 | |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 402 | Var->setInitializer(CGM.EmitNullConstant(E->getType())); |
| 403 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 404 | LValue RefTempDst = MakeAddrLValue(Object, M->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 405 | LValueBaseInfo(AlignmentSource::Decl, |
| 406 | false)); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 407 | |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 408 | switch (getEvaluationKind(E->getType())) { |
| 409 | default: llvm_unreachable("expected scalar or aggregate expression"); |
| 410 | case TEK_Scalar: |
| 411 | EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false); |
| 412 | break; |
| 413 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 414 | EmitAggExpr(E, AggValueSlot::forAddr(Object, |
Saleem Abdulrasool | b31b94a8 | 2014-10-24 20:23:43 +0000 | [diff] [blame] | 415 | E->getType().getQualifiers(), |
| 416 | AggValueSlot::IsDestructed, |
| 417 | AggValueSlot::DoesNotNeedGCBarriers, |
| 418 | AggValueSlot::IsNotAliased)); |
| 419 | break; |
| 420 | } |
| 421 | } |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 422 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 423 | pushTemporaryCleanup(*this, M, E, Object); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 424 | return RefTempDst; |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 427 | SmallVector<const Expr *, 2> CommaLHSs; |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 428 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 429 | E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
| 430 | |
Saleem Abdulrasool | 8925dc0 | 2014-10-24 19:54:32 +0000 | [diff] [blame] | 431 | for (const auto &Ignored : CommaLHSs) |
| 432 | EmitIgnoredExpr(Ignored); |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 433 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 434 | if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 435 | if (opaque->getType()->isRecordType()) { |
| 436 | assert(Adjustments.empty()); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 437 | return EmitOpaqueValueLValue(opaque); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 441 | // Create and initialize the reference temporary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 442 | Address Object = createReferenceTemporary(*this, M, E); |
| 443 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) { |
| 444 | Object = Address(llvm::ConstantExpr::getBitCast( |
| 445 | Var, ConvertTypeForMem(E->getType())->getPointerTo()), |
| 446 | Object.getAlignment()); |
Benjamin Kramer | f8b8696 | 2015-03-07 13:37:13 +0000 | [diff] [blame] | 447 | // If the temporary is a global and has a constant initializer or is a |
| 448 | // constant temporary that we promoted to a global, we may have already |
| 449 | // initialized it. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 450 | if (!Var->hasInitializer()) { |
| 451 | Var->setInitializer(CGM.EmitNullConstant(E->getType())); |
| 452 | EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); |
| 453 | } |
| 454 | } else { |
Tim Shen | 421119f | 2016-07-01 21:08:47 +0000 | [diff] [blame] | 455 | switch (M->getStorageDuration()) { |
| 456 | case SD_Automatic: |
| 457 | case SD_FullExpression: |
| 458 | if (auto *Size = EmitLifetimeStart( |
| 459 | CGM.getDataLayout().getTypeAllocSize(Object.getElementType()), |
| 460 | Object.getPointer())) { |
| 461 | if (M->getStorageDuration() == SD_Automatic) |
| 462 | pushCleanupAfterFullExpr<CallLifetimeEnd>(NormalEHLifetimeMarker, |
| 463 | Object, Size); |
| 464 | else |
| 465 | pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, Object, |
| 466 | Size); |
| 467 | } |
| 468 | break; |
| 469 | default: |
| 470 | break; |
| 471 | } |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 472 | EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); |
| 473 | } |
Nick Lewycky | 5d1159e | 2014-10-10 04:05:00 +0000 | [diff] [blame] | 474 | pushTemporaryCleanup(*this, M, E, Object); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 475 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 476 | // Perform derived-to-base casts and/or field accesses, to get from the |
| 477 | // temporary object we created (and, potentially, for which we extended |
| 478 | // the lifetime) to the subobject we're binding the reference to. |
| 479 | for (unsigned I = Adjustments.size(); I != 0; --I) { |
| 480 | SubobjectAdjustment &Adjustment = Adjustments[I-1]; |
| 481 | switch (Adjustment.Kind) { |
| 482 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 483 | Object = |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 484 | GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass, |
| 485 | Adjustment.DerivedToBase.BasePath->path_begin(), |
| 486 | Adjustment.DerivedToBase.BasePath->path_end(), |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 487 | /*NullCheckValue=*/ false, E->getExprLoc()); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 488 | break; |
Richard Smith | f3fabd2 | 2013-06-03 00:17:11 +0000 | [diff] [blame] | 489 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 490 | case SubobjectAdjustment::FieldAdjustment: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 491 | LValue LV = MakeAddrLValue(Object, E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 492 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 493 | LV = EmitLValueForField(LV, Adjustment.Field); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 494 | assert(LV.isSimple() && |
| 495 | "materialized temporary field is not a simple lvalue"); |
| 496 | Object = LV.getAddress(); |
| 497 | break; |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 500 | case SubobjectAdjustment::MemberPointerAdjustment: { |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 501 | llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 502 | Object = EmitCXXMemberDataPointerAddress(E, Object, Ptr, |
| 503 | Adjustment.Ptr.MPT); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | } |
Anders Carlsson | 7d4c083 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 507 | } |
Eli Friedman | c21cb44 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 508 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 509 | return MakeAddrLValue(Object, M->getType(), |
| 510 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | RValue |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 514 | CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) { |
| 515 | // Emit the expression as an lvalue. |
| 516 | LValue LV = EmitLValue(E); |
| 517 | assert(LV.isSimple()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 518 | llvm::Value *Value = LV.getPointer(); |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 519 | |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 520 | if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 521 | // C++11 [dcl.ref]p5 (as amended by core issue 453): |
| 522 | // If a glvalue to which a reference is directly bound designates neither |
| 523 | // an existing object or function of an appropriate type nor a region of |
| 524 | // storage of suitable size and alignment to contain an object of the |
| 525 | // reference's type, the behavior is undefined. |
| 526 | QualType Ty = E->getType(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 527 | EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 528 | } |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 529 | |
Anders Carlsson | 2969c8c6 | 2010-06-27 16:56:04 +0000 | [diff] [blame] | 530 | return RValue::get(Value); |
Anders Carlsson | 6f5a015 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 534 | /// getAccessedFieldNo - Given an encoded value and a result number, return the |
| 535 | /// input field number being accessed. |
| 536 | unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 537 | const llvm::Constant *Elts) { |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 538 | return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx)) |
| 539 | ->getZExtValue(); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 542 | /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h. |
| 543 | static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, |
| 544 | llvm::Value *High) { |
| 545 | llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL); |
| 546 | llvm::Value *K47 = Builder.getInt64(47); |
| 547 | llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul); |
| 548 | llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0); |
| 549 | llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul); |
| 550 | llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0); |
| 551 | return Builder.CreateMul(B1, KMul); |
| 552 | } |
| 553 | |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 554 | bool CodeGenFunction::sanitizePerformTypeCheck() const { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 555 | return SanOpts.has(SanitizerKind::Null) | |
| 556 | SanOpts.has(SanitizerKind::Alignment) | |
| 557 | SanOpts.has(SanitizerKind::ObjectSize) | |
| 558 | SanOpts.has(SanitizerKind::Vptr); |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 561 | void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 562 | llvm::Value *Ptr, QualType Ty, |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 563 | CharUnits Alignment, |
| 564 | SanitizerSet SkippedChecks) { |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 565 | if (!sanitizePerformTypeCheck()) |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 566 | return; |
| 567 | |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 568 | // Don't check pointers outside the default address space. The null check |
| 569 | // isn't correct, the object-size check isn't supported by LLVM, and we can't |
| 570 | // communicate the addresses to the runtime handler for the vptr check. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 571 | if (Ptr->getType()->getPointerAddressSpace()) |
Richard Smith | 2d8b294 | 2012-11-01 07:22:08 +0000 | [diff] [blame] | 572 | return; |
| 573 | |
Vedant Kumar | c420d14 | 2017-06-16 03:27:36 +0000 | [diff] [blame] | 574 | // Don't check pointers to volatile data. The behavior here is implementation- |
| 575 | // defined. |
| 576 | if (Ty.isVolatileQualified()) |
| 577 | return; |
| 578 | |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 579 | SanitizerScope SanScope(this); |
| 580 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 581 | SmallVector<std::pair<llvm::Value *, SanitizerMask>, 3> Checks; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 582 | llvm::BasicBlock *Done = nullptr; |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 583 | |
Vedant Kumar | e859ebb | 2017-04-26 02:17:21 +0000 | [diff] [blame] | 584 | // Quickly determine whether we have a pointer to an alloca. It's possible |
| 585 | // to skip null checks, and some alignment checks, for these pointers. This |
| 586 | // can reduce compile-time significantly. |
| 587 | auto PtrToAlloca = |
| 588 | dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCastsNoFollowAliases()); |
| 589 | |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 590 | bool AllowNullPointers = TCK == TCK_DowncastPointer || TCK == TCK_Upcast || |
| 591 | TCK == TCK_UpcastToVirtualBase; |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 592 | if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) && |
Vedant Kumar | e859ebb | 2017-04-26 02:17:21 +0000 | [diff] [blame] | 593 | !SkippedChecks.has(SanitizerKind::Null) && !PtrToAlloca) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 594 | // The glvalue must not be an empty glvalue. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 595 | llvm::Value *IsNonNull = Builder.CreateIsNotNull(Ptr); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 596 | |
Vedant Kumar | dbbdda4 | 2017-04-17 22:26:10 +0000 | [diff] [blame] | 597 | // The IR builder can constant-fold the null check if the pointer points to |
| 598 | // a constant. |
| 599 | bool PtrIsNonNull = |
| 600 | IsNonNull == llvm::ConstantInt::getTrue(getLLVMContext()); |
| 601 | |
| 602 | // Skip the null check if the pointer is known to be non-null. |
| 603 | if (!PtrIsNonNull) { |
| 604 | if (AllowNullPointers) { |
| 605 | // When performing pointer casts, it's OK if the value is null. |
| 606 | // Skip the remaining checks in that case. |
| 607 | Done = createBasicBlock("null"); |
| 608 | llvm::BasicBlock *Rest = createBasicBlock("not.null"); |
| 609 | Builder.CreateCondBr(IsNonNull, Rest, Done); |
| 610 | EmitBlock(Rest); |
| 611 | } else { |
| 612 | Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null)); |
| 613 | } |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 614 | } |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | bc3be65 | 2010-04-10 18:34:14 +0000 | [diff] [blame] | 616 | |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 617 | if (SanOpts.has(SanitizerKind::ObjectSize) && |
| 618 | !SkippedChecks.has(SanitizerKind::ObjectSize) && |
| 619 | !Ty->isIncompleteType()) { |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 620 | uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity(); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 621 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 622 | // The glvalue must refer to a large enough storage region. |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 623 | // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 624 | // to check this. |
Matt Arsenault | 2f15263 | 2013-10-07 19:00:18 +0000 | [diff] [blame] | 625 | // FIXME: Get object address space |
| 626 | llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy }; |
| 627 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 628 | llvm::Value *Min = Builder.getFalse(); |
George Burgess IV | a63f915 | 2017-03-21 20:09:35 +0000 | [diff] [blame] | 629 | llvm::Value *NullIsUnknown = Builder.getFalse(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | llvm::Value *CastAddr = Builder.CreateBitCast(Ptr, Int8PtrTy); |
George Burgess IV | a63f915 | 2017-03-21 20:09:35 +0000 | [diff] [blame] | 631 | llvm::Value *LargeEnough = Builder.CreateICmpUGE( |
| 632 | Builder.CreateCall(F, {CastAddr, Min, NullIsUnknown}), |
| 633 | llvm::ConstantInt::get(IntPtrTy, Size)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 634 | Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize)); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 635 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 636 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 637 | uint64_t AlignVal = 0; |
| 638 | |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 639 | if (SanOpts.has(SanitizerKind::Alignment) && |
| 640 | !SkippedChecks.has(SanitizerKind::Alignment)) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 641 | AlignVal = Alignment.getQuantity(); |
| 642 | if (!Ty->isIncompleteType() && !AlignVal) |
| 643 | AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 644 | |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 645 | // The glvalue must be suitably aligned. |
Vedant Kumar | e859ebb | 2017-04-26 02:17:21 +0000 | [diff] [blame] | 646 | if (AlignVal > 1 && |
| 647 | (!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) { |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 648 | llvm::Value *Align = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 649 | Builder.CreateAnd(Builder.CreatePtrToInt(Ptr, IntPtrTy), |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 650 | llvm::ConstantInt::get(IntPtrTy, AlignVal - 1)); |
| 651 | llvm::Value *Aligned = |
| 652 | Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 653 | Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment)); |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 654 | } |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 657 | if (Checks.size() > 0) { |
Filipe Cabecinhas | fe5e5af | 2017-01-06 14:40:12 +0000 | [diff] [blame] | 658 | // Make sure we're not losing information. Alignment needs to be a power of |
| 659 | // 2 |
| 660 | assert(!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 661 | llvm::Constant *StaticData[] = { |
Filipe Cabecinhas | fe5e5af | 2017-01-06 14:40:12 +0000 | [diff] [blame] | 662 | EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty), |
| 663 | llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1), |
| 664 | llvm::ConstantInt::get(Int8Ty, TCK)}; |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 665 | EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData, Ptr); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 666 | } |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 667 | |
Richard Smith | b1b0ab4 | 2012-11-05 22:21:05 +0000 | [diff] [blame] | 668 | // If possible, check that the vptr indicates that there is a subobject of |
| 669 | // type Ty at offset zero within this object. |
Richard Smith | be024a8 | 2012-12-18 00:22:45 +0000 | [diff] [blame] | 670 | // |
| 671 | // C++11 [basic.life]p5,6: |
| 672 | // [For storage which does not refer to an object within its lifetime] |
| 673 | // The program has undefined behavior if: |
| 674 | // -- 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] | 675 | // or call a non-static member function |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 676 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 677 | if (SanOpts.has(SanitizerKind::Vptr) && |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 678 | !SkippedChecks.has(SanitizerKind::Vptr) && |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 679 | (TCK == TCK_MemberAccess || TCK == TCK_MemberCall || |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 680 | TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference || |
| 681 | TCK == TCK_UpcastToVirtualBase) && |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 682 | RD && RD->hasDefinition() && RD->isDynamicClass()) { |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 683 | // Compute a hash of the mangled name of the type. |
| 684 | // |
| 685 | // FIXME: This is not guaranteed to be deterministic! Move to a |
| 686 | // fingerprinting mechanism once LLVM provides one. For the time |
| 687 | // being the implementation happens to be deterministic. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 688 | SmallString<64> MangledName; |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 689 | llvm::raw_svector_ostream Out(MangledName); |
| 690 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(), |
| 691 | Out); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 692 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 693 | // Blacklist based on the mangled type. |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 694 | if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType( |
| 695 | Out.str())) { |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 696 | llvm::hash_code TypeHash = hash_value(Out.str()); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 697 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 698 | // Load the vptr, and compute hash_16_bytes(TypeHash, vptr). |
| 699 | llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash); |
| 700 | llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 701 | Address VPtrAddr(Builder.CreateBitCast(Ptr, VPtrTy), getPointerAlign()); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 702 | llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr); |
| 703 | llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 704 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 705 | llvm::Value *Hash = emitHash16Bytes(Builder, Low, High); |
| 706 | Hash = Builder.CreateTrunc(Hash, IntPtrTy); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 707 | |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 708 | // Look the hash up in our cache. |
| 709 | const int CacheSize = 128; |
| 710 | llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize); |
| 711 | llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable, |
| 712 | "__ubsan_vptr_type_cache"); |
| 713 | llvm::Value *Slot = Builder.CreateAnd(Hash, |
| 714 | llvm::ConstantInt::get(IntPtrTy, |
| 715 | CacheSize-1)); |
| 716 | llvm::Value *Indices[] = { Builder.getInt32(0), Slot }; |
| 717 | llvm::Value *CacheVal = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 718 | Builder.CreateAlignedLoad(Builder.CreateInBoundsGEP(Cache, Indices), |
| 719 | getPointerAlign()); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 720 | |
| 721 | // If the hash isn't in the cache, call a runtime handler to perform the |
| 722 | // hard work of checking whether the vptr is for an object of the right |
| 723 | // type. This will either fill in the cache and return, or produce a |
| 724 | // diagnostic. |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 725 | llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 726 | llvm::Constant *StaticData[] = { |
| 727 | EmitCheckSourceLocation(Loc), |
| 728 | EmitCheckTypeDescriptor(Ty), |
| 729 | CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()), |
| 730 | llvm::ConstantInt::get(Int8Ty, TCK) |
| 731 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 732 | llvm::Value *DynamicData[] = { Ptr, Hash }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 733 | EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr), |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 734 | SanitizerHandler::DynamicTypeCacheMiss, StaticData, |
| 735 | DynamicData); |
Alexey Samsonov | 8485601 | 2014-07-10 22:34:19 +0000 | [diff] [blame] | 736 | } |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 737 | } |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 738 | |
| 739 | if (Done) { |
| 740 | Builder.CreateBr(Done); |
| 741 | EmitBlock(Done); |
| 742 | } |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 743 | } |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 744 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 745 | /// Determine whether this expression refers to a flexible array member in a |
| 746 | /// struct. We disable array bounds checks for such members. |
| 747 | static bool isFlexibleArrayMemberExpr(const Expr *E) { |
| 748 | // For compatibility with existing code, we treat arrays of length 0 or |
| 749 | // 1 as flexible array members. |
| 750 | const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 751 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 752 | if (CAT->getSize().ugt(1)) |
| 753 | return false; |
| 754 | } else if (!isa<IncompleteArrayType>(AT)) |
| 755 | return false; |
| 756 | |
| 757 | E = E->IgnoreParens(); |
| 758 | |
| 759 | // A flexible array member must be the last member in the class. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 760 | if (const auto *ME = dyn_cast<MemberExpr>(E)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 761 | // FIXME: If the base type of the member expr is not FD->getParent(), |
| 762 | // this should not be treated as a flexible array member access. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 763 | if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 764 | RecordDecl::field_iterator FI( |
| 765 | DeclContext::decl_iterator(const_cast<FieldDecl *>(FD))); |
| 766 | return ++FI == FD->getParent()->field_end(); |
| 767 | } |
Vedant Kumar | e356f1a | 2016-10-04 20:36:04 +0000 | [diff] [blame] | 768 | } else if (const auto *IRE = dyn_cast<ObjCIvarRefExpr>(E)) { |
| 769 | return IRE->getDecl()->getNextIvar() == nullptr; |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | /// If Base is known to point to the start of an array, return the length of |
| 776 | /// that array. Return 0 if the length cannot be determined. |
Benjamin Kramer | 36f89cc | 2013-03-09 15:15:22 +0000 | [diff] [blame] | 777 | static llvm::Value *getArrayIndexingBound( |
| 778 | CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 779 | // For the vector indexing extension, the bound is the number of elements. |
| 780 | if (const VectorType *VT = Base->getType()->getAs<VectorType>()) { |
| 781 | IndexedType = Base->getType(); |
| 782 | return CGF.Builder.getInt32(VT->getNumElements()); |
| 783 | } |
| 784 | |
| 785 | Base = Base->IgnoreParens(); |
| 786 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 787 | if (const auto *CE = dyn_cast<CastExpr>(Base)) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 788 | if (CE->getCastKind() == CK_ArrayToPointerDecay && |
| 789 | !isFlexibleArrayMemberExpr(CE->getSubExpr())) { |
| 790 | IndexedType = CE->getSubExpr()->getType(); |
| 791 | const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 792 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 793 | return CGF.Builder.getInt(CAT->getSize()); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 794 | else if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 795 | return CGF.getVLASize(VAT).first; |
| 796 | } |
| 797 | } |
| 798 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 799 | return nullptr; |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base, |
| 803 | llvm::Value *Index, QualType IndexType, |
| 804 | bool Accessed) { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 805 | assert(SanOpts.has(SanitizerKind::ArrayBounds) && |
Richard Smith | 6b53e22 | 2013-10-22 22:51:04 +0000 | [diff] [blame] | 806 | "should not be called unless adding bounds checks"); |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 807 | SanitizerScope SanScope(this); |
Richard Smith | 2847b22 | 2013-02-24 01:56:24 +0000 | [diff] [blame] | 808 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 809 | QualType IndexedType; |
| 810 | llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType); |
| 811 | if (!Bound) |
| 812 | return; |
| 813 | |
| 814 | bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType(); |
| 815 | llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned); |
| 816 | llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false); |
| 817 | |
| 818 | llvm::Constant *StaticData[] = { |
| 819 | EmitCheckSourceLocation(E->getExprLoc()), |
| 820 | EmitCheckTypeDescriptor(IndexedType), |
| 821 | EmitCheckTypeDescriptor(IndexType) |
| 822 | }; |
| 823 | llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal) |
| 824 | : Builder.CreateICmpULE(IndexVal, BoundVal); |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 825 | EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds), |
| 826 | SanitizerHandler::OutOfBounds, StaticData, Index); |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 830 | CodeGenFunction::ComplexPairTy CodeGenFunction:: |
| 831 | EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 832 | bool isInc, bool isPre) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 833 | ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 835 | llvm::Value *NextVal; |
| 836 | if (isa<llvm::IntegerType>(InVal.first->getType())) { |
| 837 | uint64_t AmountVal = isInc ? 1 : -1; |
| 838 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 840 | // Add the inc/dec to the real part. |
| 841 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 842 | } else { |
| 843 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
| 844 | llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); |
| 845 | if (!isInc) |
| 846 | FVal.changeSign(); |
| 847 | NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 849 | // Add the inc/dec to the real part. |
| 850 | NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 851 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 853 | ComplexPairTy IncVal(NextVal, InVal.second); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 855 | // Store the updated result through the lvalue. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 856 | EmitStoreOfComplex(IncVal, LV, /*init*/ false); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 116ce8f | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 858 | // If this is a postinc, return the value read from memory, otherwise use the |
| 859 | // updated value. |
| 860 | return isPre ? IncVal : InVal; |
| 861 | } |
| 862 | |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 863 | void CodeGenModule::EmitExplicitCastExprType(const ExplicitCastExpr *E, |
| 864 | CodeGenFunction *CGF) { |
| 865 | // Bind VLAs in the cast type. |
| 866 | if (CGF && E->getType()->isVariablyModifiedType()) |
| 867 | CGF->EmitVariablyModifiedType(E->getType()); |
| 868 | |
| 869 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 870 | DI->EmitExplicitCastType(E->getType()); |
| 871 | } |
| 872 | |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 873 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 874 | // LValue Expression Emission |
Chris Lattner | a45c5af | 2007-06-02 19:47:04 +0000 | [diff] [blame] | 875 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 876 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 877 | /// EmitPointerWithAlignment - Given an expression of pointer type, try to |
| 878 | /// derive a more accurate bound on the alignment of the pointer. |
| 879 | Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 880 | LValueBaseInfo *BaseInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 881 | // We allow this with ObjC object pointers because of fragile ABIs. |
| 882 | assert(E->getType()->isPointerType() || |
| 883 | E->getType()->isObjCObjectPointerType()); |
| 884 | E = E->IgnoreParens(); |
| 885 | |
| 886 | // Casts: |
| 887 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 888 | if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE)) |
| 889 | CGM.EmitExplicitCastExprType(ECE, this); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 890 | |
| 891 | switch (CE->getCastKind()) { |
| 892 | // Non-converting casts (but not C's implicit conversion from void*). |
| 893 | case CK_BitCast: |
| 894 | case CK_NoOp: |
| 895 | if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) { |
| 896 | if (PtrTy->getPointeeType()->isVoidType()) |
| 897 | break; |
| 898 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 899 | LValueBaseInfo InnerInfo; |
| 900 | Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), &InnerInfo); |
| 901 | if (BaseInfo) *BaseInfo = InnerInfo; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 902 | |
| 903 | // If this is an explicit bitcast, and the source l-value is |
| 904 | // opaque, honor the alignment of the casted-to type. |
| 905 | if (isa<ExplicitCastExpr>(CE) && |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 906 | InnerInfo.getAlignmentSource() != AlignmentSource::Decl) { |
| 907 | LValueBaseInfo ExpInfo; |
| 908 | CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), |
| 909 | &ExpInfo); |
| 910 | if (BaseInfo) |
| 911 | BaseInfo->mergeForCast(ExpInfo); |
| 912 | Addr = Address(Addr.getPointer(), Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Peter Collingbourne | 574975e | 2016-01-14 02:49:48 +0000 | [diff] [blame] | 915 | if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) && |
| 916 | CE->getCastKind() == CK_BitCast) { |
Peter Collingbourne | ee381ff | 2015-09-09 00:01:31 +0000 | [diff] [blame] | 917 | if (auto PT = E->getType()->getAs<PointerType>()) |
| 918 | EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(), |
| 919 | /*MayBeNull=*/true, |
| 920 | CodeGenFunction::CFITCK_UnrelatedCast, |
| 921 | CE->getLocStart()); |
| 922 | } |
| 923 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 924 | return Builder.CreateBitCast(Addr, ConvertType(E->getType())); |
| 925 | } |
| 926 | break; |
| 927 | |
| 928 | // Array-to-pointer decay. |
| 929 | case CK_ArrayToPointerDecay: |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 930 | return EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 931 | |
| 932 | // Derived-to-base conversions. |
| 933 | case CK_UncheckedDerivedToBase: |
| 934 | case CK_DerivedToBase: { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 935 | Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), BaseInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 936 | auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl(); |
| 937 | return GetAddressOfBaseClass(Addr, Derived, |
| 938 | CE->path_begin(), CE->path_end(), |
| 939 | ShouldNullCheckClassCastValue(CE), |
| 940 | CE->getExprLoc()); |
| 941 | } |
| 942 | |
| 943 | // TODO: Is there any reason to treat base-to-derived conversions |
| 944 | // specially? |
| 945 | default: |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Unary &. |
| 951 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 952 | if (UO->getOpcode() == UO_AddrOf) { |
| 953 | LValue LV = EmitLValue(UO->getSubExpr()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 954 | if (BaseInfo) *BaseInfo = LV.getBaseInfo(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 955 | return LV.getAddress(); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | // TODO: conditional operators, comma. |
| 960 | |
| 961 | // Otherwise, use the alignment of the type. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 962 | CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), BaseInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 963 | return Address(EmitScalarExpr(E), Align); |
| 964 | } |
| 965 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 966 | RValue CodeGenFunction::GetUndefRValue(QualType Ty) { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 967 | if (Ty->isVoidType()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 968 | return RValue::get(nullptr); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 969 | |
| 970 | switch (getEvaluationKind(Ty)) { |
| 971 | case TEK_Complex: { |
| 972 | llvm::Type *EltTy = |
| 973 | ConvertType(Ty->castAs<ComplexType>()->getElementType()); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 974 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 975 | return RValue::getComplex(std::make_pair(U, U)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 976 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 978 | // If this is a use of an undefined aggregate type, the aggregate must have an |
| 979 | // identifiable address. Just because the contents of the value are undefined |
| 980 | // doesn't mean that the address can't be taken and compared. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 981 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 982 | Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp"); |
Chris Lattner | 65526f0 | 2010-08-23 05:26:13 +0000 | [diff] [blame] | 983 | return RValue::getAggregate(DestPtr); |
Daniel Dunbar | 8429dbc | 2009-01-09 20:09:28 +0000 | [diff] [blame] | 984 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 985 | |
| 986 | case TEK_Scalar: |
| 987 | return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); |
| 988 | } |
| 989 | llvm_unreachable("bad evaluation kind"); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Daniel Dunbar | c79407f | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 992 | RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, |
| 993 | const char *Name) { |
| 994 | ErrorUnsupported(E, Name); |
| 995 | return GetUndefRValue(E->getType()); |
| 996 | } |
| 997 | |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 998 | LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, |
| 999 | const char *Name) { |
| 1000 | ErrorUnsupported(E, Name); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1001 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1002 | return MakeAddrLValue(Address(llvm::UndefValue::get(Ty), CharUnits::One()), |
| 1003 | E->getType()); |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 1006 | bool CodeGenFunction::IsWrappedCXXThis(const Expr *Obj) { |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 1007 | const Expr *Base = Obj; |
| 1008 | while (!isa<CXXThisExpr>(Base)) { |
| 1009 | // The result of a dynamic_cast can be null. |
| 1010 | if (isa<CXXDynamicCastExpr>(Base)) |
| 1011 | return false; |
| 1012 | |
| 1013 | if (const auto *CE = dyn_cast<CastExpr>(Base)) { |
| 1014 | Base = CE->getSubExpr(); |
| 1015 | } else if (const auto *PE = dyn_cast<ParenExpr>(Base)) { |
| 1016 | Base = PE->getSubExpr(); |
| 1017 | } else if (const auto *UO = dyn_cast<UnaryOperator>(Base)) { |
| 1018 | if (UO->getOpcode() == UO_Extension) |
| 1019 | Base = UO->getSubExpr(); |
| 1020 | else |
| 1021 | return false; |
| 1022 | } else { |
| 1023 | return false; |
| 1024 | } |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 1029 | LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) { |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 1030 | LValue LV; |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1031 | if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 1032 | LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true); |
| 1033 | else |
| 1034 | LV = EmitLValue(E); |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 1035 | if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) { |
| 1036 | SanitizerSet SkippedChecks; |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 1037 | if (const auto *ME = dyn_cast<MemberExpr>(E)) { |
| 1038 | bool IsBaseCXXThis = IsWrappedCXXThis(ME->getBase()); |
| 1039 | if (IsBaseCXXThis) |
| 1040 | SkippedChecks.set(SanitizerKind::Alignment, true); |
| 1041 | if (IsBaseCXXThis || isa<DeclRefExpr>(ME->getBase())) |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 1042 | SkippedChecks.set(SanitizerKind::Null, true); |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 1043 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1044 | EmitTypeCheck(TCK, E->getExprLoc(), LV.getPointer(), |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 1045 | E->getType(), LV.getAlignment(), SkippedChecks); |
| 1046 | } |
Mike Stump | 3f6f9fe | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1047 | return LV; |
| 1048 | } |
| 1049 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1050 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 1051 | /// of the expression. |
| 1052 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1053 | /// This can return one of two things: a simple address or a bitfield reference. |
| 1054 | /// In either case, the LLVM Value* in the LValue structure is guaranteed to be |
| 1055 | /// an LLVM pointer type. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1056 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1057 | /// If this returns a bitfield reference, nothing about the pointee type of the |
| 1058 | /// 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] | 1059 | /// |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1060 | /// If this returns a normal address, and if the lvalue's C type is fixed size, |
| 1061 | /// this method guarantees that the returned pointer type will point to an LLVM |
| 1062 | /// type of the same size of the lvalue's type. If the lvalue has a variable |
| 1063 | /// length type, this is not possible. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1064 | /// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1065 | LValue CodeGenFunction::EmitLValue(const Expr *E) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 1066 | ApplyDebugLocation DL(*this, E); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1067 | switch (E->getStmtClass()) { |
Daniel Dunbar | f2e6988 | 2008-08-25 20:45:57 +0000 | [diff] [blame] | 1068 | default: return EmitUnsupportedLValue(E, "l-value expression"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1069 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1070 | case Expr::ObjCPropertyRefExprClass: |
| 1071 | llvm_unreachable("cannot emit a property reference directly"); |
| 1072 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1073 | case Expr::ObjCSelectorExprClass: |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1074 | return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); |
Fariborz Jahanian | 531c16f | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 1075 | case Expr::ObjCIsaExprClass: |
| 1076 | return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1077 | case Expr::BinaryOperatorClass: |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 1078 | return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); |
David Majnemer | ce27e42 | 2015-02-14 01:48:17 +0000 | [diff] [blame] | 1079 | case Expr::CompoundAssignOperatorClass: { |
| 1080 | QualType Ty = E->getType(); |
| 1081 | if (const AtomicType *AT = Ty->getAs<AtomicType>()) |
| 1082 | Ty = AT->getValueType(); |
| 1083 | if (!Ty->isAnyComplexType()) |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1084 | return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
| 1085 | return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); |
David Majnemer | ce27e42 | 2015-02-14 01:48:17 +0000 | [diff] [blame] | 1086 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1087 | case Expr::CallExprClass: |
Anders Carlsson | c82555f | 2009-09-01 21:18:52 +0000 | [diff] [blame] | 1088 | case Expr::CXXMemberCallExprClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1089 | case Expr::CXXOperatorCallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1090 | case Expr::UserDefinedLiteralClass: |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1091 | return EmitCallExprLValue(cast<CallExpr>(E)); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 1092 | case Expr::VAArgExprClass: |
| 1093 | return EmitVAArgExprLValue(cast<VAArgExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1094 | case Expr::DeclRefExprClass: |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1095 | return EmitDeclRefLValue(cast<DeclRefExpr>(E)); |
Eric Christopher | d98e424 | 2011-09-08 17:15:04 +0000 | [diff] [blame] | 1096 | case Expr::ParenExprClass: |
| 1097 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1098 | case Expr::GenericSelectionExprClass: |
| 1099 | return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr()); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1100 | case Expr::PredefinedExprClass: |
| 1101 | return EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 1102 | case Expr::StringLiteralClass: |
| 1103 | return EmitStringLiteralLValue(cast<StringLiteral>(E)); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1104 | case Expr::ObjCEncodeExprClass: |
| 1105 | return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1106 | case Expr::PseudoObjectExprClass: |
| 1107 | return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 1108 | case Expr::InitListExprClass: |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 1109 | return EmitInitListLValue(cast<InitListExpr>(E)); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 1110 | case Expr::CXXTemporaryObjectExprClass: |
| 1111 | case Expr::CXXConstructExprClass: |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1112 | return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); |
| 1113 | case Expr::CXXBindTemporaryExprClass: |
| 1114 | return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1115 | case Expr::CXXUuidofExprClass: |
| 1116 | return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E)); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 1117 | case Expr::LambdaExprClass: |
| 1118 | return EmitLambdaLValue(cast<LambdaExpr>(E)); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1119 | |
| 1120 | case Expr::ExprWithCleanupsClass: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1121 | const auto *cleanups = cast<ExprWithCleanups>(E); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1122 | enterFullExpression(cleanups); |
| 1123 | RunCleanupsScope Scope(*this); |
Reid Kleckner | 092d065 | 2017-03-06 22:18:34 +0000 | [diff] [blame] | 1124 | LValue LV = EmitLValue(cleanups->getSubExpr()); |
| 1125 | if (LV.isSimple()) { |
| 1126 | // Defend against branches out of gnu statement expressions surrounded by |
| 1127 | // cleanups. |
| 1128 | llvm::Value *V = LV.getPointer(); |
| 1129 | Scope.ForceCleanup({&V}); |
| 1130 | return LValue::MakeAddr(Address(V, LV.getAlignment()), LV.getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1131 | getContext(), LV.getBaseInfo(), |
Reid Kleckner | 092d065 | 2017-03-06 22:18:34 +0000 | [diff] [blame] | 1132 | LV.getTBAAInfo()); |
| 1133 | } |
| 1134 | // FIXME: Is it possible to create an ExprWithCleanups that produces a |
| 1135 | // bitfield lvalue or some other non-simple lvalue? |
| 1136 | return LV; |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Anders Carlsson | 52ce3bb | 2009-11-14 01:51:50 +0000 | [diff] [blame] | 1139 | case Expr::CXXDefaultArgExprClass: |
| 1140 | return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1141 | case Expr::CXXDefaultInitExprClass: { |
| 1142 | CXXDefaultInitExprScope Scope(*this); |
| 1143 | return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr()); |
| 1144 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1145 | case Expr::CXXTypeidExprClass: |
| 1146 | return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 1147 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 1148 | case Expr::ObjCMessageExprClass: |
| 1149 | return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1150 | case Expr::ObjCIvarRefExprClass: |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1151 | return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 1152 | case Expr::StmtExprClass: |
| 1153 | return EmitStmtExprLValue(cast<StmtExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1154 | case Expr::UnaryOperatorClass: |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1155 | return EmitUnaryOpLValue(cast<UnaryOperator>(E)); |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 1156 | case Expr::ArraySubscriptExprClass: |
| 1157 | return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 1158 | case Expr::OMPArraySectionExprClass: |
| 1159 | return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E)); |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1160 | case Expr::ExtVectorElementExprClass: |
| 1161 | return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1162 | case Expr::MemberExprClass: |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1163 | return EmitMemberExpr(cast<MemberExpr>(E)); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 1164 | case Expr::CompoundLiteralExprClass: |
| 1165 | return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 1166 | case Expr::ConditionalOperatorClass: |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 1167 | return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1168 | case Expr::BinaryConditionalOperatorClass: |
| 1169 | return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E)); |
Chris Lattner | 053441f | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1170 | case Expr::ChooseExprClass: |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 1171 | return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr()); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 1172 | case Expr::OpaqueValueExprClass: |
| 1173 | return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E)); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1174 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 1175 | return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); |
Chris Lattner | 63d06ab | 2009-03-18 04:02:57 +0000 | [diff] [blame] | 1176 | case Expr::ImplicitCastExprClass: |
| 1177 | case Expr::CStyleCastExprClass: |
| 1178 | case Expr::CXXFunctionalCastExprClass: |
| 1179 | case Expr::CXXStaticCastExprClass: |
| 1180 | case Expr::CXXDynamicCastExprClass: |
| 1181 | case Expr::CXXReinterpretCastExprClass: |
| 1182 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1183 | case Expr::ObjCBridgedCastExprClass: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 1184 | return EmitCastLValue(cast<CastExpr>(E)); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1186 | case Expr::MaterializeTemporaryExprClass: |
| 1187 | return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E)); |
Eric Fiselier | cddaf87 | 2017-06-15 19:43:36 +0000 | [diff] [blame] | 1188 | |
| 1189 | case Expr::CoawaitExprClass: |
| 1190 | return EmitCoawaitLValue(cast<CoawaitExpr>(E)); |
| 1191 | case Expr::CoyieldExprClass: |
| 1192 | return EmitCoyieldLValue(cast<CoyieldExpr>(E)); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1196 | /// Given an object of the given canonical type, can we safely copy a |
| 1197 | /// value out of it based on its initializer? |
| 1198 | static bool isConstantEmittableObjectType(QualType type) { |
| 1199 | assert(type.isCanonical()); |
| 1200 | assert(!type->isReferenceType()); |
| 1201 | |
| 1202 | // Must be const-qualified but non-volatile. |
| 1203 | Qualifiers qs = type.getLocalQualifiers(); |
| 1204 | if (!qs.hasConst() || qs.hasVolatile()) return false; |
| 1205 | |
| 1206 | // Otherwise, all object types satisfy this except C++ classes with |
| 1207 | // mutable subobjects or non-trivial copy/destroy behavior. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1208 | if (const auto *RT = dyn_cast<RecordType>(type)) |
| 1209 | if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1210 | if (RD->hasMutableFields() || !RD->isTrivial()) |
| 1211 | return false; |
| 1212 | |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | /// Can we constant-emit a load of a reference to a variable of the |
| 1217 | /// given type? This is different from predicates like |
| 1218 | /// Decl::isUsableInConstantExpressions because we do want it to apply |
| 1219 | /// in situations that don't necessarily satisfy the language's rules |
| 1220 | /// for this (e.g. C++'s ODR-use rules). For example, we want to able |
| 1221 | /// to do this with const float variables even if those variables |
| 1222 | /// aren't marked 'constexpr'. |
| 1223 | enum ConstantEmissionKind { |
| 1224 | CEK_None, |
| 1225 | CEK_AsReferenceOnly, |
| 1226 | CEK_AsValueOrReference, |
| 1227 | CEK_AsValueOnly |
| 1228 | }; |
| 1229 | static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) { |
| 1230 | type = type.getCanonicalType(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1231 | if (const auto *ref = dyn_cast<ReferenceType>(type)) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1232 | if (isConstantEmittableObjectType(ref->getPointeeType())) |
| 1233 | return CEK_AsValueOrReference; |
| 1234 | return CEK_AsReferenceOnly; |
| 1235 | } |
| 1236 | if (isConstantEmittableObjectType(type)) |
| 1237 | return CEK_AsValueOnly; |
| 1238 | return CEK_None; |
| 1239 | } |
| 1240 | |
| 1241 | /// Try to emit a reference to the given value without producing it as |
| 1242 | /// an l-value. This is actually more than an optimization: we can't |
| 1243 | /// produce an l-value for variables that we never actually captured |
| 1244 | /// in a block or lambda, which means const int variables or constexpr |
| 1245 | /// literals or similar. |
| 1246 | CodeGenFunction::ConstantEmission |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1247 | CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { |
| 1248 | ValueDecl *value = refExpr->getDecl(); |
| 1249 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1250 | // The value needs to be an enum constant or a constant variable. |
| 1251 | ConstantEmissionKind CEK; |
| 1252 | if (isa<ParmVarDecl>(value)) { |
| 1253 | CEK = CEK_None; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1254 | } else if (auto *var = dyn_cast<VarDecl>(value)) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1255 | CEK = checkVarTypeForConstantEmission(var->getType()); |
| 1256 | } else if (isa<EnumConstantDecl>(value)) { |
| 1257 | CEK = CEK_AsValueOnly; |
| 1258 | } else { |
| 1259 | CEK = CEK_None; |
| 1260 | } |
| 1261 | if (CEK == CEK_None) return ConstantEmission(); |
| 1262 | |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1263 | Expr::EvalResult result; |
| 1264 | bool resultIsReference; |
| 1265 | QualType resultType; |
| 1266 | |
| 1267 | // It's best to evaluate all the way as an r-value if that's permitted. |
| 1268 | if (CEK != CEK_AsReferenceOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1269 | refExpr->EvaluateAsRValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1270 | resultIsReference = false; |
| 1271 | resultType = refExpr->getType(); |
| 1272 | |
| 1273 | // Otherwise, try to evaluate as an l-value. |
| 1274 | } else if (CEK != CEK_AsValueOnly && |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1275 | refExpr->EvaluateAsLValue(result, getContext())) { |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1276 | resultIsReference = true; |
| 1277 | resultType = value->getType(); |
| 1278 | |
| 1279 | // Failure. |
| 1280 | } else { |
| 1281 | return ConstantEmission(); |
| 1282 | } |
| 1283 | |
| 1284 | // In any case, if the initializer has side-effects, abandon ship. |
| 1285 | if (result.HasSideEffects) |
| 1286 | return ConstantEmission(); |
| 1287 | |
| 1288 | // Emit as a constant. |
| 1289 | llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this); |
| 1290 | |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 1291 | // Make sure we emit a debug reference to the global variable. |
| 1292 | // This should probably fire even for |
| 1293 | if (isa<VarDecl>(value)) { |
| 1294 | if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value))) |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 1295 | EmitDeclRefExprDbgValue(refExpr, result.Val); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 1296 | } else { |
| 1297 | assert(isa<EnumConstantDecl>(value)); |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 1298 | EmitDeclRefExprDbgValue(refExpr, result.Val); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 1299 | } |
John McCall | 7133505 | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 1300 | |
| 1301 | // If we emitted a reference constant, we need to dereference that. |
| 1302 | if (resultIsReference) |
| 1303 | return ConstantEmission::forReference(C); |
| 1304 | |
| 1305 | return ConstantEmission::forValue(C); |
| 1306 | } |
| 1307 | |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1308 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, |
| 1309 | SourceLocation Loc) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1310 | return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1311 | lvalue.getType(), Loc, lvalue.getBaseInfo(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1312 | lvalue.getTBAAInfo(), |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1313 | lvalue.getTBAABaseType(), lvalue.getTBAAOffset(), |
| 1314 | lvalue.isNontemporal()); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1317 | static bool hasBooleanRepresentation(QualType Ty) { |
| 1318 | if (Ty->isBooleanType()) |
| 1319 | return true; |
| 1320 | |
| 1321 | if (const EnumType *ET = Ty->getAs<EnumType>()) |
| 1322 | return ET->getDecl()->getIntegerType()->isBooleanType(); |
| 1323 | |
Douglas Gregor | 298f43d | 2012-04-12 20:42:30 +0000 | [diff] [blame] | 1324 | if (const AtomicType *AT = Ty->getAs<AtomicType>()) |
| 1325 | return hasBooleanRepresentation(AT->getValueType()); |
| 1326 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1327 | return false; |
| 1328 | } |
| 1329 | |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1330 | static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, |
| 1331 | llvm::APInt &Min, llvm::APInt &End, |
Vedant Kumar | 4593a46 | 2016-12-09 23:48:18 +0000 | [diff] [blame] | 1332 | bool StrictEnums, bool IsBool) { |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1333 | const EnumType *ET = Ty->getAs<EnumType>(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1334 | bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums && |
| 1335 | ET && !ET->getDecl()->isFixed(); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1336 | if (!IsBool && !IsRegularCPlusPlusEnum) |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1337 | return false; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1338 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1339 | if (IsBool) { |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1340 | Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0); |
| 1341 | End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1342 | } else { |
| 1343 | const EnumDecl *ED = ET->getDecl(); |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1344 | llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType()); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1345 | unsigned Bitwidth = LTy->getScalarSizeInBits(); |
| 1346 | unsigned NumNegativeBits = ED->getNumNegativeBits(); |
| 1347 | unsigned NumPositiveBits = ED->getNumPositiveBits(); |
| 1348 | |
| 1349 | if (NumNegativeBits) { |
| 1350 | unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); |
| 1351 | assert(NumBits <= Bitwidth); |
| 1352 | End = llvm::APInt(Bitwidth, 1) << (NumBits - 1); |
| 1353 | Min = -End; |
| 1354 | } else { |
| 1355 | assert(NumPositiveBits <= Bitwidth); |
| 1356 | End = llvm::APInt(Bitwidth, 1) << NumPositiveBits; |
| 1357 | Min = llvm::APInt(Bitwidth, 0); |
| 1358 | } |
| 1359 | } |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1360 | return true; |
| 1361 | } |
| 1362 | |
| 1363 | llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { |
| 1364 | llvm::APInt Min, End; |
Vedant Kumar | 4593a46 | 2016-12-09 23:48:18 +0000 | [diff] [blame] | 1365 | if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums, |
| 1366 | hasBooleanRepresentation(Ty))) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1367 | return nullptr; |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1368 | |
Duncan Sands | c720e78 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 1369 | llvm::MDBuilder MDHelper(getLLVMContext()); |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 1370 | return MDHelper.createRange(Min, End); |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Vedant Kumar | 5a97265 | 2017-02-27 19:46:19 +0000 | [diff] [blame] | 1373 | bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, |
| 1374 | SourceLocation Loc) { |
| 1375 | bool HasBoolCheck = SanOpts.has(SanitizerKind::Bool); |
| 1376 | bool HasEnumCheck = SanOpts.has(SanitizerKind::Enum); |
| 1377 | if (!HasBoolCheck && !HasEnumCheck) |
| 1378 | return false; |
| 1379 | |
| 1380 | bool IsBool = hasBooleanRepresentation(Ty) || |
| 1381 | NSAPI(CGM.getContext()).isObjCBOOLType(Ty); |
| 1382 | bool NeedsBoolCheck = HasBoolCheck && IsBool; |
| 1383 | bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>(); |
| 1384 | if (!NeedsBoolCheck && !NeedsEnumCheck) |
| 1385 | return false; |
| 1386 | |
Vedant Kumar | 129edab | 2017-03-09 16:06:27 +0000 | [diff] [blame] | 1387 | // Single-bit booleans don't need to be checked. Special-case this to avoid |
| 1388 | // a bit width mismatch when handling bitfield values. This is handled by |
| 1389 | // EmitFromMemory for the non-bitfield case. |
| 1390 | if (IsBool && |
| 1391 | cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1) |
| 1392 | return false; |
| 1393 | |
Vedant Kumar | 5a97265 | 2017-02-27 19:46:19 +0000 | [diff] [blame] | 1394 | llvm::APInt Min, End; |
| 1395 | if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool)) |
| 1396 | return true; |
| 1397 | |
| 1398 | SanitizerScope SanScope(this); |
| 1399 | llvm::Value *Check; |
| 1400 | --End; |
| 1401 | if (!Min) { |
| 1402 | Check = Builder.CreateICmpULE( |
| 1403 | Value, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1404 | } else { |
| 1405 | llvm::Value *Upper = Builder.CreateICmpSLE( |
| 1406 | Value, llvm::ConstantInt::get(getLLVMContext(), End)); |
| 1407 | llvm::Value *Lower = Builder.CreateICmpSGE( |
| 1408 | Value, llvm::ConstantInt::get(getLLVMContext(), Min)); |
| 1409 | Check = Builder.CreateAnd(Upper, Lower); |
| 1410 | } |
| 1411 | llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc), |
| 1412 | EmitCheckTypeDescriptor(Ty)}; |
| 1413 | SanitizerMask Kind = |
| 1414 | NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool; |
| 1415 | EmitCheck(std::make_pair(Check, Kind), SanitizerHandler::LoadInvalidValue, |
| 1416 | StaticArgs, EmitCheckValue(Value)); |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1420 | llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, |
| 1421 | QualType Ty, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1422 | SourceLocation Loc, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1423 | LValueBaseInfo BaseInfo, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1424 | llvm::MDNode *TBAAInfo, |
| 1425 | QualType TBAABaseType, |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1426 | uint64_t TBAAOffset, |
| 1427 | bool isNontemporal) { |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1428 | if (!CGM.getCodeGenOpts().PreserveVec3Type) { |
| 1429 | // For better performance, handle vector loads differently. |
| 1430 | if (Ty->isVectorType()) { |
| 1431 | const llvm::Type *EltTy = Addr.getElementType(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1432 | |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1433 | const auto *VTy = cast<llvm::VectorType>(EltTy); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1434 | |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1435 | // Handle vectors of size 3 like size 4 for better performance. |
| 1436 | if (VTy->getNumElements() == 3) { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1437 | |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1438 | // Bitcast to vec4 type. |
| 1439 | llvm::VectorType *vec4Ty = |
| 1440 | llvm::VectorType::get(VTy->getElementType(), 4); |
| 1441 | Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4"); |
| 1442 | // Now load value. |
| 1443 | llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4"); |
Richard Smith | f0480fc | 2012-12-13 05:41:48 +0000 | [diff] [blame] | 1444 | |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1445 | // Shuffle vector to get vec3. |
| 1446 | V = Builder.CreateShuffleVector(V, llvm::UndefValue::get(vec4Ty), |
| 1447 | {0, 1, 2}, "extractVec"); |
| 1448 | return EmitFromMemory(V, Ty); |
| 1449 | } |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Atomic operations have to be done on integral types. |
David Majnemer | a38c9f1 | 2016-05-24 16:09:25 +0000 | [diff] [blame] | 1454 | LValue AtomicLValue = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1455 | LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo); |
David Majnemer | a38c9f1 | 2016-05-24 16:09:25 +0000 | [diff] [blame] | 1456 | if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) { |
| 1457 | return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal(); |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1458 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1460 | llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile); |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1461 | if (isNontemporal) { |
| 1462 | llvm::MDNode *Node = llvm::MDNode::get( |
| 1463 | Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1))); |
| 1464 | Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); |
| 1465 | } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1466 | if (TBAAInfo) { |
Krzysztof Parzyszek | 5960a57 | 2017-05-25 12:55:47 +0000 | [diff] [blame] | 1467 | bool MayAlias = BaseInfo.getMayAlias(); |
| 1468 | llvm::MDNode *TBAA = MayAlias |
| 1469 | ? CGM.getTBAAInfo(getContext().CharTy) |
| 1470 | : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, TBAAOffset); |
| 1471 | if (TBAA) |
| 1472 | CGM.DecorateInstructionWithTBAA(Load, TBAA, MayAlias); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1473 | } |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1474 | |
Vedant Kumar | 5a97265 | 2017-02-27 19:46:19 +0000 | [diff] [blame] | 1475 | if (EmitScalarRangeCheck(Load, Ty, Loc)) { |
| 1476 | // In order to prevent the optimizer from throwing away the check, don't |
| 1477 | // attach range metadata to the load. |
Richard Smith | 1629da9 | 2012-12-13 07:11:50 +0000 | [diff] [blame] | 1478 | } else if (CGM.getCodeGenOpts().OptimizationLevel > 0) |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1479 | if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) |
| 1480 | Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1481 | |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1482 | return EmitFromMemory(Load, Ty); |
NAKAMURA Takumi | 2681efc | 2012-03-24 14:43:42 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1485 | llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { |
| 1486 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1487 | if (hasBooleanRepresentation(Ty)) { |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1488 | // This should really always be an i1, but sometimes it's already |
| 1489 | // an i8, and it's awkward to track those cases down. |
| 1490 | if (Value->getType()->isIntegerTy(1)) |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1491 | return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool"); |
| 1492 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1493 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | return Value; |
| 1497 | } |
| 1498 | |
| 1499 | llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { |
| 1500 | // Bool has a different representation in memory than in registers. |
Rafael Espindola | 5c0034a | 2012-03-24 16:50:34 +0000 | [diff] [blame] | 1501 | if (hasBooleanRepresentation(Ty)) { |
Eli Friedman | b369f44 | 2012-11-13 02:05:15 +0000 | [diff] [blame] | 1502 | assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && |
| 1503 | "wrong value rep of bool"); |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1504 | return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); |
| 1505 | } |
| 1506 | |
| 1507 | return Value; |
| 1508 | } |
| 1509 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1510 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, |
| 1511 | bool Volatile, QualType Ty, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1512 | LValueBaseInfo BaseInfo, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1513 | llvm::MDNode *TBAAInfo, |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1514 | bool isInit, QualType TBAABaseType, |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1515 | uint64_t TBAAOffset, |
| 1516 | bool isNontemporal) { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1517 | |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1518 | if (!CGM.getCodeGenOpts().PreserveVec3Type) { |
| 1519 | // Handle vectors differently to get better performance. |
| 1520 | if (Ty->isVectorType()) { |
| 1521 | llvm::Type *SrcTy = Value->getType(); |
Simon Pilgrim | a5dbbc6 | 2017-06-01 20:13:34 +0000 | [diff] [blame] | 1522 | auto *VecTy = dyn_cast<llvm::VectorType>(SrcTy); |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1523 | // Handle vec3 special. |
Simon Pilgrim | a5dbbc6 | 2017-06-01 20:13:34 +0000 | [diff] [blame] | 1524 | if (VecTy && VecTy->getNumElements() == 3) { |
Jin-Gu Kang | e7cdcde | 2017-04-04 16:40:25 +0000 | [diff] [blame] | 1525 | // Our source is a vec3, do a shuffle vector to make it a vec4. |
| 1526 | llvm::Constant *Mask[] = {Builder.getInt32(0), Builder.getInt32(1), |
| 1527 | Builder.getInt32(2), |
| 1528 | llvm::UndefValue::get(Builder.getInt32Ty())}; |
| 1529 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1530 | Value = Builder.CreateShuffleVector(Value, llvm::UndefValue::get(VecTy), |
| 1531 | MaskV, "extractVec"); |
| 1532 | SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4); |
| 1533 | } |
| 1534 | if (Addr.getElementType() != SrcTy) { |
| 1535 | Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp"); |
| 1536 | } |
Tanya Lattner | a9dd49f | 2012-08-16 00:10:13 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1539 | |
John McCall | 3a7f692 | 2010-10-27 20:58:56 +0000 | [diff] [blame] | 1540 | Value = EmitToMemory(Value, Ty); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1541 | |
David Majnemer | a38c9f1 | 2016-05-24 16:09:25 +0000 | [diff] [blame] | 1542 | LValue AtomicLValue = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1543 | LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 1544 | if (Ty->isAtomicType() || |
David Majnemer | a38c9f1 | 2016-05-24 16:09:25 +0000 | [diff] [blame] | 1545 | (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) { |
| 1546 | EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit); |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1550 | llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1551 | if (isNontemporal) { |
| 1552 | llvm::MDNode *Node = |
| 1553 | llvm::MDNode::get(Store->getContext(), |
| 1554 | llvm::ConstantAsMetadata::get(Builder.getInt32(1))); |
| 1555 | Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); |
| 1556 | } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1557 | if (TBAAInfo) { |
Krzysztof Parzyszek | 5960a57 | 2017-05-25 12:55:47 +0000 | [diff] [blame] | 1558 | bool MayAlias = BaseInfo.getMayAlias(); |
| 1559 | llvm::MDNode *TBAA = MayAlias |
| 1560 | ? CGM.getTBAAInfo(getContext().CharTy) |
| 1561 | : CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo, TBAAOffset); |
| 1562 | if (TBAA) |
| 1563 | CGM.DecorateInstructionWithTBAA(Store, TBAA, MayAlias); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1564 | } |
Daniel Dunbar | 1d42546 | 2009-02-10 00:57:50 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1567 | void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1568 | bool isInit) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1569 | EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1570 | lvalue.getType(), lvalue.getBaseInfo(), |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 1571 | lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(), |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 1572 | lvalue.getTBAAOffset(), lvalue.isNontemporal()); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1575 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this |
| 1576 | /// method emits the address of the lvalue, then loads the result as an rvalue, |
| 1577 | /// returning the rvalue. |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1578 | RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) { |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1579 | if (LV.isObjCWeak()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1580 | // load of a __weak object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1581 | Address AddrWeakObj = LV.getAddress(); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1582 | return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, |
| 1583 | AddrWeakObj)); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1584 | } |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 1585 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1586 | // In MRC mode, we do a load+autorelease. |
| 1587 | if (!getLangOpts().ObjCAutoRefCount) { |
| 1588 | return RValue::get(EmitARCLoadWeak(LV.getAddress())); |
| 1589 | } |
| 1590 | |
| 1591 | // In ARC mode, we load retained and then consume the value. |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 1592 | llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress()); |
| 1593 | Object = EmitObjCConsumeObject(LV.getType(), Object); |
| 1594 | return RValue::get(Object); |
| 1595 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1597 | if (LV.isSimple()) { |
John McCall | d68b2d0 | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 1598 | assert(!LV.getType()->isFunctionType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1599 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1600 | // Everything needs a load. |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1601 | return RValue::get(EmitLoadOfScalar(LV, Loc)); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1602 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1603 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1604 | if (LV.isVectorElt()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1605 | llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(), |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1606 | LV.isVolatileQualified()); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 1607 | return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(), |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 1608 | "vecext")); |
| 1609 | } |
Chris Lattner | 73ab9b3 | 2007-08-03 00:16:29 +0000 | [diff] [blame] | 1610 | |
| 1611 | // If this is a reference to a subset of the elements of a vector, either |
| 1612 | // shuffle the input or extract/insert them as appropriate. |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1613 | if (LV.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1614 | return EmitLoadOfExtVectorElementLValue(LV); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1615 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1616 | // Global Register variables always invoke intrinsics |
| 1617 | if (LV.isGlobalReg()) |
| 1618 | return EmitLoadOfGlobalRegLValue(LV); |
| 1619 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1620 | assert(LV.isBitField() && "Unknown LValue type!"); |
Vedant Kumar | 129edab | 2017-03-09 16:06:27 +0000 | [diff] [blame] | 1621 | return EmitLoadOfBitfieldLValue(LV, Loc); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Vedant Kumar | 129edab | 2017-03-09 16:06:27 +0000 | [diff] [blame] | 1624 | RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, |
| 1625 | SourceLocation Loc) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1626 | const CGBitFieldInfo &Info = LV.getBitFieldInfo(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | 3447a02 | 2010-04-13 23:34:15 +0000 | [diff] [blame] | 1628 | // Get the output type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1629 | llvm::Type *ResLTy = ConvertType(LV.getType()); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1630 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1631 | Address Ptr = LV.getBitFieldAddress(); |
| 1632 | llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1633 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1634 | if (Info.IsSigned) { |
David Greene | c5ff624 | 2013-01-15 23:13:47 +0000 | [diff] [blame] | 1635 | assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1636 | unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size; |
| 1637 | if (HighBits) |
| 1638 | Val = Builder.CreateShl(Val, HighBits, "bf.shl"); |
| 1639 | if (Info.Offset + HighBits) |
| 1640 | Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr"); |
| 1641 | } else { |
| 1642 | if (Info.Offset) |
| 1643 | Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr"); |
Eli Bendersky | 03b913d | 2012-12-18 22:22:16 +0000 | [diff] [blame] | 1644 | if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize) |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1645 | Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1646 | Info.Size), |
| 1647 | "bf.clear"); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1648 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1649 | Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast"); |
Vedant Kumar | 129edab | 2017-03-09 16:06:27 +0000 | [diff] [blame] | 1650 | EmitScalarRangeCheck(Val, LV.getType(), Loc); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1651 | return RValue::get(Val); |
Lauro Ramos Venancio | 2ddcb25a3 | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1654 | // If this is a reference to a subset of the elements of a vector, create an |
| 1655 | // appropriate shufflevector. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1656 | RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1657 | llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(), |
| 1658 | LV.isVolatileQualified()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1659 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1660 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1661 | |
| 1662 | // If the result of the expression is a non-vector type, we must be extracting |
| 1663 | // a single element. Just codegen as an extractelement. |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1664 | const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1665 | if (!ExprVT) { |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 1666 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Michael J. Spencer | dd59775 | 2014-05-31 00:22:12 +0000 | [diff] [blame] | 1667 | llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1668 | return RValue::get(Builder.CreateExtractElement(Vec, Elt)); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1669 | } |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Always use shuffle vector to try to retain the original program structure |
Chris Lattner | 8eab8ff | 2007-08-10 17:10:08 +0000 | [diff] [blame] | 1672 | unsigned NumResultElts = ExprVT->getNumElements(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1674 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1675 | for (unsigned i = 0; i != NumResultElts; ++i) |
| 1676 | Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1678 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
| 1679 | Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1680 | MaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1681 | return RValue::get(Vec); |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1684 | /// @brief Generates lvalue for partial ext_vector access. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1685 | Address CodeGenFunction::EmitExtVectorElementLValue(LValue LV) { |
| 1686 | Address VectorAddress = LV.getExtVectorAddress(); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1687 | const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); |
| 1688 | QualType EQT = ExprVT->getElementType(); |
| 1689 | llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1690 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1691 | Address CastToPointerElement = |
| 1692 | Builder.CreateElementBitCast(VectorAddress, VectorElementTy, |
| 1693 | "conv.ptr.element"); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1694 | |
| 1695 | const llvm::Constant *Elts = LV.getExtVectorElts(); |
| 1696 | unsigned ix = getAccessedFieldNo(0, Elts); |
| 1697 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1698 | Address VectorBasePtrPlusIx = |
| 1699 | Builder.CreateConstInBoundsGEP(CastToPointerElement, ix, |
| 1700 | getContext().getTypeSizeInChars(EQT), |
| 1701 | "vector.elt"); |
| 1702 | |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 1703 | return VectorBasePtrPlusIx; |
| 1704 | } |
| 1705 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1706 | /// @brief Load of global gamed gegisters are always calls to intrinsics. |
| 1707 | RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) { |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1708 | assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) && |
| 1709 | "Bad type for register variable"); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1710 | llvm::MDNode *RegName = cast<llvm::MDNode>( |
| 1711 | cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata()); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1712 | |
| 1713 | // We accept integer and pointer types only |
| 1714 | llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType()); |
| 1715 | llvm::Type *Ty = OrigTy; |
| 1716 | if (OrigTy->isPointerTy()) |
| 1717 | Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); |
| 1718 | llvm::Type *Types[] = { Ty }; |
| 1719 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1720 | 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] | 1721 | llvm::Value *Call = Builder.CreateCall( |
| 1722 | F, llvm::MetadataAsValue::get(Ty->getContext(), RegName)); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1723 | if (OrigTy->isPointerTy()) |
| 1724 | Call = Builder.CreateIntToPtr(Call, OrigTy); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1725 | return RValue::get(Call); |
| 1726 | } |
Chris Lattner | 40ff701 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 9369a56 | 2007-06-29 16:31:29 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1729 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 1730 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 1731 | /// is 'Ty'. |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1732 | void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1733 | bool isInit) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1734 | if (!Dst.isSimple()) { |
| 1735 | if (Dst.isVectorElt()) { |
| 1736 | // Read/modify/write the vector, inserting the new element. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1737 | llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(), |
| 1738 | Dst.isVolatileQualified()); |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1739 | Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1740 | Dst.getVectorIdx(), "vecins"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1741 | Builder.CreateStore(Vec, Dst.getVectorAddress(), |
| 1742 | Dst.isVolatileQualified()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1743 | return; |
| 1744 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1745 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1746 | // If this is an update of extended vector elements, insert them as |
| 1747 | // appropriate. |
| 1748 | if (Dst.isExtVectorElt()) |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1749 | return EmitStoreThroughExtVectorComponentLValue(Src, Dst); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1750 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1751 | if (Dst.isGlobalReg()) |
| 1752 | return EmitStoreThroughGlobalRegLValue(Src, Dst); |
| 1753 | |
John McCall | c109a25 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 1754 | assert(Dst.isBitField() && "Unknown LValue type"); |
| 1755 | return EmitStoreThroughBitfieldLValue(Src, Dst); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1756 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1757 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1758 | // There's special magic for assigning into an ARC-qualified l-value. |
| 1759 | if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) { |
| 1760 | switch (Lifetime) { |
| 1761 | case Qualifiers::OCL_None: |
| 1762 | llvm_unreachable("present but none"); |
| 1763 | |
| 1764 | case Qualifiers::OCL_ExplicitNone: |
| 1765 | // nothing special |
| 1766 | break; |
| 1767 | |
| 1768 | case Qualifiers::OCL_Strong: |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 1769 | if (isInit) { |
| 1770 | Src = RValue::get(EmitARCRetain(Dst.getType(), Src.getScalarVal())); |
| 1771 | break; |
| 1772 | } |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1773 | EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1774 | return; |
| 1775 | |
| 1776 | case Qualifiers::OCL_Weak: |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 1777 | if (isInit) |
| 1778 | // Initialize and then skip the primitive store. |
| 1779 | EmitARCInitWeak(Dst.getAddress(), Src.getScalarVal()); |
| 1780 | else |
| 1781 | EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1782 | return; |
| 1783 | |
| 1784 | case Qualifiers::OCL_Autoreleasing: |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1785 | Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(), |
| 1786 | Src.getScalarVal())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1787 | // fall into the normal path |
| 1788 | break; |
| 1789 | } |
| 1790 | } |
| 1791 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1792 | if (Dst.isObjCWeak() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1793 | // load of a __weak object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1794 | Address LvalueDst = Dst.getAddress(); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1795 | llvm::Value *src = Src.getScalarVal(); |
Mike Stump | ca5ae66 | 2009-04-14 00:57:29 +0000 | [diff] [blame] | 1796 | CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1797 | return; |
| 1798 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1799 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 1800 | if (Dst.isObjCStrong() && !Dst.isNonGC()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1801 | // load of a __strong object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1802 | Address LvalueDst = Dst.getAddress(); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1803 | llvm::Value *src = Src.getScalarVal(); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1804 | if (Dst.isObjCIvar()) { |
| 1805 | assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1806 | llvm::Type *ResultType = IntPtrTy; |
| 1807 | Address dst = EmitPointerWithAlignment(Dst.getBaseIvarExp()); |
| 1808 | llvm::Value *RHS = dst.getPointer(); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1809 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1810 | llvm::Value *LHS = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1811 | Builder.CreatePtrToInt(LvalueDst.getPointer(), ResultType, |
| 1812 | "sub.ptr.lhs.cast"); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1813 | llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); |
Fariborz Jahanian | 1f9ed58 | 2009-09-25 00:00:20 +0000 | [diff] [blame] | 1814 | CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1815 | BytesBetween); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1816 | } else if (Dst.isGlobalObjCRef()) { |
| 1817 | CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, |
| 1818 | Dst.isThreadLocalRef()); |
| 1819 | } |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 1820 | else |
| 1821 | CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 6278e6a | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 1825 | assert(Src.isScalar() && "Can't emit an agg store with this method"); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1826 | EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1829 | void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1830 | llvm::Value **Result) { |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 1831 | const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1832 | llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1833 | Address Ptr = Dst.getBitFieldAddress(); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1834 | |
Daniel Dunbar | 67aba79 | 2010-04-15 03:47:33 +0000 | [diff] [blame] | 1835 | // Get the source value, truncated to the width of the bit-field. |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1836 | llvm::Value *SrcVal = Src.getScalarVal(); |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1837 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1838 | // Cast the source to the storage type and shift it into place. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1839 | SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(), |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1840 | /*IsSigned=*/false); |
| 1841 | llvm::Value *MaskedVal = SrcVal; |
Anders Carlsson | 8345a70 | 2010-04-17 21:52:22 +0000 | [diff] [blame] | 1842 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1843 | // See if there are other bits in the bitfield's storage we'll need to load |
| 1844 | // and mask together with source before storing. |
| 1845 | if (Info.StorageSize != Info.Size) { |
| 1846 | assert(Info.StorageSize > Info.Size && "Invalid bitfield size."); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1847 | llvm::Value *Val = |
| 1848 | Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load"); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Mask the source value as needed. |
| 1851 | if (!hasBooleanRepresentation(Dst.getType())) |
| 1852 | SrcVal = Builder.CreateAnd(SrcVal, |
| 1853 | llvm::APInt::getLowBitsSet(Info.StorageSize, |
| 1854 | Info.Size), |
| 1855 | "bf.value"); |
| 1856 | MaskedVal = SrcVal; |
| 1857 | if (Info.Offset) |
| 1858 | SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl"); |
| 1859 | |
| 1860 | // Mask out the original value. |
| 1861 | Val = Builder.CreateAnd(Val, |
| 1862 | ~llvm::APInt::getBitsSet(Info.StorageSize, |
| 1863 | Info.Offset, |
| 1864 | Info.Offset + Info.Size), |
| 1865 | "bf.clear"); |
| 1866 | |
| 1867 | // Or together the unchanged values and the source value. |
| 1868 | SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set"); |
| 1869 | } else { |
| 1870 | assert(Info.Offset == 0); |
| 1871 | } |
| 1872 | |
| 1873 | // Write the new value back out. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1874 | Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified()); |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1875 | |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1876 | // Return the new value of the bit-field, if requested. |
| 1877 | if (Result) { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1878 | llvm::Value *ResultVal = MaskedVal; |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1879 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1880 | // Sign extend the value if needed. |
| 1881 | if (Info.IsSigned) { |
| 1882 | assert(Info.Size <= Info.StorageSize); |
| 1883 | unsigned HighBits = Info.StorageSize - Info.Size; |
| 1884 | if (HighBits) { |
| 1885 | ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl"); |
| 1886 | ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr"); |
| 1887 | } |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 1890 | ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned, |
| 1891 | "bf.result.cast"); |
Eli Friedman | 39b685e | 2012-12-19 00:26:58 +0000 | [diff] [blame] | 1892 | *Result = EmitFromMemory(ResultVal, Dst.getType()); |
Daniel Dunbar | ead7c91 | 2008-08-06 05:08:45 +0000 | [diff] [blame] | 1893 | } |
Lauro Ramos Venancio | 09af71c | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1896 | void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1897 | LValue Dst) { |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1898 | // This access turns into a read/modify/write of the vector. Load the input |
| 1899 | // value now. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1900 | llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddress(), |
| 1901 | Dst.isVolatileQualified()); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1902 | const llvm::Constant *Elts = Dst.getExtVectorElts(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1904 | llvm::Value *SrcVal = Src.getScalarVal(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1905 | |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1906 | if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1907 | unsigned NumSrcElts = VTy->getNumElements(); |
Craig Topper | f2f1a09 | 2016-07-08 02:17:35 +0000 | [diff] [blame] | 1908 | unsigned NumDstElts = Vec->getType()->getVectorNumElements(); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1909 | if (NumDstElts == NumSrcElts) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1910 | // Use shuffle vector is the src and destination are the same number of |
| 1911 | // elements and restore the vector mask since it is on the side it will be |
| 1912 | // stored. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1913 | SmallVector<llvm::Constant*, 4> Mask(NumDstElts); |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1914 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1915 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1916 | |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1917 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1918 | Vec = Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1919 | llvm::UndefValue::get(Vec->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1920 | MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1921 | } else if (NumDstElts > NumSrcElts) { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1922 | // Extended the source vector to the same length and then shuffle it |
| 1923 | // into the destination. |
| 1924 | // FIXME: since we're shuffling with undef, can we just use the indices |
| 1925 | // into that? This could be simpler. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1926 | SmallVector<llvm::Constant*, 4> ExtMask; |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1927 | for (unsigned i = 0; i != NumSrcElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1928 | ExtMask.push_back(Builder.getInt32(i)); |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 1929 | ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1930 | llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1931 | llvm::Value *ExtSrcVal = |
Daniel Dunbar | 3d926cb | 2009-02-17 18:31:04 +0000 | [diff] [blame] | 1932 | Builder.CreateShuffleVector(SrcVal, |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1933 | llvm::UndefValue::get(SrcVal->getType()), |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1934 | ExtMaskV); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1935 | // build identity |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1936 | SmallVector<llvm::Constant*, 4> Mask; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1937 | for (unsigned i = 0; i != NumDstElts; ++i) |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1938 | Mask.push_back(Builder.getInt32(i)); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1939 | |
Joey Gouly | cf4143b | 2013-11-21 17:09:05 +0000 | [diff] [blame] | 1940 | // When the vector size is odd and .odd or .hi is used, the last element |
| 1941 | // of the Elts constant array will be one past the size of the vector. |
| 1942 | // Ignore the last element here, if it is greater than the mask size. |
| 1943 | if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size()) |
| 1944 | NumSrcElts--; |
| 1945 | |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1946 | // modify when what gets shuffled in |
Chris Lattner | 2d6b7b9 | 2012-01-25 05:34:41 +0000 | [diff] [blame] | 1947 | for (unsigned i = 0; i != NumSrcElts; ++i) |
| 1948 | Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1949 | llvm::Value *MaskV = llvm::ConstantVector::get(Mask); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1950 | Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1951 | } else { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1952 | // We should never shorten the vector |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1953 | llvm_unreachable("unexpected shorten vector length"); |
Chris Lattner | 3a44aa7 | 2007-08-03 16:37:04 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } else { |
| 1956 | // 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] | 1957 | unsigned InIdx = getAccessedFieldNo(0, Elts); |
Michael J. Spencer | dd59775 | 2014-05-31 00:22:12 +0000 | [diff] [blame] | 1958 | llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1959 | Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1960 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1961 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1962 | Builder.CreateStore(Vec, Dst.getExtVectorAddress(), |
| 1963 | Dst.isVolatileQualified()); |
Chris Lattner | 41d480e | 2007-08-03 16:28:33 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1966 | /// @brief Store of global named registers are always calls to intrinsics. |
| 1967 | void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) { |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1968 | assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) && |
| 1969 | "Bad type for register variable"); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1970 | llvm::MDNode *RegName = cast<llvm::MDNode>( |
| 1971 | cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata()); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1972 | assert(RegName && "Register LValue is not metadata"); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1973 | |
| 1974 | // We accept integer and pointer types only |
| 1975 | llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType()); |
| 1976 | llvm::Type *Ty = OrigTy; |
| 1977 | if (OrigTy->isPointerTy()) |
| 1978 | Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); |
| 1979 | llvm::Type *Types[] = { Ty }; |
| 1980 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1981 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types); |
| 1982 | llvm::Value *Value = Src.getScalarVal(); |
Renato Golin | 2e31e4e | 2014-06-05 16:45:22 +0000 | [diff] [blame] | 1983 | if (OrigTy->isPointerTy()) |
| 1984 | Value = Builder.CreatePtrToInt(Value, Ty); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 1985 | Builder.CreateCall( |
| 1986 | F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value}); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Eric Christopher | c9e2a68 | 2014-05-20 17:10:39 +0000 | [diff] [blame] | 1989 | // setObjCGCLValueClass - sets class of the lvalue for the purpose of |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1990 | // generating write-barries API. It is currently a global, ivar, |
| 1991 | // or neither. |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 1992 | static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1993 | LValue &LV, |
| 1994 | bool IsMemberAccess=false) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1995 | if (Ctx.getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 1996 | return; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 1997 | |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 1998 | if (isa<ObjCIvarRefExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 1999 | QualType ExpTy = E->getType(); |
| 2000 | if (IsMemberAccess && ExpTy->isPointerType()) { |
| 2001 | // If ivar is a structure pointer, assigning to field of |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2002 | // this struct follows gcc's behavior and makes it a non-ivar |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2003 | // writer-barrier conservatively. |
| 2004 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 2005 | if (ExpTy->isRecordType()) { |
| 2006 | LV.setObjCIvar(false); |
| 2007 | return; |
| 2008 | } |
| 2009 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2010 | LV.setObjCIvar(true); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2011 | auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E)); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2012 | LV.setBaseIvarExp(Exp->getBase()); |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2013 | LV.setObjCArray(E->getType()->isArrayType()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2016 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2017 | if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) { |
| 2018 | if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 2019 | if (VD->hasGlobalStorage()) { |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2020 | LV.setGlobalObjCRef(true); |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 2021 | LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2022 | } |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2023 | } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2024 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2025 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2026 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2027 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2028 | if (const auto *Exp = dyn_cast<UnaryOperator>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2029 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2030 | return; |
| 2031 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2032 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2033 | if (const auto *Exp = dyn_cast<ParenExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2034 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 2035 | if (LV.isObjCIvar()) { |
| 2036 | // If cast is to a structure pointer, follow gcc's behavior and make it |
| 2037 | // a non-ivar write-barrier. |
| 2038 | QualType ExpTy = E->getType(); |
| 2039 | if (ExpTy->isPointerType()) |
| 2040 | ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); |
| 2041 | if (ExpTy->isRecordType()) |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2042 | LV.setObjCIvar(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2043 | } |
| 2044 | return; |
Fariborz Jahanian | e01e434 | 2009-09-30 17:10:29 +0000 | [diff] [blame] | 2045 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2046 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2047 | if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2048 | setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV); |
| 2049 | return; |
| 2050 | } |
| 2051 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2052 | if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2053 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2056 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2057 | if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2058 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2061 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2062 | if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2063 | setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
| 2066 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2067 | if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) { |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2068 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2069 | if (LV.isObjCIvar() && !LV.isObjCArray()) |
| 2070 | // 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] | 2071 | // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2072 | LV.setObjCIvar(false); |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 2073 | else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2074 | // Using array syntax to assigning to what global points to is not |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 2075 | // same as assigning to the global itself. {id *G;} G[i] = 0; |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2076 | LV.setGlobalObjCRef(false); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2077 | return; |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 2078 | } |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2079 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2080 | if (const auto *Exp = dyn_cast<MemberExpr>(E)) { |
Fariborz Jahanian | 0c78427 | 2011-09-30 18:23:36 +0000 | [diff] [blame] | 2081 | setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true); |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 2082 | // We don't know if member is an 'ivar', but this flag is looked at |
| 2083 | // only in the context of LV.isObjCIvar(). |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 2084 | LV.setObjCArray(E->getType()->isArrayType()); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2085 | return; |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2089 | static llvm::Value * |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 2090 | EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2091 | llvm::Value *V, llvm::Type *IRType, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2092 | StringRef Name = StringRef()) { |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2093 | unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); |
Chandler Carruth | 4678f67 | 2011-07-12 08:58:26 +0000 | [diff] [blame] | 2094 | return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2097 | static LValue EmitThreadPrivateVarDeclLValue( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2098 | CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, |
| 2099 | llvm::Type *RealVarTy, SourceLocation Loc) { |
| 2100 | Addr = CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc); |
| 2101 | Addr = CGF.Builder.CreateElementBitCast(Addr, RealVarTy); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2102 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2103 | return CGF.MakeAddrLValue(Addr, T, BaseInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | Address CodeGenFunction::EmitLoadOfReference(Address Addr, |
| 2107 | const ReferenceType *RefTy, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2108 | LValueBaseInfo *BaseInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2109 | llvm::Value *Ptr = Builder.CreateLoad(Addr); |
| 2110 | return Address(Ptr, getNaturalTypeAlignment(RefTy->getPointeeType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2111 | BaseInfo, /*forPointee*/ true)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | LValue CodeGenFunction::EmitLoadOfReferenceLValue(Address RefAddr, |
| 2115 | const ReferenceType *RefTy) { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2116 | LValueBaseInfo BaseInfo; |
| 2117 | Address Addr = EmitLoadOfReference(RefAddr, RefTy, &BaseInfo); |
| 2118 | return MakeAddrLValue(Addr, RefTy->getPointeeType(), BaseInfo); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2121 | Address CodeGenFunction::EmitLoadOfPointer(Address Ptr, |
| 2122 | const PointerType *PtrTy, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2123 | LValueBaseInfo *BaseInfo) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2124 | llvm::Value *Addr = Builder.CreateLoad(Ptr); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2125 | return Address(Addr, getNaturalTypeAlignment(PtrTy->getPointeeType(), |
| 2126 | BaseInfo, |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2127 | /*forPointeeType=*/true)); |
| 2128 | } |
| 2129 | |
| 2130 | LValue CodeGenFunction::EmitLoadOfPointerLValue(Address PtrAddr, |
| 2131 | const PointerType *PtrTy) { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2132 | LValueBaseInfo BaseInfo; |
| 2133 | Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &BaseInfo); |
| 2134 | return MakeAddrLValue(Addr, PtrTy->getPointeeType(), BaseInfo); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2137 | static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, |
| 2138 | const Expr *E, const VarDecl *VD) { |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2139 | QualType T = E->getType(); |
| 2140 | |
| 2141 | // 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] | 2142 | if (VD->getTLSKind() == VarDecl::TLS_Dynamic && |
| 2143 | CGF.CGM.getCXXABI().usesThreadWrapperFunction()) |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2144 | return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T); |
| 2145 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2146 | llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2147 | llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); |
| 2148 | V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2149 | CharUnits Alignment = CGF.getContext().getDeclAlign(VD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2150 | Address Addr(V, Alignment); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2151 | LValue LV; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2152 | // Emit reference to the private copy of the variable if it is an OpenMP |
| 2153 | // threadprivate variable. |
| 2154 | if (CGF.getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2155 | return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2156 | E->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2157 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) { |
| 2158 | LV = CGF.EmitLoadOfReferenceLValue(Addr, RefTy); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2159 | } else { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2160 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2161 | LV = CGF.MakeAddrLValue(Addr, T, BaseInfo); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2162 | } |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2163 | setObjCGCLValueClass(CGF.getContext(), E, LV); |
| 2164 | return LV; |
| 2165 | } |
| 2166 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2167 | static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM, |
| 2168 | const FunctionDecl *FD) { |
| 2169 | if (FD->hasAttr<WeakRefAttr>()) { |
| 2170 | ConstantAddress aliasee = CGM.GetWeakRefReference(FD); |
| 2171 | return aliasee.getPointer(); |
| 2172 | } |
| 2173 | |
| 2174 | llvm::Constant *V = CGM.GetAddrOfFunction(FD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2175 | if (!FD->hasPrototype()) { |
| 2176 | if (const FunctionProtoType *Proto = |
| 2177 | FD->getType()->getAs<FunctionProtoType>()) { |
| 2178 | // Ugly case: for a K&R-style definition, the type of the definition |
| 2179 | // isn't the same as the type of a use. Correct for this with a |
| 2180 | // bitcast. |
| 2181 | QualType NoProtoType = |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2182 | CGM.getContext().getFunctionNoProtoType(Proto->getReturnType()); |
| 2183 | NoProtoType = CGM.getContext().getPointerType(NoProtoType); |
| 2184 | V = llvm::ConstantExpr::getBitCast(V, |
| 2185 | CGM.getTypes().ConvertType(NoProtoType)); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2188 | return V; |
| 2189 | } |
| 2190 | |
| 2191 | static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, |
| 2192 | const Expr *E, const FunctionDecl *FD) { |
| 2193 | llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, FD); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 2194 | CharUnits Alignment = CGF.getContext().getDeclAlign(FD); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2195 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2196 | return CGF.MakeAddrLValue(V, E->getType(), Alignment, BaseInfo); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Ben Langmuir | 3b4c30b | 2013-05-09 19:17:11 +0000 | [diff] [blame] | 2199 | static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, |
| 2200 | llvm::Value *ThisValue) { |
| 2201 | QualType TagType = CGF.getContext().getTagDeclType(FD->getParent()); |
| 2202 | LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType); |
| 2203 | return CGF.EmitLValueForField(LV, FD); |
| 2204 | } |
| 2205 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2206 | /// Named Registers are named metadata pointing to the register name |
| 2207 | /// which will be read from/written to as an argument to the intrinsic |
| 2208 | /// @llvm.read/write_register. |
| 2209 | /// So far, only the name is being passed down, but other options such as |
| 2210 | /// register type, allocation type or even optimization options could be |
| 2211 | /// passed down via the metadata node. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2212 | static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM) { |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2213 | SmallString<64> Name("llvm.named.register."); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2214 | AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>(); |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2215 | assert(Asm->getLabel().size() < 64-Name.size() && |
| 2216 | "Register name too big"); |
| 2217 | Name.append(Asm->getLabel()); |
Renato Golin | 156a853 | 2014-05-19 22:36:19 +0000 | [diff] [blame] | 2218 | llvm::NamedMDNode *M = |
Renato Golin | c296d95 | 2014-05-19 23:25:25 +0000 | [diff] [blame] | 2219 | CGM.getModule().getOrInsertNamedMetadata(Name); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2220 | if (M->getNumOperands() == 0) { |
| 2221 | llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(), |
| 2222 | Asm->getLabel()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2223 | llvm::Metadata *Ops[] = {Str}; |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2224 | M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
| 2225 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2226 | |
| 2227 | CharUnits Alignment = CGM.getContext().getDeclAlign(VD); |
| 2228 | |
| 2229 | llvm::Value *Ptr = |
| 2230 | llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0)); |
| 2231 | return LValue::MakeGlobalReg(Address(Ptr, Alignment), VD->getType()); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 2234 | LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 2235 | const NamedDecl *ND = E->getDecl(); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2236 | QualType T = E->getType(); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 2237 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2238 | if (const auto *VD = dyn_cast<VarDecl>(ND)) { |
| 2239 | // Global Named registers access via intrinsics only |
| 2240 | if (VD->getStorageClass() == SC_Register && |
| 2241 | VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2242 | return EmitGlobalNamedRegister(VD, CGM); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2243 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2244 | // A DeclRefExpr for a reference initialized by a constant expression can |
| 2245 | // appear without being odr-used. Directly emit the constant initializer. |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2246 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2247 | if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() && |
| 2248 | VD->isUsableInConstantExpressions(getContext()) && |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2249 | VD->checkInitIsICE() && |
| 2250 | // Do not emit if it is private OpenMP variable. |
| 2251 | !(E->refersToEnclosingVariableOrCapture() && CapturedStmtInfo && |
| 2252 | LocalDeclMap.count(VD))) { |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2253 | llvm::Constant *Val = |
| 2254 | CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this); |
| 2255 | assert(Val && "failed to emit reference constant expression"); |
| 2256 | // FIXME: Eventually we will want to emit vector element references. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2257 | |
| 2258 | // Should we be using the alignment of the constant pointer we emitted? |
| 2259 | CharUnits Alignment = getNaturalTypeAlignment(E->getType(), nullptr, |
| 2260 | /*pointee*/ true); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2261 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2262 | return MakeAddrLValue(Address(Val, Alignment), T, BaseInfo); |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2263 | } |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Check for captured variables. |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 2266 | if (E->refersToEnclosingVariableOrCapture()) { |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2267 | if (auto *FD = LambdaCaptureFields.lookup(VD)) |
| 2268 | return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue); |
| 2269 | else if (CapturedStmtInfo) { |
Alexey Bataev | ac5eabb | 2016-11-07 11:16:04 +0000 | [diff] [blame] | 2270 | auto I = LocalDeclMap.find(VD); |
| 2271 | if (I != LocalDeclMap.end()) { |
| 2272 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) |
| 2273 | return EmitLoadOfReferenceLValue(I->second, RefTy); |
| 2274 | return MakeAddrLValue(I->second, T); |
Alexey Bataev | caacd53 | 2015-09-04 11:26:21 +0000 | [diff] [blame] | 2275 | } |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2276 | LValue CapLVal = |
| 2277 | EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD), |
| 2278 | CapturedStmtInfo->getContextValue()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2279 | bool MayAlias = CapLVal.getBaseInfo().getMayAlias(); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2280 | return MakeAddrLValue( |
| 2281 | Address(CapLVal.getPointer(), getContext().getDeclAlign(VD)), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2282 | CapLVal.getType(), LValueBaseInfo(AlignmentSource::Decl, MayAlias)); |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2283 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2284 | |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2285 | assert(isa<BlockDecl>(CurCodeDecl)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2286 | Address addr = GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2287 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2288 | return MakeAddrLValue(addr, T, BaseInfo); |
David Majnemer | 602cfe7 | 2015-01-01 09:49:44 +0000 | [diff] [blame] | 2289 | } |
Richard Smith | 5a1104b | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Eli Friedman | 5720e34 | 2012-01-21 04:52:58 +0000 | [diff] [blame] | 2292 | // FIXME: We should be able to assert this for FunctionDecls as well! |
| 2293 | // FIXME: We should be able to assert this for all DeclRefExprs, not just |
| 2294 | // those with a valid source location. |
| 2295 | assert((ND->isUsed(false) || !isa<VarDecl>(ND) || |
| 2296 | !E->getLocation().isValid()) && |
| 2297 | "Should not use decl without marking it used!"); |
| 2298 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2299 | if (ND->hasAttr<WeakRefAttr>()) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2300 | const auto *VD = cast<ValueDecl>(ND); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2301 | ConstantAddress Aliasee = CGM.GetWeakRefReference(VD); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2302 | return MakeAddrLValue(Aliasee, T, |
| 2303 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Renato Golin | e7b3d5d | 2014-05-27 16:46:27 +0000 | [diff] [blame] | 2306 | if (const auto *VD = dyn_cast<VarDecl>(ND)) { |
Anders Carlsson | 2ff6395d | 2009-11-07 22:53:10 +0000 | [diff] [blame] | 2307 | // Check if this is a global variable. |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2308 | if (VD->hasLinkage() || VD->isStaticDataMember()) |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 2309 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Anders Carlsson | 6eee972 | 2009-11-07 22:46:42 +0000 | [diff] [blame] | 2310 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2311 | Address addr = Address::invalid(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2312 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2313 | // The variable should generally be present in the local decl map. |
| 2314 | auto iter = LocalDeclMap.find(VD); |
| 2315 | if (iter != LocalDeclMap.end()) { |
| 2316 | addr = iter->second; |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 2317 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2318 | // Otherwise, it might be static local we haven't emitted yet for |
| 2319 | // some reason; most likely, because it's in an outer function. |
| 2320 | } else if (VD->isStaticLocal()) { |
| 2321 | addr = Address(CGM.getOrCreateStaticVarDecl( |
| 2322 | *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false)), |
| 2323 | getContext().getDeclAlign(VD)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2324 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2325 | // No other cases for now. |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2326 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2327 | llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?"); |
| 2328 | } |
| 2329 | |
| 2330 | |
| 2331 | // Check for OpenMP threadprivate variables. |
| 2332 | if (getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { |
| 2333 | return EmitThreadPrivateVarDeclLValue( |
| 2334 | *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()), |
| 2335 | E->getExprLoc()); |
| 2336 | } |
| 2337 | |
| 2338 | // Drill into block byref variables. |
| 2339 | bool isBlockByref = VD->hasAttr<BlocksAttr>(); |
| 2340 | if (isBlockByref) { |
| 2341 | addr = emitBlockByrefAddress(addr, VD); |
| 2342 | } |
| 2343 | |
| 2344 | // Drill into reference types. |
| 2345 | LValue LV; |
| 2346 | if (auto RefTy = VD->getType()->getAs<ReferenceType>()) { |
| 2347 | LV = EmitLoadOfReferenceLValue(addr, RefTy); |
| 2348 | } else { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2349 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
| 2350 | LV = MakeAddrLValue(addr, T, BaseInfo); |
Eli Friedman | d20adbd | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 2351 | } |
Chris Lattner | 3f32d69 | 2011-07-12 06:52:18 +0000 | [diff] [blame] | 2352 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2353 | bool isLocalStorage = VD->hasLocalStorage(); |
| 2354 | |
| 2355 | bool NonGCable = isLocalStorage && |
| 2356 | !VD->getType()->isReferenceType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2357 | !isBlockByref; |
Fariborz Jahanian | 44a41d1 | 2010-11-19 18:17:09 +0000 | [diff] [blame] | 2358 | if (NonGCable) { |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2359 | LV.getQuals().removeObjCGCAttr(); |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2360 | LV.setNonGC(true); |
| 2361 | } |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2362 | |
| 2363 | bool isImpreciseLifetime = |
| 2364 | (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>()); |
| 2365 | if (isImpreciseLifetime) |
| 2366 | LV.setARCPreciseLifetime(ARCImpreciseLifetime); |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 2367 | setObjCGCLValueClass(getContext(), E, LV); |
Fariborz Jahanian | 003e830 | 2008-11-20 00:15:42 +0000 | [diff] [blame] | 2368 | return LV; |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2369 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2370 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2371 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2372 | return EmitFunctionDeclLValue(*this, E, FD); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2373 | |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 2374 | // FIXME: While we're emitting a binding from an enclosing scope, all other |
| 2375 | // DeclRefExprs we see should be implicitly treated as if they also refer to |
| 2376 | // an enclosing scope. |
| 2377 | if (const auto *BD = dyn_cast<BindingDecl>(ND)) |
| 2378 | return EmitLValue(BD->getBinding()); |
| 2379 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2380 | llvm_unreachable("Unhandled DeclRefExpr"); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 2381 | } |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 2382 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2383 | LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { |
| 2384 | // __extension__ doesn't affect lvalue-ness. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2385 | if (E->getOpcode() == UO_Extension) |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2386 | return EmitLValue(E->getSubExpr()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2387 | |
Chris Lattner | 0f398c4 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 2388 | QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2389 | switch (E->getOpcode()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2390 | default: llvm_unreachable("Unknown unary operator lvalue!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2391 | case UO_Deref: { |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2392 | QualType T = E->getSubExpr()->getType()->getPointeeType(); |
| 2393 | assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2394 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2395 | LValueBaseInfo BaseInfo; |
| 2396 | Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo); |
| 2397 | LValue LV = MakeAddrLValue(Addr, T, BaseInfo); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 2398 | LV.getQuals().setAddressSpace(ExprTy.getAddressSpace()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2399 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2400 | // We should not generate __weak write barrier on indirect reference |
| 2401 | // of a pointer to object; as in void foo (__weak id *param); *param = 0; |
| 2402 | // But, we continue to generate __strong write barrier on indirect write |
| 2403 | // into a pointer to object. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2404 | if (getLangOpts().ObjC1 && |
| 2405 | getLangOpts().getGC() != LangOptions::NonGC && |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2406 | LV.isObjCWeak()) |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 2407 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 2408 | return LV; |
| 2409 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2410 | case UO_Real: |
| 2411 | case UO_Imag: { |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2412 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2413 | assert(LV.isSimple() && "real/imag on non-ordinary l-value"); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2414 | |
Richard Smith | 0b6b8e4 | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 2415 | // __real is valid on scalars. This is a faster way of testing that. |
| 2416 | // __imag can only produce an rvalue on scalars. |
| 2417 | if (E->getOpcode() == UO_Real && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2418 | !LV.getAddress().getElementType()->isStructTy()) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2419 | assert(E->getSubExpr()->getType()->isArithmeticType()); |
| 2420 | return LV; |
| 2421 | } |
| 2422 | |
Alexey Bataev | 611b0a1 | 2016-11-07 18:15:02 +0000 | [diff] [blame] | 2423 | QualType T = ExprTy->castAs<ComplexType>()->getElementType(); |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2424 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2425 | Address Component = |
| 2426 | (E->getOpcode() == UO_Real |
| 2427 | ? emitAddrOfRealComponent(LV.getAddress(), LV.getType()) |
| 2428 | : emitAddrOfImagComponent(LV.getAddress(), LV.getType())); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2429 | LValue ElemLV = MakeAddrLValue(Component, T, LV.getBaseInfo()); |
Alexey Bataev | 611b0a1 | 2016-11-07 18:15:02 +0000 | [diff] [blame] | 2430 | ElemLV.getQuals().addQualifiers(LV.getQuals()); |
| 2431 | return ElemLV; |
Chris Lattner | 595db86 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 2432 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2433 | case UO_PreInc: |
| 2434 | case UO_PreDec: { |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 2435 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2436 | bool isInc = E->getOpcode() == UO_PreInc; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | bb8976e | 2010-01-09 21:44:40 +0000 | [diff] [blame] | 2438 | if (E->getType()->isAnyComplexType()) |
| 2439 | EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 2440 | else |
| 2441 | EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); |
| 2442 | return LV; |
| 2443 | } |
Eli Friedman | a72bf0f | 2009-11-09 04:20:47 +0000 | [diff] [blame] | 2444 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 2447 | LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 2448 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2449 | E->getType(), |
| 2450 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2453 | LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { |
Daniel Dunbar | 2e442a0 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 2454 | return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2455 | E->getType(), |
| 2456 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2459 | LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2460 | auto SL = E->getFunctionName(); |
| 2461 | assert(SL != nullptr && "No StringLiteral name in PredefinedExpr"); |
| 2462 | StringRef FnName = CurFn->getName(); |
| 2463 | if (FnName.startswith("\01")) |
| 2464 | FnName = FnName.substr(1); |
| 2465 | StringRef NameItems[] = { |
| 2466 | PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName}; |
| 2467 | std::string GVName = llvm::join(NameItems, NameItems + 2, "."); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2468 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
Mehdi Amini | dc9bf8f | 2016-11-16 07:07:28 +0000 | [diff] [blame] | 2469 | if (auto *BD = dyn_cast<BlockDecl>(CurCodeDecl)) { |
| 2470 | std::string Name = SL->getString(); |
| 2471 | if (!Name.empty()) { |
| 2472 | unsigned Discriminator = |
| 2473 | CGM.getCXXABI().getMangleContext().getBlockId(BD, true); |
| 2474 | if (Discriminator) |
| 2475 | Name += "_" + Twine(Discriminator + 1).str(); |
| 2476 | auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2477 | return MakeAddrLValue(C, E->getType(), BaseInfo); |
Mehdi Amini | dc9bf8f | 2016-11-16 07:07:28 +0000 | [diff] [blame] | 2478 | } else { |
| 2479 | auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2480 | return MakeAddrLValue(C, E->getType(), BaseInfo); |
Mehdi Amini | dc9bf8f | 2016-11-16 07:07:28 +0000 | [diff] [blame] | 2481 | } |
Fariborz Jahanian | 68e7938 | 2014-11-14 23:55:27 +0000 | [diff] [blame] | 2482 | } |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2483 | auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2484 | return MakeAddrLValue(C, E->getType(), BaseInfo); |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2487 | /// Emit a type description suitable for use by a runtime sanitizer library. The |
| 2488 | /// format of a type descriptor is |
| 2489 | /// |
| 2490 | /// \code |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2491 | /// { i16 TypeKind, i16 TypeInfo } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2492 | /// \endcode |
| 2493 | /// |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2494 | /// followed by an array of i8 containing the type name. TypeKind is 0 for an |
| 2495 | /// integer, 1 for a floating point value, and -1 for anything else. |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2496 | llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) { |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2497 | // Only emit each type's descriptor once. |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2498 | if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T)) |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2499 | return C; |
| 2500 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2501 | uint16_t TypeKind = -1; |
| 2502 | uint16_t TypeInfo = 0; |
Mike Stump | 9a4e012 | 2009-12-15 00:59:40 +0000 | [diff] [blame] | 2503 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2504 | if (T->isIntegerType()) { |
| 2505 | TypeKind = 0; |
| 2506 | TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) | |
Aaron Ballman | f505d55 | 2012-11-30 21:44:01 +0000 | [diff] [blame] | 2507 | (T->isSignedIntegerType() ? 1 : 0); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2508 | } else if (T->isFloatingType()) { |
| 2509 | TypeKind = 1; |
| 2510 | TypeInfo = getContext().getTypeSize(T); |
| 2511 | } |
| 2512 | |
| 2513 | // Format the type name as if for a diagnostic, including quotes and |
| 2514 | // optionally an 'aka'. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2515 | SmallString<32> Buffer; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2516 | CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype, |
| 2517 | (intptr_t)T.getAsOpaquePtr(), |
Craig Topper | 3aa4fb3 | 2014-06-12 05:32:35 +0000 | [diff] [blame] | 2518 | StringRef(), StringRef(), None, Buffer, |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2519 | None); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2520 | |
| 2521 | llvm::Constant *Components[] = { |
Richard Smith | 683398a | 2012-10-09 23:55:19 +0000 | [diff] [blame] | 2522 | Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo), |
| 2523 | llvm::ConstantDataArray::getString(getLLVMContext(), Buffer) |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2524 | }; |
| 2525 | llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components); |
| 2526 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2527 | auto *GV = new llvm::GlobalVariable( |
| 2528 | CGM.getModule(), Descriptor->getType(), |
| 2529 | /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 2530 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 2531 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV); |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Remember the descriptor for this type. |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 2534 | CGM.setTypeDescriptorInMap(T, GV); |
Will Dietz | 949ec54 | 2013-11-08 01:09:22 +0000 | [diff] [blame] | 2535 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2536 | return GV; |
| 2537 | } |
| 2538 | |
| 2539 | llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) { |
| 2540 | llvm::Type *TargetTy = IntPtrTy; |
| 2541 | |
Richard Smith | 48366f7 | 2013-03-22 00:47:07 +0000 | [diff] [blame] | 2542 | // Floating-point types which fit into intptr_t are bitcast to integers |
| 2543 | // and then passed directly (after zero-extension, if necessary). |
| 2544 | if (V->getType()->isFloatingPointTy()) { |
| 2545 | unsigned Bits = V->getType()->getPrimitiveSizeInBits(); |
| 2546 | if (Bits <= TargetTy->getIntegerBitWidth()) |
| 2547 | V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(), |
| 2548 | Bits)); |
| 2549 | } |
| 2550 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2551 | // Integers which fit in intptr_t are zero-extended and passed directly. |
| 2552 | if (V->getType()->isIntegerTy() && |
| 2553 | V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth()) |
| 2554 | return Builder.CreateZExt(V, TargetTy); |
| 2555 | |
| 2556 | // Pointers are passed directly, everything else is passed by address. |
| 2557 | if (!V->getType()->isPointerTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2558 | Address Ptr = CreateDefaultAlignTempAlloca(V->getType()); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2559 | Builder.CreateStore(V, Ptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2560 | V = Ptr.getPointer(); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2561 | } |
| 2562 | return Builder.CreatePtrToInt(V, TargetTy); |
| 2563 | } |
| 2564 | |
| 2565 | /// \brief Emit a representation of a SourceLocation for passing to a handler |
| 2566 | /// in a sanitizer runtime library. The format for this data is: |
| 2567 | /// \code |
| 2568 | /// struct SourceLocation { |
| 2569 | /// const char *Filename; |
| 2570 | /// int32_t Line, Column; |
| 2571 | /// }; |
| 2572 | /// \endcode |
| 2573 | /// For an invalid SourceLocation, the Filename pointer is null. |
| 2574 | llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) { |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2575 | llvm::Constant *Filename; |
| 2576 | int Line, Column; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2577 | |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2578 | PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc); |
| 2579 | if (PLoc.isValid()) { |
Filipe Cabecinhas | ab731f7 | 2016-05-12 16:51:36 +0000 | [diff] [blame] | 2580 | StringRef FilenameString = PLoc.getFilename(); |
| 2581 | |
| 2582 | int PathComponentsToStrip = |
| 2583 | CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip; |
| 2584 | if (PathComponentsToStrip < 0) { |
| 2585 | assert(PathComponentsToStrip != INT_MIN); |
| 2586 | int PathComponentsToKeep = -PathComponentsToStrip; |
| 2587 | auto I = llvm::sys::path::rbegin(FilenameString); |
| 2588 | auto E = llvm::sys::path::rend(FilenameString); |
| 2589 | while (I != E && --PathComponentsToKeep) |
| 2590 | ++I; |
| 2591 | |
| 2592 | FilenameString = FilenameString.substr(I - E); |
| 2593 | } else if (PathComponentsToStrip > 0) { |
| 2594 | auto I = llvm::sys::path::begin(FilenameString); |
| 2595 | auto E = llvm::sys::path::end(FilenameString); |
| 2596 | while (I != E && PathComponentsToStrip--) |
| 2597 | ++I; |
| 2598 | |
| 2599 | if (I != E) |
| 2600 | FilenameString = |
| 2601 | FilenameString.substr(I - llvm::sys::path::begin(FilenameString)); |
| 2602 | else |
| 2603 | FilenameString = llvm::sys::path::filename(FilenameString); |
| 2604 | } |
| 2605 | |
| 2606 | auto FilenameGV = CGM.GetAddrOfConstantCString(FilenameString, ".src"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2607 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal( |
| 2608 | cast<llvm::GlobalVariable>(FilenameGV.getPointer())); |
| 2609 | Filename = FilenameGV.getPointer(); |
Alexey Samsonov | 6c12414 | 2014-07-18 17:50:06 +0000 | [diff] [blame] | 2610 | Line = PLoc.getLine(); |
| 2611 | Column = PLoc.getColumn(); |
| 2612 | } else { |
| 2613 | Filename = llvm::Constant::getNullValue(Int8PtrTy); |
| 2614 | Line = Column = 0; |
| 2615 | } |
| 2616 | |
| 2617 | llvm::Constant *Data[] = {Filename, Builder.getInt32(Line), |
| 2618 | Builder.getInt32(Column)}; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2619 | |
| 2620 | return llvm::ConstantStruct::getAnon(Data); |
| 2621 | } |
| 2622 | |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2623 | namespace { |
| 2624 | /// \brief Specify under what conditions this check can be recovered |
| 2625 | enum class CheckRecoverableKind { |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2626 | /// Always terminate program execution if this check fails. |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2627 | Unrecoverable, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2628 | /// Check supports recovering, runtime has both fatal (noreturn) and |
| 2629 | /// non-fatal handlers for this check. |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2630 | Recoverable, |
| 2631 | /// Runtime conditionally aborts, always need to support recovery. |
| 2632 | AlwaysRecoverable |
| 2633 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2634 | } |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2635 | |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 2636 | static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind) { |
| 2637 | assert(llvm::countPopulation(Kind) == 1); |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2638 | switch (Kind) { |
| 2639 | case SanitizerKind::Vptr: |
| 2640 | return CheckRecoverableKind::AlwaysRecoverable; |
| 2641 | case SanitizerKind::Return: |
| 2642 | case SanitizerKind::Unreachable: |
| 2643 | return CheckRecoverableKind::Unrecoverable; |
| 2644 | default: |
| 2645 | return CheckRecoverableKind::Recoverable; |
| 2646 | } |
| 2647 | } |
| 2648 | |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2649 | namespace { |
| 2650 | struct SanitizerHandlerInfo { |
| 2651 | char const *const Name; |
| 2652 | unsigned Version; |
| 2653 | }; |
Saleem Abdulrasool | ca6e2b4 | 2016-12-13 03:27:35 +0000 | [diff] [blame] | 2654 | } |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2655 | |
| 2656 | const SanitizerHandlerInfo SanitizerHandlers[] = { |
| 2657 | #define SANITIZER_CHECK(Enum, Name, Version) {#Name, Version}, |
| 2658 | LIST_SANITIZER_CHECKS |
| 2659 | #undef SANITIZER_CHECK |
| 2660 | }; |
| 2661 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2662 | static void emitCheckHandlerCall(CodeGenFunction &CGF, |
| 2663 | llvm::FunctionType *FnType, |
| 2664 | ArrayRef<llvm::Value *> FnArgs, |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2665 | SanitizerHandler CheckHandler, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2666 | CheckRecoverableKind RecoverKind, bool IsFatal, |
| 2667 | llvm::BasicBlock *ContBB) { |
| 2668 | assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable); |
| 2669 | bool NeedsAbortSuffix = |
| 2670 | IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable; |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2671 | const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler]; |
| 2672 | const StringRef CheckName = CheckInfo.Name; |
| 2673 | std::string FnName = |
| 2674 | ("__ubsan_handle_" + CheckName + |
Vedant Kumar | 4881bdf | 2016-12-12 18:47:33 +0000 | [diff] [blame] | 2675 | (CheckInfo.Version ? "_v" + llvm::utostr(CheckInfo.Version) : "") + |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2676 | (NeedsAbortSuffix ? "_abort" : "")) |
| 2677 | .str(); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2678 | bool MayReturn = |
| 2679 | !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable; |
| 2680 | |
| 2681 | llvm::AttrBuilder B; |
| 2682 | if (!MayReturn) { |
| 2683 | B.addAttribute(llvm::Attribute::NoReturn) |
| 2684 | .addAttribute(llvm::Attribute::NoUnwind); |
| 2685 | } |
| 2686 | B.addAttribute(llvm::Attribute::UWTable); |
| 2687 | |
| 2688 | llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction( |
| 2689 | FnType, FnName, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2690 | llvm::AttributeList::get(CGF.getLLVMContext(), |
| 2691 | llvm::AttributeList::FunctionIndex, B), |
Saleem Abdulrasool | 05b8fde | 2016-12-15 16:30:20 +0000 | [diff] [blame] | 2692 | /*Local=*/true); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2693 | llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs); |
| 2694 | if (!MayReturn) { |
| 2695 | HandlerCall->setDoesNotReturn(); |
| 2696 | CGF.Builder.CreateUnreachable(); |
| 2697 | } else { |
| 2698 | CGF.Builder.CreateBr(ContBB); |
| 2699 | } |
| 2700 | } |
| 2701 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2702 | void CodeGenFunction::EmitCheck( |
Peter Collingbourne | 3eea677 | 2015-05-11 21:39:14 +0000 | [diff] [blame] | 2703 | ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked, |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2704 | SanitizerHandler CheckHandler, ArrayRef<llvm::Constant *> StaticArgs, |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2705 | ArrayRef<llvm::Value *> DynamicArgs) { |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 2706 | assert(IsSanitizerScope); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2707 | assert(Checked.size() > 0); |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2708 | assert(CheckHandler >= 0 && |
| 2709 | CheckHandler < sizeof(SanitizerHandlers) / sizeof(*SanitizerHandlers)); |
| 2710 | const StringRef CheckName = SanitizerHandlers[CheckHandler].Name; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2711 | |
| 2712 | llvm::Value *FatalCond = nullptr; |
| 2713 | llvm::Value *RecoverableCond = nullptr; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2714 | llvm::Value *TrapCond = nullptr; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2715 | for (int i = 0, n = Checked.size(); i < n; ++i) { |
| 2716 | llvm::Value *Check = Checked[i].first; |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2717 | // -fsanitize-trap= overrides -fsanitize-recover=. |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2718 | llvm::Value *&Cond = |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2719 | CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second) |
| 2720 | ? TrapCond |
| 2721 | : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second) |
| 2722 | ? RecoverableCond |
| 2723 | : FatalCond; |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2724 | Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check; |
| 2725 | } |
| 2726 | |
Peter Collingbourne | 9881b78 | 2015-06-18 23:59:22 +0000 | [diff] [blame] | 2727 | if (TrapCond) |
| 2728 | EmitTrapCheck(TrapCond); |
| 2729 | if (!FatalCond && !RecoverableCond) |
| 2730 | return; |
| 2731 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2732 | llvm::Value *JointCond; |
| 2733 | if (FatalCond && RecoverableCond) |
| 2734 | JointCond = Builder.CreateAnd(FatalCond, RecoverableCond); |
| 2735 | else |
| 2736 | JointCond = FatalCond ? FatalCond : RecoverableCond; |
| 2737 | assert(JointCond); |
| 2738 | |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2739 | CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second); |
Evgeniy Stepanov | 02279ed | 2016-03-15 20:19:29 +0000 | [diff] [blame] | 2740 | assert(SanOpts.has(Checked[0].second)); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2741 | #ifndef NDEBUG |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2742 | for (int i = 1, n = Checked.size(); i < n; ++i) { |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2743 | assert(RecoverKind == getRecoverableKind(Checked[i].second) && |
Alexey Samsonov | 4c1a96f | 2014-11-10 22:27:30 +0000 | [diff] [blame] | 2744 | "All recoverable kinds in a single check must be same!"); |
Evgeniy Stepanov | 02279ed | 2016-03-15 20:19:29 +0000 | [diff] [blame] | 2745 | assert(SanOpts.has(Checked[i].second)); |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2746 | } |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2747 | #endif |
Chad Rosier | ae229d5 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 2748 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2749 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2750 | llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName); |
| 2751 | llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers); |
Will Dietz | ddd282a | 2012-12-15 01:39:14 +0000 | [diff] [blame] | 2752 | // Give hint that we very much don't expect to execute the handler |
| 2753 | // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp |
| 2754 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 2755 | llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); |
| 2756 | Branch->setMetadata(llvm::LLVMContext::MD_prof, Node); |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2757 | EmitBlock(Handlers); |
Will Dietz | ddd282a | 2012-12-15 01:39:14 +0000 | [diff] [blame] | 2758 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2759 | // Handler functions take an i8* pointing to the (handler-specific) static |
| 2760 | // information block, followed by a sequence of intptr_t arguments |
| 2761 | // representing operand values. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2762 | SmallVector<llvm::Value *, 4> Args; |
| 2763 | SmallVector<llvm::Type *, 4> ArgTypes; |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2764 | Args.reserve(DynamicArgs.size() + 1); |
| 2765 | ArgTypes.reserve(DynamicArgs.size() + 1); |
| 2766 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2767 | // Emit handler arguments and create handler function type. |
| 2768 | if (!StaticArgs.empty()) { |
| 2769 | llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); |
| 2770 | auto *InfoPtr = |
| 2771 | new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, |
| 2772 | llvm::GlobalVariable::PrivateLinkage, Info); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 2773 | InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2774 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); |
| 2775 | Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy)); |
| 2776 | ArgTypes.push_back(Int8PtrTy); |
| 2777 | } |
| 2778 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2779 | for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) { |
| 2780 | Args.push_back(EmitCheckValue(DynamicArgs[i])); |
| 2781 | ArgTypes.push_back(IntPtrTy); |
| 2782 | } |
| 2783 | |
| 2784 | llvm::FunctionType *FnType = |
| 2785 | llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false); |
Will Dietz | 88e0233 | 2012-12-02 19:50:33 +0000 | [diff] [blame] | 2786 | |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2787 | if (!FatalCond || !RecoverableCond) { |
| 2788 | // Simple case: we need to generate a single handler call, either |
| 2789 | // fatal, or non-fatal. |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2790 | emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2791 | (FatalCond != nullptr), Cont); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2792 | } else { |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2793 | // Emit two handler calls: first one for set of unrecoverable checks, |
| 2794 | // another one for recoverable. |
| 2795 | llvm::BasicBlock *NonFatalHandlerBB = |
| 2796 | createBasicBlock("non_fatal." + CheckName); |
| 2797 | llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName); |
| 2798 | Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB); |
| 2799 | EmitBlock(FatalHandlerBB); |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2800 | emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, true, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2801 | NonFatalHandlerBB); |
| 2802 | EmitBlock(NonFatalHandlerBB); |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2803 | emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, false, |
Alexey Samsonov | 8845952 | 2015-01-12 22:39:12 +0000 | [diff] [blame] | 2804 | Cont); |
Richard Smith | 4d3110a | 2012-10-25 02:14:12 +0000 | [diff] [blame] | 2805 | } |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 2806 | |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 2807 | EmitBlock(Cont); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2810 | void CodeGenFunction::EmitCfiSlowPathCheck( |
| 2811 | SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId, |
| 2812 | llvm::Value *Ptr, ArrayRef<llvm::Constant *> StaticArgs) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2813 | llvm::BasicBlock *Cont = createBasicBlock("cfi.cont"); |
| 2814 | |
| 2815 | llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath"); |
| 2816 | llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB); |
| 2817 | |
| 2818 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 2819 | llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); |
| 2820 | BI->setMetadata(llvm::LLVMContext::MD_prof, Node); |
| 2821 | |
| 2822 | EmitBlock(CheckBB); |
| 2823 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2824 | bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind); |
| 2825 | |
| 2826 | llvm::CallInst *CheckCall; |
| 2827 | if (WithDiag) { |
| 2828 | llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); |
| 2829 | auto *InfoPtr = |
| 2830 | new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, |
| 2831 | llvm::GlobalVariable::PrivateLinkage, Info); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 2832 | InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2833 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); |
| 2834 | |
| 2835 | llvm::Constant *SlowPathDiagFn = CGM.getModule().getOrInsertFunction( |
| 2836 | "__cfi_slowpath_diag", |
| 2837 | llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, |
| 2838 | false)); |
| 2839 | CheckCall = Builder.CreateCall( |
| 2840 | SlowPathDiagFn, |
| 2841 | {TypeId, Ptr, Builder.CreateBitCast(InfoPtr, Int8PtrTy)}); |
| 2842 | } else { |
| 2843 | llvm::Constant *SlowPathFn = CGM.getModule().getOrInsertFunction( |
| 2844 | "__cfi_slowpath", |
| 2845 | llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false)); |
| 2846 | CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr}); |
| 2847 | } |
| 2848 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2849 | CheckCall->setDoesNotThrow(); |
| 2850 | |
| 2851 | EmitBlock(Cont); |
| 2852 | } |
| 2853 | |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 2854 | // Emit a stub for __cfi_check function so that the linker knows about this |
| 2855 | // symbol in LTO mode. |
| 2856 | void CodeGenFunction::EmitCfiCheckStub() { |
| 2857 | llvm::Module *M = &CGM.getModule(); |
| 2858 | auto &Ctx = M->getContext(); |
| 2859 | llvm::Function *F = llvm::Function::Create( |
| 2860 | llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false), |
| 2861 | llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M); |
| 2862 | llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F); |
| 2863 | // FIXME: consider emitting an intrinsic call like |
| 2864 | // call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2) |
| 2865 | // which can be lowered in CrossDSOCFI pass to the actual contents of |
| 2866 | // __cfi_check. This would allow inlining of __cfi_check calls. |
| 2867 | llvm::CallInst::Create( |
| 2868 | llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB); |
| 2869 | llvm::ReturnInst::Create(Ctx, nullptr, BB); |
| 2870 | } |
| 2871 | |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2872 | // This function is basically a switch over the CFI failure kind, which is |
| 2873 | // extracted from CFICheckFailData (1st function argument). Each case is either |
| 2874 | // llvm.trap or a call to one of the two runtime handlers, based on |
| 2875 | // -fsanitize-trap and -fsanitize-recover settings. Default case (invalid |
| 2876 | // failure kind) traps, but this should really never happen. CFICheckFailData |
| 2877 | // can be nullptr if the calling module has -fsanitize-trap behavior for this |
| 2878 | // check kind; in this case __cfi_check_fail traps as well. |
| 2879 | void CodeGenFunction::EmitCfiCheckFail() { |
| 2880 | SanitizerScope SanScope(this); |
| 2881 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2882 | ImplicitParamDecl ArgData(getContext(), getContext().VoidPtrTy, |
| 2883 | ImplicitParamDecl::Other); |
| 2884 | ImplicitParamDecl ArgAddr(getContext(), getContext().VoidPtrTy, |
| 2885 | ImplicitParamDecl::Other); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2886 | Args.push_back(&ArgData); |
| 2887 | Args.push_back(&ArgAddr); |
| 2888 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2889 | const CGFunctionInfo &FI = |
| 2890 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, Args); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2891 | |
| 2892 | llvm::Function *F = llvm::Function::Create( |
| 2893 | llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false), |
| 2894 | llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule()); |
| 2895 | F->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 2896 | |
| 2897 | StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args, |
| 2898 | SourceLocation()); |
| 2899 | |
| 2900 | llvm::Value *Data = |
| 2901 | EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false, |
| 2902 | CGM.getContext().VoidPtrTy, ArgData.getLocation()); |
| 2903 | llvm::Value *Addr = |
| 2904 | EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false, |
| 2905 | CGM.getContext().VoidPtrTy, ArgAddr.getLocation()); |
| 2906 | |
| 2907 | // Data == nullptr means the calling module has trap behaviour for this check. |
| 2908 | llvm::Value *DataIsNotNullPtr = |
| 2909 | Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy)); |
| 2910 | EmitTrapCheck(DataIsNotNullPtr); |
| 2911 | |
| 2912 | llvm::StructType *SourceLocationTy = |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2913 | llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2914 | llvm::StructType *CfiCheckFailDataTy = |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2915 | llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2916 | |
| 2917 | llvm::Value *V = Builder.CreateConstGEP2_32( |
| 2918 | CfiCheckFailDataTy, |
| 2919 | Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0, |
| 2920 | 0); |
| 2921 | Address CheckKindAddr(V, getIntAlign()); |
| 2922 | llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr); |
| 2923 | |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2924 | llvm::Value *AllVtables = llvm::MetadataAsValue::get( |
| 2925 | CGM.getLLVMContext(), |
| 2926 | llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); |
| 2927 | llvm::Value *ValidVtable = Builder.CreateZExt( |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2928 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test), |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2929 | {Addr, AllVtables}), |
| 2930 | IntPtrTy); |
| 2931 | |
Evgeniy Stepanov | 4d3b087 | 2016-01-25 23:45:37 +0000 | [diff] [blame] | 2932 | const std::pair<int, SanitizerMask> CheckKinds[] = { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2933 | {CFITCK_VCall, SanitizerKind::CFIVCall}, |
| 2934 | {CFITCK_NVCall, SanitizerKind::CFINVCall}, |
| 2935 | {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast}, |
| 2936 | {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast}, |
| 2937 | {CFITCK_ICall, SanitizerKind::CFIICall}}; |
| 2938 | |
| 2939 | SmallVector<std::pair<llvm::Value *, SanitizerMask>, 5> Checks; |
| 2940 | for (auto CheckKindMaskPair : CheckKinds) { |
| 2941 | int Kind = CheckKindMaskPair.first; |
| 2942 | SanitizerMask Mask = CheckKindMaskPair.second; |
| 2943 | llvm::Value *Cond = |
| 2944 | Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind)); |
Evgeniy Stepanov | 02279ed | 2016-03-15 20:19:29 +0000 | [diff] [blame] | 2945 | if (CGM.getLangOpts().Sanitize.has(Mask)) |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2946 | EmitCheck(std::make_pair(Cond, Mask), SanitizerHandler::CFICheckFail, {}, |
Evgeniy Stepanov | 02279ed | 2016-03-15 20:19:29 +0000 | [diff] [blame] | 2947 | {Data, Addr, ValidVtable}); |
| 2948 | else |
| 2949 | EmitTrapCheck(Cond); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | FinishFunction(); |
| 2953 | // The only reference to this function will be created during LTO link. |
| 2954 | // Make sure it survives until then. |
| 2955 | CGM.addUsedGlobal(F); |
| 2956 | } |
| 2957 | |
Chad Rosier | ae229d5 | 2013-01-29 23:31:22 +0000 | [diff] [blame] | 2958 | void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) { |
Richard Smith | de67068 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2959 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 2960 | |
| 2961 | // If we're optimizing, collapse all calls to trap down to just one per |
| 2962 | // function to save on code size. |
| 2963 | if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) { |
| 2964 | TrapBB = createBasicBlock("trap"); |
| 2965 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2966 | EmitBlock(TrapBB); |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2967 | llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); |
Richard Smith | de67068 | 2012-11-01 22:15:34 +0000 | [diff] [blame] | 2968 | TrapCall->setDoesNotReturn(); |
| 2969 | TrapCall->setDoesNotThrow(); |
| 2970 | Builder.CreateUnreachable(); |
| 2971 | } else { |
| 2972 | Builder.CreateCondBr(Checked, Cont, TrapBB); |
| 2973 | } |
| 2974 | |
| 2975 | EmitBlock(Cont); |
| 2976 | } |
| 2977 | |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2978 | llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) { |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 2979 | llvm::CallInst *TrapCall = Builder.CreateCall(CGM.getIntrinsic(IntrID)); |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2980 | |
Amaury Sechet | 21f51b3 | 2016-09-09 04:42:49 +0000 | [diff] [blame] | 2981 | if (!CGM.getCodeGenOpts().TrapFuncName.empty()) { |
| 2982 | auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name", |
| 2983 | CGM.getCodeGenOpts().TrapFuncName); |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2984 | TrapCall->addAttribute(llvm::AttributeList::FunctionIndex, A); |
Amaury Sechet | 21f51b3 | 2016-09-09 04:42:49 +0000 | [diff] [blame] | 2985 | } |
Akira Hatanaka | 85365cd | 2015-07-02 22:15:41 +0000 | [diff] [blame] | 2986 | |
| 2987 | return TrapCall; |
| 2988 | } |
| 2989 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2990 | Address CodeGenFunction::EmitArrayToPointerDecay(const Expr *E, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2991 | LValueBaseInfo *BaseInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2992 | assert(E->getType()->isArrayType() && |
| 2993 | "Array to pointer decay must have array source type!"); |
| 2994 | |
| 2995 | // Expressions of array type can't be bitfields or vector elements. |
| 2996 | LValue LV = EmitLValue(E); |
| 2997 | Address Addr = LV.getAddress(); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 2998 | if (BaseInfo) *BaseInfo = LV.getBaseInfo(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2999 | |
| 3000 | // If the array type was an incomplete type, we need to make sure |
| 3001 | // the decay ends up being the right type. |
| 3002 | llvm::Type *NewTy = ConvertType(E->getType()); |
| 3003 | Addr = Builder.CreateElementBitCast(Addr, NewTy); |
| 3004 | |
| 3005 | // Note that VLA pointers are always decayed, so we don't need to do |
| 3006 | // anything here. |
| 3007 | if (!E->getType()->isVariableArrayType()) { |
| 3008 | assert(isa<llvm::ArrayType>(Addr.getElementType()) && |
| 3009 | "Expected pointer to array"); |
| 3010 | Addr = Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), "arraydecay"); |
| 3011 | } |
| 3012 | |
| 3013 | QualType EltType = E->getType()->castAsArrayTypeUnsafe()->getElementType(); |
| 3014 | return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType)); |
| 3015 | } |
| 3016 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 3017 | /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an |
| 3018 | /// array to pointer, return the array subexpression. |
| 3019 | static const Expr *isSimpleArrayDecayOperand(const Expr *E) { |
| 3020 | // If this isn't just an array->pointer decay, bail out. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3021 | const auto *CE = dyn_cast<CastExpr>(E); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3022 | if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 3023 | return nullptr; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3024 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 3025 | // If this is a decay from variable width array, bail out. |
| 3026 | const Expr *SubExpr = CE->getSubExpr(); |
| 3027 | if (SubExpr->getType()->isVariableArrayType()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3028 | return nullptr; |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3029 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 3030 | return SubExpr; |
| 3031 | } |
| 3032 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3033 | static llvm::Value *emitArraySubscriptGEP(CodeGenFunction &CGF, |
| 3034 | llvm::Value *ptr, |
| 3035 | ArrayRef<llvm::Value*> indices, |
| 3036 | bool inbounds, |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3037 | bool signedIndices, |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3038 | SourceLocation loc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3039 | const llvm::Twine &name = "arrayidx") { |
| 3040 | if (inbounds) { |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3041 | return CGF.EmitCheckedInBoundsGEP(ptr, indices, signedIndices, loc, name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3042 | } else { |
| 3043 | return CGF.Builder.CreateGEP(ptr, indices, name); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | static CharUnits getArrayElementAlign(CharUnits arrayAlign, |
| 3048 | llvm::Value *idx, |
| 3049 | CharUnits eltSize) { |
| 3050 | // If we have a constant index, we can use the exact offset of the |
| 3051 | // element we're accessing. |
| 3052 | if (auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) { |
| 3053 | CharUnits offset = constantIdx->getZExtValue() * eltSize; |
| 3054 | return arrayAlign.alignmentAtOffset(offset); |
| 3055 | |
| 3056 | // Otherwise, use the worst-case alignment for any element. |
| 3057 | } else { |
| 3058 | return arrayAlign.alignmentOfArrayElement(eltSize); |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | static QualType getFixedSizeElementType(const ASTContext &ctx, |
| 3063 | const VariableArrayType *vla) { |
| 3064 | QualType eltType; |
| 3065 | do { |
| 3066 | eltType = vla->getElementType(); |
| 3067 | } while ((vla = ctx.getAsVariableArrayType(eltType))); |
| 3068 | return eltType; |
| 3069 | } |
| 3070 | |
| 3071 | static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr, |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3072 | ArrayRef<llvm::Value *> indices, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3073 | QualType eltType, bool inbounds, |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3074 | bool signedIndices, SourceLocation loc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3075 | const llvm::Twine &name = "arrayidx") { |
| 3076 | // All the indices except that last must be zero. |
| 3077 | #ifndef NDEBUG |
| 3078 | for (auto idx : indices.drop_back()) |
| 3079 | assert(isa<llvm::ConstantInt>(idx) && |
| 3080 | cast<llvm::ConstantInt>(idx)->isZero()); |
| 3081 | #endif |
| 3082 | |
| 3083 | // Determine the element size of the statically-sized base. This is |
| 3084 | // the thing that the indices are expressed in terms of. |
| 3085 | if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) { |
| 3086 | eltType = getFixedSizeElementType(CGF.getContext(), vla); |
| 3087 | } |
| 3088 | |
| 3089 | // We can use that to compute the best alignment of the element. |
| 3090 | CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType); |
| 3091 | CharUnits eltAlign = |
| 3092 | getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize); |
| 3093 | |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3094 | llvm::Value *eltPtr = emitArraySubscriptGEP( |
| 3095 | CGF, addr.getPointer(), indices, inbounds, signedIndices, loc, name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3096 | return Address(eltPtr, eltAlign); |
| 3097 | } |
| 3098 | |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 3099 | LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E, |
| 3100 | bool Accessed) { |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3101 | // The index must always be an integer, which is not an aggregate. Emit it |
| 3102 | // in lexical order (this complexity is, sadly, required by C++17). |
| 3103 | llvm::Value *IdxPre = |
| 3104 | (E->getLHS() == E->getIdx()) ? EmitScalarExpr(E->getIdx()) : nullptr; |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3105 | bool SignedIndices = false; |
Richard Smith | 4088571 | 2016-09-27 00:53:24 +0000 | [diff] [blame] | 3106 | auto EmitIdxAfterBase = [&, IdxPre](bool Promote) -> llvm::Value * { |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3107 | auto *Idx = IdxPre; |
| 3108 | if (E->getLHS() != E->getIdx()) { |
| 3109 | assert(E->getRHS() == E->getIdx() && "index was neither LHS nor RHS"); |
| 3110 | Idx = EmitScalarExpr(E->getIdx()); |
| 3111 | } |
Eli Friedman | 07bbeca | 2009-06-06 19:09:26 +0000 | [diff] [blame] | 3112 | |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3113 | QualType IdxTy = E->getIdx()->getType(); |
| 3114 | bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType(); |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3115 | SignedIndices |= IdxSigned; |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3116 | |
| 3117 | if (SanOpts.has(SanitizerKind::ArrayBounds)) |
| 3118 | EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed); |
| 3119 | |
| 3120 | // Extend or truncate the index type to 32 or 64-bits. |
| 3121 | if (Promote && Idx->getType() != IntPtrTy) |
| 3122 | Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom"); |
| 3123 | |
| 3124 | return Idx; |
| 3125 | }; |
| 3126 | IdxPre = nullptr; |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 3128 | // If the base is a vector type, then we are forming a vector element lvalue |
| 3129 | // with this subscript. |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 3130 | if (E->getBase()->getType()->isVectorType() && |
| 3131 | !isa<ExtVectorElementExpr>(E->getBase())) { |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 3132 | // Emit the vector as an lvalue to get its address. |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 3133 | LValue LHS = EmitLValue(E->getBase()); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3134 | auto *Idx = EmitIdxAfterBase(/*Promote*/false); |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 3135 | assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); |
Eli Friedman | 327944b | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 3136 | return LValue::MakeVectorElt(LHS.getAddress(), Idx, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3137 | E->getBase()->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3138 | LHS.getBaseInfo()); |
Chris Lattner | 08c4b9f | 2007-07-10 21:17:59 +0000 | [diff] [blame] | 3139 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3140 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3141 | // All the other cases basically behave like simple offsetting. |
| 3142 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3143 | // Handle the extvector case we ignored above. |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 3144 | if (isa<ExtVectorElementExpr>(E->getBase())) { |
| 3145 | LValue LV = EmitLValue(E->getBase()); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3146 | auto *Idx = EmitIdxAfterBase(/*Promote*/true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3147 | Address Addr = EmitExtVectorElementLValue(LV); |
| 3148 | |
| 3149 | QualType EltType = LV.getType()->castAs<VectorType>()->getElementType(); |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3150 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true, |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3151 | SignedIndices, E->getExprLoc()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3152 | return MakeAddrLValue(Addr, EltType, LV.getBaseInfo()); |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 3153 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3154 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3155 | LValueBaseInfo BaseInfo; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3156 | Address Addr = Address::invalid(); |
| 3157 | if (const VariableArrayType *vla = |
Fariborz Jahanian | 91b2fa2 | 2014-08-19 17:17:40 +0000 | [diff] [blame] | 3158 | getContext().getAsVariableArrayType(E->getType())) { |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 3159 | // The base must be a pointer, which is not an aggregate. Emit |
| 3160 | // it. It needs to be emitted first in case it's what captures |
| 3161 | // the VLA bounds. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3162 | Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3163 | auto *Idx = EmitIdxAfterBase(/*Promote*/true); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3164 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 3165 | // The element count here is the total number of non-VLA elements. |
| 3166 | llvm::Value *numElements = getVLASize(vla).first; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3167 | |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 3168 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 3169 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 3170 | // signed-overflow, so we use the same semantics for our explicit |
| 3171 | // multiply. We suppress this if overflow is not undefined behavior. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3172 | if (getLangOpts().isSignedOverflowDefined()) { |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 3173 | Idx = Builder.CreateMul(Idx, numElements); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 3174 | } else { |
| 3175 | Idx = Builder.CreateNSWMul(Idx, numElements); |
John McCall | 77527a8 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 3176 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3177 | |
| 3178 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(), |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3179 | !getLangOpts().isSignedOverflowDefined(), |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3180 | SignedIndices, E->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3181 | |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 3182 | } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ |
| 3183 | // Indexing over an interface, as in "NSString *P; P[4];" |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 3184 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3185 | // Emit the base pointer. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3186 | Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3187 | auto *Idx = EmitIdxAfterBase(/*Promote*/true); |
| 3188 | |
| 3189 | CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT); |
| 3190 | llvm::Value *InterfaceSizeVal = |
| 3191 | llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity()); |
| 3192 | |
| 3193 | llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3194 | |
| 3195 | // We don't necessarily build correct LLVM struct types for ObjC |
| 3196 | // interfaces, so we can't rely on GEP to do this scaling |
| 3197 | // correctly, so we need to cast to i8*. FIXME: is this actually |
| 3198 | // true? A lot of other things in the fragile ABI would break... |
| 3199 | llvm::Type *OrigBaseTy = Addr.getType(); |
| 3200 | Addr = Builder.CreateElementBitCast(Addr, Int8Ty); |
| 3201 | |
| 3202 | // Do the GEP. |
| 3203 | CharUnits EltAlign = |
| 3204 | getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize); |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3205 | llvm::Value *EltPtr = |
| 3206 | emitArraySubscriptGEP(*this, Addr.getPointer(), ScaledIdx, false, |
| 3207 | SignedIndices, E->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3208 | Addr = Address(EltPtr, EltAlign); |
| 3209 | |
| 3210 | // Cast back. |
| 3211 | Addr = Builder.CreateBitCast(Addr, OrigBaseTy); |
Chris Lattner | 6c5abe8 | 2010-06-26 23:03:20 +0000 | [diff] [blame] | 3212 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 3213 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 3214 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 3215 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 3216 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 3217 | assert(Array->getType()->isArrayType() && |
| 3218 | "Array to pointer decay must have array source type!"); |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 3219 | LValue ArrayLV; |
| 3220 | // For simple multidimensional array indexing, set the 'accessed' flag for |
| 3221 | // better bounds-checking of the base expression. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3222 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) |
Richard Smith | 539e4a7 | 2013-02-23 02:53:19 +0000 | [diff] [blame] | 3223 | ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); |
| 3224 | else |
| 3225 | ArrayLV = EmitLValue(Array); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3226 | auto *Idx = EmitIdxAfterBase(/*Promote*/true); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3227 | |
Daniel Dunbar | 8263427 | 2011-04-01 00:49:43 +0000 | [diff] [blame] | 3228 | // Propagate the alignment from the array itself to the result. |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3229 | Addr = emitArraySubscriptGEP( |
| 3230 | *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx}, |
| 3231 | E->getType(), !getLangOpts().isSignedOverflowDefined(), SignedIndices, |
| 3232 | E->getExprLoc()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3233 | BaseInfo = ArrayLV.getBaseInfo(); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 3234 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3235 | // The base must be a pointer; emit it with an estimate of its alignment. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3236 | Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo); |
Richard Smith | 9e67b99 | 2016-09-26 23:49:47 +0000 | [diff] [blame] | 3237 | auto *Idx = EmitIdxAfterBase(/*Promote*/true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3238 | Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(), |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3239 | !getLangOpts().isSignedOverflowDefined(), |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3240 | SignedIndices, E->getExprLoc()); |
Anders Carlsson | 3d312f8 | 2008-12-21 00:11:23 +0000 | [diff] [blame] | 3241 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3242 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3243 | LValue LV = MakeAddrLValue(Addr, E->getType(), BaseInfo); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3244 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3245 | // TODO: Preserve/extend path TBAA metadata? |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3246 | |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 3247 | if (getLangOpts().ObjC1 && |
| 3248 | getLangOpts().getGC() != LangOptions::NonGC) { |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 3249 | LV.setNonGC(!E->isOBJCGCCandidate(getContext())); |
Fariborz Jahanian | a7fa6be | 2009-09-16 21:37:16 +0000 | [diff] [blame] | 3250 | setObjCGCLValueClass(getContext(), E, LV); |
| 3251 | } |
Fariborz Jahanian | a9fecf3 | 2009-02-21 23:37:19 +0000 | [diff] [blame] | 3252 | return LV; |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3255 | static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3256 | LValueBaseInfo &BaseInfo, |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3257 | QualType BaseTy, QualType ElTy, |
| 3258 | bool IsLowerBound) { |
| 3259 | LValue BaseLVal; |
| 3260 | if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts())) { |
| 3261 | BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound); |
| 3262 | if (BaseTy->isArrayType()) { |
| 3263 | Address Addr = BaseLVal.getAddress(); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3264 | BaseInfo = BaseLVal.getBaseInfo(); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3265 | |
| 3266 | // If the array type was an incomplete type, we need to make sure |
| 3267 | // the decay ends up being the right type. |
| 3268 | llvm::Type *NewTy = CGF.ConvertType(BaseTy); |
| 3269 | Addr = CGF.Builder.CreateElementBitCast(Addr, NewTy); |
| 3270 | |
| 3271 | // Note that VLA pointers are always decayed, so we don't need to do |
| 3272 | // anything here. |
| 3273 | if (!BaseTy->isVariableArrayType()) { |
| 3274 | assert(isa<llvm::ArrayType>(Addr.getElementType()) && |
| 3275 | "Expected pointer to array"); |
| 3276 | Addr = CGF.Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), |
| 3277 | "arraydecay"); |
| 3278 | } |
| 3279 | |
| 3280 | return CGF.Builder.CreateElementBitCast(Addr, |
| 3281 | CGF.ConvertTypeForMem(ElTy)); |
| 3282 | } |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3283 | LValueBaseInfo TypeInfo; |
| 3284 | CharUnits Align = CGF.getNaturalTypeAlignment(ElTy, &TypeInfo); |
| 3285 | BaseInfo.mergeForCast(TypeInfo); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3286 | return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()), Align); |
| 3287 | } |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3288 | return CGF.EmitPointerWithAlignment(Base, &BaseInfo); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3291 | LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, |
| 3292 | bool IsLowerBound) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3293 | QualType BaseTy; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3294 | if (auto *ASE = |
| 3295 | dyn_cast<OMPArraySectionExpr>(E->getBase()->IgnoreParenImpCasts())) |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3296 | BaseTy = OMPArraySectionExpr::getBaseOriginalType(ASE); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3297 | else |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3298 | BaseTy = E->getBase()->getType(); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3299 | QualType ResultExprTy; |
| 3300 | if (auto *AT = getContext().getAsArrayType(BaseTy)) |
| 3301 | ResultExprTy = AT->getElementType(); |
| 3302 | else |
| 3303 | ResultExprTy = BaseTy->getPointeeType(); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3304 | llvm::Value *Idx = nullptr; |
Benjamin Kramer | 5ff6747 | 2016-04-11 08:26:13 +0000 | [diff] [blame] | 3305 | if (IsLowerBound || E->getColonLoc().isInvalid()) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3306 | // Requesting lower bound or upper bound, but without provided length and |
| 3307 | // without ':' symbol for the default length -> length = 1. |
| 3308 | // Idx = LowerBound ?: 0; |
| 3309 | if (auto *LowerBound = E->getLowerBound()) { |
| 3310 | Idx = Builder.CreateIntCast( |
| 3311 | EmitScalarExpr(LowerBound), IntPtrTy, |
| 3312 | LowerBound->getType()->hasSignedIntegerRepresentation()); |
| 3313 | } else |
| 3314 | Idx = llvm::ConstantInt::getNullValue(IntPtrTy); |
| 3315 | } else { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3316 | // Try to emit length or lower bound as constant. If this is possible, 1 |
| 3317 | // is subtracted from constant length or lower bound. Otherwise, emit LLVM |
| 3318 | // IR (LB + Len) - 1. |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3319 | auto &C = CGM.getContext(); |
| 3320 | auto *Length = E->getLength(); |
| 3321 | llvm::APSInt ConstLength; |
| 3322 | if (Length) { |
| 3323 | // Idx = LowerBound + Length - 1; |
| 3324 | if (Length->isIntegerConstantExpr(ConstLength, C)) { |
| 3325 | ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); |
| 3326 | Length = nullptr; |
| 3327 | } |
| 3328 | auto *LowerBound = E->getLowerBound(); |
| 3329 | llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false); |
| 3330 | if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) { |
| 3331 | ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits); |
| 3332 | LowerBound = nullptr; |
| 3333 | } |
| 3334 | if (!Length) |
| 3335 | --ConstLength; |
| 3336 | else if (!LowerBound) |
| 3337 | --ConstLowerBound; |
| 3338 | |
| 3339 | if (Length || LowerBound) { |
| 3340 | auto *LowerBoundVal = |
| 3341 | LowerBound |
| 3342 | ? Builder.CreateIntCast( |
| 3343 | EmitScalarExpr(LowerBound), IntPtrTy, |
| 3344 | LowerBound->getType()->hasSignedIntegerRepresentation()) |
| 3345 | : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound); |
| 3346 | auto *LengthVal = |
| 3347 | Length |
| 3348 | ? Builder.CreateIntCast( |
| 3349 | EmitScalarExpr(Length), IntPtrTy, |
| 3350 | Length->getType()->hasSignedIntegerRepresentation()) |
| 3351 | : llvm::ConstantInt::get(IntPtrTy, ConstLength); |
| 3352 | Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len", |
| 3353 | /*HasNUW=*/false, |
| 3354 | !getLangOpts().isSignedOverflowDefined()); |
| 3355 | if (Length && LowerBound) { |
| 3356 | Idx = Builder.CreateSub( |
| 3357 | Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1", |
| 3358 | /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); |
| 3359 | } |
| 3360 | } else |
| 3361 | Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound); |
| 3362 | } else { |
| 3363 | // Idx = ArraySize - 1; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3364 | QualType ArrayTy = BaseTy->isPointerType() |
| 3365 | ? E->getBase()->IgnoreParenImpCasts()->getType() |
| 3366 | : BaseTy; |
| 3367 | if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3368 | Length = VAT->getSizeExpr(); |
| 3369 | if (Length->isIntegerConstantExpr(ConstLength, C)) |
| 3370 | Length = nullptr; |
| 3371 | } else { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3372 | auto *CAT = C.getAsConstantArrayType(ArrayTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3373 | ConstLength = CAT->getSize(); |
| 3374 | } |
| 3375 | if (Length) { |
| 3376 | auto *LengthVal = Builder.CreateIntCast( |
| 3377 | EmitScalarExpr(Length), IntPtrTy, |
| 3378 | Length->getType()->hasSignedIntegerRepresentation()); |
| 3379 | Idx = Builder.CreateSub( |
| 3380 | LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1", |
| 3381 | /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); |
| 3382 | } else { |
| 3383 | ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); |
| 3384 | --ConstLength; |
| 3385 | Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength); |
| 3386 | } |
| 3387 | } |
| 3388 | } |
| 3389 | assert(Idx); |
| 3390 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3391 | Address EltPtr = Address::invalid(); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3392 | LValueBaseInfo BaseInfo; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3393 | if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3394 | // The base must be a pointer, which is not an aggregate. Emit |
| 3395 | // it. It needs to be emitted first in case it's what captures |
| 3396 | // the VLA bounds. |
| 3397 | Address Base = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3398 | emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, BaseTy, |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3399 | VLA->getElementType(), IsLowerBound); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3400 | // The element count here is the total number of non-VLA elements. |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3401 | llvm::Value *NumElements = getVLASize(VLA).first; |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3402 | |
| 3403 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 3404 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 3405 | // signed-overflow, so we use the same semantics for our explicit |
| 3406 | // multiply. We suppress this if overflow is not undefined behavior. |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3407 | if (getLangOpts().isSignedOverflowDefined()) |
| 3408 | Idx = Builder.CreateMul(Idx, NumElements); |
| 3409 | else |
| 3410 | Idx = Builder.CreateNSWMul(Idx, NumElements); |
| 3411 | EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(), |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3412 | !getLangOpts().isSignedOverflowDefined(), |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3413 | /*SignedIndices=*/false, E->getExprLoc()); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3414 | } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { |
| 3415 | // If this is A[i] where A is an array, the frontend will have decayed the |
| 3416 | // base to be a ArrayToPointerDecay implicit cast. While correct, it is |
| 3417 | // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a |
| 3418 | // "gep x, i" here. Emit one "gep A, 0, i". |
| 3419 | assert(Array->getType()->isArrayType() && |
| 3420 | "Array to pointer decay must have array source type!"); |
| 3421 | LValue ArrayLV; |
| 3422 | // For simple multidimensional array indexing, set the 'accessed' flag for |
| 3423 | // better bounds-checking of the base expression. |
| 3424 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) |
| 3425 | ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); |
| 3426 | else |
| 3427 | ArrayLV = EmitLValue(Array); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3428 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3429 | // Propagate the alignment from the array itself to the result. |
| 3430 | EltPtr = emitArraySubscriptGEP( |
| 3431 | *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx}, |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3432 | ResultExprTy, !getLangOpts().isSignedOverflowDefined(), |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3433 | /*SignedIndices=*/false, E->getExprLoc()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3434 | BaseInfo = ArrayLV.getBaseInfo(); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3435 | } else { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3436 | Address Base = emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3437 | BaseTy, ResultExprTy, IsLowerBound); |
| 3438 | EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy, |
Vedant Kumar | a125eb5 | 2017-06-01 19:22:18 +0000 | [diff] [blame] | 3439 | !getLangOpts().isSignedOverflowDefined(), |
Vedant Kumar | 6dbf427 | 2017-06-12 18:42:51 +0000 | [diff] [blame] | 3440 | /*SignedIndices=*/false, E->getExprLoc()); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3443 | return MakeAddrLValue(EltPtr, ResultExprTy, BaseInfo); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3446 | LValue CodeGenFunction:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3447 | EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3448 | // Emit the base vector as an l-value. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3449 | LValue Base; |
| 3450 | |
| 3451 | // ExtVectorElementExpr's base can either be a vector or pointer to vector. |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3452 | if (E->isArrow()) { |
| 3453 | // If it is a pointer to a vector, emit the address and form an lvalue with |
| 3454 | // it. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3455 | LValueBaseInfo BaseInfo; |
| 3456 | Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3457 | const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3458 | Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo); |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3459 | Base.getQuals().removeObjCGCAttr(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3460 | } else if (E->getBase()->isGLValue()) { |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3461 | // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), |
| 3462 | // emit the base as an lvalue. |
| 3463 | assert(E->getBase()->getType()->isVectorType()); |
| 3464 | Base = EmitLValue(E->getBase()); |
| 3465 | } else { |
| 3466 | // 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] | 3467 | assert(E->getBase()->getType()->isVectorType() && |
Daniel Dunbar | 5b90195 | 2010-01-04 18:02:28 +0000 | [diff] [blame] | 3468 | "Result must be a vector"); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3469 | llvm::Value *Vec = EmitScalarExpr(E->getBase()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | f0a9ba3 | 2009-12-23 21:33:41 +0000 | [diff] [blame] | 3471 | // Store the vector to memory (because LValue wants an address). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3472 | Address VecMem = CreateMemTemp(E->getBase()->getType()); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3473 | Builder.CreateStore(Vec, VecMem); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3474 | Base = MakeAddrLValue(VecMem, E->getBase()->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3475 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Chris Lattner | 4e1a323 | 2009-12-23 21:31:11 +0000 | [diff] [blame] | 3476 | } |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 3477 | |
| 3478 | QualType type = |
| 3479 | E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3480 | |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3481 | // Encode the element access list into a vector of unsigned indices. |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 3482 | SmallVector<uint32_t, 4> Indices; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3483 | E->getEncodedElementAccess(Indices); |
| 3484 | |
| 3485 | if (Base.isSimple()) { |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 3486 | llvm::Constant *CV = |
| 3487 | llvm::ConstantDataVector::get(getLLVMContext(), Indices); |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 3488 | return LValue::MakeExtVectorElt(Base.getAddress(), CV, type, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3489 | Base.getBaseInfo()); |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3490 | } |
| 3491 | assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); |
| 3492 | |
| 3493 | llvm::Constant *BaseElts = Base.getExtVectorElts(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3494 | SmallVector<llvm::Constant *, 4> CElts; |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3495 | |
Chris Lattner | 595ba3a | 2012-01-30 06:20:36 +0000 | [diff] [blame] | 3496 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 3497 | CElts.push_back(BaseElts->getAggregateElement(Indices[i])); |
Chris Lattner | 91c08ad | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 3498 | llvm::Constant *CV = llvm::ConstantVector::get(CElts); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3499 | return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type, |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3500 | Base.getBaseInfo()); |
Chris Lattner | 9e751ca | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3503 | LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { |
Devang Patel | d68df20 | 2007-10-24 22:26:28 +0000 | [diff] [blame] | 3504 | Expr *BaseExpr = E->getBase(); |
Chris Lattner | 4e4186b | 2007-12-02 18:52:07 +0000 | [diff] [blame] | 3505 | // 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] | 3506 | LValue BaseLV; |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3507 | if (E->isArrow()) { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3508 | LValueBaseInfo BaseInfo; |
| 3509 | Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3510 | QualType PtrTy = BaseExpr->getType()->getPointeeType(); |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 3511 | SanitizerSet SkippedChecks; |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 3512 | bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr); |
| 3513 | if (IsBaseCXXThis) |
| 3514 | SkippedChecks.set(SanitizerKind::Alignment, true); |
| 3515 | if (IsBaseCXXThis || isa<DeclRefExpr>(BaseExpr)) |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 3516 | SkippedChecks.set(SanitizerKind::Null, true); |
| 3517 | EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Addr.getPointer(), PtrTy, |
| 3518 | /*Alignment=*/CharUnits::Zero(), SkippedChecks); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3519 | BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 3520 | } else |
Richard Smith | 4d1458e | 2012-09-08 02:08:36 +0000 | [diff] [blame] | 3521 | BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess); |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3522 | |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 3523 | NamedDecl *ND = E->getMemberDecl(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3524 | if (auto *Field = dyn_cast<FieldDecl>(ND)) { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3525 | LValue LV = EmitLValueForField(BaseLV, Field); |
Anders Carlsson | ea4c30b | 2009-11-07 23:06:58 +0000 | [diff] [blame] | 3526 | setObjCGCLValueClass(getContext(), E, LV); |
| 3527 | return LV; |
| 3528 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3529 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3530 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
Anders Carlsson | 5bbdc9f | 2009-11-07 23:16:50 +0000 | [diff] [blame] | 3531 | return EmitGlobalVarDeclLValue(*this, E, VD); |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 3532 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3533 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
Eli Friedman | d15eb34d | 2009-11-26 06:08:14 +0000 | [diff] [blame] | 3534 | return EmitFunctionDeclLValue(*this, E, FD); |
| 3535 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3536 | llvm_unreachable("Unhandled member declaration!"); |
Eli Friedman | a62f3e1 | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 3537 | } |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3538 | |
John McCall | dec348f7 | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 3539 | /// Given that we are currently emitting a lambda, emit an l-value for |
| 3540 | /// one of its members. |
| 3541 | LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) { |
| 3542 | assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda()); |
| 3543 | assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent()); |
| 3544 | QualType LambdaTagType = |
| 3545 | getContext().getTagDeclType(Field->getParent()); |
| 3546 | LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType); |
| 3547 | return EmitLValueForField(LambdaLV, Field); |
| 3548 | } |
| 3549 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3550 | /// Drill down to the storage of a field without walking into |
| 3551 | /// reference types. |
| 3552 | /// |
| 3553 | /// The resulting address doesn't necessarily have the right type. |
| 3554 | static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, |
| 3555 | const FieldDecl *field) { |
| 3556 | const RecordDecl *rec = field->getParent(); |
| 3557 | |
| 3558 | unsigned idx = |
| 3559 | CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); |
| 3560 | |
| 3561 | CharUnits offset; |
| 3562 | // Adjust the alignment down to the given offset. |
| 3563 | // As a special case, if the LLVM field index is 0, we know that this |
| 3564 | // is zero. |
| 3565 | assert((idx != 0 || CGF.getContext().getASTRecordLayout(rec) |
| 3566 | .getFieldOffset(field->getFieldIndex()) == 0) && |
| 3567 | "LLVM field at index zero had non-zero offset?"); |
| 3568 | if (idx != 0) { |
| 3569 | auto &recLayout = CGF.getContext().getASTRecordLayout(rec); |
| 3570 | auto offsetInBits = recLayout.getFieldOffset(field->getFieldIndex()); |
| 3571 | offset = CGF.getContext().toCharUnitsFromBits(offsetInBits); |
| 3572 | } |
| 3573 | |
| 3574 | return CGF.Builder.CreateStructGEP(base, idx, offset, field->getName()); |
| 3575 | } |
| 3576 | |
Piotr Padlewski | c1d2606 | 2017-06-01 18:39:34 +0000 | [diff] [blame] | 3577 | static bool hasAnyVptr(const QualType Type, const ASTContext &Context) { |
| 3578 | const auto *RD = Type.getTypePtr()->getAsCXXRecordDecl(); |
| 3579 | if (!RD) |
| 3580 | return false; |
| 3581 | |
| 3582 | if (RD->isDynamicClass()) |
| 3583 | return true; |
| 3584 | |
| 3585 | for (const auto &Base : RD->bases()) |
| 3586 | if (hasAnyVptr(Base.getType(), Context)) |
| 3587 | return true; |
| 3588 | |
| 3589 | for (const FieldDecl *Field : RD->fields()) |
| 3590 | if (hasAnyVptr(Field->getType(), Context)) |
| 3591 | return true; |
| 3592 | |
| 3593 | return false; |
| 3594 | } |
| 3595 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3596 | LValue CodeGenFunction::EmitLValueForField(LValue base, |
| 3597 | const FieldDecl *field) { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3598 | LValueBaseInfo BaseInfo = base.getBaseInfo(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3599 | AlignmentSource fieldAlignSource = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3600 | getFieldAlignmentSource(BaseInfo.getAlignmentSource()); |
| 3601 | LValueBaseInfo FieldBaseInfo(fieldAlignSource, BaseInfo.getMayAlias()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3602 | |
Krzysztof Parzyszek | 5960a57 | 2017-05-25 12:55:47 +0000 | [diff] [blame] | 3603 | const RecordDecl *rec = field->getParent(); |
| 3604 | if (rec->isUnion() || rec->hasAttr<MayAliasAttr>()) |
| 3605 | FieldBaseInfo.setMayAlias(true); |
| 3606 | bool mayAlias = FieldBaseInfo.getMayAlias(); |
| 3607 | |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3608 | if (field->isBitField()) { |
| 3609 | const CGRecordLayout &RL = |
| 3610 | CGM.getTypes().getCGRecordLayout(field->getParent()); |
| 3611 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(field); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3612 | Address Addr = base.getAddress(); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3613 | unsigned Idx = RL.getLLVMFieldNo(field); |
| 3614 | if (Idx != 0) |
| 3615 | // For structs, we GEP to the field that the record layout suggests. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3616 | Addr = Builder.CreateStructGEP(Addr, Idx, Info.StorageOffset, |
| 3617 | field->getName()); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3618 | // Get the access type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3619 | llvm::Type *FieldIntTy = |
| 3620 | llvm::Type::getIntNTy(getLLVMContext(), Info.StorageSize); |
| 3621 | if (Addr.getElementType() != FieldIntTy) |
| 3622 | Addr = Builder.CreateElementBitCast(Addr, FieldIntTy); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 3623 | |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3624 | QualType fieldType = |
| 3625 | field->getType().withCVRQualifiers(base.getVRQualifiers()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3626 | return LValue::MakeBitfield(Addr, Info, fieldType, FieldBaseInfo); |
Eli Friedman | c24e2fb | 2012-06-27 21:19:48 +0000 | [diff] [blame] | 3627 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 3628 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3629 | QualType type = field->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3630 | Address addr = base.getAddress(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3631 | unsigned cvr = base.getVRQualifiers(); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3632 | bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3633 | if (rec->isUnion()) { |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 3634 | // For unions, there is no pointer adjustment. |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3635 | assert(!type->isReferenceType() && "union has reference member"); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3636 | // TODO: handle path-aware TBAA for union. |
| 3637 | TBAAPath = false; |
Piotr Padlewski | c1d2606 | 2017-06-01 18:39:34 +0000 | [diff] [blame] | 3638 | |
| 3639 | const auto FieldType = field->getType(); |
| 3640 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
| 3641 | hasAnyVptr(FieldType, getContext())) |
| 3642 | // Because unions can easily skip invariant.barriers, we need to add |
| 3643 | // a barrier every time CXXRecord field with vptr is referenced. |
| 3644 | addr = Address(Builder.CreateInvariantGroupBarrier(addr.getPointer()), |
| 3645 | addr.getAlignment()); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3646 | } else { |
| 3647 | // For structs, we GEP to the field that the record layout suggests. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3648 | addr = emitAddrOfFieldStorage(*this, addr, field); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3649 | |
| 3650 | // If this is a reference field, load the reference right now. |
| 3651 | if (const ReferenceType *refType = type->getAs<ReferenceType>()) { |
| 3652 | llvm::LoadInst *load = Builder.CreateLoad(addr, "ref"); |
| 3653 | if (cvr & Qualifiers::Volatile) load->setVolatile(true); |
| 3654 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3655 | // Loading the reference will disable path-aware TBAA. |
| 3656 | TBAAPath = false; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3657 | if (CGM.shouldUseTBAA()) { |
| 3658 | llvm::MDNode *tbaa; |
| 3659 | if (mayAlias) |
| 3660 | tbaa = CGM.getTBAAInfo(getContext().CharTy); |
| 3661 | else |
| 3662 | tbaa = CGM.getTBAAInfo(type); |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 3663 | if (tbaa) |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 3664 | CGM.DecorateInstructionWithTBAA(load, tbaa); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3667 | mayAlias = false; |
| 3668 | type = refType->getPointeeType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3669 | |
| 3670 | CharUnits alignment = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3671 | getNaturalTypeAlignment(type, &FieldBaseInfo, /*pointee*/ true); |
| 3672 | FieldBaseInfo.setMayAlias(false); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3673 | addr = Address(load, alignment); |
| 3674 | |
| 3675 | // Qualifiers on the struct don't apply to the referencee, and |
| 3676 | // we'll pick up CVR from the actual type later, so reset these |
| 3677 | // additional qualifiers now. |
| 3678 | cvr = 0; |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3679 | } |
Devang Patel | ed93c3c | 2007-10-26 19:42:18 +0000 | [diff] [blame] | 3680 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3681 | |
Chris Lattner | 13ee4f4 | 2011-07-10 05:34:54 +0000 | [diff] [blame] | 3682 | // Make sure that the address is pointing to the right type. This is critical |
| 3683 | // for both unions and structs. A union needs a bitcast, a struct element |
| 3684 | // will need a bitcast if the LLVM type laid out doesn't match the desired |
| 3685 | // type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3686 | addr = Builder.CreateElementBitCast(addr, |
| 3687 | CGM.getTypes().ConvertTypeForMem(type), |
| 3688 | field->getName()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3689 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3690 | if (field->hasAttr<AnnotateAttr>()) |
| 3691 | addr = EmitFieldAnnotations(field, addr); |
| 3692 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3693 | LValue LV = MakeAddrLValue(addr, type, FieldBaseInfo); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3694 | LV.getQuals().addCVRQualifiers(cvr); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3695 | if (TBAAPath) { |
| 3696 | const ASTRecordLayout &Layout = |
| 3697 | getContext().getASTRecordLayout(field->getParent()); |
| 3698 | // Set the base type to be the base type of the base LValue and |
| 3699 | // update offset to be relative to the base type. |
Manman Ren | 0e52166 | 2013-04-27 00:39:37 +0000 | [diff] [blame] | 3700 | LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType()); |
| 3701 | LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() + |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 3702 | Layout.getFieldOffset(field->getFieldIndex()) / |
| 3703 | getContext().getCharWidth()); |
| 3704 | } |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3705 | |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 3706 | // __weak attribute on a field is ignored. |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3707 | if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak) |
| 3708 | LV.getQuals().removeObjCGCAttr(); |
John McCall | 53fcbd2 | 2011-02-26 08:07:02 +0000 | [diff] [blame] | 3709 | |
| 3710 | // Fields of may_alias structs act like 'char' for TBAA purposes. |
| 3711 | // FIXME: this should get propagated down through anonymous structs |
| 3712 | // and unions. |
| 3713 | if (mayAlias && LV.getTBAAInfo()) |
| 3714 | LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy)); |
| 3715 | |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 3716 | return LV; |
Devang Patel | 30efa2e | 2007-10-23 20:28:39 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3719 | LValue |
| 3720 | CodeGenFunction::EmitLValueForFieldInitialization(LValue Base, |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3721 | const FieldDecl *Field) { |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3722 | QualType FieldType = Field->getType(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3723 | |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3724 | if (!FieldType->isReferenceType()) |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3725 | return EmitLValueForField(Base, Field); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3726 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3727 | Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3728 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3729 | // Make sure that the address is pointing to the right type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3730 | llvm::Type *llvmType = ConvertTypeForMem(FieldType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3731 | V = Builder.CreateElementBitCast(V, llvmType, Field->getName()); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 3732 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3733 | // TODO: access-path TBAA? |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3734 | LValueBaseInfo BaseInfo = Base.getBaseInfo(); |
| 3735 | LValueBaseInfo FieldBaseInfo( |
| 3736 | getFieldAlignmentSource(BaseInfo.getAlignmentSource()), |
| 3737 | BaseInfo.getMayAlias()); |
| 3738 | return MakeAddrLValue(V, FieldType, FieldBaseInfo); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 3741 | LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3742 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
Richard Smith | 2d988f0 | 2011-11-22 22:48:32 +0000 | [diff] [blame] | 3743 | if (E->isFileScope()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3744 | ConstantAddress GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3745 | return MakeAddrLValue(GlobalPtr, E->getType(), BaseInfo); |
Richard Smith | 2d988f0 | 2011-11-22 22:48:32 +0000 | [diff] [blame] | 3746 | } |
Fariborz Jahanian | 5d53fcd | 2012-06-07 18:15:55 +0000 | [diff] [blame] | 3747 | if (E->getType()->isVariablyModifiedType()) |
| 3748 | // make sure to emit the VLA size. |
| 3749 | EmitVariablyModifiedType(E->getType()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3750 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3751 | Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); |
Chris Lattner | f53c096 | 2010-09-06 00:11:41 +0000 | [diff] [blame] | 3752 | const Expr *InitExpr = E->getInitializer(); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3753 | LValue Result = MakeAddrLValue(DeclPtr, E->getType(), BaseInfo); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 3754 | |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 3755 | EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), |
| 3756 | /*Init*/ true); |
Eli Friedman | 9fd8b68 | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 3757 | |
| 3758 | return Result; |
| 3759 | } |
| 3760 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3761 | LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) { |
| 3762 | if (!E->isGLValue()) |
| 3763 | // Initializing an aggregate temporary in C++11: T{...}. |
| 3764 | return EmitAggExprToLValue(E); |
| 3765 | |
| 3766 | // An lvalue initializer list must be initializing a reference. |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 3767 | assert(E->isTransparent() && "non-transparent glvalue init list"); |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3768 | return EmitLValue(E->getInit(0)); |
| 3769 | } |
| 3770 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3771 | /// Emit the operand of a glvalue conditional operator. This is either a glvalue |
| 3772 | /// or a (possibly-parenthesized) throw-expression. If this is a throw, no |
| 3773 | /// LValue is returned and the current block has been terminated. |
| 3774 | static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF, |
| 3775 | const Expr *Operand) { |
| 3776 | if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) { |
| 3777 | CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false); |
| 3778 | return None; |
| 3779 | } |
| 3780 | |
| 3781 | return CGF.EmitLValue(Operand); |
| 3782 | } |
| 3783 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3784 | LValue CodeGenFunction:: |
| 3785 | EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { |
| 3786 | if (!expr->isGLValue()) { |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3787 | // ?: here should be an aggregate. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3788 | assert(hasAggregateEvaluationKind(expr->getType()) && |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3789 | "Unexpected conditional operator!"); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3790 | return EmitAggExprToLValue(expr); |
Anders Carlsson | 1450adb | 2009-09-15 16:35:24 +0000 | [diff] [blame] | 3791 | } |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 3792 | |
Eli Friedman | 5995489 | 2012-01-25 05:04:17 +0000 | [diff] [blame] | 3793 | OpaqueValueMapping binding(*this, expr); |
| 3794 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3795 | const Expr *condExpr = expr->getCond(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 3796 | bool CondExprBool; |
| 3797 | if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3798 | const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr(); |
Chris Lattner | 41c6ab5 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 3799 | if (!CondExprBool) std::swap(live, dead); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3800 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3801 | if (!ContainsLabel(dead)) { |
Justin Bogner | ea278c3 | 2014-01-07 00:20:28 +0000 | [diff] [blame] | 3802 | // If the true case is live, we need to track its region. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3803 | if (CondExprBool) |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3804 | incrementProfileCounter(expr); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3805 | return EmitLValue(live); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 3806 | } |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3809 | llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true"); |
| 3810 | llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false"); |
| 3811 | llvm::BasicBlock *contBlock = createBasicBlock("cond.end"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3812 | |
| 3813 | ConditionalEvaluation eval(*this); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3814 | EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, getProfileCount(expr)); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3815 | |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3816 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3817 | EmitBlock(lhsBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 3818 | incrementProfileCounter(expr); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3819 | eval.begin(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3820 | Optional<LValue> lhs = |
| 3821 | EmitLValueOrThrowExpression(*this, expr->getTrueExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3822 | eval.end(*this); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3823 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3824 | if (lhs && !lhs->isSimple()) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3825 | return EmitUnsupportedLValue(expr, "conditional operator"); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3826 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3827 | lhsBlock = Builder.GetInsertBlock(); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3828 | if (lhs) |
| 3829 | Builder.CreateBr(contBlock); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3830 | |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3831 | // Any temporaries created here are conditional. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3832 | EmitBlock(rhsBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3833 | eval.begin(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3834 | Optional<LValue> rhs = |
| 3835 | EmitLValueOrThrowExpression(*this, expr->getFalseExpr()); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3836 | eval.end(*this); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3837 | if (rhs && !rhs->isSimple()) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3838 | return EmitUnsupportedLValue(expr, "conditional operator"); |
| 3839 | rhsBlock = Builder.GetInsertBlock(); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3840 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3841 | EmitBlock(contBlock); |
John McCall | 0a6bf2e | 2011-01-26 19:21:13 +0000 | [diff] [blame] | 3842 | |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3843 | if (lhs && rhs) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3844 | llvm::PHINode *phi = Builder.CreatePHI(lhs->getPointer()->getType(), |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3845 | 2, "cond-lvalue"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3846 | phi->addIncoming(lhs->getPointer(), lhsBlock); |
| 3847 | phi->addIncoming(rhs->getPointer(), rhsBlock); |
| 3848 | Address result(phi, std::min(lhs->getAlignment(), rhs->getAlignment())); |
| 3849 | AlignmentSource alignSource = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3850 | std::max(lhs->getBaseInfo().getAlignmentSource(), |
| 3851 | rhs->getBaseInfo().getAlignmentSource()); |
| 3852 | bool MayAlias = lhs->getBaseInfo().getMayAlias() || |
| 3853 | rhs->getBaseInfo().getMayAlias(); |
| 3854 | return MakeAddrLValue(result, expr->getType(), |
| 3855 | LValueBaseInfo(alignSource, MayAlias)); |
Richard Smith | f3076ff | 2014-06-20 18:43:47 +0000 | [diff] [blame] | 3856 | } else { |
| 3857 | assert((lhs || rhs) && |
| 3858 | "both operands of glvalue conditional are throw-expressions?"); |
| 3859 | return lhs ? *lhs : *rhs; |
| 3860 | } |
Daniel Dunbar | bf1fe8c | 2009-03-24 02:38:23 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Richard Smith | bb653bd | 2012-05-14 21:57:21 +0000 | [diff] [blame] | 3863 | /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference |
| 3864 | /// 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] | 3865 | /// otherwise if a cast is needed by the code generator in an lvalue context, |
| 3866 | /// 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] | 3867 | /// 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] | 3868 | /// are permitted with aggregate result, including noop aggregate casts, and |
| 3869 | /// cast from scalar to union. |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 3870 | LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3871 | switch (E->getCastKind()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3872 | case CK_ToVoid: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3873 | case CK_BitCast: |
| 3874 | case CK_ArrayToPointerDecay: |
| 3875 | case CK_FunctionToPointerDecay: |
| 3876 | case CK_NullToMemberPointer: |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3877 | case CK_NullToPointer: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3878 | case CK_IntegralToPointer: |
| 3879 | case CK_PointerToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3880 | case CK_PointerToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3881 | case CK_VectorSplat: |
| 3882 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 3883 | case CK_BooleanToSignedIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3884 | case CK_IntegralToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3885 | case CK_IntegralToFloating: |
| 3886 | case CK_FloatingToIntegral: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3887 | case CK_FloatingToBoolean: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3888 | case CK_FloatingCast: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3889 | case CK_FloatingRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3890 | case CK_FloatingComplexToReal: |
| 3891 | case CK_FloatingComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3892 | case CK_FloatingComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3893 | case CK_FloatingComplexToIntegralComplex: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3894 | case CK_IntegralRealToComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3895 | case CK_IntegralComplexToReal: |
| 3896 | case CK_IntegralComplexToBoolean: |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3897 | case CK_IntegralComplexCast: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3898 | case CK_IntegralComplexToFloatingComplex: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3899 | case CK_DerivedToBaseMemberPointer: |
| 3900 | case CK_BaseToDerivedMemberPointer: |
| 3901 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 3902 | case CK_ReinterpretMemberPointer: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3903 | case CK_AnyPointerToBlockPointerCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3904 | case CK_ARCProduceObject: |
| 3905 | case CK_ARCConsumeObject: |
| 3906 | case CK_ARCReclaimReturnedObject: |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3907 | case CK_ARCExtendBlockObject: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3908 | case CK_CopyAndAutoreleaseBlockObject: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 3909 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3910 | case CK_IntToOCLSampler: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3911 | return EmitUnsupportedLValue(E, "unexpected cast lvalue"); |
| 3912 | |
| 3913 | case CK_Dependent: |
| 3914 | llvm_unreachable("dependent cast kind in IR gen!"); |
| 3915 | |
| 3916 | case CK_BuiltinFnToFnPtr: |
| 3917 | llvm_unreachable("builtin functions are handled elsewhere"); |
| 3918 | |
Eli Friedman | be4504d | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 3919 | // These are never l-values; just use the aggregate emission code. |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3920 | case CK_NonAtomicToAtomic: |
| 3921 | case CK_AtomicToNonAtomic: |
Eli Friedman | be4504d | 2013-07-11 01:32:21 +0000 | [diff] [blame] | 3922 | return EmitAggExprToLValue(E); |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3923 | |
Anders Carlsson | 8a01a75 | 2011-04-11 02:03:26 +0000 | [diff] [blame] | 3924 | case CK_Dynamic: { |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3925 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3926 | Address V = LV.getAddress(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3927 | const auto *DCE = cast<CXXDynamicCastExpr>(E); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3928 | return MakeNaturalAlignAddrLValue(EmitDynamicCast(V, DCE), E->getType()); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3931 | case CK_ConstructorConversion: |
| 3932 | case CK_UserDefinedConversion: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3933 | case CK_CPointerToObjCPointerCast: |
| 3934 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c7ad5c4 | 2013-06-28 00:23:34 +0000 | [diff] [blame] | 3935 | case CK_NoOp: |
| 3936 | case CK_LValueToRValue: |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 3937 | return EmitLValue(E->getSubExpr()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3938 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3939 | case CK_UncheckedDerivedToBase: |
| 3940 | case CK_DerivedToBase: { |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3941 | const RecordType *DerivedClassTy = |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3942 | E->getSubExpr()->getType()->getAs<RecordType>(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3943 | auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3944 | |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3945 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3946 | Address This = LV.getAddress(); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3947 | |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3948 | // Perform the derived-to-base conversion |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3949 | Address Base = GetAddressOfBaseClass( |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 3950 | This, DerivedClassDecl, E->path_begin(), E->path_end(), |
| 3951 | /*NullCheckValue=*/false, E->getExprLoc()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3952 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3953 | return MakeAddrLValue(Base, E->getType(), LV.getBaseInfo()); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 3954 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3955 | case CK_ToUnion: |
Daniel Dunbar | 9c4e465 | 2010-02-05 20:02:42 +0000 | [diff] [blame] | 3956 | return EmitAggExprToLValue(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3957 | case CK_BaseToDerived: { |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3958 | const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3959 | auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3960 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3961 | LValue LV = EmitLValue(E->getSubExpr()); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 3962 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 3963 | // Perform the base-to-derived conversion |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3964 | Address Derived = |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3965 | GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3966 | E->path_begin(), E->path_end(), |
| 3967 | /*NullCheckValue=*/false); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3968 | |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3969 | // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is |
| 3970 | // performed and the object is not of the derived type. |
Alexey Samsonov | ac4afe4 | 2014-07-07 23:59:57 +0000 | [diff] [blame] | 3971 | if (sanitizePerformTypeCheck()) |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3972 | EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3973 | Derived.getPointer(), E->getType()); |
Filipe Cabecinhas | 178a8df | 2013-08-08 01:08:17 +0000 | [diff] [blame] | 3974 | |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3975 | if (SanOpts.has(SanitizerKind::CFIDerivedCast)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3976 | EmitVTablePtrCheckForCast(E->getType(), Derived.getPointer(), |
| 3977 | /*MayBeNull=*/false, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 3978 | CFITCK_DerivedCast, E->getLocStart()); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3979 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3980 | return MakeAddrLValue(Derived, E->getType(), LV.getBaseInfo()); |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3981 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3982 | case CK_LValueBitCast: { |
Eli Friedman | 8c98dff | 2009-11-16 05:48:01 +0000 | [diff] [blame] | 3983 | // This must be a reinterpret_cast (or c-style equivalent). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3984 | const auto *CE = cast<ExplicitCastExpr>(E); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 3985 | |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 3986 | CGM.EmitExplicitCastExprType(CE, this); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 3987 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3988 | Address V = Builder.CreateBitCast(LV.getAddress(), |
| 3989 | ConvertType(CE->getTypeAsWritten())); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3990 | |
| 3991 | if (SanOpts.has(SanitizerKind::CFIUnrelatedCast)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3992 | EmitVTablePtrCheckForCast(E->getType(), V.getPointer(), |
| 3993 | /*MayBeNull=*/false, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 3994 | CFITCK_UnrelatedCast, E->getLocStart()); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 3995 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 3996 | return MakeAddrLValue(V, E->getType(), LV.getBaseInfo()); |
Anders Carlsson | 50cb321 | 2009-11-14 21:21:42 +0000 | [diff] [blame] | 3997 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3998 | case CK_ObjCObjectLValueCast: { |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3999 | LValue LV = EmitLValue(E->getSubExpr()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4000 | Address V = Builder.CreateElementBitCast(LV.getAddress(), |
| 4001 | ConvertType(E->getType())); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4002 | return MakeAddrLValue(V, E->getType(), LV.getBaseInfo()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4003 | } |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 4004 | case CK_ZeroToOCLQueue: |
| 4005 | llvm_unreachable("NULL to OpenCL queue lvalue cast is not valid"); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4006 | case CK_ZeroToOCLEvent: |
| 4007 | llvm_unreachable("NULL to OpenCL event lvalue cast is not valid"); |
Anders Carlsson | d95f960 | 2009-09-12 16:16:49 +0000 | [diff] [blame] | 4008 | } |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4009 | |
Douglas Gregor | cdb466e | 2010-07-15 18:58:16 +0000 | [diff] [blame] | 4010 | llvm_unreachable("Unhandled lvalue cast kind?"); |
Chris Lattner | 28bcf1a | 2009-03-18 18:28:57 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 4013 | LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) { |
John McCall | 9a54961 | 2011-11-08 22:54:08 +0000 | [diff] [blame] | 4014 | assert(OpaqueValueMappingData::shouldBindAsLValue(e)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4015 | return getOpaqueLValueMapping(e); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 4018 | RValue CodeGenFunction::EmitRValueForField(LValue LV, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4019 | const FieldDecl *FD, |
| 4020 | SourceLocation Loc) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4021 | QualType FT = FD->getType(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 4022 | LValue FieldLV = EmitLValueForField(LV, FD); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4023 | switch (getEvaluationKind(FT)) { |
| 4024 | case TEK_Complex: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4025 | return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4026 | case TEK_Aggregate: |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 4027 | return FieldLV.asAggregateRValue(); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4028 | case TEK_Scalar: |
Reid Kleckner | 9d03109 | 2016-05-02 22:42:34 +0000 | [diff] [blame] | 4029 | // This routine is used to load fields one-by-one to perform a copy, so |
| 4030 | // don't load reference fields. |
| 4031 | if (FD->getType()->isReferenceType()) |
| 4032 | return RValue::get(FieldLV.getPointer()); |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4033 | return EmitLoadOfLValue(FieldLV, Loc); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4034 | } |
| 4035 | llvm_unreachable("bad evaluation kind"); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4036 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4037 | |
Chris Lattner | e47e440 | 2007-06-01 18:02:12 +0000 | [diff] [blame] | 4038 | //===--------------------------------------------------------------------===// |
| 4039 | // Expression Emission |
| 4040 | //===--------------------------------------------------------------------===// |
| 4041 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4042 | RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 4043 | ReturnValueSlot ReturnValue) { |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 4044 | // Builtins never have block type. |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 4045 | if (E->getCallee()->getType()->isBlockPointerType()) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 4046 | return EmitBlockCallExpr(E, ReturnValue); |
Daniel Dunbar | bb197e4 | 2009-01-09 16:50:52 +0000 | [diff] [blame] | 4047 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4048 | if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E)) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 4049 | return EmitCXXMemberCallExpr(CE, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4050 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4051 | if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E)) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 4052 | return EmitCUDAKernelCallExpr(CE, ReturnValue); |
| 4053 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4054 | if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E)) |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4055 | if (const CXXMethodDecl *MD = |
| 4056 | dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl())) |
Anders Carlsson | bfb3671 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 4057 | return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4058 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4059 | CGCallee callee = EmitCallee(E->getCallee()); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4060 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4061 | if (callee.isBuiltin()) { |
| 4062 | return EmitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(), |
| 4063 | E, ReturnValue); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4064 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4065 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4066 | if (callee.isPseudoDestructor()) { |
| 4067 | return EmitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr()); |
| 4068 | } |
| 4069 | |
| 4070 | return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue); |
| 4071 | } |
| 4072 | |
| 4073 | /// Emit a CallExpr without considering whether it might be a subclass. |
| 4074 | RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E, |
| 4075 | ReturnValueSlot ReturnValue) { |
| 4076 | CGCallee Callee = EmitCallee(E->getCallee()); |
| 4077 | return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue); |
| 4078 | } |
| 4079 | |
| 4080 | static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) { |
| 4081 | if (auto builtinID = FD->getBuiltinID()) { |
| 4082 | return CGCallee::forBuiltin(builtinID, FD); |
| 4083 | } |
| 4084 | |
| 4085 | llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD); |
| 4086 | return CGCallee::forDirect(calleePtr, FD); |
| 4087 | } |
| 4088 | |
| 4089 | CGCallee CodeGenFunction::EmitCallee(const Expr *E) { |
| 4090 | E = E->IgnoreParens(); |
| 4091 | |
| 4092 | // Look through function-to-pointer decay. |
| 4093 | if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 4094 | if (ICE->getCastKind() == CK_FunctionToPointerDecay || |
| 4095 | ICE->getCastKind() == CK_BuiltinFnToFnPtr) { |
| 4096 | return EmitCallee(ICE->getSubExpr()); |
| 4097 | } |
| 4098 | |
| 4099 | // Resolve direct calls. |
| 4100 | } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) { |
| 4101 | if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 4102 | return EmitDirectCallee(*this, FD); |
| 4103 | } |
| 4104 | } else if (auto ME = dyn_cast<MemberExpr>(E)) { |
| 4105 | if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) { |
| 4106 | EmitIgnoredExpr(ME->getBase()); |
| 4107 | return EmitDirectCallee(*this, FD); |
| 4108 | } |
| 4109 | |
| 4110 | // Look through template substitutions. |
| 4111 | } else if (auto NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 4112 | return EmitCallee(NTTP->getReplacement()); |
| 4113 | |
| 4114 | // Treat pseudo-destructor calls differently. |
| 4115 | } else if (auto PDE = dyn_cast<CXXPseudoDestructorExpr>(E)) { |
| 4116 | return CGCallee::forPseudoDestructor(PDE); |
| 4117 | } |
| 4118 | |
| 4119 | // Otherwise, we have an indirect reference. |
| 4120 | llvm::Value *calleePtr; |
| 4121 | QualType functionType; |
| 4122 | if (auto ptrType = E->getType()->getAs<PointerType>()) { |
| 4123 | calleePtr = EmitScalarExpr(E); |
| 4124 | functionType = ptrType->getPointeeType(); |
| 4125 | } else { |
| 4126 | functionType = E->getType(); |
| 4127 | calleePtr = EmitLValue(E).getPointer(); |
| 4128 | } |
| 4129 | assert(functionType->isFunctionType()); |
| 4130 | CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), |
| 4131 | E->getReferencedDeclOfCallee()); |
| 4132 | CGCallee callee(calleeInfo, calleePtr); |
| 4133 | return callee; |
Chris Lattner | 9e47ead | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 4136 | LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 4137 | // 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] | 4138 | if (E->getOpcode() == BO_Comma) { |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 4139 | EmitIgnoredExpr(E->getLHS()); |
Eli Friedman | 5445f6e | 2009-12-07 20:18:11 +0000 | [diff] [blame] | 4140 | EnsureInsertPoint(); |
Chris Lattner | e541ea3 | 2009-05-12 21:28:12 +0000 | [diff] [blame] | 4141 | return EmitLValue(E->getRHS()); |
| 4142 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4143 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4144 | if (E->getOpcode() == BO_PtrMemD || |
| 4145 | E->getOpcode() == BO_PtrMemI) |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 4146 | return EmitPointerToDataMemberBinaryExpr(E); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 4147 | |
John McCall | a2342eb | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 4148 | assert(E->getOpcode() == BO_Assign && "unexpected binary l-value"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4149 | |
| 4150 | // Note that in all of these cases, __block variables need the RHS |
| 4151 | // evaluated first just in case the variable gets moved by the RHS. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4152 | |
| 4153 | switch (getEvaluationKind(E->getType())) { |
| 4154 | case TEK_Scalar: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4155 | switch (E->getLHS()->getType().getObjCLifetime()) { |
| 4156 | case Qualifiers::OCL_Strong: |
| 4157 | return EmitARCStoreStrong(E, /*ignored*/ false).first; |
| 4158 | |
| 4159 | case Qualifiers::OCL_Autoreleasing: |
| 4160 | return EmitARCStoreAutoreleasing(E).first; |
| 4161 | |
| 4162 | // No reason to do any of these differently. |
| 4163 | case Qualifiers::OCL_None: |
| 4164 | case Qualifiers::OCL_ExplicitNone: |
| 4165 | case Qualifiers::OCL_Weak: |
| 4166 | break; |
| 4167 | } |
| 4168 | |
John McCall | d0a3001 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 4169 | RValue RV = EmitAnyExpr(E->getRHS()); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 4170 | LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store); |
Vedant Kumar | 6b22dda | 2017-04-26 21:55:17 +0000 | [diff] [blame] | 4171 | if (RV.isScalar()) |
| 4172 | EmitNullabilityCheck(LV, RV.getScalarVal(), E->getExprLoc()); |
John McCall | 55e1fbc | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 4173 | EmitStoreThroughLValue(RV, LV); |
Anders Carlsson | 0999aaf | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 4174 | return LV; |
| 4175 | } |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 4176 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4177 | case TEK_Complex: |
John McCall | 4f29b49 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 4178 | return EmitComplexAssignmentLValue(E); |
| 4179 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4180 | case TEK_Aggregate: |
| 4181 | return EmitAggExprToLValue(E); |
| 4182 | } |
| 4183 | llvm_unreachable("bad evaluation kind"); |
Daniel Dunbar | 8cde00a | 2008-09-04 03:20:13 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 4186 | LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 4187 | RValue RV = EmitCallExpr(E); |
Anders Carlsson | 4ae70ff | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 4188 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4189 | if (!RV.isScalar()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4190 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4191 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4192 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 4193 | assert(E->getCallReturnType(getContext())->isReferenceType() && |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4194 | "Can't have a scalar return unless the return type is a " |
| 4195 | "reference type!"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4197 | return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); |
Christopher Lamb | d91c3d4 | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 4200 | LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { |
| 4201 | // FIXME: This shouldn't require another copy. |
Daniel Dunbar | d0bc7b9 | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 4202 | return EmitAggExprToLValue(E); |
Daniel Dunbar | 8d9dc4a | 2009-02-11 20:59:32 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 4205 | LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 4206 | assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() |
| 4207 | && "binding l-value to type which needs a temporary"); |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4208 | AggValueSlot Slot = CreateAggTemp(E->getType()); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4209 | EmitCXXConstructExpr(E, Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4210 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4211 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Anders Carlsson | 3be22e2 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 4214 | LValue |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 4215 | CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4216 | return MakeNaturalAlignAddrLValue(EmitCXXTypeidExpr(E), E->getType()); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4219 | Address CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4220 | return Builder.CreateElementBitCast(CGM.GetAddrOfUuidDescriptor(E), |
| 4221 | ConvertType(E->getType())); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4225 | return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4226 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 4229 | LValue |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 4230 | CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 4231 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 4232 | Slot.setExternallyDestructed(); |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 4233 | EmitAggExpr(E->getSubExpr(), Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4234 | EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddress()); |
| 4235 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4236 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Anders Carlsson | fd2af0c | 2009-05-30 23:30:54 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 4239 | LValue |
| 4240 | CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) { |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 4241 | AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 4242 | EmitLambdaExpr(E, Slot); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4243 | return MakeAddrLValue(Slot.getAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4244 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Eli Friedman | 5bc1712 | 2012-02-08 05:34:55 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 4247 | LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 4248 | RValue RV = EmitObjCMessageExpr(E); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4249 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 4250 | if (!RV.isScalar()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4251 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4252 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4253 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4254 | assert(E->getMethodDecl()->getReturnType()->isReferenceType() && |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 4255 | "Can't have a scalar return unless the return type is a " |
| 4256 | "reference type!"); |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4257 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4258 | return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4261 | LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4262 | Address V = |
| 4263 | CGM.getObjCRuntime().GetAddrOfSelector(*this, E->getSelector()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4264 | return MakeAddrLValue(V, E->getType(), |
| 4265 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4268 | llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 4269 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4270 | return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4273 | LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, |
| 4274 | llvm::Value *BaseValue, |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 4275 | const ObjCIvarDecl *Ivar, |
| 4276 | unsigned CVRQualifiers) { |
Chris Lattner | c4688d2 | 2009-04-17 17:44:48 +0000 | [diff] [blame] | 4277 | return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, |
Daniel Dunbar | 9ebf951 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4278 | Ivar, CVRQualifiers); |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 4282 | // FIXME: A lot of the code below could be shared with EmitMemberExpr. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4283 | llvm::Value *BaseValue = nullptr; |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 4284 | const Expr *BaseExpr = E->getBase(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4285 | Qualifiers BaseQuals; |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4286 | QualType ObjectTy; |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 4287 | if (E->isArrow()) { |
| 4288 | BaseValue = EmitScalarExpr(BaseExpr); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4289 | ObjectTy = BaseExpr->getType()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4290 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 4291 | } else { |
| 4292 | LValue BaseLV = EmitLValue(BaseExpr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4293 | BaseValue = BaseLV.getPointer(); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4294 | ObjectTy = BaseExpr->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4295 | BaseQuals = ObjectTy.getQualifiers(); |
Anders Carlsson | c13b85a | 2008-08-25 01:53:23 +0000 | [diff] [blame] | 4296 | } |
Daniel Dunbar | 1c64e5d | 2008-09-24 04:00:38 +0000 | [diff] [blame] | 4297 | |
Craig Topper | 99e7927 | 2013-07-26 05:59:26 +0000 | [diff] [blame] | 4298 | LValue LV = |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4299 | EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), |
| 4300 | BaseQuals.getCVRQualifiers()); |
Fariborz Jahanian | de1d324 | 2009-09-16 23:11:23 +0000 | [diff] [blame] | 4301 | setObjCGCLValueClass(getContext(), E, LV); |
| 4302 | return LV; |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 4305 | LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 4306 | // Can only get l-value for message expression returning aggregate type |
| 4307 | RValue RV = EmitAnyExprToTemp(E); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4308 | return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4309 | LValueBaseInfo(AlignmentSource::Decl, false)); |
Chris Lattner | a4185c5 | 2009-04-25 19:35:26 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4312 | RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 4313 | const CallExpr *E, ReturnValueSlot ReturnValue, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4314 | llvm::Value *Chain) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 4315 | // Get the actual function type. The callee type will always be a pointer to |
| 4316 | // function type or a block pointer type. |
| 4317 | assert(CalleeType->isFunctionPointerType() && |
Anders Carlsson | d8db853 | 2009-04-07 18:53:02 +0000 | [diff] [blame] | 4318 | "Call must have function pointer type!"); |
| 4319 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4320 | const Decl *TargetDecl = OrigCallee.getAbstractInfo().getCalleeDecl(); |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 4321 | |
Eric Christopher | 2b2d56f | 2015-11-12 00:44:12 +0000 | [diff] [blame] | 4322 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
Eric Christopher | 39db726 | 2015-11-14 01:56:04 +0000 | [diff] [blame] | 4323 | // We can only guarantee that a function is called from the correct |
| 4324 | // context/function based on the appropriate target attributes, |
| 4325 | // so only check in the case where we have both always_inline and target |
| 4326 | // since otherwise we could be making a conditional call after a check for |
| 4327 | // the proper cpu features (and it won't cause code generation issues due to |
| 4328 | // function based code generation). |
Eric Christopher | 2b2d56f | 2015-11-12 00:44:12 +0000 | [diff] [blame] | 4329 | if (TargetDecl->hasAttr<AlwaysInlineAttr>() && |
| 4330 | TargetDecl->hasAttr<TargetAttr>()) |
| 4331 | checkTargetFeatures(E, FD); |
| 4332 | |
John McCall | 6fd4c23 | 2009-10-23 08:22:42 +0000 | [diff] [blame] | 4333 | CalleeType = getContext().getCanonicalType(CalleeType); |
| 4334 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4335 | const auto *FnType = |
| 4336 | cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType()); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 4337 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4338 | CGCallee Callee = OrigCallee; |
| 4339 | |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 4340 | if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) && |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4341 | (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { |
| 4342 | if (llvm::Constant *PrefixSig = |
| 4343 | CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) { |
Alexey Samsonov | 24cad99 | 2014-07-17 18:46:27 +0000 | [diff] [blame] | 4344 | SanitizerScope SanScope(this); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4345 | llvm::Constant *FTRTTIConst = |
| 4346 | CGM.GetAddrOfRTTIDescriptor(QualType(FnType, 0), /*ForEH=*/true); |
| 4347 | llvm::Type *PrefixStructTyElems[] = { |
| 4348 | PrefixSig->getType(), |
| 4349 | FTRTTIConst->getType() |
| 4350 | }; |
| 4351 | llvm::StructType *PrefixStructTy = llvm::StructType::get( |
| 4352 | CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true); |
| 4353 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4354 | llvm::Value *CalleePtr = Callee.getFunctionPointer(); |
| 4355 | |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4356 | llvm::Value *CalleePrefixStruct = Builder.CreateBitCast( |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4357 | CalleePtr, llvm::PointerType::getUnqual(PrefixStructTy)); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4358 | llvm::Value *CalleeSigPtr = |
David Blaikie | 17ea266 | 2015-04-04 21:07:17 +0000 | [diff] [blame] | 4359 | Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4360 | llvm::Value *CalleeSig = |
| 4361 | Builder.CreateAlignedLoad(CalleeSigPtr, getIntAlign()); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4362 | llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig); |
| 4363 | |
| 4364 | llvm::BasicBlock *Cont = createBasicBlock("cont"); |
| 4365 | llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck"); |
| 4366 | Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont); |
| 4367 | |
| 4368 | EmitBlock(TypeCheck); |
| 4369 | llvm::Value *CalleeRTTIPtr = |
David Blaikie | 17ea266 | 2015-04-04 21:07:17 +0000 | [diff] [blame] | 4370 | Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4371 | llvm::Value *CalleeRTTI = |
| 4372 | Builder.CreateAlignedLoad(CalleeRTTIPtr, getPointerAlign()); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4373 | llvm::Value *CalleeRTTIMatch = |
| 4374 | Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst); |
| 4375 | llvm::Constant *StaticData[] = { |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 4376 | EmitCheckSourceLocation(E->getLocStart()), |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4377 | EmitCheckTypeDescriptor(CalleeType) |
| 4378 | }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 4379 | EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function), |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 4380 | SanitizerHandler::FunctionTypeMismatch, StaticData, CalleePtr); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 4381 | |
| 4382 | Builder.CreateBr(Cont); |
| 4383 | EmitBlock(Cont); |
| 4384 | } |
| 4385 | } |
| 4386 | |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4387 | // If we are checking indirect calls and this call is indirect, check that the |
| 4388 | // function pointer is a member of the bit set for the function type. |
| 4389 | if (SanOpts.has(SanitizerKind::CFIICall) && |
| 4390 | (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { |
| 4391 | SanitizerScope SanScope(this); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 4392 | EmitSanitizerStatReport(llvm::SanStat_CFI_ICall); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4393 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4394 | llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4395 | llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4396 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4397 | llvm::Value *CalleePtr = Callee.getFunctionPointer(); |
| 4398 | llvm::Value *CastedCallee = Builder.CreateBitCast(CalleePtr, Int8PtrTy); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4399 | llvm::Value *TypeTest = Builder.CreateCall( |
| 4400 | CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedCallee, TypeId}); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4401 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4402 | auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD); |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 4403 | llvm::Constant *StaticData[] = { |
| 4404 | llvm::ConstantInt::get(Int8Ty, CFITCK_ICall), |
| 4405 | EmitCheckSourceLocation(E->getLocStart()), |
| 4406 | EmitCheckTypeDescriptor(QualType(FnType, 0)), |
| 4407 | }; |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4408 | if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) { |
| 4409 | EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId, |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 4410 | CastedCallee, StaticData); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4411 | } else { |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4412 | EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall), |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 4413 | SanitizerHandler::CFICheckFail, StaticData, |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4414 | {CastedCallee, llvm::UndefValue::get(IntPtrTy)}); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4415 | } |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 4418 | CallArgList Args; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4419 | if (Chain) |
| 4420 | Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)), |
| 4421 | CGM.getContext().VoidPtrTy); |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 4422 | |
| 4423 | // C++17 requires that we evaluate arguments to a call using assignment syntax |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 4424 | // right-to-left, and that we evaluate arguments to certain other operators |
| 4425 | // left-to-right. Note that we allow this to override the order dictated by |
| 4426 | // the calling convention on the MS ABI, which means that parameter |
| 4427 | // destruction order is not necessarily reverse construction order. |
| 4428 | // FIXME: Revisit this based on C++ committee response to unimplementability. |
| 4429 | EvaluationOrder Order = EvaluationOrder::Default; |
| 4430 | if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4431 | if (OCE->isAssignmentOp()) |
| 4432 | Order = EvaluationOrder::ForceRightToLeft; |
| 4433 | else { |
| 4434 | switch (OCE->getOperator()) { |
| 4435 | case OO_LessLess: |
| 4436 | case OO_GreaterGreater: |
| 4437 | case OO_AmpAmp: |
| 4438 | case OO_PipePipe: |
| 4439 | case OO_Comma: |
| 4440 | case OO_ArrowStar: |
| 4441 | Order = EvaluationOrder::ForceLeftToRight; |
| 4442 | break; |
| 4443 | default: |
| 4444 | break; |
| 4445 | } |
| 4446 | } |
| 4447 | } |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 4448 | |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 4449 | EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arguments(), |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 4450 | E->getDirectCallee(), /*ParamsToSkip*/ 0, Order); |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 4451 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4452 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall( |
| 4453 | Args, FnType, /*isChainCall=*/Chain); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 4454 | |
| 4455 | // C99 6.5.2.2p6: |
| 4456 | // If the expression that denotes the called function has a type |
| 4457 | // that does not include a prototype, [the default argument |
| 4458 | // promotions are performed]. If the number of arguments does not |
| 4459 | // equal the number of parameters, the behavior is undefined. If |
| 4460 | // the function is defined with a type that includes a prototype, |
| 4461 | // and either the prototype ends with an ellipsis (, ...) or the |
| 4462 | // types of the arguments after promotion are not compatible with |
| 4463 | // the types of the parameters, the behavior is undefined. If the |
| 4464 | // function is defined with a type that does not include a |
| 4465 | // prototype, and the types of the arguments after promotion are |
| 4466 | // not compatible with those of the parameters after promotion, |
| 4467 | // the behavior is undefined [except in some trivial cases]. |
| 4468 | // That is, in the general case, we should assume that a call |
| 4469 | // through an unprototyped function type works like a *non-variadic* |
| 4470 | // call. The way we make this work is to cast to the exact type |
| 4471 | // of the promoted arguments. |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 4472 | // |
| 4473 | // Chain calls use this same code path to add the invisible chain parameter |
| 4474 | // to the function type. |
| 4475 | if (isa<FunctionNoProtoType>(FnType) || Chain) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 4476 | llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 4477 | CalleeTy = CalleeTy->getPointerTo(); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4478 | |
| 4479 | llvm::Value *CalleePtr = Callee.getFunctionPointer(); |
| 4480 | CalleePtr = Builder.CreateBitCast(CalleePtr, CalleeTy, "callee.knr.cast"); |
| 4481 | Callee.setFunctionPointer(CalleePtr); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4484 | return EmitCall(FnInfo, Callee, ReturnValue, Args); |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 4485 | } |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 4486 | |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4487 | LValue CodeGenFunction:: |
| 4488 | EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4489 | Address BaseAddr = Address::invalid(); |
| 4490 | if (E->getOpcode() == BO_PtrMemI) { |
| 4491 | BaseAddr = EmitPointerWithAlignment(E->getLHS()); |
| 4492 | } else { |
| 4493 | BaseAddr = EmitLValue(E->getLHS()).getAddress(); |
| 4494 | } |
Chris Lattner | ab5e0af | 2009-10-28 17:39:19 +0000 | [diff] [blame] | 4495 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 4496 | llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); |
| 4497 | |
| 4498 | const MemberPointerType *MPT |
| 4499 | = E->getRHS()->getType()->getAs<MemberPointerType>(); |
| 4500 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4501 | LValueBaseInfo BaseInfo; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4502 | Address MemberAddr = |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4503 | EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT, &BaseInfo); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 4504 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4505 | return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), BaseInfo); |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 4506 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4507 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4508 | /// Given the address of a temporary variable, produce an r-value of |
| 4509 | /// its type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4510 | RValue CodeGenFunction::convertTempToRValue(Address addr, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4511 | QualType type, |
| 4512 | SourceLocation loc) { |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4513 | LValue lvalue = MakeAddrLValue(addr, type, |
| 4514 | LValueBaseInfo(AlignmentSource::Decl, false)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4515 | switch (getEvaluationKind(type)) { |
| 4516 | case TEK_Complex: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4517 | return RValue::getComplex(EmitLoadOfComplex(lvalue, loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4518 | case TEK_Aggregate: |
| 4519 | return lvalue.asAggregateRValue(); |
| 4520 | case TEK_Scalar: |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 4521 | return RValue::get(EmitLoadOfScalar(lvalue, loc)); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4522 | } |
| 4523 | llvm_unreachable("bad evaluation kind"); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 4526 | void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) { |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4527 | assert(Val->getType()->isFPOrFPVectorTy()); |
Duncan Sands | e81111c | 2012-04-10 08:23:07 +0000 | [diff] [blame] | 4528 | if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val)) |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4529 | return; |
| 4530 | |
Duncan Sands | 65229ed | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 4531 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 4532 | llvm::MDNode *Node = MDHelper.createFPMath(Accuracy); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4533 | |
Duncan Sands | 6fc4619 | 2012-04-14 12:37:26 +0000 | [diff] [blame] | 4534 | cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node); |
Peter Collingbourne | 95fd2ca | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 4535 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4536 | |
| 4537 | namespace { |
| 4538 | struct LValueOrRValue { |
| 4539 | LValue LV; |
| 4540 | RValue RV; |
| 4541 | }; |
| 4542 | } |
| 4543 | |
| 4544 | static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, |
| 4545 | const PseudoObjectExpr *E, |
| 4546 | bool forLValue, |
| 4547 | AggValueSlot slot) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4548 | SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4549 | |
| 4550 | // Find the result expression, if any. |
| 4551 | const Expr *resultExpr = E->getResultExpr(); |
| 4552 | LValueOrRValue result; |
| 4553 | |
| 4554 | for (PseudoObjectExpr::const_semantics_iterator |
| 4555 | i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { |
| 4556 | const Expr *semantic = *i; |
| 4557 | |
| 4558 | // If this semantic expression is an opaque value, bind it |
| 4559 | // to the result of its source expression. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4560 | if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4561 | |
| 4562 | // If this is the result expression, we may need to evaluate |
| 4563 | // directly into the slot. |
| 4564 | typedef CodeGenFunction::OpaqueValueMappingData OVMA; |
| 4565 | OVMA opaqueData; |
| 4566 | if (ov == resultExpr && ov->isRValue() && !forLValue && |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4567 | CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4568 | CGF.EmitAggExpr(ov->getSourceExpr(), slot); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4569 | LValueBaseInfo BaseInfo(AlignmentSource::Decl, false); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4570 | LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(), |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 4571 | BaseInfo); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4572 | opaqueData = OVMA::bind(CGF, ov, LV); |
| 4573 | result.RV = slot.asRValue(); |
| 4574 | |
| 4575 | // Otherwise, emit as normal. |
| 4576 | } else { |
| 4577 | opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); |
| 4578 | |
| 4579 | // If this is the result, also evaluate the result now. |
| 4580 | if (ov == resultExpr) { |
| 4581 | if (forLValue) |
| 4582 | result.LV = CGF.EmitLValue(ov); |
| 4583 | else |
| 4584 | result.RV = CGF.EmitAnyExpr(ov, slot); |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | opaques.push_back(opaqueData); |
| 4589 | |
| 4590 | // Otherwise, if the expression is the result, evaluate it |
| 4591 | // and remember the result. |
| 4592 | } else if (semantic == resultExpr) { |
| 4593 | if (forLValue) |
| 4594 | result.LV = CGF.EmitLValue(semantic); |
| 4595 | else |
| 4596 | result.RV = CGF.EmitAnyExpr(semantic, slot); |
| 4597 | |
| 4598 | // Otherwise, evaluate the expression in an ignored context. |
| 4599 | } else { |
| 4600 | CGF.EmitIgnoredExpr(semantic); |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | // Unbind all the opaques now. |
| 4605 | for (unsigned i = 0, e = opaques.size(); i != e; ++i) |
| 4606 | opaques[i].unbind(CGF); |
| 4607 | |
| 4608 | return result; |
| 4609 | } |
| 4610 | |
| 4611 | RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E, |
| 4612 | AggValueSlot slot) { |
| 4613 | return emitPseudoObjectExpr(*this, E, false, slot).RV; |
| 4614 | } |
| 4615 | |
| 4616 | LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) { |
| 4617 | return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV; |
| 4618 | } |