blob: 9fd07bdb187d4bb7213882512b97ef20dc2095d9 [file] [log] [blame]
Daniel Dunbard4f616b2008-08-23 03:10:25 +00001//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Dunbard4f616b2008-08-23 03:10:25 +00006//
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 Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
15#define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
Daniel Dunbard4f616b2008-08-23 03:10:25 +000016
Daniel Dunbar93b00a92010-08-21 02:53:44 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000018#include "clang/AST/Type.h"
John McCall47fb9502013-03-07 21:37:08 +000019#include "llvm/IR/Value.h"
David Blaikiee3b172a2015-04-02 18:55:21 +000020#include "llvm/IR/Type.h"
John McCall7f416cc2015-09-08 08:05:57 +000021#include "Address.h"
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000022#include "CodeGenTBAA.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000023
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000024namespace llvm {
25 class Constant;
John McCall47fb9502013-03-07 21:37:08 +000026 class MDNode;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000027}
28
Daniel Dunbard4f616b2008-08-23 03:10:25 +000029namespace clang {
30namespace CodeGen {
John McCallfe96e0b2011-11-06 09:01:30 +000031 class AggValueSlot;
Akira Hatanakaf139ae32019-12-03 15:17:01 -080032 class CodeGenFunction;
Chandler Carruthff0e3a12012-12-06 11:14:44 +000033 struct CGBitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +000034
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.
39class RValue {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000040 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump11289f42009-09-09 15:08:12 +000041
John McCall7f416cc2015-09-08 08:05:57 +000042 // The shift to make to an aggregate's alignment to make it look
43 // like a pointer.
44 enum { AggAlignShift = 4 };
45
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000046 // 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 Dunbard4f616b2008-08-23 03:10:25 +000051public:
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000052 bool isScalar() const { return V1.getInt() == Scalar; }
53 bool isComplex() const { return V1.getInt() == Complex; }
54 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump11289f42009-09-09 15:08:12 +000055
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000056 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump93700fc2009-05-23 20:21:36 +000057
Mike Stump462a4aa2009-11-03 16:11:57 +000058 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbard4f616b2008-08-23 03:10:25 +000059 llvm::Value *getScalarVal() const {
60 assert(isScalar() && "Not a scalar!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000061 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000062 }
63
64 /// getComplexVal - Return the real/imag components of this complex value.
65 ///
66 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000067 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbard4f616b2008-08-23 03:10:25 +000068 }
Mike Stump11289f42009-09-09 15:08:12 +000069
Daniel Dunbard4f616b2008-08-23 03:10:25 +000070 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
John McCall7f416cc2015-09-08 08:05:57 +000071 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 Dunbard4f616b2008-08-23 03:10:25 +000077 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000078 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
John McCall7f416cc2015-09-08 08:05:57 +000081 static RValue getIgnored() {
82 // FIXME: should we make this a more explicit state?
83 return get(nullptr);
84 }
85
Daniel Dunbard4f616b2008-08-23 03:10:25 +000086 static RValue get(llvm::Value *V) {
87 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000088 ER.V1.setPointer(V);
89 ER.V1.setInt(Scalar);
90 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000091 return ER;
92 }
93 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
94 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000095 ER.V1.setPointer(V1);
96 ER.V2.setPointer(V2);
97 ER.V1.setInt(Complex);
98 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000099 return ER;
100 }
101 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000102 return getComplex(C.first, C.second);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000103 }
Mike Stump93700fc2009-05-23 20:21:36 +0000104 // 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 McCall7f416cc2015-09-08 08:05:57 +0000107 static RValue getAggregate(Address addr, bool isVolatile = false) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000108 RValue ER;
John McCall7f416cc2015-09-08 08:05:57 +0000109 ER.V1.setPointer(addr.getPointer());
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000110 ER.V1.setInt(Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +0000111
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 Dunbard4f616b2008-08-23 03:10:25 +0000115 return ER;
116 }
117};
118
John McCallcdda29c2013-03-13 03:10:54 +0000119/// Does an ARC strong l-value have precise lifetime?
120enum ARCPreciseLifetime_t {
John McCall4d31b812013-03-13 05:02:21 +0000121 ARCImpreciseLifetime, ARCPreciseLifetime
John McCallcdda29c2013-03-13 03:10:54 +0000122};
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000123
John McCall7f416cc2015-09-08 08:05:57 +0000124/// The source of the alignment of an l-value; an expression of
125/// confidence in the alignment actually matching the estimate.
126enum 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?
144static 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 Parzyszek8f248232017-05-18 17:07:11 +0000150class LValueBaseInfo {
151 AlignmentSource AlignSource;
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000152
153public:
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000154 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
155 : AlignSource(Source) {}
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000156 AlignmentSource getAlignmentSource() const { return AlignSource; }
157 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000158
159 void mergeForCast(const LValueBaseInfo &Info) {
160 setAlignmentSource(Info.getAlignmentSource());
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000161 }
162};
163
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000164/// 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.
167class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000168 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 Golin230c5eb2014-05-19 18:15:42 +0000172 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
173 GlobalReg // This is a register l-value, use getGlobalReg()
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000174 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000175
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000176 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000177
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000178 union {
179 // Index into a vector subscript: V[i]
180 llvm::Value *VectorIdx;
181
182 // ExtVector element subset: V.xyx
183 llvm::Constant *VectorElts;
Mike Stump11289f42009-09-09 15:08:12 +0000184
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000185 // BitField start bit and size
Daniel Dunbardc406b82010-04-05 21:36:35 +0000186 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000187 };
188
John McCall1553b192011-06-16 04:16:24 +0000189 QualType Type;
190
John McCall8ccfcb52009-09-24 19:53:00 +0000191 // 'const' is unused here
192 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000193
Eli Friedman610bb872012-03-22 22:36:39 +0000194 // The alignment to use when accessing this lvalue. (For vector elements,
195 // this is the alignment of the whole vector.)
Yaxun Liud9389822018-03-14 15:02:28 +0000196 unsigned Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000197
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000198 // objective-c's ivar
199 bool Ivar:1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000200
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000201 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000202 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000203
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000204 // LValue is non-gc'able for any reason, including being a parameter or local
205 // variable.
206 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000207
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000208 // Lvalue is a global reference of an objective-c object
209 bool GlobalObjCRef : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000210
Fariborz Jahanian217af242010-07-20 20:30:03 +0000211 // Lvalue is a thread local reference
212 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000213
John McCallcdda29c2013-03-13 03:10:54 +0000214 // Lvalue has ARC imprecise lifetime. We store this inverted to try
215 // to make the default bitfield pattern all-zeroes.
216 bool ImpreciseLifetime : 1;
217
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000218 // This flag shows if a nontemporal load/stores should be used when accessing
219 // this lvalue.
220 bool Nontemporal : 1;
221
Yaxun Liud9389822018-03-14 15:02:28 +0000222 LValueBaseInfo BaseInfo;
223 TBAAAccessInfo TBAAInfo;
224
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000225 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000226
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000227private:
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000228 void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment,
229 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall7f416cc2015-09-08 08:05:57 +0000230 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
231 "initializing l-value with zero alignment!");
John McCall1553b192011-06-16 04:16:24 +0000232 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000233 this->Quals = Quals;
Yaxun Liud9389822018-03-14 15:02:28 +0000234 const unsigned MaxAlign = 1U << 31;
235 this->Alignment = Alignment.getQuantity() <= MaxAlign
236 ? Alignment.getQuantity()
237 : MaxAlign;
Eli Friedmana0544d62011-12-03 04:14:32 +0000238 assert(this->Alignment == Alignment.getQuantity() &&
239 "Alignment exceeds allowed max!");
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000240 this->BaseInfo = BaseInfo;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000241 this->TBAAInfo = TBAAInfo;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000242
243 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000244 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000245 this->ImpreciseLifetime = false;
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000246 this->Nontemporal = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000247 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000248 this->BaseIvarExp = nullptr;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000249 }
Mike Stump11289f42009-09-09 15:08:12 +0000250
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000251public:
252 bool isSimple() const { return LVType == Simple; }
253 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000254 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000255 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000256 bool isGlobalReg() const { return LVType == GlobalReg; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000257
John McCall8ccfcb52009-09-24 19:53:00 +0000258 bool isVolatileQualified() const { return Quals.hasVolatile(); }
259 bool isRestrictQualified() const { return Quals.hasRestrict(); }
260 unsigned getVRQualifiers() const {
261 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
John McCall1553b192011-06-16 04:16:24 +0000264 QualType getType() const { return Type; }
265
266 Qualifiers::ObjCLifetime getObjCLifetime() const {
267 return Quals.getObjCLifetime();
268 }
269
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000270 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000271 void setObjCIvar(bool Value) { Ivar = Value; }
272
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000273 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000274 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000275
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000276 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000277 void setNonGC(bool Value) { NonGC = Value; }
278
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000279 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000280 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
281
Fariborz Jahanian217af242010-07-20 20:30:03 +0000282 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000283 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
284
John McCallcdda29c2013-03-13 03:10:54 +0000285 ARCPreciseLifetime_t isARCPreciseLifetime() const {
286 return ARCPreciseLifetime_t(!ImpreciseLifetime);
287 }
288 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
289 ImpreciseLifetime = (value == ARCImpreciseLifetime);
290 }
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000291 bool isNontemporal() const { return Nontemporal; }
292 void setNontemporal(bool Value) { Nontemporal = Value; }
John McCallcdda29c2013-03-13 03:10:54 +0000293
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000294 bool isObjCWeak() const {
295 return Quals.getObjCGCAttr() == Qualifiers::Weak;
296 }
297 bool isObjCStrong() const {
298 return Quals.getObjCGCAttr() == Qualifiers::Strong;
299 }
John McCall1553b192011-06-16 04:16:24 +0000300
301 bool isVolatile() const {
302 return Quals.hasVolatile();
303 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000304
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000305 Expr *getBaseIvarExp() const { return BaseIvarExp; }
306 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000307
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000308 TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
309 void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
Manman Renc451e572013-04-04 21:53:22 +0000310
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000311 const Qualifiers &getQuals() const { return Quals; }
312 Qualifiers &getQuals() { return Quals; }
313
Alexander Richardson6d989432017-10-15 18:48:14 +0000314 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000315
Eli Friedmana0544d62011-12-03 04:14:32 +0000316 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
317 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000318
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000319 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
320 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
John McCall7f416cc2015-09-08 08:05:57 +0000321
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000322 // simple lvalue
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800323 llvm::Value *getPointer(CodeGenFunction &CGF) const {
John McCall1553b192011-06-16 04:16:24 +0000324 assert(isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000325 return V;
326 }
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800327 Address getAddress(CodeGenFunction &CGF) const {
328 return Address(getPointer(CGF), getAlignment());
329 }
John McCall7f416cc2015-09-08 08:05:57 +0000330 void setAddress(Address address) {
331 assert(isSimple());
332 V = address.getPointer();
333 Alignment = address.getAlignment().getQuantity();
John McCall1553b192011-06-16 04:16:24 +0000334 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000335
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000336 // vector elt lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000337 Address getVectorAddress() const {
338 return Address(getVectorPointer(), getAlignment());
339 }
340 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000341 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000342
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000343 // extended vector elements.
John McCall7f416cc2015-09-08 08:05:57 +0000344 Address getExtVectorAddress() const {
345 return Address(getExtVectorPointer(), getAlignment());
346 }
347 llvm::Value *getExtVectorPointer() const {
348 assert(isExtVectorElt());
349 return V;
350 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000351 llvm::Constant *getExtVectorElts() const {
352 assert(isExtVectorElt());
353 return VectorElts;
354 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000355
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000356 // bitfield lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000357 Address getBitFieldAddress() const {
358 return Address(getBitFieldPointer(), getAlignment());
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000359 }
John McCall7f416cc2015-09-08 08:05:57 +0000360 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000361 const CGBitFieldInfo &getBitFieldInfo() const {
362 assert(isBitField());
363 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000364 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000365
Renato Golin230c5eb2014-05-19 18:15:42 +0000366 // global register lvalue
367 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
368
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000369 static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
370 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall1553b192011-06-16 04:16:24 +0000371 Qualifiers qs = type.getQualifiers();
372 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000373
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000374 LValue R;
375 R.LVType = Simple;
John McCall7f416cc2015-09-08 08:05:57 +0000376 assert(address.getPointer()->getType()->isPointerTy());
377 R.V = address.getPointer();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000378 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000379 return R;
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
John McCall7f416cc2015-09-08 08:05:57 +0000382 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000383 QualType type, LValueBaseInfo BaseInfo,
384 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000385 LValue R;
386 R.LVType = VectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000387 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000388 R.VectorIdx = Idx;
John McCall7f416cc2015-09-08 08:05:57 +0000389 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000390 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000391 return R;
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
John McCall7f416cc2015-09-08 08:05:57 +0000394 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000395 QualType type, LValueBaseInfo BaseInfo,
396 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000397 LValue R;
398 R.LVType = ExtVectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000399 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000400 R.VectorElts = Elts;
John McCall7f416cc2015-09-08 08:05:57 +0000401 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000402 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000403 return R;
404 }
405
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000406 /// Create a new object to represent a bit-field access.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000407 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000408 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000409 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000410 /// \param Info - The information describing how to perform the bit-field
411 /// access.
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000412 static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
413 QualType type, LValueBaseInfo BaseInfo,
414 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000415 LValue R;
416 R.LVType = BitField;
John McCall7f416cc2015-09-08 08:05:57 +0000417 R.V = Addr.getPointer();
Daniel Dunbardc406b82010-04-05 21:36:35 +0000418 R.BitFieldInfo = &Info;
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000419 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
420 TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000421 return R;
422 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000423
John McCall7f416cc2015-09-08 08:05:57 +0000424 static LValue MakeGlobalReg(Address Reg, QualType type) {
Renato Golin230c5eb2014-05-19 18:15:42 +0000425 LValue R;
426 R.LVType = GlobalReg;
John McCall7f416cc2015-09-08 08:05:57 +0000427 R.V = Reg.getPointer();
428 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000429 LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
Renato Golin230c5eb2014-05-19 18:15:42 +0000430 return R;
431 }
432
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800433 RValue asAggregateRValue(CodeGenFunction &CGF) const {
434 return RValue::getAggregate(getAddress(CGF), isVolatileQualified());
Eli Friedman2869b5a2011-12-03 03:08:40 +0000435 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000436};
437
John McCall7a626f62010-09-15 10:14:12 +0000438/// An aggregate value slot.
439class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000440 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000441 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000442
443 // Qualifiers
444 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000445
David Majnemerec4b7342016-03-02 06:48:47 +0000446 unsigned Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000447
John McCallcac93852011-08-26 08:02:37 +0000448 /// DestructedFlag - This is set to true if some external code is
449 /// responsible for setting up a destructor for the slot. Otherwise
450 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000451 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000452
453 /// ObjCGCFlag - This is set to true if writing to the memory in the
454 /// slot might require calling an appropriate Objective-C GC
455 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000456 bool ObjCGCFlag : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000457
John McCallcac93852011-08-26 08:02:37 +0000458 /// ZeroedFlag - This is set to true if the memory in the slot is
459 /// known to be zero before the assignment into it. This means that
460 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000461 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000462
John McCallcac93852011-08-26 08:02:37 +0000463 /// AliasedFlag - This is set to true if the slot might be aliased
464 /// and it's not undefined behavior to access it through such an
465 /// alias. Note that it's always undefined behavior to access a C++
466 /// object that's under construction through an alias derived from
467 /// outside the construction process.
468 ///
469 /// This flag controls whether calls that produce the aggregate
470 /// value may be evaluated directly into the slot, or whether they
471 /// must be evaluated into an unaliased temporary and then memcpy'ed
472 /// over. Since it's invalid in general to memcpy a non-POD C++
473 /// object, it's important that this flag never be set when
474 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000475 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000476
Fangrui Song6907ce22018-07-30 19:24:48 +0000477 /// This is set to true if the tail padding of this slot might overlap
Richard Smithe78fac52018-04-05 20:52:58 +0000478 /// another object that may have already been initialized (and whose
479 /// value must be preserved by this initialization). If so, we may only
480 /// store up to the dsize of the type. Otherwise we can widen stores to
481 /// the size of the type.
482 bool OverlapFlag : 1;
483
Serge Pavlov37605182018-07-28 15:33:03 +0000484 /// If is set to true, sanitizer checks are already generated for this address
485 /// or not required. For instance, if this address represents an object
486 /// created in 'new' expression, sanitizer checks for memory is made as a part
487 /// of 'operator new' emission and object constructor should not generate
488 /// them.
489 bool SanitizerCheckedFlag : 1;
490
John McCall7a626f62010-09-15 10:14:12 +0000491public:
John McCalla5efa732011-08-25 23:04:34 +0000492 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000493 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000494 enum IsZeroed_t { IsNotZeroed, IsZeroed };
Richard Smithe78fac52018-04-05 20:52:58 +0000495 enum Overlap_t { DoesNotOverlap, MayOverlap };
John McCall8d6fc952011-08-25 20:40:09 +0000496 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
Serge Pavlov37605182018-07-28 15:33:03 +0000497 enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
John McCall8d6fc952011-08-25 20:40:09 +0000498
John McCall7a626f62010-09-15 10:14:12 +0000499 /// ignored - Returns an aggregate value slot indicating that the
500 /// aggregate value is being ignored.
501 static AggValueSlot ignored() {
John McCall7f416cc2015-09-08 08:05:57 +0000502 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
Richard Smithe78fac52018-04-05 20:52:58 +0000503 DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
John McCall7a626f62010-09-15 10:14:12 +0000504 }
505
506 /// forAddr - Make a slot for an aggregate value.
507 ///
John McCall8d6fc952011-08-25 20:40:09 +0000508 /// \param quals - The qualifiers that dictate how the slot should
509 /// be initialied. Only 'volatile' and the Objective-C lifetime
510 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000511 ///
John McCall8d6fc952011-08-25 20:40:09 +0000512 /// \param isDestructed - true if something else is responsible
513 /// for calling destructors on this object
514 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000515 /// somewhere that ObjC GC calls should be emitted for
John McCall7f416cc2015-09-08 08:05:57 +0000516 static AggValueSlot forAddr(Address addr,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000517 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000518 IsDestructed_t isDestructed,
519 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000520 IsAliased_t isAliased,
Richard Smithe78fac52018-04-05 20:52:58 +0000521 Overlap_t mayOverlap,
Serge Pavlov37605182018-07-28 15:33:03 +0000522 IsZeroed_t isZeroed = IsNotZeroed,
523 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
John McCall7a626f62010-09-15 10:14:12 +0000524 AggValueSlot AV;
John McCall7f416cc2015-09-08 08:05:57 +0000525 if (addr.isValid()) {
526 AV.Addr = addr.getPointer();
527 AV.Alignment = addr.getAlignment().getQuantity();
528 } else {
529 AV.Addr = nullptr;
530 AV.Alignment = 0;
531 }
John McCall8d6fc952011-08-25 20:40:09 +0000532 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000533 AV.DestructedFlag = isDestructed;
534 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000535 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000536 AV.AliasedFlag = isAliased;
Richard Smithe78fac52018-04-05 20:52:58 +0000537 AV.OverlapFlag = mayOverlap;
Serge Pavlov37605182018-07-28 15:33:03 +0000538 AV.SanitizerCheckedFlag = isChecked;
John McCall7a626f62010-09-15 10:14:12 +0000539 return AV;
540 }
541
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800542 static AggValueSlot
543 forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed,
544 NeedsGCBarriers_t needsGC, IsAliased_t isAliased,
545 Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed,
546 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
547 return forAddr(LV.getAddress(CGF), LV.getQuals(), isDestructed, needsGC,
Serge Pavlov37605182018-07-28 15:33:03 +0000548 isAliased, mayOverlap, isZeroed, isChecked);
John McCall7a626f62010-09-15 10:14:12 +0000549 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000550
John McCallcac93852011-08-26 08:02:37 +0000551 IsDestructed_t isExternallyDestructed() const {
552 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000553 }
John McCallcac93852011-08-26 08:02:37 +0000554 void setExternallyDestructed(bool destructed = true) {
555 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000556 }
557
John McCall31168b02011-06-15 23:02:42 +0000558 Qualifiers getQualifiers() const { return Quals; }
559
John McCall7a626f62010-09-15 10:14:12 +0000560 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000561 return Quals.hasVolatile();
562 }
563
Fariborz Jahanian78652202013-01-25 23:57:05 +0000564 void setVolatile(bool flag) {
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000565 if (flag)
566 Quals.addVolatile();
567 else
568 Quals.removeVolatile();
Fariborz Jahanian78652202013-01-25 23:57:05 +0000569 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000570
John McCall31168b02011-06-15 23:02:42 +0000571 Qualifiers::ObjCLifetime getObjCLifetime() const {
572 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000573 }
574
John McCall8d6fc952011-08-25 20:40:09 +0000575 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000576 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000577 }
John McCall7f416cc2015-09-08 08:05:57 +0000578
579 llvm::Value *getPointer() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000580 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000581 }
582
John McCall7f416cc2015-09-08 08:05:57 +0000583 Address getAddress() const {
584 return Address(Addr, getAlignment());
585 }
586
John McCall7a626f62010-09-15 10:14:12 +0000587 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000588 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000589 }
590
Eli Friedman38cd36d2011-12-03 02:13:40 +0000591 CharUnits getAlignment() const {
592 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000593 }
594
John McCalla5efa732011-08-25 23:04:34 +0000595 IsAliased_t isPotentiallyAliased() const {
596 return IsAliased_t(AliasedFlag);
597 }
598
Richard Smithe78fac52018-04-05 20:52:58 +0000599 Overlap_t mayOverlap() const {
600 return Overlap_t(OverlapFlag);
601 }
602
Serge Pavlov37605182018-07-28 15:33:03 +0000603 bool isSanitizerChecked() const {
604 return SanitizerCheckedFlag;
605 }
606
John McCall7a626f62010-09-15 10:14:12 +0000607 RValue asRValue() const {
John McCall7f416cc2015-09-08 08:05:57 +0000608 if (isIgnored()) {
609 return RValue::getIgnored();
610 } else {
611 return RValue::getAggregate(getAddress(), isVolatile());
612 }
John McCall7a626f62010-09-15 10:14:12 +0000613 }
John McCallfe96e0b2011-11-06 09:01:30 +0000614
John McCall8d6fc952011-08-25 20:40:09 +0000615 void setZeroed(bool V = true) { ZeroedFlag = V; }
616 IsZeroed_t isZeroed() const {
617 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000618 }
Richard Smithe78fac52018-04-05 20:52:58 +0000619
620 /// Get the preferred size to use when storing a value to this slot. This
621 /// is the type size unless that might overlap another object, in which
622 /// case it's the dsize.
623 CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
624 return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).first
625 : Ctx.getTypeSizeInChars(Type);
626 }
John McCall7a626f62010-09-15 10:14:12 +0000627};
628
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000629} // end namespace CodeGen
630} // end namespace clang
631
632#endif