Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 1 | //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // These classes implement wrappers around llvm::Value in order to |
| 10 | // fully represent the range of values for C L- and R- values. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 16 | |
Daniel Dunbar | 93b00a9 | 2010-08-21 02:53:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
Diogo Sampaio | 6a24339 | 2020-01-21 15:13:24 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Value.h" |
Diogo Sampaio | 2147703 | 2020-01-21 15:31:33 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Type.h" |
| 21 | #include "Address.h" |
| 22 | #include "CodeGenTBAA.h" |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | class Constant; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 26 | class MDNode; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 29 | namespace clang { |
| 30 | namespace CodeGen { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 31 | class AggValueSlot; |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 32 | class CodeGenFunction; |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 33 | struct CGBitFieldInfo; |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 34 | |
| 35 | /// RValue - This trivial value class is used to represent the result of an |
| 36 | /// expression that is evaluated. It can be one of three things: either a |
| 37 | /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the |
| 38 | /// address of an aggregate value in memory. |
| 39 | class RValue { |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 40 | enum Flavor { Scalar, Complex, Aggregate }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 42 | // The shift to make to an aggregate's alignment to make it look |
| 43 | // like a pointer. |
| 44 | enum { AggAlignShift = 4 }; |
| 45 | |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 46 | // Stores first value and flavor. |
| 47 | llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1; |
| 48 | // Stores second value and volatility. |
| 49 | llvm::PointerIntPair<llvm::Value *, 1, bool> V2; |
| 50 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 51 | public: |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 52 | bool isScalar() const { return V1.getInt() == Scalar; } |
| 53 | bool isComplex() const { return V1.getInt() == Complex; } |
| 54 | bool isAggregate() const { return V1.getInt() == Aggregate; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 56 | bool isVolatileQualified() const { return V2.getInt(); } |
Mike Stump | 93700fc | 2009-05-23 20:21:36 +0000 | [diff] [blame] | 57 | |
Mike Stump | 462a4aa | 2009-11-03 16:11:57 +0000 | [diff] [blame] | 58 | /// getScalarVal() - Return the Value* of this scalar value. |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 59 | llvm::Value *getScalarVal() const { |
| 60 | assert(isScalar() && "Not a scalar!"); |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 61 | return V1.getPointer(); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// getComplexVal - Return the real/imag components of this complex value. |
| 65 | /// |
| 66 | std::pair<llvm::Value *, llvm::Value *> getComplexVal() const { |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 67 | return std::make_pair(V1.getPointer(), V2.getPointer()); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 68 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 70 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 71 | Address getAggregateAddress() const { |
| 72 | assert(isAggregate() && "Not an aggregate!"); |
| 73 | auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift; |
| 74 | return Address(V1.getPointer(), CharUnits::fromQuantity(align)); |
| 75 | } |
| 76 | llvm::Value *getAggregatePointer() const { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 77 | assert(isAggregate() && "Not an aggregate!"); |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 78 | return V1.getPointer(); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 79 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 81 | static RValue getIgnored() { |
| 82 | // FIXME: should we make this a more explicit state? |
| 83 | return get(nullptr); |
| 84 | } |
| 85 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 86 | static RValue get(llvm::Value *V) { |
| 87 | RValue ER; |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 88 | ER.V1.setPointer(V); |
| 89 | ER.V1.setInt(Scalar); |
| 90 | ER.V2.setInt(false); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 91 | return ER; |
| 92 | } |
| 93 | static RValue getComplex(llvm::Value *V1, llvm::Value *V2) { |
| 94 | RValue ER; |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 95 | ER.V1.setPointer(V1); |
| 96 | ER.V2.setPointer(V2); |
| 97 | ER.V1.setInt(Complex); |
| 98 | ER.V2.setInt(false); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 99 | return ER; |
| 100 | } |
| 101 | static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) { |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 102 | return getComplex(C.first, C.second); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 103 | } |
Mike Stump | 93700fc | 2009-05-23 20:21:36 +0000 | [diff] [blame] | 104 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 105 | // volatile or not. Remove default to find all places that probably get this |
| 106 | // wrong. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 107 | static RValue getAggregate(Address addr, bool isVolatile = false) { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 108 | RValue ER; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 109 | ER.V1.setPointer(addr.getPointer()); |
Benjamin Kramer | 2718b4d | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 110 | ER.V1.setInt(Aggregate); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 111 | |
| 112 | auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity()); |
| 113 | ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift)); |
| 114 | ER.V2.setInt(isVolatile); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 115 | return ER; |
| 116 | } |
| 117 | }; |
| 118 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 119 | /// Does an ARC strong l-value have precise lifetime? |
| 120 | enum ARCPreciseLifetime_t { |
John McCall | 4d31b81 | 2013-03-13 05:02:21 +0000 | [diff] [blame] | 121 | ARCImpreciseLifetime, ARCPreciseLifetime |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 122 | }; |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 123 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 124 | /// The source of the alignment of an l-value; an expression of |
| 125 | /// confidence in the alignment actually matching the estimate. |
| 126 | enum class AlignmentSource { |
| 127 | /// The l-value was an access to a declared entity or something |
| 128 | /// equivalently strong, like the address of an array allocated by a |
| 129 | /// language runtime. |
| 130 | Decl, |
| 131 | |
| 132 | /// The l-value was considered opaque, so the alignment was |
| 133 | /// determined from a type, but that type was an explicitly-aligned |
| 134 | /// typedef. |
| 135 | AttributedType, |
| 136 | |
| 137 | /// The l-value was considered opaque, so the alignment was |
| 138 | /// determined from a type. |
| 139 | Type |
| 140 | }; |
| 141 | |
| 142 | /// Given that the base address has the given alignment source, what's |
| 143 | /// our confidence in the alignment of the field? |
| 144 | static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) { |
| 145 | // For now, we don't distinguish fields of opaque pointers from |
| 146 | // top-level declarations, but maybe we should. |
| 147 | return AlignmentSource::Decl; |
| 148 | } |
| 149 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 150 | class LValueBaseInfo { |
| 151 | AlignmentSource AlignSource; |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 152 | |
| 153 | public: |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 154 | explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type) |
| 155 | : AlignSource(Source) {} |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 156 | AlignmentSource getAlignmentSource() const { return AlignSource; } |
| 157 | void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; } |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 158 | |
| 159 | void mergeForCast(const LValueBaseInfo &Info) { |
| 160 | setAlignmentSource(Info.getAlignmentSource()); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 164 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 165 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 166 | /// bitrange. |
| 167 | class LValue { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 168 | enum { |
| 169 | Simple, // This is a normal l-value, use getAddress(). |
| 170 | VectorElt, // This is a vector element l-value (V[i]), use getVector* |
| 171 | BitField, // This is a bitfield l-value, use getBitfield*. |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 172 | ExtVectorElt, // This is an extended vector subset, use getExtVectorComp |
Florian Hahn | 8f3f88d | 2020-06-01 19:42:03 +0100 | [diff] [blame] | 173 | GlobalReg, // This is a register l-value, use getGlobalReg() |
| 174 | MatrixElt // This is a matrix element, use getVector* |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 175 | } LVType; |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 177 | llvm::Value *V; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 179 | union { |
| 180 | // Index into a vector subscript: V[i] |
| 181 | llvm::Value *VectorIdx; |
| 182 | |
| 183 | // ExtVector element subset: V.xyx |
| 184 | llvm::Constant *VectorElts; |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 185 | |
Diogo Sampaio | 2147703 | 2020-01-21 15:31:33 +0000 | [diff] [blame] | 186 | // BitField start bit and size |
| 187 | const CGBitFieldInfo *BitFieldInfo; |
| 188 | }; |
Diogo Sampaio | 6a24339 | 2020-01-21 15:13:24 +0000 | [diff] [blame] | 189 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 190 | QualType Type; |
| 191 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 192 | // 'const' is unused here |
| 193 | Qualifiers Quals; |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 194 | |
Eli Friedman | 610bb87 | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 195 | // The alignment to use when accessing this lvalue. (For vector elements, |
| 196 | // this is the alignment of the whole vector.) |
Yaxun Liu | d938982 | 2018-03-14 15:02:28 +0000 | [diff] [blame] | 197 | unsigned Alignment; |
Daniel Dunbar | e3b8dd4 | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 198 | |
Fariborz Jahanian | 75686a5 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 199 | // objective-c's ivar |
| 200 | bool Ivar:1; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 201 | |
Fariborz Jahanian | 2e32ddc | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 202 | // objective-c's ivar is an array |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 203 | bool ObjIsArray:1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 205 | // LValue is non-gc'able for any reason, including being a parameter or local |
| 206 | // variable. |
| 207 | bool NonGC: 1; |
Fariborz Jahanian | 75686a5 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 208 | |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 209 | // Lvalue is a global reference of an objective-c object |
| 210 | bool GlobalObjCRef : 1; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 211 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 212 | // Lvalue is a thread local reference |
| 213 | bool ThreadLocalRef : 1; |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 214 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 215 | // Lvalue has ARC imprecise lifetime. We store this inverted to try |
| 216 | // to make the default bitfield pattern all-zeroes. |
| 217 | bool ImpreciseLifetime : 1; |
| 218 | |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 219 | // This flag shows if a nontemporal load/stores should be used when accessing |
| 220 | // this lvalue. |
| 221 | bool Nontemporal : 1; |
| 222 | |
Yaxun Liu | d938982 | 2018-03-14 15:02:28 +0000 | [diff] [blame] | 223 | LValueBaseInfo BaseInfo; |
| 224 | TBAAAccessInfo TBAAInfo; |
| 225 | |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 226 | Expr *BaseIvarExp; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 228 | private: |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 229 | void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment, |
| 230 | LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 231 | assert((!Alignment.isZero() || Type->isIncompleteType()) && |
| 232 | "initializing l-value with zero alignment!"); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 233 | this->Type = Type; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 234 | this->Quals = Quals; |
Yaxun Liu | d938982 | 2018-03-14 15:02:28 +0000 | [diff] [blame] | 235 | const unsigned MaxAlign = 1U << 31; |
| 236 | this->Alignment = Alignment.getQuantity() <= MaxAlign |
| 237 | ? Alignment.getQuantity() |
| 238 | : MaxAlign; |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 239 | assert(this->Alignment == Alignment.getQuantity() && |
| 240 | "Alignment exceeds allowed max!"); |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 241 | this->BaseInfo = BaseInfo; |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 242 | this->TBAAInfo = TBAAInfo; |
Daniel Dunbar | e3b8dd4 | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 243 | |
| 244 | // Initialize Objective-C flags. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 245 | this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false; |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 246 | this->ImpreciseLifetime = false; |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 247 | this->Nontemporal = false; |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 248 | this->ThreadLocalRef = false; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 249 | this->BaseIvarExp = nullptr; |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 250 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 252 | public: |
| 253 | bool isSimple() const { return LVType == Simple; } |
| 254 | bool isVectorElt() const { return LVType == VectorElt; } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 255 | bool isBitField() const { return LVType == BitField; } |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 256 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 257 | bool isGlobalReg() const { return LVType == GlobalReg; } |
Florian Hahn | 8f3f88d | 2020-06-01 19:42:03 +0100 | [diff] [blame] | 258 | bool isMatrixElt() const { return LVType == MatrixElt; } |
Daniel Dunbar | 9e22c0d | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 259 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 260 | bool isVolatileQualified() const { return Quals.hasVolatile(); } |
| 261 | bool isRestrictQualified() const { return Quals.hasRestrict(); } |
| 262 | unsigned getVRQualifiers() const { |
| 263 | return Quals.getCVRQualifiers() & ~Qualifiers::Const; |
Chris Lattner | e084c01 | 2009-02-16 22:25:49 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 266 | QualType getType() const { return Type; } |
| 267 | |
| 268 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
| 269 | return Quals.getObjCLifetime(); |
| 270 | } |
| 271 | |
Fariborz Jahanian | 75686a5 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 272 | bool isObjCIvar() const { return Ivar; } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 273 | void setObjCIvar(bool Value) { Ivar = Value; } |
| 274 | |
Fariborz Jahanian | 38c3ae9 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 275 | bool isObjCArray() const { return ObjIsArray; } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 276 | void setObjCArray(bool Value) { ObjIsArray = Value; } |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 277 | |
Fariborz Jahanian | 10bec10 | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 278 | bool isNonGC () const { return NonGC; } |
Daniel Dunbar | e50dda9 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 279 | void setNonGC(bool Value) { NonGC = Value; } |
| 280 | |
Fariborz Jahanian | 32ff7ae | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 281 | bool isGlobalObjCRef() const { return GlobalObjCRef; } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 282 | void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; } |
| 283 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 284 | bool isThreadLocalRef() const { return ThreadLocalRef; } |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 285 | void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;} |
| 286 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 287 | ARCPreciseLifetime_t isARCPreciseLifetime() const { |
| 288 | return ARCPreciseLifetime_t(!ImpreciseLifetime); |
| 289 | } |
| 290 | void setARCPreciseLifetime(ARCPreciseLifetime_t value) { |
| 291 | ImpreciseLifetime = (value == ARCImpreciseLifetime); |
| 292 | } |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 293 | bool isNontemporal() const { return Nontemporal; } |
| 294 | void setNontemporal(bool Value) { Nontemporal = Value; } |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 295 | |
Daniel Dunbar | 4bb04ce | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 296 | bool isObjCWeak() const { |
| 297 | return Quals.getObjCGCAttr() == Qualifiers::Weak; |
| 298 | } |
| 299 | bool isObjCStrong() const { |
| 300 | return Quals.getObjCGCAttr() == Qualifiers::Strong; |
| 301 | } |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 302 | |
| 303 | bool isVolatile() const { |
| 304 | return Quals.hasVolatile(); |
| 305 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 306 | |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 307 | Expr *getBaseIvarExp() const { return BaseIvarExp; } |
| 308 | void setBaseIvarExp(Expr *V) { BaseIvarExp = V; } |
Mon P Wang | acedf77 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 309 | |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 310 | TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; } |
| 311 | void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 312 | |
Daniel Dunbar | b657ac5 | 2010-08-21 03:29:54 +0000 | [diff] [blame] | 313 | const Qualifiers &getQuals() const { return Quals; } |
| 314 | Qualifiers &getQuals() { return Quals; } |
| 315 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 316 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
Mon P Wang | acedf77 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 317 | |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 318 | CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); } |
| 319 | void setAlignment(CharUnits A) { Alignment = A.getQuantity(); } |
Daniel Dunbar | e3b8dd4 | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 320 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 321 | LValueBaseInfo getBaseInfo() const { return BaseInfo; } |
| 322 | void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 324 | // simple lvalue |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 325 | llvm::Value *getPointer(CodeGenFunction &CGF) const { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 326 | assert(isSimple()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 327 | return V; |
| 328 | } |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 329 | Address getAddress(CodeGenFunction &CGF) const { |
| 330 | return Address(getPointer(CGF), getAlignment()); |
| 331 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 332 | void setAddress(Address address) { |
| 333 | assert(isSimple()); |
| 334 | V = address.getPointer(); |
| 335 | Alignment = address.getAlignment().getQuantity(); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 336 | } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 337 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 338 | // vector elt lvalue |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 339 | Address getVectorAddress() const { |
| 340 | return Address(getVectorPointer(), getAlignment()); |
| 341 | } |
Florian Hahn | 8f3f88d | 2020-06-01 19:42:03 +0100 | [diff] [blame] | 342 | llvm::Value *getVectorPointer() const { |
| 343 | assert(isVectorElt()); |
| 344 | return V; |
| 345 | } |
| 346 | llvm::Value *getVectorIdx() const { |
| 347 | assert(isVectorElt()); |
| 348 | return VectorIdx; |
| 349 | } |
| 350 | |
| 351 | Address getMatrixAddress() const { |
| 352 | return Address(getMatrixPointer(), getAlignment()); |
| 353 | } |
| 354 | llvm::Value *getMatrixPointer() const { |
| 355 | assert(isMatrixElt()); |
| 356 | return V; |
| 357 | } |
| 358 | llvm::Value *getMatrixIdx() const { |
| 359 | assert(isMatrixElt()); |
| 360 | return VectorIdx; |
| 361 | } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 363 | // extended vector elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 364 | Address getExtVectorAddress() const { |
| 365 | return Address(getExtVectorPointer(), getAlignment()); |
| 366 | } |
| 367 | llvm::Value *getExtVectorPointer() const { |
| 368 | assert(isExtVectorElt()); |
| 369 | return V; |
| 370 | } |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 371 | llvm::Constant *getExtVectorElts() const { |
| 372 | assert(isExtVectorElt()); |
| 373 | return VectorElts; |
| 374 | } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 376 | // bitfield lvalue |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 377 | Address getBitFieldAddress() const { |
| 378 | return Address(getBitFieldPointer(), getAlignment()); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 379 | } |
Diogo Sampaio | 2147703 | 2020-01-21 15:31:33 +0000 | [diff] [blame] | 380 | llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 381 | const CGBitFieldInfo &getBitFieldInfo() const { |
| 382 | assert(isBitField()); |
Diogo Sampaio | 2147703 | 2020-01-21 15:31:33 +0000 | [diff] [blame] | 383 | return *BitFieldInfo; |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 384 | } |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 385 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 386 | // global register lvalue |
| 387 | llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; } |
| 388 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 389 | static LValue MakeAddr(Address address, QualType type, ASTContext &Context, |
| 390 | LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 391 | Qualifiers qs = type.getQualifiers(); |
| 392 | qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); |
Daniel Dunbar | 226bdda | 2010-08-21 03:58:45 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 394 | LValue R; |
| 395 | R.LVType = Simple; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 396 | assert(address.getPointer()->getType()->isPointerTy()); |
| 397 | R.V = address.getPointer(); |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 398 | R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 399 | return R; |
| 400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 402 | static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx, |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 403 | QualType type, LValueBaseInfo BaseInfo, |
| 404 | TBAAAccessInfo TBAAInfo) { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 405 | LValue R; |
| 406 | R.LVType = VectorElt; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 407 | R.V = vecAddress.getPointer(); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 408 | R.VectorIdx = Idx; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 409 | R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(), |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 410 | BaseInfo, TBAAInfo); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 411 | return R; |
| 412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 414 | static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts, |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 415 | QualType type, LValueBaseInfo BaseInfo, |
| 416 | TBAAAccessInfo TBAAInfo) { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 417 | LValue R; |
| 418 | R.LVType = ExtVectorElt; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 419 | R.V = vecAddress.getPointer(); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 420 | R.VectorElts = Elts; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 421 | R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(), |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 422 | BaseInfo, TBAAInfo); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 423 | return R; |
| 424 | } |
| 425 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 426 | /// Create a new object to represent a bit-field access. |
Daniel Dunbar | c75c8bd | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 427 | /// |
NAKAMURA Takumi | 368d2ee | 2012-12-24 01:48:48 +0000 | [diff] [blame] | 428 | /// \param Addr - The base address of the bit-field sequence this |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 429 | /// bit-field refers to. |
Daniel Dunbar | c75c8bd | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 430 | /// \param Info - The information describing how to perform the bit-field |
| 431 | /// access. |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 432 | static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info, |
| 433 | QualType type, LValueBaseInfo BaseInfo, |
| 434 | TBAAAccessInfo TBAAInfo) { |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 435 | LValue R; |
| 436 | R.LVType = BitField; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 437 | R.V = Addr.getPointer(); |
Diogo Sampaio | 2147703 | 2020-01-21 15:31:33 +0000 | [diff] [blame] | 438 | R.BitFieldInfo = &Info; |
Ivan A. Kosarev | d17f12a | 2017-10-17 10:17:43 +0000 | [diff] [blame] | 439 | R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo, |
| 440 | TBAAInfo); |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 441 | return R; |
| 442 | } |
Eli Friedman | 2869b5a | 2011-12-03 03:08:40 +0000 | [diff] [blame] | 443 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 444 | static LValue MakeGlobalReg(Address Reg, QualType type) { |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 445 | LValue R; |
| 446 | R.LVType = GlobalReg; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 447 | R.V = Reg.getPointer(); |
| 448 | R.Initialize(type, type.getQualifiers(), Reg.getAlignment(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 449 | LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo()); |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 450 | return R; |
| 451 | } |
| 452 | |
Florian Hahn | 8f3f88d | 2020-06-01 19:42:03 +0100 | [diff] [blame] | 453 | static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx, |
| 454 | QualType type, LValueBaseInfo BaseInfo, |
| 455 | TBAAAccessInfo TBAAInfo) { |
| 456 | LValue R; |
| 457 | R.LVType = MatrixElt; |
| 458 | R.V = matAddress.getPointer(); |
| 459 | R.VectorIdx = Idx; |
| 460 | R.Initialize(type, type.getQualifiers(), matAddress.getAlignment(), |
| 461 | BaseInfo, TBAAInfo); |
| 462 | return R; |
| 463 | } |
| 464 | |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 465 | RValue asAggregateRValue(CodeGenFunction &CGF) const { |
| 466 | return RValue::getAggregate(getAddress(CGF), isVolatileQualified()); |
Eli Friedman | 2869b5a | 2011-12-03 03:08:40 +0000 | [diff] [blame] | 467 | } |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 470 | /// An aggregate value slot. |
| 471 | class AggValueSlot { |
Fariborz Jahanian | b60e70f | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 472 | /// The address. |
John McCall | 2a8b9a3 | 2010-09-16 03:16:41 +0000 | [diff] [blame] | 473 | llvm::Value *Addr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 474 | |
| 475 | // Qualifiers |
| 476 | Qualifiers Quals; |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 477 | |
David Majnemer | ec4b734 | 2016-03-02 06:48:47 +0000 | [diff] [blame] | 478 | unsigned Alignment; |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 479 | |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 480 | /// DestructedFlag - This is set to true if some external code is |
| 481 | /// responsible for setting up a destructor for the slot. Otherwise |
| 482 | /// the code which constructs it should push the appropriate cleanup. |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 483 | bool DestructedFlag : 1; |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 484 | |
| 485 | /// ObjCGCFlag - This is set to true if writing to the memory in the |
| 486 | /// slot might require calling an appropriate Objective-C GC |
| 487 | /// barrier. The exact interaction here is unnecessarily mysterious. |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 488 | bool ObjCGCFlag : 1; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 489 | |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 490 | /// ZeroedFlag - This is set to true if the memory in the slot is |
| 491 | /// known to be zero before the assignment into it. This means that |
| 492 | /// zero fields don't need to be set. |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 493 | bool ZeroedFlag : 1; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 494 | |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 495 | /// AliasedFlag - This is set to true if the slot might be aliased |
| 496 | /// and it's not undefined behavior to access it through such an |
| 497 | /// alias. Note that it's always undefined behavior to access a C++ |
| 498 | /// object that's under construction through an alias derived from |
| 499 | /// outside the construction process. |
| 500 | /// |
| 501 | /// This flag controls whether calls that produce the aggregate |
| 502 | /// value may be evaluated directly into the slot, or whether they |
| 503 | /// must be evaluated into an unaliased temporary and then memcpy'ed |
| 504 | /// over. Since it's invalid in general to memcpy a non-POD C++ |
| 505 | /// object, it's important that this flag never be set when |
| 506 | /// evaluating an expression which constructs such an object. |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 507 | bool AliasedFlag : 1; |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 508 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 509 | /// This is set to true if the tail padding of this slot might overlap |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 510 | /// another object that may have already been initialized (and whose |
| 511 | /// value must be preserved by this initialization). If so, we may only |
| 512 | /// store up to the dsize of the type. Otherwise we can widen stores to |
| 513 | /// the size of the type. |
| 514 | bool OverlapFlag : 1; |
| 515 | |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 516 | /// If is set to true, sanitizer checks are already generated for this address |
| 517 | /// or not required. For instance, if this address represents an object |
| 518 | /// created in 'new' expression, sanitizer checks for memory is made as a part |
| 519 | /// of 'operator new' emission and object constructor should not generate |
| 520 | /// them. |
| 521 | bool SanitizerCheckedFlag : 1; |
| 522 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 523 | public: |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 524 | enum IsAliased_t { IsNotAliased, IsAliased }; |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 525 | enum IsDestructed_t { IsNotDestructed, IsDestructed }; |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 526 | enum IsZeroed_t { IsNotZeroed, IsZeroed }; |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 527 | enum Overlap_t { DoesNotOverlap, MayOverlap }; |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 528 | enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers }; |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 529 | enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked }; |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 530 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 531 | /// ignored - Returns an aggregate value slot indicating that the |
| 532 | /// aggregate value is being ignored. |
| 533 | static AggValueSlot ignored() { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 534 | return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 535 | DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /// forAddr - Make a slot for an aggregate value. |
| 539 | /// |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 540 | /// \param quals - The qualifiers that dictate how the slot should |
| 541 | /// be initialied. Only 'volatile' and the Objective-C lifetime |
| 542 | /// qualifiers matter. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 543 | /// |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 544 | /// \param isDestructed - true if something else is responsible |
| 545 | /// for calling destructors on this object |
| 546 | /// \param needsGC - true if the slot is potentially located |
John McCall | 58649dc | 2010-09-16 03:13:23 +0000 | [diff] [blame] | 547 | /// somewhere that ObjC GC calls should be emitted for |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 548 | static AggValueSlot forAddr(Address addr, |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 549 | Qualifiers quals, |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 550 | IsDestructed_t isDestructed, |
| 551 | NeedsGCBarriers_t needsGC, |
John McCall | 46759f4 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 552 | IsAliased_t isAliased, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 553 | Overlap_t mayOverlap, |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 554 | IsZeroed_t isZeroed = IsNotZeroed, |
| 555 | IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) { |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 556 | AggValueSlot AV; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 557 | if (addr.isValid()) { |
| 558 | AV.Addr = addr.getPointer(); |
| 559 | AV.Alignment = addr.getAlignment().getQuantity(); |
| 560 | } else { |
| 561 | AV.Addr = nullptr; |
| 562 | AV.Alignment = 0; |
| 563 | } |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 564 | AV.Quals = quals; |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 565 | AV.DestructedFlag = isDestructed; |
| 566 | AV.ObjCGCFlag = needsGC; |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 567 | AV.ZeroedFlag = isZeroed; |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 568 | AV.AliasedFlag = isAliased; |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 569 | AV.OverlapFlag = mayOverlap; |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 570 | AV.SanitizerCheckedFlag = isChecked; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 571 | return AV; |
| 572 | } |
| 573 | |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 574 | static AggValueSlot |
| 575 | forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed, |
| 576 | NeedsGCBarriers_t needsGC, IsAliased_t isAliased, |
| 577 | Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed, |
| 578 | IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) { |
| 579 | return forAddr(LV.getAddress(CGF), LV.getQuals(), isDestructed, needsGC, |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 580 | isAliased, mayOverlap, isZeroed, isChecked); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 581 | } |
Fariborz Jahanian | c123623 | 2010-10-22 22:05:03 +0000 | [diff] [blame] | 582 | |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 583 | IsDestructed_t isExternallyDestructed() const { |
| 584 | return IsDestructed_t(DestructedFlag); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 585 | } |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 586 | void setExternallyDestructed(bool destructed = true) { |
| 587 | DestructedFlag = destructed; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 588 | } |
| 589 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 590 | Qualifiers getQualifiers() const { return Quals; } |
| 591 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 592 | bool isVolatile() const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 593 | return Quals.hasVolatile(); |
| 594 | } |
| 595 | |
Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 596 | void setVolatile(bool flag) { |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 597 | if (flag) |
| 598 | Quals.addVolatile(); |
| 599 | else |
| 600 | Quals.removeVolatile(); |
Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 601 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 602 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 603 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
| 604 | return Quals.getObjCLifetime(); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 605 | } |
| 606 | |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 607 | NeedsGCBarriers_t requiresGCollection() const { |
John McCall | cac9385 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 608 | return NeedsGCBarriers_t(ObjCGCFlag); |
Fariborz Jahanian | b60e70f | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 609 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 610 | |
| 611 | llvm::Value *getPointer() const { |
John McCall | 2a8b9a3 | 2010-09-16 03:16:41 +0000 | [diff] [blame] | 612 | return Addr; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 613 | } |
| 614 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 615 | Address getAddress() const { |
| 616 | return Address(Addr, getAlignment()); |
| 617 | } |
| 618 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 619 | bool isIgnored() const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 620 | return Addr == nullptr; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 623 | CharUnits getAlignment() const { |
| 624 | return CharUnits::fromQuantity(Alignment); |
Eli Friedman | c1d85b9 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 625 | } |
| 626 | |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 627 | IsAliased_t isPotentiallyAliased() const { |
| 628 | return IsAliased_t(AliasedFlag); |
| 629 | } |
| 630 | |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 631 | Overlap_t mayOverlap() const { |
| 632 | return Overlap_t(OverlapFlag); |
| 633 | } |
| 634 | |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 635 | bool isSanitizerChecked() const { |
| 636 | return SanitizerCheckedFlag; |
| 637 | } |
| 638 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 639 | RValue asRValue() const { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 640 | if (isIgnored()) { |
| 641 | return RValue::getIgnored(); |
| 642 | } else { |
| 643 | return RValue::getAggregate(getAddress(), isVolatile()); |
| 644 | } |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 645 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 646 | |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 647 | void setZeroed(bool V = true) { ZeroedFlag = V; } |
| 648 | IsZeroed_t isZeroed() const { |
| 649 | return IsZeroed_t(ZeroedFlag); |
Chris Lattner | 27a3631 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 650 | } |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 651 | |
| 652 | /// Get the preferred size to use when storing a value to this slot. This |
| 653 | /// is the type size unless that might overlap another object, in which |
| 654 | /// case it's the dsize. |
| 655 | CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const { |
Bevin Hansson | 101309f | 2020-08-24 10:19:29 +0200 | [diff] [blame] | 656 | return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).Width |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 657 | : Ctx.getTypeSizeInChars(Type); |
| 658 | } |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
Daniel Dunbar | d4f616b | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 661 | } // end namespace CodeGen |
| 662 | } // end namespace clang |
| 663 | |
| 664 | #endif |