blob: 33575c97da99122cb76e183f6c443230588734bf [file] [log] [blame]
Daniel Dunbard4f616b2008-08-23 03:10:25 +00001//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes implement wrappers around llvm::Value in order to
11// fully represent the range of values for C L- and R- values.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
16#define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
Daniel Dunbard4f616b2008-08-23 03:10:25 +000017
Daniel Dunbar93b00a92010-08-21 02:53:44 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000019#include "clang/AST/Type.h"
John McCall47fb9502013-03-07 21:37:08 +000020#include "llvm/IR/Value.h"
David Blaikiee3b172a2015-04-02 18:55:21 +000021#include "llvm/IR/Type.h"
John McCall7f416cc2015-09-08 08:05:57 +000022#include "Address.h"
Ivan A. Kosareva511ed72017-10-03 10:52:39 +000023#include "CodeGenTBAA.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000024
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000025namespace llvm {
26 class Constant;
John McCall47fb9502013-03-07 21:37:08 +000027 class MDNode;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000028}
29
Daniel Dunbard4f616b2008-08-23 03:10:25 +000030namespace clang {
31namespace CodeGen {
John McCallfe96e0b2011-11-06 09:01:30 +000032 class AggValueSlot;
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;
152 bool MayAlias;
153
154public:
155 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type,
156 bool Alias = false)
157 : AlignSource(Source), MayAlias(Alias) {}
158 AlignmentSource getAlignmentSource() const { return AlignSource; }
159 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
160 bool getMayAlias() const { return MayAlias; }
161 void setMayAlias(bool Alias) { MayAlias = Alias; }
162
163 void mergeForCast(const LValueBaseInfo &Info) {
164 setAlignmentSource(Info.getAlignmentSource());
165 setMayAlias(getMayAlias() || Info.getMayAlias());
166 }
167};
168
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000169/// LValue - This represents an lvalue references. Because C/C++ allow
170/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
171/// bitrange.
172class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000173 enum {
174 Simple, // This is a normal l-value, use getAddress().
175 VectorElt, // This is a vector element l-value (V[i]), use getVector*
176 BitField, // This is a bitfield l-value, use getBitfield*.
Renato Golin230c5eb2014-05-19 18:15:42 +0000177 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
178 GlobalReg // This is a register l-value, use getGlobalReg()
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000179 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000180
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000181 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000183 union {
184 // Index into a vector subscript: V[i]
185 llvm::Value *VectorIdx;
186
187 // ExtVector element subset: V.xyx
188 llvm::Constant *VectorElts;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000190 // BitField start bit and size
Daniel Dunbardc406b82010-04-05 21:36:35 +0000191 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000192 };
193
John McCall1553b192011-06-16 04:16:24 +0000194 QualType Type;
195
John McCall8ccfcb52009-09-24 19:53:00 +0000196 // 'const' is unused here
197 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000198
Eli Friedman610bb872012-03-22 22:36:39 +0000199 // The alignment to use when accessing this lvalue. (For vector elements,
200 // this is the alignment of the whole vector.)
John Criswelledc84502012-08-15 18:40:30 +0000201 int64_t Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000202
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000203 // objective-c's ivar
204 bool Ivar:1;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000205
206 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000207 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000209 // LValue is non-gc'able for any reason, including being a parameter or local
210 // variable.
211 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000212
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000213 // Lvalue is a global reference of an objective-c object
214 bool GlobalObjCRef : 1;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000215
216 // Lvalue is a thread local reference
217 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000218
John McCallcdda29c2013-03-13 03:10:54 +0000219 // Lvalue has ARC imprecise lifetime. We store this inverted to try
220 // to make the default bitfield pattern all-zeroes.
221 bool ImpreciseLifetime : 1;
222
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000223 LValueBaseInfo BaseInfo;
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000224 TBAAAccessInfo TBAAInfo;
John McCall7f416cc2015-09-08 08:05:57 +0000225
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000226 // This flag shows if a nontemporal load/stores should be used when accessing
227 // this lvalue.
228 bool Nontemporal : 1;
229
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000230 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000231
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000232private:
Eli Friedmana0544d62011-12-03 04:14:32 +0000233 void Initialize(QualType Type, Qualifiers Quals,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000234 CharUnits Alignment, LValueBaseInfo BaseInfo,
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000235 TBAAAccessInfo TBAAInfo = TBAAAccessInfo()) {
John McCall7f416cc2015-09-08 08:05:57 +0000236 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
237 "initializing l-value with zero alignment!");
John McCall1553b192011-06-16 04:16:24 +0000238 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000239 this->Quals = Quals;
Eli Friedmana0544d62011-12-03 04:14:32 +0000240 this->Alignment = Alignment.getQuantity();
241 assert(this->Alignment == Alignment.getQuantity() &&
242 "Alignment exceeds allowed max!");
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000243 this->BaseInfo = BaseInfo;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000244 this->TBAAInfo = TBAAInfo;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000245
246 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000247 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000248 this->ImpreciseLifetime = false;
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000249 this->Nontemporal = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000250 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000251 this->BaseIvarExp = nullptr;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000252 }
Mike Stump11289f42009-09-09 15:08:12 +0000253
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000254public:
255 bool isSimple() const { return LVType == Simple; }
256 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000257 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000258 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000259 bool isGlobalReg() const { return LVType == GlobalReg; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000260
John McCall8ccfcb52009-09-24 19:53:00 +0000261 bool isVolatileQualified() const { return Quals.hasVolatile(); }
262 bool isRestrictQualified() const { return Quals.hasRestrict(); }
263 unsigned getVRQualifiers() const {
264 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
John McCall1553b192011-06-16 04:16:24 +0000267 QualType getType() const { return Type; }
268
269 Qualifiers::ObjCLifetime getObjCLifetime() const {
270 return Quals.getObjCLifetime();
271 }
272
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000273 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000274 void setObjCIvar(bool Value) { Ivar = Value; }
275
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000276 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000277 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000278
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000279 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000280 void setNonGC(bool Value) { NonGC = Value; }
281
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000282 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000283 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
284
Fariborz Jahanian217af242010-07-20 20:30:03 +0000285 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000286 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
287
John McCallcdda29c2013-03-13 03:10:54 +0000288 ARCPreciseLifetime_t isARCPreciseLifetime() const {
289 return ARCPreciseLifetime_t(!ImpreciseLifetime);
290 }
291 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
292 ImpreciseLifetime = (value == ARCImpreciseLifetime);
293 }
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000294 bool isNontemporal() const { return Nontemporal; }
295 void setNontemporal(bool Value) { Nontemporal = Value; }
John McCallcdda29c2013-03-13 03:10:54 +0000296
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000297 bool isObjCWeak() const {
298 return Quals.getObjCGCAttr() == Qualifiers::Weak;
299 }
300 bool isObjCStrong() const {
301 return Quals.getObjCGCAttr() == Qualifiers::Strong;
302 }
John McCall1553b192011-06-16 04:16:24 +0000303
304 bool isVolatile() const {
305 return Quals.hasVolatile();
306 }
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000307
308 Expr *getBaseIvarExp() const { return BaseIvarExp; }
309 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000310
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000311 TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
312 void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
Manman Renc451e572013-04-04 21:53:22 +0000313
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000314 const Qualifiers &getQuals() const { return Quals; }
315 Qualifiers &getQuals() { return Quals; }
316
John McCall8ccfcb52009-09-24 19:53:00 +0000317 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000318
Eli Friedmana0544d62011-12-03 04:14:32 +0000319 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
320 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000321
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000322 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
323 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
John McCall7f416cc2015-09-08 08:05:57 +0000324
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000325 // simple lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000326 llvm::Value *getPointer() const {
John McCall1553b192011-06-16 04:16:24 +0000327 assert(isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000328 return V;
329 }
330 Address getAddress() const { return Address(getPointer(), getAlignment()); }
331 void setAddress(Address address) {
332 assert(isSimple());
333 V = address.getPointer();
334 Alignment = address.getAlignment().getQuantity();
John McCall1553b192011-06-16 04:16:24 +0000335 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000336
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000337 // vector elt lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000338 Address getVectorAddress() const {
339 return Address(getVectorPointer(), getAlignment());
340 }
341 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000342 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000343
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000344 // extended vector elements.
John McCall7f416cc2015-09-08 08:05:57 +0000345 Address getExtVectorAddress() const {
346 return Address(getExtVectorPointer(), getAlignment());
347 }
348 llvm::Value *getExtVectorPointer() const {
349 assert(isExtVectorElt());
350 return V;
351 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000352 llvm::Constant *getExtVectorElts() const {
353 assert(isExtVectorElt());
354 return VectorElts;
355 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000356
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000357 // bitfield lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000358 Address getBitFieldAddress() const {
359 return Address(getBitFieldPointer(), getAlignment());
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000360 }
John McCall7f416cc2015-09-08 08:05:57 +0000361 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000362 const CGBitFieldInfo &getBitFieldInfo() const {
363 assert(isBitField());
364 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000365 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000366
Renato Golin230c5eb2014-05-19 18:15:42 +0000367 // global register lvalue
368 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
369
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000370 static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
371 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall1553b192011-06-16 04:16:24 +0000372 Qualifiers qs = type.getQualifiers();
373 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000374
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000375 LValue R;
376 R.LVType = Simple;
John McCall7f416cc2015-09-08 08:05:57 +0000377 assert(address.getPointer()->getType()->isPointerTy());
378 R.V = address.getPointer();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000379 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000380 return R;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
John McCall7f416cc2015-09-08 08:05:57 +0000383 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000384 QualType type, LValueBaseInfo BaseInfo) {
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(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000390 BaseInfo);
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,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000395 QualType type, LValueBaseInfo BaseInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000396 LValue R;
397 R.LVType = ExtVectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000398 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000399 R.VectorElts = Elts;
John McCall7f416cc2015-09-08 08:05:57 +0000400 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000401 BaseInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000402 return R;
403 }
404
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000405 /// \brief Create a new object to represent a bit-field access.
406 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000407 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000408 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000409 /// \param Info - The information describing how to perform the bit-field
410 /// access.
John McCall7f416cc2015-09-08 08:05:57 +0000411 static LValue MakeBitfield(Address Addr,
John McCall1553b192011-06-16 04:16:24 +0000412 const CGBitFieldInfo &Info,
John McCall7f416cc2015-09-08 08:05:57 +0000413 QualType type,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000414 LValueBaseInfo BaseInfo) {
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;
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000419 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000420 return R;
421 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000422
John McCall7f416cc2015-09-08 08:05:57 +0000423 static LValue MakeGlobalReg(Address Reg, QualType type) {
Renato Golin230c5eb2014-05-19 18:15:42 +0000424 LValue R;
425 R.LVType = GlobalReg;
John McCall7f416cc2015-09-08 08:05:57 +0000426 R.V = Reg.getPointer();
427 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000428 LValueBaseInfo(AlignmentSource::Decl, false));
Renato Golin230c5eb2014-05-19 18:15:42 +0000429 return R;
430 }
431
Eli Friedman2869b5a2011-12-03 03:08:40 +0000432 RValue asAggregateRValue() const {
Eli Friedman2869b5a2011-12-03 03:08:40 +0000433 return RValue::getAggregate(getAddress(), isVolatileQualified());
434 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000435};
436
John McCall7a626f62010-09-15 10:14:12 +0000437/// An aggregate value slot.
438class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000439 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000440 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000441
442 // Qualifiers
443 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000444
David Majnemerec4b7342016-03-02 06:48:47 +0000445 unsigned Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000446
John McCallcac93852011-08-26 08:02:37 +0000447 /// DestructedFlag - This is set to true if some external code is
448 /// responsible for setting up a destructor for the slot. Otherwise
449 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000450 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000451
452 /// ObjCGCFlag - This is set to true if writing to the memory in the
453 /// slot might require calling an appropriate Objective-C GC
454 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000455 bool ObjCGCFlag : 1;
Chris Lattner27a36312010-12-02 07:07:26 +0000456
John McCallcac93852011-08-26 08:02:37 +0000457 /// ZeroedFlag - This is set to true if the memory in the slot is
458 /// known to be zero before the assignment into it. This means that
459 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000460 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000461
John McCallcac93852011-08-26 08:02:37 +0000462 /// AliasedFlag - This is set to true if the slot might be aliased
463 /// and it's not undefined behavior to access it through such an
464 /// alias. Note that it's always undefined behavior to access a C++
465 /// object that's under construction through an alias derived from
466 /// outside the construction process.
467 ///
468 /// This flag controls whether calls that produce the aggregate
469 /// value may be evaluated directly into the slot, or whether they
470 /// must be evaluated into an unaliased temporary and then memcpy'ed
471 /// over. Since it's invalid in general to memcpy a non-POD C++
472 /// object, it's important that this flag never be set when
473 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000474 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000475
John McCall7a626f62010-09-15 10:14:12 +0000476public:
John McCalla5efa732011-08-25 23:04:34 +0000477 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000478 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000479 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall8d6fc952011-08-25 20:40:09 +0000480 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
481
John McCall7a626f62010-09-15 10:14:12 +0000482 /// ignored - Returns an aggregate value slot indicating that the
483 /// aggregate value is being ignored.
484 static AggValueSlot ignored() {
John McCall7f416cc2015-09-08 08:05:57 +0000485 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000486 DoesNotNeedGCBarriers, IsNotAliased);
John McCall7a626f62010-09-15 10:14:12 +0000487 }
488
489 /// forAddr - Make a slot for an aggregate value.
490 ///
John McCall8d6fc952011-08-25 20:40:09 +0000491 /// \param quals - The qualifiers that dictate how the slot should
492 /// be initialied. Only 'volatile' and the Objective-C lifetime
493 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000494 ///
John McCall8d6fc952011-08-25 20:40:09 +0000495 /// \param isDestructed - true if something else is responsible
496 /// for calling destructors on this object
497 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000498 /// somewhere that ObjC GC calls should be emitted for
John McCall7f416cc2015-09-08 08:05:57 +0000499 static AggValueSlot forAddr(Address addr,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000500 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000501 IsDestructed_t isDestructed,
502 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000503 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000504 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall7a626f62010-09-15 10:14:12 +0000505 AggValueSlot AV;
John McCall7f416cc2015-09-08 08:05:57 +0000506 if (addr.isValid()) {
507 AV.Addr = addr.getPointer();
508 AV.Alignment = addr.getAlignment().getQuantity();
509 } else {
510 AV.Addr = nullptr;
511 AV.Alignment = 0;
512 }
John McCall8d6fc952011-08-25 20:40:09 +0000513 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000514 AV.DestructedFlag = isDestructed;
515 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000516 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000517 AV.AliasedFlag = isAliased;
John McCall7a626f62010-09-15 10:14:12 +0000518 return AV;
519 }
520
John McCall4e8ca4f2012-07-02 23:58:38 +0000521 static AggValueSlot forLValue(const LValue &LV,
522 IsDestructed_t isDestructed,
John McCall8d6fc952011-08-25 20:40:09 +0000523 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000524 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000525 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall7f416cc2015-09-08 08:05:57 +0000526 return forAddr(LV.getAddress(),
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000527 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
John McCall7a626f62010-09-15 10:14:12 +0000528 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000529
John McCallcac93852011-08-26 08:02:37 +0000530 IsDestructed_t isExternallyDestructed() const {
531 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000532 }
John McCallcac93852011-08-26 08:02:37 +0000533 void setExternallyDestructed(bool destructed = true) {
534 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000535 }
536
John McCall31168b02011-06-15 23:02:42 +0000537 Qualifiers getQualifiers() const { return Quals; }
538
John McCall7a626f62010-09-15 10:14:12 +0000539 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000540 return Quals.hasVolatile();
541 }
542
Fariborz Jahanian78652202013-01-25 23:57:05 +0000543 void setVolatile(bool flag) {
544 Quals.setVolatile(flag);
545 }
546
John McCall31168b02011-06-15 23:02:42 +0000547 Qualifiers::ObjCLifetime getObjCLifetime() const {
548 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000549 }
550
John McCall8d6fc952011-08-25 20:40:09 +0000551 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000552 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000553 }
John McCall7f416cc2015-09-08 08:05:57 +0000554
555 llvm::Value *getPointer() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000556 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000557 }
558
John McCall7f416cc2015-09-08 08:05:57 +0000559 Address getAddress() const {
560 return Address(Addr, getAlignment());
561 }
562
John McCall7a626f62010-09-15 10:14:12 +0000563 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000564 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000565 }
566
Eli Friedman38cd36d2011-12-03 02:13:40 +0000567 CharUnits getAlignment() const {
568 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000569 }
570
John McCalla5efa732011-08-25 23:04:34 +0000571 IsAliased_t isPotentiallyAliased() const {
572 return IsAliased_t(AliasedFlag);
573 }
574
John McCall7a626f62010-09-15 10:14:12 +0000575 RValue asRValue() const {
John McCall7f416cc2015-09-08 08:05:57 +0000576 if (isIgnored()) {
577 return RValue::getIgnored();
578 } else {
579 return RValue::getAggregate(getAddress(), isVolatile());
580 }
John McCall7a626f62010-09-15 10:14:12 +0000581 }
John McCallfe96e0b2011-11-06 09:01:30 +0000582
John McCall8d6fc952011-08-25 20:40:09 +0000583 void setZeroed(bool V = true) { ZeroedFlag = V; }
584 IsZeroed_t isZeroed() const {
585 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000586 }
John McCall7a626f62010-09-15 10:14:12 +0000587};
588
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000589} // end namespace CodeGen
590} // end namespace clang
591
592#endif