blob: 71f95abe488a9096307b0765ff21fc2daef12f73 [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;
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;
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000151
152public:
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000153 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
154 : AlignSource(Source) {}
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000155 AlignmentSource getAlignmentSource() const { return AlignSource; }
156 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000157
158 void mergeForCast(const LValueBaseInfo &Info) {
159 setAlignmentSource(Info.getAlignmentSource());
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000160 }
161};
162
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000163/// LValue - This represents an lvalue references. Because C/C++ allow
164/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
165/// bitrange.
166class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000167 enum {
168 Simple, // This is a normal l-value, use getAddress().
169 VectorElt, // This is a vector element l-value (V[i]), use getVector*
170 BitField, // This is a bitfield l-value, use getBitfield*.
Renato Golin230c5eb2014-05-19 18:15:42 +0000171 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
172 GlobalReg // This is a register l-value, use getGlobalReg()
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000173 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000174
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000175 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000176
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000177 union {
178 // Index into a vector subscript: V[i]
179 llvm::Value *VectorIdx;
180
181 // ExtVector element subset: V.xyx
182 llvm::Constant *VectorElts;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000184 // BitField start bit and size
Daniel Dunbardc406b82010-04-05 21:36:35 +0000185 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000186 };
187
John McCall1553b192011-06-16 04:16:24 +0000188 QualType Type;
189
John McCall8ccfcb52009-09-24 19:53:00 +0000190 // 'const' is unused here
191 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000192
Eli Friedman610bb872012-03-22 22:36:39 +0000193 // The alignment to use when accessing this lvalue. (For vector elements,
194 // this is the alignment of the whole vector.)
Yaxun Liud9389822018-03-14 15:02:28 +0000195 unsigned Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000196
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000197 // objective-c's ivar
198 bool Ivar:1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000199
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000200 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000201 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000203 // LValue is non-gc'able for any reason, including being a parameter or local
204 // variable.
205 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000206
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000207 // Lvalue is a global reference of an objective-c object
208 bool GlobalObjCRef : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000209
Fariborz Jahanian217af242010-07-20 20:30:03 +0000210 // Lvalue is a thread local reference
211 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000212
John McCallcdda29c2013-03-13 03:10:54 +0000213 // Lvalue has ARC imprecise lifetime. We store this inverted to try
214 // to make the default bitfield pattern all-zeroes.
215 bool ImpreciseLifetime : 1;
216
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000217 // This flag shows if a nontemporal load/stores should be used when accessing
218 // this lvalue.
219 bool Nontemporal : 1;
220
Yaxun Liud9389822018-03-14 15:02:28 +0000221 LValueBaseInfo BaseInfo;
222 TBAAAccessInfo TBAAInfo;
223
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000224 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000225
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000226private:
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000227 void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment,
228 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall7f416cc2015-09-08 08:05:57 +0000229 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
230 "initializing l-value with zero alignment!");
John McCall1553b192011-06-16 04:16:24 +0000231 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000232 this->Quals = Quals;
Yaxun Liud9389822018-03-14 15:02:28 +0000233 const unsigned MaxAlign = 1U << 31;
234 this->Alignment = Alignment.getQuantity() <= MaxAlign
235 ? Alignment.getQuantity()
236 : MaxAlign;
Eli Friedmana0544d62011-12-03 04:14:32 +0000237 assert(this->Alignment == Alignment.getQuantity() &&
238 "Alignment exceeds allowed max!");
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000239 this->BaseInfo = BaseInfo;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000240 this->TBAAInfo = TBAAInfo;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000241
242 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000243 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000244 this->ImpreciseLifetime = false;
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000245 this->Nontemporal = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000246 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000247 this->BaseIvarExp = nullptr;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000248 }
Mike Stump11289f42009-09-09 15:08:12 +0000249
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000250public:
251 bool isSimple() const { return LVType == Simple; }
252 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000253 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000254 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000255 bool isGlobalReg() const { return LVType == GlobalReg; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000256
John McCall8ccfcb52009-09-24 19:53:00 +0000257 bool isVolatileQualified() const { return Quals.hasVolatile(); }
258 bool isRestrictQualified() const { return Quals.hasRestrict(); }
259 unsigned getVRQualifiers() const {
260 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000261 }
Mike Stump11289f42009-09-09 15:08:12 +0000262
John McCall1553b192011-06-16 04:16:24 +0000263 QualType getType() const { return Type; }
264
265 Qualifiers::ObjCLifetime getObjCLifetime() const {
266 return Quals.getObjCLifetime();
267 }
268
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000269 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000270 void setObjCIvar(bool Value) { Ivar = Value; }
271
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000272 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000273 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000274
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000275 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000276 void setNonGC(bool Value) { NonGC = Value; }
277
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000278 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000279 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
280
Fariborz Jahanian217af242010-07-20 20:30:03 +0000281 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000282 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
283
John McCallcdda29c2013-03-13 03:10:54 +0000284 ARCPreciseLifetime_t isARCPreciseLifetime() const {
285 return ARCPreciseLifetime_t(!ImpreciseLifetime);
286 }
287 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
288 ImpreciseLifetime = (value == ARCImpreciseLifetime);
289 }
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000290 bool isNontemporal() const { return Nontemporal; }
291 void setNontemporal(bool Value) { Nontemporal = Value; }
John McCallcdda29c2013-03-13 03:10:54 +0000292
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000293 bool isObjCWeak() const {
294 return Quals.getObjCGCAttr() == Qualifiers::Weak;
295 }
296 bool isObjCStrong() const {
297 return Quals.getObjCGCAttr() == Qualifiers::Strong;
298 }
John McCall1553b192011-06-16 04:16:24 +0000299
300 bool isVolatile() const {
301 return Quals.hasVolatile();
302 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000303
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000304 Expr *getBaseIvarExp() const { return BaseIvarExp; }
305 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000306
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000307 TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
308 void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
Manman Renc451e572013-04-04 21:53:22 +0000309
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000310 const Qualifiers &getQuals() const { return Quals; }
311 Qualifiers &getQuals() { return Quals; }
312
Alexander Richardson6d989432017-10-15 18:48:14 +0000313 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000314
Eli Friedmana0544d62011-12-03 04:14:32 +0000315 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
316 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000317
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000318 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
319 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
John McCall7f416cc2015-09-08 08:05:57 +0000320
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000321 // simple lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000322 llvm::Value *getPointer() const {
John McCall1553b192011-06-16 04:16:24 +0000323 assert(isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000324 return V;
325 }
326 Address getAddress() const { return Address(getPointer(), getAlignment()); }
327 void setAddress(Address address) {
328 assert(isSimple());
329 V = address.getPointer();
330 Alignment = address.getAlignment().getQuantity();
John McCall1553b192011-06-16 04:16:24 +0000331 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000332
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000333 // vector elt lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000334 Address getVectorAddress() const {
335 return Address(getVectorPointer(), getAlignment());
336 }
337 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000338 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000339
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000340 // extended vector elements.
John McCall7f416cc2015-09-08 08:05:57 +0000341 Address getExtVectorAddress() const {
342 return Address(getExtVectorPointer(), getAlignment());
343 }
344 llvm::Value *getExtVectorPointer() const {
345 assert(isExtVectorElt());
346 return V;
347 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000348 llvm::Constant *getExtVectorElts() const {
349 assert(isExtVectorElt());
350 return VectorElts;
351 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000352
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000353 // bitfield lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000354 Address getBitFieldAddress() const {
355 return Address(getBitFieldPointer(), getAlignment());
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000356 }
John McCall7f416cc2015-09-08 08:05:57 +0000357 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000358 const CGBitFieldInfo &getBitFieldInfo() const {
359 assert(isBitField());
360 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000361 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000362
Renato Golin230c5eb2014-05-19 18:15:42 +0000363 // global register lvalue
364 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
365
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000366 static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
367 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall1553b192011-06-16 04:16:24 +0000368 Qualifiers qs = type.getQualifiers();
369 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000370
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000371 LValue R;
372 R.LVType = Simple;
John McCall7f416cc2015-09-08 08:05:57 +0000373 assert(address.getPointer()->getType()->isPointerTy());
374 R.V = address.getPointer();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000375 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000376 return R;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
John McCall7f416cc2015-09-08 08:05:57 +0000379 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000380 QualType type, LValueBaseInfo BaseInfo,
381 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000382 LValue R;
383 R.LVType = VectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000384 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000385 R.VectorIdx = Idx;
John McCall7f416cc2015-09-08 08:05:57 +0000386 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000387 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000388 return R;
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
John McCall7f416cc2015-09-08 08:05:57 +0000391 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000392 QualType type, LValueBaseInfo BaseInfo,
393 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000394 LValue R;
395 R.LVType = ExtVectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000396 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000397 R.VectorElts = Elts;
John McCall7f416cc2015-09-08 08:05:57 +0000398 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000399 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000400 return R;
401 }
402
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Create a new object to represent a bit-field access.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000404 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000405 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000406 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000407 /// \param Info - The information describing how to perform the bit-field
408 /// access.
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000409 static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
410 QualType type, LValueBaseInfo BaseInfo,
411 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000412 LValue R;
413 R.LVType = BitField;
John McCall7f416cc2015-09-08 08:05:57 +0000414 R.V = Addr.getPointer();
Daniel Dunbardc406b82010-04-05 21:36:35 +0000415 R.BitFieldInfo = &Info;
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000416 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
417 TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000418 return R;
419 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000420
John McCall7f416cc2015-09-08 08:05:57 +0000421 static LValue MakeGlobalReg(Address Reg, QualType type) {
Renato Golin230c5eb2014-05-19 18:15:42 +0000422 LValue R;
423 R.LVType = GlobalReg;
John McCall7f416cc2015-09-08 08:05:57 +0000424 R.V = Reg.getPointer();
425 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000426 LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
Renato Golin230c5eb2014-05-19 18:15:42 +0000427 return R;
428 }
429
Eli Friedman2869b5a2011-12-03 03:08:40 +0000430 RValue asAggregateRValue() const {
Eli Friedman2869b5a2011-12-03 03:08:40 +0000431 return RValue::getAggregate(getAddress(), isVolatileQualified());
432 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000433};
434
John McCall7a626f62010-09-15 10:14:12 +0000435/// An aggregate value slot.
436class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000437 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000438 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000439
440 // Qualifiers
441 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000442
David Majnemerec4b7342016-03-02 06:48:47 +0000443 unsigned Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000444
John McCallcac93852011-08-26 08:02:37 +0000445 /// DestructedFlag - This is set to true if some external code is
446 /// responsible for setting up a destructor for the slot. Otherwise
447 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000448 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000449
450 /// ObjCGCFlag - This is set to true if writing to the memory in the
451 /// slot might require calling an appropriate Objective-C GC
452 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000453 bool ObjCGCFlag : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000454
John McCallcac93852011-08-26 08:02:37 +0000455 /// ZeroedFlag - This is set to true if the memory in the slot is
456 /// known to be zero before the assignment into it. This means that
457 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000458 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000459
John McCallcac93852011-08-26 08:02:37 +0000460 /// AliasedFlag - This is set to true if the slot might be aliased
461 /// and it's not undefined behavior to access it through such an
462 /// alias. Note that it's always undefined behavior to access a C++
463 /// object that's under construction through an alias derived from
464 /// outside the construction process.
465 ///
466 /// This flag controls whether calls that produce the aggregate
467 /// value may be evaluated directly into the slot, or whether they
468 /// must be evaluated into an unaliased temporary and then memcpy'ed
469 /// over. Since it's invalid in general to memcpy a non-POD C++
470 /// object, it's important that this flag never be set when
471 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000472 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000473
Fangrui Song6907ce22018-07-30 19:24:48 +0000474 /// This is set to true if the tail padding of this slot might overlap
Richard Smithe78fac52018-04-05 20:52:58 +0000475 /// another object that may have already been initialized (and whose
476 /// value must be preserved by this initialization). If so, we may only
477 /// store up to the dsize of the type. Otherwise we can widen stores to
478 /// the size of the type.
479 bool OverlapFlag : 1;
480
Serge Pavlov37605182018-07-28 15:33:03 +0000481 /// If is set to true, sanitizer checks are already generated for this address
482 /// or not required. For instance, if this address represents an object
483 /// created in 'new' expression, sanitizer checks for memory is made as a part
484 /// of 'operator new' emission and object constructor should not generate
485 /// them.
486 bool SanitizerCheckedFlag : 1;
487
John McCall7a626f62010-09-15 10:14:12 +0000488public:
John McCalla5efa732011-08-25 23:04:34 +0000489 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000490 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000491 enum IsZeroed_t { IsNotZeroed, IsZeroed };
Richard Smithe78fac52018-04-05 20:52:58 +0000492 enum Overlap_t { DoesNotOverlap, MayOverlap };
John McCall8d6fc952011-08-25 20:40:09 +0000493 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
Serge Pavlov37605182018-07-28 15:33:03 +0000494 enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
John McCall8d6fc952011-08-25 20:40:09 +0000495
John McCall7a626f62010-09-15 10:14:12 +0000496 /// ignored - Returns an aggregate value slot indicating that the
497 /// aggregate value is being ignored.
498 static AggValueSlot ignored() {
John McCall7f416cc2015-09-08 08:05:57 +0000499 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
Richard Smithe78fac52018-04-05 20:52:58 +0000500 DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
John McCall7a626f62010-09-15 10:14:12 +0000501 }
502
503 /// forAddr - Make a slot for an aggregate value.
504 ///
John McCall8d6fc952011-08-25 20:40:09 +0000505 /// \param quals - The qualifiers that dictate how the slot should
506 /// be initialied. Only 'volatile' and the Objective-C lifetime
507 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000508 ///
John McCall8d6fc952011-08-25 20:40:09 +0000509 /// \param isDestructed - true if something else is responsible
510 /// for calling destructors on this object
511 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000512 /// somewhere that ObjC GC calls should be emitted for
John McCall7f416cc2015-09-08 08:05:57 +0000513 static AggValueSlot forAddr(Address addr,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000514 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000515 IsDestructed_t isDestructed,
516 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000517 IsAliased_t isAliased,
Richard Smithe78fac52018-04-05 20:52:58 +0000518 Overlap_t mayOverlap,
Serge Pavlov37605182018-07-28 15:33:03 +0000519 IsZeroed_t isZeroed = IsNotZeroed,
520 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
John McCall7a626f62010-09-15 10:14:12 +0000521 AggValueSlot AV;
John McCall7f416cc2015-09-08 08:05:57 +0000522 if (addr.isValid()) {
523 AV.Addr = addr.getPointer();
524 AV.Alignment = addr.getAlignment().getQuantity();
525 } else {
526 AV.Addr = nullptr;
527 AV.Alignment = 0;
528 }
John McCall8d6fc952011-08-25 20:40:09 +0000529 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000530 AV.DestructedFlag = isDestructed;
531 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000532 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000533 AV.AliasedFlag = isAliased;
Richard Smithe78fac52018-04-05 20:52:58 +0000534 AV.OverlapFlag = mayOverlap;
Serge Pavlov37605182018-07-28 15:33:03 +0000535 AV.SanitizerCheckedFlag = isChecked;
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,
Richard Smithe78fac52018-04-05 20:52:58 +0000543 Overlap_t mayOverlap,
Serge Pavlov37605182018-07-28 15:33:03 +0000544 IsZeroed_t isZeroed = IsNotZeroed,
545 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
Richard Smithe78fac52018-04-05 20:52:58 +0000546 return forAddr(LV.getAddress(), LV.getQuals(), isDestructed, needsGC,
Serge Pavlov37605182018-07-28 15:33:03 +0000547 isAliased, mayOverlap, isZeroed, isChecked);
John McCall7a626f62010-09-15 10:14:12 +0000548 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000549
John McCallcac93852011-08-26 08:02:37 +0000550 IsDestructed_t isExternallyDestructed() const {
551 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000552 }
John McCallcac93852011-08-26 08:02:37 +0000553 void setExternallyDestructed(bool destructed = true) {
554 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000555 }
556
John McCall31168b02011-06-15 23:02:42 +0000557 Qualifiers getQualifiers() const { return Quals; }
558
John McCall7a626f62010-09-15 10:14:12 +0000559 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000560 return Quals.hasVolatile();
561 }
562
Fariborz Jahanian78652202013-01-25 23:57:05 +0000563 void setVolatile(bool flag) {
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000564 if (flag)
565 Quals.addVolatile();
566 else
567 Quals.removeVolatile();
Fariborz Jahanian78652202013-01-25 23:57:05 +0000568 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000569
John McCall31168b02011-06-15 23:02:42 +0000570 Qualifiers::ObjCLifetime getObjCLifetime() const {
571 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000572 }
573
John McCall8d6fc952011-08-25 20:40:09 +0000574 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000575 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000576 }
John McCall7f416cc2015-09-08 08:05:57 +0000577
578 llvm::Value *getPointer() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000579 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000580 }
581
John McCall7f416cc2015-09-08 08:05:57 +0000582 Address getAddress() const {
583 return Address(Addr, getAlignment());
584 }
585
John McCall7a626f62010-09-15 10:14:12 +0000586 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000587 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000588 }
589
Eli Friedman38cd36d2011-12-03 02:13:40 +0000590 CharUnits getAlignment() const {
591 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000592 }
593
John McCalla5efa732011-08-25 23:04:34 +0000594 IsAliased_t isPotentiallyAliased() const {
595 return IsAliased_t(AliasedFlag);
596 }
597
Richard Smithe78fac52018-04-05 20:52:58 +0000598 Overlap_t mayOverlap() const {
599 return Overlap_t(OverlapFlag);
600 }
601
Serge Pavlov37605182018-07-28 15:33:03 +0000602 bool isSanitizerChecked() const {
603 return SanitizerCheckedFlag;
604 }
605
John McCall7a626f62010-09-15 10:14:12 +0000606 RValue asRValue() const {
John McCall7f416cc2015-09-08 08:05:57 +0000607 if (isIgnored()) {
608 return RValue::getIgnored();
609 } else {
610 return RValue::getAggregate(getAddress(), isVolatile());
611 }
John McCall7a626f62010-09-15 10:14:12 +0000612 }
John McCallfe96e0b2011-11-06 09:01:30 +0000613
John McCall8d6fc952011-08-25 20:40:09 +0000614 void setZeroed(bool V = true) { ZeroedFlag = V; }
615 IsZeroed_t isZeroed() const {
616 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000617 }
Richard Smithe78fac52018-04-05 20:52:58 +0000618
619 /// Get the preferred size to use when storing a value to this slot. This
620 /// is the type size unless that might overlap another object, in which
621 /// case it's the dsize.
622 CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
623 return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).first
624 : Ctx.getTypeSizeInChars(Type);
625 }
John McCall7a626f62010-09-15 10:14:12 +0000626};
627
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000628} // end namespace CodeGen
629} // end namespace clang
630
631#endif