blob: 4b39a0520833fbfd75ce45445654919915368132 [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"
Diogo Sampaio6a243392020-01-21 15:13:24 +000019#include "llvm/IR/Value.h"
Diogo Sampaio21477032020-01-21 15:31:33 +000020#include "llvm/IR/Type.h"
21#include "Address.h"
22#include "CodeGenTBAA.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000023
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000024namespace llvm {
25 class Constant;
John McCall47fb9502013-03-07 21:37:08 +000026 class MDNode;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000027}
28
Daniel Dunbard4f616b2008-08-23 03:10:25 +000029namespace clang {
30namespace CodeGen {
John McCallfe96e0b2011-11-06 09:01:30 +000031 class AggValueSlot;
Akira Hatanakaf139ae32019-12-03 15:17:01 -080032 class CodeGenFunction;
Chandler Carruthff0e3a12012-12-06 11:14:44 +000033 struct CGBitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +000034
35/// RValue - This trivial value class is used to represent the result of an
36/// expression that is evaluated. It can be one of three things: either a
37/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
38/// address of an aggregate value in memory.
39class RValue {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000040 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump11289f42009-09-09 15:08:12 +000041
John McCall7f416cc2015-09-08 08:05:57 +000042 // The shift to make to an aggregate's alignment to make it look
43 // like a pointer.
44 enum { AggAlignShift = 4 };
45
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000046 // Stores first value and flavor.
47 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
48 // Stores second value and volatility.
49 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
50
Daniel Dunbard4f616b2008-08-23 03:10:25 +000051public:
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000052 bool isScalar() const { return V1.getInt() == Scalar; }
53 bool isComplex() const { return V1.getInt() == Complex; }
54 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump11289f42009-09-09 15:08:12 +000055
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000056 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump93700fc2009-05-23 20:21:36 +000057
Mike Stump462a4aa2009-11-03 16:11:57 +000058 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbard4f616b2008-08-23 03:10:25 +000059 llvm::Value *getScalarVal() const {
60 assert(isScalar() && "Not a scalar!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000061 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000062 }
63
64 /// getComplexVal - Return the real/imag components of this complex value.
65 ///
66 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000067 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbard4f616b2008-08-23 03:10:25 +000068 }
Mike Stump11289f42009-09-09 15:08:12 +000069
Daniel Dunbard4f616b2008-08-23 03:10:25 +000070 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
John McCall7f416cc2015-09-08 08:05:57 +000071 Address getAggregateAddress() const {
72 assert(isAggregate() && "Not an aggregate!");
73 auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
74 return Address(V1.getPointer(), CharUnits::fromQuantity(align));
75 }
76 llvm::Value *getAggregatePointer() const {
Daniel Dunbard4f616b2008-08-23 03:10:25 +000077 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000078 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
John McCall7f416cc2015-09-08 08:05:57 +000081 static RValue getIgnored() {
82 // FIXME: should we make this a more explicit state?
83 return get(nullptr);
84 }
85
Daniel Dunbard4f616b2008-08-23 03:10:25 +000086 static RValue get(llvm::Value *V) {
87 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000088 ER.V1.setPointer(V);
89 ER.V1.setInt(Scalar);
90 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000091 return ER;
92 }
93 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
94 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000095 ER.V1.setPointer(V1);
96 ER.V2.setPointer(V2);
97 ER.V1.setInt(Complex);
98 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000099 return ER;
100 }
101 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000102 return getComplex(C.first, C.second);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000103 }
Mike Stump93700fc2009-05-23 20:21:36 +0000104 // FIXME: Aggregate rvalues need to retain information about whether they are
105 // volatile or not. Remove default to find all places that probably get this
106 // wrong.
John McCall7f416cc2015-09-08 08:05:57 +0000107 static RValue getAggregate(Address addr, bool isVolatile = false) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000108 RValue ER;
John McCall7f416cc2015-09-08 08:05:57 +0000109 ER.V1.setPointer(addr.getPointer());
Benjamin Kramer2718b4d2010-05-02 14:59:10 +0000110 ER.V1.setInt(Aggregate);
John McCall7f416cc2015-09-08 08:05:57 +0000111
112 auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
113 ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
114 ER.V2.setInt(isVolatile);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000115 return ER;
116 }
117};
118
John McCallcdda29c2013-03-13 03:10:54 +0000119/// Does an ARC strong l-value have precise lifetime?
120enum ARCPreciseLifetime_t {
John McCall4d31b812013-03-13 05:02:21 +0000121 ARCImpreciseLifetime, ARCPreciseLifetime
John McCallcdda29c2013-03-13 03:10:54 +0000122};
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000123
John McCall7f416cc2015-09-08 08:05:57 +0000124/// The source of the alignment of an l-value; an expression of
125/// confidence in the alignment actually matching the estimate.
126enum class AlignmentSource {
127 /// The l-value was an access to a declared entity or something
128 /// equivalently strong, like the address of an array allocated by a
129 /// language runtime.
130 Decl,
131
132 /// The l-value was considered opaque, so the alignment was
133 /// determined from a type, but that type was an explicitly-aligned
134 /// typedef.
135 AttributedType,
136
137 /// The l-value was considered opaque, so the alignment was
138 /// determined from a type.
139 Type
140};
141
142/// Given that the base address has the given alignment source, what's
143/// our confidence in the alignment of the field?
144static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
145 // For now, we don't distinguish fields of opaque pointers from
146 // top-level declarations, but maybe we should.
147 return AlignmentSource::Decl;
148}
149
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000150class LValueBaseInfo {
151 AlignmentSource AlignSource;
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000152
153public:
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000154 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
155 : AlignSource(Source) {}
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000156 AlignmentSource getAlignmentSource() const { return AlignSource; }
157 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000158
159 void mergeForCast(const LValueBaseInfo &Info) {
160 setAlignmentSource(Info.getAlignmentSource());
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000161 }
162};
163
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000164/// LValue - This represents an lvalue references. Because C/C++ allow
165/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
166/// bitrange.
167class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000168 enum {
169 Simple, // This is a normal l-value, use getAddress().
170 VectorElt, // This is a vector element l-value (V[i]), use getVector*
171 BitField, // This is a bitfield l-value, use getBitfield*.
Renato Golin230c5eb2014-05-19 18:15:42 +0000172 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
Florian Hahn8f3f88d2020-06-01 19:42:03 +0100173 GlobalReg, // This is a register l-value, use getGlobalReg()
174 MatrixElt // This is a matrix element, use getVector*
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000175 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000176
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000177 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000179 union {
180 // Index into a vector subscript: V[i]
181 llvm::Value *VectorIdx;
182
183 // ExtVector element subset: V.xyx
184 llvm::Constant *VectorElts;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000185
Diogo Sampaio21477032020-01-21 15:31:33 +0000186 // BitField start bit and size
187 const CGBitFieldInfo *BitFieldInfo;
188 };
Diogo Sampaio6a243392020-01-21 15:13:24 +0000189
John McCall1553b192011-06-16 04:16:24 +0000190 QualType Type;
191
John McCall8ccfcb52009-09-24 19:53:00 +0000192 // 'const' is unused here
193 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000194
Eli Friedman610bb872012-03-22 22:36:39 +0000195 // The alignment to use when accessing this lvalue. (For vector elements,
196 // this is the alignment of the whole vector.)
Yaxun Liud9389822018-03-14 15:02:28 +0000197 unsigned Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000198
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000199 // objective-c's ivar
200 bool Ivar:1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000201
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000202 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000203 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000204
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000205 // LValue is non-gc'able for any reason, including being a parameter or local
206 // variable.
207 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000208
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000209 // Lvalue is a global reference of an objective-c object
210 bool GlobalObjCRef : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000211
Fariborz Jahanian217af242010-07-20 20:30:03 +0000212 // Lvalue is a thread local reference
213 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000214
John McCallcdda29c2013-03-13 03:10:54 +0000215 // Lvalue has ARC imprecise lifetime. We store this inverted to try
216 // to make the default bitfield pattern all-zeroes.
217 bool ImpreciseLifetime : 1;
218
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000219 // This flag shows if a nontemporal load/stores should be used when accessing
220 // this lvalue.
221 bool Nontemporal : 1;
222
Yaxun Liud9389822018-03-14 15:02:28 +0000223 LValueBaseInfo BaseInfo;
224 TBAAAccessInfo TBAAInfo;
225
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000226 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000227
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000228private:
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000229 void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment,
230 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall7f416cc2015-09-08 08:05:57 +0000231 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
232 "initializing l-value with zero alignment!");
John McCall1553b192011-06-16 04:16:24 +0000233 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000234 this->Quals = Quals;
Yaxun Liud9389822018-03-14 15:02:28 +0000235 const unsigned MaxAlign = 1U << 31;
236 this->Alignment = Alignment.getQuantity() <= MaxAlign
237 ? Alignment.getQuantity()
238 : MaxAlign;
Eli Friedmana0544d62011-12-03 04:14:32 +0000239 assert(this->Alignment == Alignment.getQuantity() &&
240 "Alignment exceeds allowed max!");
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000241 this->BaseInfo = BaseInfo;
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000242 this->TBAAInfo = TBAAInfo;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000243
244 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000245 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000246 this->ImpreciseLifetime = false;
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000247 this->Nontemporal = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000248 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000249 this->BaseIvarExp = nullptr;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000250 }
Mike Stump11289f42009-09-09 15:08:12 +0000251
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000252public:
253 bool isSimple() const { return LVType == Simple; }
254 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000255 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000256 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000257 bool isGlobalReg() const { return LVType == GlobalReg; }
Florian Hahn8f3f88d2020-06-01 19:42:03 +0100258 bool isMatrixElt() const { return LVType == MatrixElt; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000259
John McCall8ccfcb52009-09-24 19:53:00 +0000260 bool isVolatileQualified() const { return Quals.hasVolatile(); }
261 bool isRestrictQualified() const { return Quals.hasRestrict(); }
262 unsigned getVRQualifiers() const {
263 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
John McCall1553b192011-06-16 04:16:24 +0000266 QualType getType() const { return Type; }
267
268 Qualifiers::ObjCLifetime getObjCLifetime() const {
269 return Quals.getObjCLifetime();
270 }
271
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000272 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000273 void setObjCIvar(bool Value) { Ivar = Value; }
274
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000275 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000276 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000277
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000278 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000279 void setNonGC(bool Value) { NonGC = Value; }
280
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000281 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000282 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
283
Fariborz Jahanian217af242010-07-20 20:30:03 +0000284 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000285 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
286
John McCallcdda29c2013-03-13 03:10:54 +0000287 ARCPreciseLifetime_t isARCPreciseLifetime() const {
288 return ARCPreciseLifetime_t(!ImpreciseLifetime);
289 }
290 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
291 ImpreciseLifetime = (value == ARCImpreciseLifetime);
292 }
Michael Zolotukhin84df1232015-09-08 23:52:33 +0000293 bool isNontemporal() const { return Nontemporal; }
294 void setNontemporal(bool Value) { Nontemporal = Value; }
John McCallcdda29c2013-03-13 03:10:54 +0000295
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000296 bool isObjCWeak() const {
297 return Quals.getObjCGCAttr() == Qualifiers::Weak;
298 }
299 bool isObjCStrong() const {
300 return Quals.getObjCGCAttr() == Qualifiers::Strong;
301 }
John McCall1553b192011-06-16 04:16:24 +0000302
303 bool isVolatile() const {
304 return Quals.hasVolatile();
305 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000306
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000307 Expr *getBaseIvarExp() const { return BaseIvarExp; }
308 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000309
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000310 TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
311 void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
Manman Renc451e572013-04-04 21:53:22 +0000312
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000313 const Qualifiers &getQuals() const { return Quals; }
314 Qualifiers &getQuals() { return Quals; }
315
Alexander Richardson6d989432017-10-15 18:48:14 +0000316 LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000317
Eli Friedmana0544d62011-12-03 04:14:32 +0000318 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
319 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000320
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000321 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
322 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
John McCall7f416cc2015-09-08 08:05:57 +0000323
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000324 // simple lvalue
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800325 llvm::Value *getPointer(CodeGenFunction &CGF) const {
John McCall1553b192011-06-16 04:16:24 +0000326 assert(isSimple());
John McCall7f416cc2015-09-08 08:05:57 +0000327 return V;
328 }
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800329 Address getAddress(CodeGenFunction &CGF) const {
330 return Address(getPointer(CGF), getAlignment());
331 }
John McCall7f416cc2015-09-08 08:05:57 +0000332 void setAddress(Address address) {
333 assert(isSimple());
334 V = address.getPointer();
335 Alignment = address.getAlignment().getQuantity();
John McCall1553b192011-06-16 04:16:24 +0000336 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000337
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000338 // vector elt lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000339 Address getVectorAddress() const {
340 return Address(getVectorPointer(), getAlignment());
341 }
Florian Hahn8f3f88d2020-06-01 19:42:03 +0100342 llvm::Value *getVectorPointer() const {
343 assert(isVectorElt());
344 return V;
345 }
346 llvm::Value *getVectorIdx() const {
347 assert(isVectorElt());
348 return VectorIdx;
349 }
350
351 Address getMatrixAddress() const {
352 return Address(getMatrixPointer(), getAlignment());
353 }
354 llvm::Value *getMatrixPointer() const {
355 assert(isMatrixElt());
356 return V;
357 }
358 llvm::Value *getMatrixIdx() const {
359 assert(isMatrixElt());
360 return VectorIdx;
361 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000362
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000363 // extended vector elements.
John McCall7f416cc2015-09-08 08:05:57 +0000364 Address getExtVectorAddress() const {
365 return Address(getExtVectorPointer(), getAlignment());
366 }
367 llvm::Value *getExtVectorPointer() const {
368 assert(isExtVectorElt());
369 return V;
370 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000371 llvm::Constant *getExtVectorElts() const {
372 assert(isExtVectorElt());
373 return VectorElts;
374 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000375
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000376 // bitfield lvalue
John McCall7f416cc2015-09-08 08:05:57 +0000377 Address getBitFieldAddress() const {
378 return Address(getBitFieldPointer(), getAlignment());
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000379 }
Diogo Sampaio21477032020-01-21 15:31:33 +0000380 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000381 const CGBitFieldInfo &getBitFieldInfo() const {
382 assert(isBitField());
Diogo Sampaio21477032020-01-21 15:31:33 +0000383 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000384 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000385
Renato Golin230c5eb2014-05-19 18:15:42 +0000386 // global register lvalue
387 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
388
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000389 static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
390 LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
John McCall1553b192011-06-16 04:16:24 +0000391 Qualifiers qs = type.getQualifiers();
392 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000393
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000394 LValue R;
395 R.LVType = Simple;
John McCall7f416cc2015-09-08 08:05:57 +0000396 assert(address.getPointer()->getType()->isPointerTy());
397 R.V = address.getPointer();
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000398 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000399 return R;
400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
John McCall7f416cc2015-09-08 08:05:57 +0000402 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000403 QualType type, LValueBaseInfo BaseInfo,
404 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000405 LValue R;
406 R.LVType = VectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000407 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000408 R.VectorIdx = Idx;
John McCall7f416cc2015-09-08 08:05:57 +0000409 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000410 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000411 return R;
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
John McCall7f416cc2015-09-08 08:05:57 +0000414 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000415 QualType type, LValueBaseInfo BaseInfo,
416 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000417 LValue R;
418 R.LVType = ExtVectorElt;
John McCall7f416cc2015-09-08 08:05:57 +0000419 R.V = vecAddress.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000420 R.VectorElts = Elts;
John McCall7f416cc2015-09-08 08:05:57 +0000421 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000422 BaseInfo, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000423 return R;
424 }
425
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000426 /// Create a new object to represent a bit-field access.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000427 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000428 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000429 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000430 /// \param Info - The information describing how to perform the bit-field
431 /// access.
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000432 static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
433 QualType type, LValueBaseInfo BaseInfo,
434 TBAAAccessInfo TBAAInfo) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000435 LValue R;
436 R.LVType = BitField;
John McCall7f416cc2015-09-08 08:05:57 +0000437 R.V = Addr.getPointer();
Diogo Sampaio21477032020-01-21 15:31:33 +0000438 R.BitFieldInfo = &Info;
Ivan A. Kosarevd17f12a2017-10-17 10:17:43 +0000439 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
440 TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000441 return R;
442 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000443
John McCall7f416cc2015-09-08 08:05:57 +0000444 static LValue MakeGlobalReg(Address Reg, QualType type) {
Renato Golin230c5eb2014-05-19 18:15:42 +0000445 LValue R;
446 R.LVType = GlobalReg;
John McCall7f416cc2015-09-08 08:05:57 +0000447 R.V = Reg.getPointer();
448 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000449 LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
Renato Golin230c5eb2014-05-19 18:15:42 +0000450 return R;
451 }
452
Florian Hahn8f3f88d2020-06-01 19:42:03 +0100453 static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx,
454 QualType type, LValueBaseInfo BaseInfo,
455 TBAAAccessInfo TBAAInfo) {
456 LValue R;
457 R.LVType = MatrixElt;
458 R.V = matAddress.getPointer();
459 R.VectorIdx = Idx;
460 R.Initialize(type, type.getQualifiers(), matAddress.getAlignment(),
461 BaseInfo, TBAAInfo);
462 return R;
463 }
464
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800465 RValue asAggregateRValue(CodeGenFunction &CGF) const {
466 return RValue::getAggregate(getAddress(CGF), isVolatileQualified());
Eli Friedman2869b5a2011-12-03 03:08:40 +0000467 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000468};
469
John McCall7a626f62010-09-15 10:14:12 +0000470/// An aggregate value slot.
471class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000472 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000473 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000474
475 // Qualifiers
476 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000477
David Majnemerec4b7342016-03-02 06:48:47 +0000478 unsigned Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000479
John McCallcac93852011-08-26 08:02:37 +0000480 /// DestructedFlag - This is set to true if some external code is
481 /// responsible for setting up a destructor for the slot. Otherwise
482 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000483 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000484
485 /// ObjCGCFlag - This is set to true if writing to the memory in the
486 /// slot might require calling an appropriate Objective-C GC
487 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000488 bool ObjCGCFlag : 1;
Fangrui Song6907ce22018-07-30 19:24:48 +0000489
John McCallcac93852011-08-26 08:02:37 +0000490 /// ZeroedFlag - This is set to true if the memory in the slot is
491 /// known to be zero before the assignment into it. This means that
492 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000493 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000494
John McCallcac93852011-08-26 08:02:37 +0000495 /// AliasedFlag - This is set to true if the slot might be aliased
496 /// and it's not undefined behavior to access it through such an
497 /// alias. Note that it's always undefined behavior to access a C++
498 /// object that's under construction through an alias derived from
499 /// outside the construction process.
500 ///
501 /// This flag controls whether calls that produce the aggregate
502 /// value may be evaluated directly into the slot, or whether they
503 /// must be evaluated into an unaliased temporary and then memcpy'ed
504 /// over. Since it's invalid in general to memcpy a non-POD C++
505 /// object, it's important that this flag never be set when
506 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000507 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000508
Fangrui Song6907ce22018-07-30 19:24:48 +0000509 /// This is set to true if the tail padding of this slot might overlap
Richard Smithe78fac52018-04-05 20:52:58 +0000510 /// another object that may have already been initialized (and whose
511 /// value must be preserved by this initialization). If so, we may only
512 /// store up to the dsize of the type. Otherwise we can widen stores to
513 /// the size of the type.
514 bool OverlapFlag : 1;
515
Serge Pavlov37605182018-07-28 15:33:03 +0000516 /// If is set to true, sanitizer checks are already generated for this address
517 /// or not required. For instance, if this address represents an object
518 /// created in 'new' expression, sanitizer checks for memory is made as a part
519 /// of 'operator new' emission and object constructor should not generate
520 /// them.
521 bool SanitizerCheckedFlag : 1;
522
John McCall7a626f62010-09-15 10:14:12 +0000523public:
John McCalla5efa732011-08-25 23:04:34 +0000524 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000525 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000526 enum IsZeroed_t { IsNotZeroed, IsZeroed };
Richard Smithe78fac52018-04-05 20:52:58 +0000527 enum Overlap_t { DoesNotOverlap, MayOverlap };
John McCall8d6fc952011-08-25 20:40:09 +0000528 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
Serge Pavlov37605182018-07-28 15:33:03 +0000529 enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
John McCall8d6fc952011-08-25 20:40:09 +0000530
John McCall7a626f62010-09-15 10:14:12 +0000531 /// ignored - Returns an aggregate value slot indicating that the
532 /// aggregate value is being ignored.
533 static AggValueSlot ignored() {
John McCall7f416cc2015-09-08 08:05:57 +0000534 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
Richard Smithe78fac52018-04-05 20:52:58 +0000535 DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
John McCall7a626f62010-09-15 10:14:12 +0000536 }
537
538 /// forAddr - Make a slot for an aggregate value.
539 ///
John McCall8d6fc952011-08-25 20:40:09 +0000540 /// \param quals - The qualifiers that dictate how the slot should
541 /// be initialied. Only 'volatile' and the Objective-C lifetime
542 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000543 ///
John McCall8d6fc952011-08-25 20:40:09 +0000544 /// \param isDestructed - true if something else is responsible
545 /// for calling destructors on this object
546 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000547 /// somewhere that ObjC GC calls should be emitted for
John McCall7f416cc2015-09-08 08:05:57 +0000548 static AggValueSlot forAddr(Address addr,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000549 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000550 IsDestructed_t isDestructed,
551 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000552 IsAliased_t isAliased,
Richard Smithe78fac52018-04-05 20:52:58 +0000553 Overlap_t mayOverlap,
Serge Pavlov37605182018-07-28 15:33:03 +0000554 IsZeroed_t isZeroed = IsNotZeroed,
555 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
John McCall7a626f62010-09-15 10:14:12 +0000556 AggValueSlot AV;
John McCall7f416cc2015-09-08 08:05:57 +0000557 if (addr.isValid()) {
558 AV.Addr = addr.getPointer();
559 AV.Alignment = addr.getAlignment().getQuantity();
560 } else {
561 AV.Addr = nullptr;
562 AV.Alignment = 0;
563 }
John McCall8d6fc952011-08-25 20:40:09 +0000564 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000565 AV.DestructedFlag = isDestructed;
566 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000567 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000568 AV.AliasedFlag = isAliased;
Richard Smithe78fac52018-04-05 20:52:58 +0000569 AV.OverlapFlag = mayOverlap;
Serge Pavlov37605182018-07-28 15:33:03 +0000570 AV.SanitizerCheckedFlag = isChecked;
John McCall7a626f62010-09-15 10:14:12 +0000571 return AV;
572 }
573
Akira Hatanakaf139ae32019-12-03 15:17:01 -0800574 static AggValueSlot
575 forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed,
576 NeedsGCBarriers_t needsGC, IsAliased_t isAliased,
577 Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed,
578 IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
579 return forAddr(LV.getAddress(CGF), LV.getQuals(), isDestructed, needsGC,
Serge Pavlov37605182018-07-28 15:33:03 +0000580 isAliased, mayOverlap, isZeroed, isChecked);
John McCall7a626f62010-09-15 10:14:12 +0000581 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000582
John McCallcac93852011-08-26 08:02:37 +0000583 IsDestructed_t isExternallyDestructed() const {
584 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000585 }
John McCallcac93852011-08-26 08:02:37 +0000586 void setExternallyDestructed(bool destructed = true) {
587 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000588 }
589
John McCall31168b02011-06-15 23:02:42 +0000590 Qualifiers getQualifiers() const { return Quals; }
591
John McCall7a626f62010-09-15 10:14:12 +0000592 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000593 return Quals.hasVolatile();
594 }
595
Fariborz Jahanian78652202013-01-25 23:57:05 +0000596 void setVolatile(bool flag) {
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000597 if (flag)
598 Quals.addVolatile();
599 else
600 Quals.removeVolatile();
Fariborz Jahanian78652202013-01-25 23:57:05 +0000601 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000602
John McCall31168b02011-06-15 23:02:42 +0000603 Qualifiers::ObjCLifetime getObjCLifetime() const {
604 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000605 }
606
John McCall8d6fc952011-08-25 20:40:09 +0000607 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000608 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000609 }
John McCall7f416cc2015-09-08 08:05:57 +0000610
611 llvm::Value *getPointer() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000612 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000613 }
614
John McCall7f416cc2015-09-08 08:05:57 +0000615 Address getAddress() const {
616 return Address(Addr, getAlignment());
617 }
618
John McCall7a626f62010-09-15 10:14:12 +0000619 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000620 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000621 }
622
Eli Friedman38cd36d2011-12-03 02:13:40 +0000623 CharUnits getAlignment() const {
624 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000625 }
626
John McCalla5efa732011-08-25 23:04:34 +0000627 IsAliased_t isPotentiallyAliased() const {
628 return IsAliased_t(AliasedFlag);
629 }
630
Richard Smithe78fac52018-04-05 20:52:58 +0000631 Overlap_t mayOverlap() const {
632 return Overlap_t(OverlapFlag);
633 }
634
Serge Pavlov37605182018-07-28 15:33:03 +0000635 bool isSanitizerChecked() const {
636 return SanitizerCheckedFlag;
637 }
638
John McCall7a626f62010-09-15 10:14:12 +0000639 RValue asRValue() const {
John McCall7f416cc2015-09-08 08:05:57 +0000640 if (isIgnored()) {
641 return RValue::getIgnored();
642 } else {
643 return RValue::getAggregate(getAddress(), isVolatile());
644 }
John McCall7a626f62010-09-15 10:14:12 +0000645 }
John McCallfe96e0b2011-11-06 09:01:30 +0000646
John McCall8d6fc952011-08-25 20:40:09 +0000647 void setZeroed(bool V = true) { ZeroedFlag = V; }
648 IsZeroed_t isZeroed() const {
649 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000650 }
Richard Smithe78fac52018-04-05 20:52:58 +0000651
652 /// Get the preferred size to use when storing a value to this slot. This
653 /// is the type size unless that might overlap another object, in which
654 /// case it's the dsize.
655 CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
Bevin Hansson101309f2020-08-24 10:19:29 +0200656 return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).Width
Richard Smithe78fac52018-04-05 20:52:58 +0000657 : Ctx.getTypeSizeInChars(Type);
658 }
John McCall7a626f62010-09-15 10:14:12 +0000659};
660
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000661} // end namespace CodeGen
662} // end namespace clang
663
664#endif