blob: b768eb86367bef8c53fc6050dc83664bb9f6dc94 [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"
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;
Chandler Carruthff0e3a12012-12-06 11:14:44 +000032 struct CGBitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +000033
34/// RValue - This trivial value class is used to represent the result of an
35/// expression that is evaluated. It can be one of three things: either a
36/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
37/// address of an aggregate value in memory.
38class RValue {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000039 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump11289f42009-09-09 15:08:12 +000040
John McCall7f416cc2015-09-08 08:05:57 +000041 // The shift to make to an aggregate's alignment to make it look
42 // like a pointer.
43 enum { AggAlignShift = 4 };
44
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000045 // Stores first value and flavor.
46 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
47 // Stores second value and volatility.
48 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
49
Daniel Dunbard4f616b2008-08-23 03:10:25 +000050public:
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000051 bool isScalar() const { return V1.getInt() == Scalar; }
52 bool isComplex() const { return V1.getInt() == Complex; }
53 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump11289f42009-09-09 15:08:12 +000054
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000055 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump93700fc2009-05-23 20:21:36 +000056
Mike Stump462a4aa2009-11-03 16:11:57 +000057 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbard4f616b2008-08-23 03:10:25 +000058 llvm::Value *getScalarVal() const {
59 assert(isScalar() && "Not a scalar!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000060 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000061 }
62
63 /// getComplexVal - Return the real/imag components of this complex value.
64 ///
65 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000066 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbard4f616b2008-08-23 03:10:25 +000067 }
Mike Stump11289f42009-09-09 15:08:12 +000068
Daniel Dunbard4f616b2008-08-23 03:10:25 +000069 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
John McCall7f416cc2015-09-08 08:05:57 +000070 Address getAggregateAddress() const {
71 assert(isAggregate() && "Not an aggregate!");
72 auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
73 return Address(V1.getPointer(), CharUnits::fromQuantity(align));
74 }
75 llvm::Value *getAggregatePointer() const {
Daniel Dunbard4f616b2008-08-23 03:10:25 +000076 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000077 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000078 }
Mike Stump11289f42009-09-09 15:08:12 +000079
John McCall7f416cc2015-09-08 08:05:57 +000080 static RValue getIgnored() {
81 // FIXME: should we make this a more explicit state?
82 return get(nullptr);
83 }
84
Daniel Dunbard4f616b2008-08-23 03:10:25 +000085 static RValue get(llvm::Value *V) {
86 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000087 ER.V1.setPointer(V);
88 ER.V1.setInt(Scalar);
89 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000090 return ER;
91 }
92 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
93 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000094 ER.V1.setPointer(V1);
95 ER.V2.setPointer(V2);
96 ER.V1.setInt(Complex);
97 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000098 return ER;
99 }
100 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000101 return getComplex(C.first, C.second);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000102 }
Mike Stump93700fc2009-05-23 20:21:36 +0000103 // FIXME: Aggregate rvalues need to retain information about whether they are
104 // volatile or not. Remove default to find all places that probably get this
105 // wrong.
John McCall7f416cc2015-09-08 08:05:57 +0000106 static RValue getAggregate(Address addr, bool isVolatile = false) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000107 RValue ER;
John McCall7f416cc2015-09-08 08:05:57 +0000108 ER.V1.setPointer(addr.getPointer());
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000109 ER.V1.setInt(Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +0000110
111 auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
112 ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
113 ER.V2.setInt(isVolatile);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000114 return ER;
115 }
116};
117
John McCallcdda29c2013-03-13 03:10:54 +0000118/// Does an ARC strong l-value have precise lifetime?
119enum ARCPreciseLifetime_t {
John McCall4d31b812013-03-13 05:02:21 +0000120 ARCImpreciseLifetime, ARCPreciseLifetime
John McCallcdda29c2013-03-13 03:10:54 +0000121};
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000122
John McCall7f416cc2015-09-08 08:05:57 +0000123/// The source of the alignment of an l-value; an expression of
124/// confidence in the alignment actually matching the estimate.
125enum class AlignmentSource {
126 /// The l-value was an access to a declared entity or something
127 /// equivalently strong, like the address of an array allocated by a
128 /// language runtime.
129 Decl,
130
131 /// The l-value was considered opaque, so the alignment was
132 /// determined from a type, but that type was an explicitly-aligned
133 /// typedef.
134 AttributedType,
135
136 /// The l-value was considered opaque, so the alignment was
137 /// determined from a type.
138 Type
139};
140
141/// Given that the base address has the given alignment source, what's
142/// our confidence in the alignment of the field?
143static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
144 // For now, we don't distinguish fields of opaque pointers from
145 // top-level declarations, but maybe we should.
146 return AlignmentSource::Decl;
147}
148
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000149class LValueBaseInfo {
150 AlignmentSource AlignSource;
151 bool MayAlias;
152
153public:
154 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type,
155 bool Alias = false)
156 : AlignSource(Source), MayAlias(Alias) {}
157 AlignmentSource getAlignmentSource() const { return AlignSource; }
158 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
159 bool getMayAlias() const { return MayAlias; }
160 void setMayAlias(bool Alias) { MayAlias = Alias; }
161
162 void mergeForCast(const LValueBaseInfo &Info) {
163 setAlignmentSource(Info.getAlignmentSource());
164 setMayAlias(getMayAlias() || Info.getMayAlias());
165 }
166};
167
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000168/// LValue - This represents an lvalue references. Because C/C++ allow
169/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
170/// bitrange.
171class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000172 enum {
173 Simple, // This is a normal l-value, use getAddress().
174 VectorElt, // This is a vector element l-value (V[i]), use getVector*
175 BitField, // This is a bitfield l-value, use getBitfield*.
Renato Golin230c5eb2014-05-19 18:15:42 +0000176 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
177 GlobalReg // This is a register l-value, use getGlobalReg()
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000178 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000179
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000180 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000182 union {
183 // Index into a vector subscript: V[i]
184 llvm::Value *VectorIdx;
185
186 // ExtVector element subset: V.xyx
187 llvm::Constant *VectorElts;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000189 // BitField start bit and size
Daniel Dunbardc406b82010-04-05 21:36:35 +0000190 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000191 };
192
John McCall1553b192011-06-16 04:16:24 +0000193 QualType Type;
194
John McCall8ccfcb52009-09-24 19:53:00 +0000195 // 'const' is unused here
196 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000197
Eli Friedman610bb872012-03-22 22:36:39 +0000198 // The alignment to use when accessing this lvalue. (For vector elements,
199 // this is the alignment of the whole vector.)
John Criswelledc84502012-08-15 18:40:30 +0000200 int64_t Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000201
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000202 // objective-c's ivar
203 bool Ivar:1;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000204
205 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000206 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000208 // LValue is non-gc'able for any reason, including being a parameter or local
209 // variable.
210 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000211
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000212 // Lvalue is a global reference of an objective-c object
213 bool GlobalObjCRef : 1;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000214
215 // Lvalue is a thread local reference
216 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000217
John McCallcdda29c2013-03-13 03:10:54 +0000218 // Lvalue has ARC imprecise lifetime. We store this inverted to try
219 // to make the default bitfield pattern all-zeroes.
220 bool ImpreciseLifetime : 1;
221
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000222 LValueBaseInfo BaseInfo;
John McCall7f416cc2015-09-08 08:05:57 +0000223
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000224 // This flag shows if a nontemporal load/stores should be used when accessing
225 // this lvalue.
226 bool Nontemporal : 1;
227
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000228 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000229
Manman Renc451e572013-04-04 21:53:22 +0000230 /// Used by struct-path-aware TBAA.
231 QualType TBAABaseType;
232 /// Offset relative to the base type.
233 uint64_t TBAAOffset;
234
Dan Gohman947c9af2010-10-14 23:06:10 +0000235 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
236 llvm::MDNode *TBAAInfo;
237
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000238private:
Eli Friedmana0544d62011-12-03 04:14:32 +0000239 void Initialize(QualType Type, Qualifiers Quals,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000240 CharUnits Alignment, LValueBaseInfo BaseInfo,
Craig Topper8a13c412014-05-21 05:09:00 +0000241 llvm::MDNode *TBAAInfo = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +0000242 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
243 "initializing l-value with zero alignment!");
John McCall1553b192011-06-16 04:16:24 +0000244 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000245 this->Quals = Quals;
Eli Friedmana0544d62011-12-03 04:14:32 +0000246 this->Alignment = Alignment.getQuantity();
247 assert(this->Alignment == Alignment.getQuantity() &&
248 "Alignment exceeds allowed max!");
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000249 this->BaseInfo = BaseInfo;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000250
251 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000252 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000253 this->ImpreciseLifetime = false;
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000254 this->Nontemporal = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000255 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000256 this->BaseIvarExp = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000257
258 // Initialize fields for TBAA.
259 this->TBAABaseType = Type;
260 this->TBAAOffset = 0;
Dan Gohman947c9af2010-10-14 23:06:10 +0000261 this->TBAAInfo = TBAAInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000264public:
265 bool isSimple() const { return LVType == Simple; }
266 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000267 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000268 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000269 bool isGlobalReg() const { return LVType == GlobalReg; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000270
John McCall8ccfcb52009-09-24 19:53:00 +0000271 bool isVolatileQualified() const { return Quals.hasVolatile(); }
272 bool isRestrictQualified() const { return Quals.hasRestrict(); }
273 unsigned getVRQualifiers() const {
274 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000275 }
Mike Stump11289f42009-09-09 15:08:12 +0000276
John McCall1553b192011-06-16 04:16:24 +0000277 QualType getType() const { return Type; }
278
279 Qualifiers::ObjCLifetime getObjCLifetime() const {
280 return Quals.getObjCLifetime();
281 }
282
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000283 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000284 void setObjCIvar(bool Value) { Ivar = Value; }
285
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000286 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000287 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000288
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000289 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000290 void setNonGC(bool Value) { NonGC = Value; }
291
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000292 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000293 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
294
Fariborz Jahanian217af242010-07-20 20:30:03 +0000295 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000296 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
297
John McCallcdda29c2013-03-13 03:10:54 +0000298 ARCPreciseLifetime_t isARCPreciseLifetime() const {
299 return ARCPreciseLifetime_t(!ImpreciseLifetime);
300 }
301 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
302 ImpreciseLifetime = (value == ARCImpreciseLifetime);
303 }
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000304 bool isNontemporal() const { return Nontemporal; }
305 void setNontemporal(bool Value) { Nontemporal = Value; }
John McCallcdda29c2013-03-13 03:10:54 +0000306
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000307 bool isObjCWeak() const {
308 return Quals.getObjCGCAttr() == Qualifiers::Weak;
309 }
310 bool isObjCStrong() const {
311 return Quals.getObjCGCAttr() == Qualifiers::Strong;
312 }
John McCall1553b192011-06-16 04:16:24 +0000313
314 bool isVolatile() const {
315 return Quals.hasVolatile();
316 }
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000317
318 Expr *getBaseIvarExp() const { return BaseIvarExp; }
319 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000320
Manman Renc451e572013-04-04 21:53:22 +0000321 QualType getTBAABaseType() const { return TBAABaseType; }
322 void setTBAABaseType(QualType T) { TBAABaseType = T; }
323
324 uint64_t getTBAAOffset() const { return TBAAOffset; }
325 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
326
Dan Gohman947c9af2010-10-14 23:06:10 +0000327 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
328 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
329
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000330 const Qualifiers &getQuals() const { return Quals; }
331 Qualifiers &getQuals() { return Quals; }
332
John McCall8ccfcb52009-09-24 19:53:00 +0000333 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000334
Eli Friedmana0544d62011-12-03 04:14:32 +0000335 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
336 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000337
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000338 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
339 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
John McCall7f416cc2015-09-08 08:05:57 +0000340
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000341 // simple lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000342 llvm::Value *getPointer() const {
John McCall1553b192011-06-16 04:16:24 +0000343 assert(isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000344 return V;
345 }
346 Address getAddress() const { return Address(getPointer(), getAlignment()); }
347 void setAddress(Address address) {
348 assert(isSimple());
349 V = address.getPointer();
350 Alignment = address.getAlignment().getQuantity();
John McCall1553b192011-06-16 04:16:24 +0000351 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000352
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000353 // vector elt lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000354 Address getVectorAddress() const {
355 return Address(getVectorPointer(), getAlignment());
356 }
357 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000358 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000359
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000360 // extended vector elements.
John McCall7f416cc2015-09-08 08:05:57 +0000361 Address getExtVectorAddress() const {
362 return Address(getExtVectorPointer(), getAlignment());
363 }
364 llvm::Value *getExtVectorPointer() const {
365 assert(isExtVectorElt());
366 return V;
367 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000368 llvm::Constant *getExtVectorElts() const {
369 assert(isExtVectorElt());
370 return VectorElts;
371 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000372
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000373 // bitfield lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000374 Address getBitFieldAddress() const {
375 return Address(getBitFieldPointer(), getAlignment());
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000376 }
John McCall7f416cc2015-09-08 08:05:57 +0000377 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000378 const CGBitFieldInfo &getBitFieldInfo() const {
379 assert(isBitField());
380 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000381 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000382
Renato Golin230c5eb2014-05-19 18:15:42 +0000383 // global register lvalue
384 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
385
John McCall7f416cc2015-09-08 08:05:57 +0000386 static LValue MakeAddr(Address address, QualType type,
387 ASTContext &Context,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000388 LValueBaseInfo BaseInfo,
Craig Topper8a13c412014-05-21 05:09:00 +0000389 llvm::MDNode *TBAAInfo = nullptr) {
John McCall1553b192011-06-16 04:16:24 +0000390 Qualifiers qs = type.getQualifiers();
391 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000392
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000393 LValue R;
394 R.LVType = Simple;
John McCall7f416cc2015-09-08 08:05:57 +0000395 assert(address.getPointer()->getType()->isPointerTy());
396 R.V = address.getPointer();
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000397 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000398 return R;
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
John McCall7f416cc2015-09-08 08:05:57 +0000401 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000402 QualType type, LValueBaseInfo BaseInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000403 LValue R;
404 R.LVType = VectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000405 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000406 R.VectorIdx = Idx;
John McCall7f416cc2015-09-08 08:05:57 +0000407 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000408 BaseInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000409 return R;
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
John McCall7f416cc2015-09-08 08:05:57 +0000412 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000413 QualType type, LValueBaseInfo BaseInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000414 LValue R;
415 R.LVType = ExtVectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000416 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000417 R.VectorElts = Elts;
John McCall7f416cc2015-09-08 08:05:57 +0000418 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000419 BaseInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000420 return R;
421 }
422
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000423 /// \brief Create a new object to represent a bit-field access.
424 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000425 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000426 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000427 /// \param Info - The information describing how to perform the bit-field
428 /// access.
John McCall7f416cc2015-09-08 08:05:57 +0000429 static LValue MakeBitfield(Address Addr,
John McCall1553b192011-06-16 04:16:24 +0000430 const CGBitFieldInfo &Info,
John McCall7f416cc2015-09-08 08:05:57 +0000431 QualType type,
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000432 LValueBaseInfo BaseInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000433 LValue R;
434 R.LVType = BitField;
John McCall7f416cc2015-09-08 08:05:57 +0000435 R.V = Addr.getPointer();
Daniel Dunbardc406b82010-04-05 21:36:35 +0000436 R.BitFieldInfo = &Info;
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000437 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000438 return R;
439 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000440
John McCall7f416cc2015-09-08 08:05:57 +0000441 static LValue MakeGlobalReg(Address Reg, QualType type) {
Renato Golin230c5eb2014-05-19 18:15:42 +0000442 LValue R;
443 R.LVType = GlobalReg;
John McCall7f416cc2015-09-08 08:05:57 +0000444 R.V = Reg.getPointer();
445 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000446 LValueBaseInfo(AlignmentSource::Decl, false));
Renato Golin230c5eb2014-05-19 18:15:42 +0000447 return R;
448 }
449
Eli Friedman2869b5a2011-12-03 03:08:40 +0000450 RValue asAggregateRValue() const {
Eli Friedman2869b5a2011-12-03 03:08:40 +0000451 return RValue::getAggregate(getAddress(), isVolatileQualified());
452 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000453};
454
John McCall7a626f62010-09-15 10:14:12 +0000455/// An aggregate value slot.
456class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000457 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000458 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000459
460 // Qualifiers
461 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000462
David Majnemerec4b7342016-03-02 06:48:47 +0000463 unsigned Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000464
John McCallcac93852011-08-26 08:02:37 +0000465 /// DestructedFlag - This is set to true if some external code is
466 /// responsible for setting up a destructor for the slot. Otherwise
467 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000468 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000469
470 /// ObjCGCFlag - This is set to true if writing to the memory in the
471 /// slot might require calling an appropriate Objective-C GC
472 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000473 bool ObjCGCFlag : 1;
Chris Lattner27a36312010-12-02 07:07:26 +0000474
John McCallcac93852011-08-26 08:02:37 +0000475 /// ZeroedFlag - This is set to true if the memory in the slot is
476 /// known to be zero before the assignment into it. This means that
477 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000478 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000479
John McCallcac93852011-08-26 08:02:37 +0000480 /// AliasedFlag - This is set to true if the slot might be aliased
481 /// and it's not undefined behavior to access it through such an
482 /// alias. Note that it's always undefined behavior to access a C++
483 /// object that's under construction through an alias derived from
484 /// outside the construction process.
485 ///
486 /// This flag controls whether calls that produce the aggregate
487 /// value may be evaluated directly into the slot, or whether they
488 /// must be evaluated into an unaliased temporary and then memcpy'ed
489 /// over. Since it's invalid in general to memcpy a non-POD C++
490 /// object, it's important that this flag never be set when
491 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000492 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000493
John McCall7a626f62010-09-15 10:14:12 +0000494public:
John McCalla5efa732011-08-25 23:04:34 +0000495 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000496 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000497 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall8d6fc952011-08-25 20:40:09 +0000498 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
499
John McCall7a626f62010-09-15 10:14:12 +0000500 /// ignored - Returns an aggregate value slot indicating that the
501 /// aggregate value is being ignored.
502 static AggValueSlot ignored() {
John McCall7f416cc2015-09-08 08:05:57 +0000503 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000504 DoesNotNeedGCBarriers, IsNotAliased);
John McCall7a626f62010-09-15 10:14:12 +0000505 }
506
507 /// forAddr - Make a slot for an aggregate value.
508 ///
John McCall8d6fc952011-08-25 20:40:09 +0000509 /// \param quals - The qualifiers that dictate how the slot should
510 /// be initialied. Only 'volatile' and the Objective-C lifetime
511 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000512 ///
John McCall8d6fc952011-08-25 20:40:09 +0000513 /// \param isDestructed - true if something else is responsible
514 /// for calling destructors on this object
515 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000516 /// somewhere that ObjC GC calls should be emitted for
John McCall7f416cc2015-09-08 08:05:57 +0000517 static AggValueSlot forAddr(Address addr,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000518 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000519 IsDestructed_t isDestructed,
520 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000521 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000522 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall7a626f62010-09-15 10:14:12 +0000523 AggValueSlot AV;
John McCall7f416cc2015-09-08 08:05:57 +0000524 if (addr.isValid()) {
525 AV.Addr = addr.getPointer();
526 AV.Alignment = addr.getAlignment().getQuantity();
527 } else {
528 AV.Addr = nullptr;
529 AV.Alignment = 0;
530 }
John McCall8d6fc952011-08-25 20:40:09 +0000531 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000532 AV.DestructedFlag = isDestructed;
533 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000534 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000535 AV.AliasedFlag = isAliased;
John McCall7a626f62010-09-15 10:14:12 +0000536 return AV;
537 }
538
John McCall4e8ca4f2012-07-02 23:58:38 +0000539 static AggValueSlot forLValue(const LValue &LV,
540 IsDestructed_t isDestructed,
John McCall8d6fc952011-08-25 20:40:09 +0000541 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000542 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000543 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall7f416cc2015-09-08 08:05:57 +0000544 return forAddr(LV.getAddress(),
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000545 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
John McCall7a626f62010-09-15 10:14:12 +0000546 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000547
John McCallcac93852011-08-26 08:02:37 +0000548 IsDestructed_t isExternallyDestructed() const {
549 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000550 }
John McCallcac93852011-08-26 08:02:37 +0000551 void setExternallyDestructed(bool destructed = true) {
552 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000553 }
554
John McCall31168b02011-06-15 23:02:42 +0000555 Qualifiers getQualifiers() const { return Quals; }
556
John McCall7a626f62010-09-15 10:14:12 +0000557 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000558 return Quals.hasVolatile();
559 }
560
Fariborz Jahanian78652202013-01-25 23:57:05 +0000561 void setVolatile(bool flag) {
562 Quals.setVolatile(flag);
563 }
564
John McCall31168b02011-06-15 23:02:42 +0000565 Qualifiers::ObjCLifetime getObjCLifetime() const {
566 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000567 }
568
John McCall8d6fc952011-08-25 20:40:09 +0000569 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000570 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000571 }
John McCall7f416cc2015-09-08 08:05:57 +0000572
573 llvm::Value *getPointer() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000574 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000575 }
576
John McCall7f416cc2015-09-08 08:05:57 +0000577 Address getAddress() const {
578 return Address(Addr, getAlignment());
579 }
580
John McCall7a626f62010-09-15 10:14:12 +0000581 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000582 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000583 }
584
Eli Friedman38cd36d2011-12-03 02:13:40 +0000585 CharUnits getAlignment() const {
586 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000587 }
588
John McCalla5efa732011-08-25 23:04:34 +0000589 IsAliased_t isPotentiallyAliased() const {
590 return IsAliased_t(AliasedFlag);
591 }
592
John McCall7a626f62010-09-15 10:14:12 +0000593 RValue asRValue() const {
John McCall7f416cc2015-09-08 08:05:57 +0000594 if (isIgnored()) {
595 return RValue::getIgnored();
596 } else {
597 return RValue::getAggregate(getAddress(), isVolatile());
598 }
John McCall7a626f62010-09-15 10:14:12 +0000599 }
John McCallfe96e0b2011-11-06 09:01:30 +0000600
John McCall8d6fc952011-08-25 20:40:09 +0000601 void setZeroed(bool V = true) { ZeroedFlag = V; }
602 IsZeroed_t isZeroed() const {
603 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000604 }
John McCall7a626f62010-09-15 10:14:12 +0000605};
606
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000607} // end namespace CodeGen
608} // end namespace clang
609
610#endif