blob: b625b866c072b5e8af693bc3cd6635055f62ed06 [file] [log] [blame]
Daniel Dunbar2eecaab2008-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
15#ifndef CLANG_CODEGEN_CGVALUE_H
16#define CLANG_CODEGEN_CGVALUE_H
17
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +000018#include "clang/AST/ASTContext.h"
Eli Friedmand7722d92011-12-03 02:13:40 +000019#include "clang/AST/CharUnits.h"
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000020#include "clang/AST/Type.h"
John McCall9d232c82013-03-07 21:37:08 +000021#include "llvm/IR/Value.h"
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000022
Daniel Dunbar46f45b92008-09-09 01:06:48 +000023namespace llvm {
24 class Constant;
John McCall9d232c82013-03-07 21:37:08 +000025 class MDNode;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000026}
27
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000028namespace clang {
29namespace CodeGen {
John McCall4b9c2d22011-11-06 09:01:30 +000030 class AggValueSlot;
Chandler Carruth72d2dab2012-12-06 11:14:44 +000031 struct CGBitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000032
33/// RValue - This trivial value class is used to represent the result of an
34/// expression that is evaluated. It can be one of three things: either a
35/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
36/// address of an aggregate value in memory.
37class RValue {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000038 enum Flavor { Scalar, Complex, Aggregate };
Mike Stump1eb44332009-09-09 15:08:12 +000039
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000040 // Stores first value and flavor.
41 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
42 // Stores second value and volatility.
43 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
44
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000045public:
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000046 bool isScalar() const { return V1.getInt() == Scalar; }
47 bool isComplex() const { return V1.getInt() == Complex; }
48 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000050 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump8b3d93a2009-05-23 20:21:36 +000051
Mike Stump519202d2009-11-03 16:11:57 +000052 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000053 llvm::Value *getScalarVal() const {
54 assert(isScalar() && "Not a scalar!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000055 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000056 }
57
58 /// getComplexVal - Return the real/imag components of this complex value.
59 ///
60 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000061 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000062 }
Mike Stump1eb44332009-09-09 15:08:12 +000063
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000064 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
65 llvm::Value *getAggregateAddr() const {
66 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000067 return V1.getPointer();
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000068 }
Mike Stump1eb44332009-09-09 15:08:12 +000069
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000070 static RValue get(llvm::Value *V) {
71 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000072 ER.V1.setPointer(V);
73 ER.V1.setInt(Scalar);
74 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000075 return ER;
76 }
77 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
78 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000079 ER.V1.setPointer(V1);
80 ER.V2.setPointer(V2);
81 ER.V1.setInt(Complex);
82 ER.V2.setInt(false);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000083 return ER;
84 }
85 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000086 return getComplex(C.first, C.second);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000087 }
Mike Stump8b3d93a2009-05-23 20:21:36 +000088 // FIXME: Aggregate rvalues need to retain information about whether they are
89 // volatile or not. Remove default to find all places that probably get this
90 // wrong.
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000091 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000092 RValue ER;
Benjamin Kramer81bf3b32010-05-02 14:59:10 +000093 ER.V1.setPointer(V);
94 ER.V1.setInt(Aggregate);
95 ER.V2.setInt(Volatile);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +000096 return ER;
97 }
98};
99
John McCall5b07e802013-03-13 03:10:54 +0000100/// Does an ARC strong l-value have precise lifetime?
101enum ARCPreciseLifetime_t {
John McCall99c64182013-03-13 05:02:21 +0000102 ARCImpreciseLifetime, ARCPreciseLifetime
John McCall5b07e802013-03-13 03:10:54 +0000103};
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000104
105/// LValue - This represents an lvalue references. Because C/C++ allow
106/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
107/// bitrange.
108class LValue {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000109 enum {
110 Simple, // This is a normal l-value, use getAddress().
111 VectorElt, // This is a vector element l-value (V[i]), use getVector*
112 BitField, // This is a bitfield l-value, use getBitfield*.
John McCalldb458062011-11-07 03:59:57 +0000113 ExtVectorElt // This is an extended vector subset, use getExtVectorComp
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000114 } LVType;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000115
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000116 llvm::Value *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000118 union {
119 // Index into a vector subscript: V[i]
120 llvm::Value *VectorIdx;
121
122 // ExtVector element subset: V.xyx
123 llvm::Constant *VectorElts;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000125 // BitField start bit and size
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000126 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000127 };
128
John McCalla07398e2011-06-16 04:16:24 +0000129 QualType Type;
130
John McCall0953e762009-09-24 19:53:00 +0000131 // 'const' is unused here
132 Qualifiers Quals;
Fariborz Jahanian58626502008-11-19 00:59:10 +0000133
Eli Friedmane5a8aeb2012-03-22 22:36:39 +0000134 // The alignment to use when accessing this lvalue. (For vector elements,
135 // this is the alignment of the whole vector.)
John Criswell9e4abb42012-08-15 18:40:30 +0000136 int64_t Alignment;
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000137
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000138 // objective-c's ivar
139 bool Ivar:1;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000140
141 // objective-c's ivar is an array
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000142 bool ObjIsArray:1;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000144 // LValue is non-gc'able for any reason, including being a parameter or local
145 // variable.
146 bool NonGC: 1;
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000147
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000148 // Lvalue is a global reference of an objective-c object
149 bool GlobalObjCRef : 1;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000150
151 // Lvalue is a thread local reference
152 bool ThreadLocalRef : 1;
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000153
John McCall5b07e802013-03-13 03:10:54 +0000154 // Lvalue has ARC imprecise lifetime. We store this inverted to try
155 // to make the default bitfield pattern all-zeroes.
156 bool ImpreciseLifetime : 1;
157
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000158 Expr *BaseIvarExp;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000159
Manman Renb37a73d2013-04-04 21:53:22 +0000160 /// Used by struct-path-aware TBAA.
161 QualType TBAABaseType;
162 /// Offset relative to the base type.
163 uint64_t TBAAOffset;
164
Dan Gohman3d5aff52010-10-14 23:06:10 +0000165 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
166 llvm::MDNode *TBAAInfo;
167
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000168private:
Eli Friedman6da2c712011-12-03 04:14:32 +0000169 void Initialize(QualType Type, Qualifiers Quals,
Eli Friedmanf4bcfa12012-06-27 21:19:48 +0000170 CharUnits Alignment,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000171 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000172 this->Type = Type;
John McCall0953e762009-09-24 19:53:00 +0000173 this->Quals = Quals;
Eli Friedman6da2c712011-12-03 04:14:32 +0000174 this->Alignment = Alignment.getQuantity();
175 assert(this->Alignment == Alignment.getQuantity() &&
176 "Alignment exceeds allowed max!");
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000177
178 // Initialize Objective-C flags.
John McCall0953e762009-09-24 19:53:00 +0000179 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCall5b07e802013-03-13 03:10:54 +0000180 this->ImpreciseLifetime = false;
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000181 this->ThreadLocalRef = false;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000182 this->BaseIvarExp = 0;
Manman Renb37a73d2013-04-04 21:53:22 +0000183
184 // Initialize fields for TBAA.
185 this->TBAABaseType = Type;
186 this->TBAAOffset = 0;
Dan Gohman3d5aff52010-10-14 23:06:10 +0000187 this->TBAAInfo = TBAAInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000190public:
191 bool isSimple() const { return LVType == Simple; }
192 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000193 bool isBitField() const { return LVType == BitField; }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000194 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000195
John McCall0953e762009-09-24 19:53:00 +0000196 bool isVolatileQualified() const { return Quals.hasVolatile(); }
197 bool isRestrictQualified() const { return Quals.hasRestrict(); }
198 unsigned getVRQualifiers() const {
199 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattner1bd885e2009-02-16 22:25:49 +0000200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
John McCalla07398e2011-06-16 04:16:24 +0000202 QualType getType() const { return Type; }
203
204 Qualifiers::ObjCLifetime getObjCLifetime() const {
205 return Quals.getObjCLifetime();
206 }
207
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000208 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000209 void setObjCIvar(bool Value) { Ivar = Value; }
210
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000211 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000212 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000213
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000214 bool isNonGC () const { return NonGC; }
Daniel Dunbarea619172010-08-21 03:22:38 +0000215 void setNonGC(bool Value) { NonGC = Value; }
216
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000217 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000218 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
219
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000220 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000221 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
222
John McCall5b07e802013-03-13 03:10:54 +0000223 ARCPreciseLifetime_t isARCPreciseLifetime() const {
224 return ARCPreciseLifetime_t(!ImpreciseLifetime);
225 }
226 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
227 ImpreciseLifetime = (value == ARCImpreciseLifetime);
228 }
229
Daniel Dunbar3491b3d2010-08-21 03:51:29 +0000230 bool isObjCWeak() const {
231 return Quals.getObjCGCAttr() == Qualifiers::Weak;
232 }
233 bool isObjCStrong() const {
234 return Quals.getObjCGCAttr() == Qualifiers::Strong;
235 }
John McCalla07398e2011-06-16 04:16:24 +0000236
237 bool isVolatile() const {
238 return Quals.hasVolatile();
239 }
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000240
241 Expr *getBaseIvarExp() const { return BaseIvarExp; }
242 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000243
Manman Renb37a73d2013-04-04 21:53:22 +0000244 QualType getTBAABaseType() const { return TBAABaseType; }
245 void setTBAABaseType(QualType T) { TBAABaseType = T; }
246
247 uint64_t getTBAAOffset() const { return TBAAOffset; }
248 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
249
Dan Gohman3d5aff52010-10-14 23:06:10 +0000250 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
251 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
252
Daniel Dunbar99ad7df2010-08-21 03:29:54 +0000253 const Qualifiers &getQuals() const { return Quals; }
254 Qualifiers &getQuals() { return Quals; }
255
John McCall0953e762009-09-24 19:53:00 +0000256 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangc6a38a42009-07-22 03:08:17 +0000257
Eli Friedman6da2c712011-12-03 04:14:32 +0000258 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
259 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbar9f4f7cf2010-08-21 02:39:23 +0000260
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000261 // simple lvalue
262 llvm::Value *getAddress() const { assert(isSimple()); return V; }
John McCalla07398e2011-06-16 04:16:24 +0000263 void setAddress(llvm::Value *address) {
264 assert(isSimple());
265 V = address;
266 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000267
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000268 // vector elt lvalue
269 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
270 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000271
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000272 // extended vector elements.
273 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
274 llvm::Constant *getExtVectorElts() const {
275 assert(isExtVectorElt());
276 return VectorElts;
277 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000278
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000279 // bitfield lvalue
Chandler Carruth72d2dab2012-12-06 11:14:44 +0000280 llvm::Value *getBitFieldAddr() const {
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000281 assert(isBitField());
282 return V;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000283 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000284 const CGBitFieldInfo &getBitFieldInfo() const {
285 assert(isBitField());
286 return *BitFieldInfo;
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000287 }
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000288
John McCalla07398e2011-06-16 04:16:24 +0000289 static LValue MakeAddr(llvm::Value *address, QualType type,
Eli Friedman6da2c712011-12-03 04:14:32 +0000290 CharUnits alignment, ASTContext &Context,
Dan Gohman3d5aff52010-10-14 23:06:10 +0000291 llvm::MDNode *TBAAInfo = 0) {
John McCalla07398e2011-06-16 04:16:24 +0000292 Qualifiers qs = type.getQualifiers();
293 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbarf1fbda32010-08-21 03:58:45 +0000294
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000295 LValue R;
296 R.LVType = Simple;
John McCalla07398e2011-06-16 04:16:24 +0000297 R.V = address;
298 R.Initialize(type, qs, alignment, TBAAInfo);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000299 return R;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000302 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
Eli Friedmane5a8aeb2012-03-22 22:36:39 +0000303 QualType type, CharUnits Alignment) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000304 LValue R;
305 R.LVType = VectorElt;
306 R.V = Vec;
307 R.VectorIdx = Idx;
Eli Friedmane5a8aeb2012-03-22 22:36:39 +0000308 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000309 return R;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000312 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
Eli Friedmane5a8aeb2012-03-22 22:36:39 +0000313 QualType type, CharUnits Alignment) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000314 LValue R;
315 R.LVType = ExtVectorElt;
316 R.V = Vec;
317 R.VectorElts = Elts;
Eli Friedmane5a8aeb2012-03-22 22:36:39 +0000318 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000319 return R;
320 }
321
Daniel Dunbar7f289642010-04-08 02:59:45 +0000322 /// \brief Create a new object to represent a bit-field access.
323 ///
NAKAMURA Takumi0481e542012-12-24 01:48:48 +0000324 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruth72d2dab2012-12-06 11:14:44 +0000325 /// bit-field refers to.
Daniel Dunbar7f289642010-04-08 02:59:45 +0000326 /// \param Info - The information describing how to perform the bit-field
327 /// access.
Chandler Carruth72d2dab2012-12-06 11:14:44 +0000328 static LValue MakeBitfield(llvm::Value *Addr,
John McCalla07398e2011-06-16 04:16:24 +0000329 const CGBitFieldInfo &Info,
Eli Friedmanf4bcfa12012-06-27 21:19:48 +0000330 QualType type, CharUnits Alignment) {
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000331 LValue R;
332 R.LVType = BitField;
Chandler Carruth72d2dab2012-12-06 11:14:44 +0000333 R.V = Addr;
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000334 R.BitFieldInfo = &Info;
Eli Friedmanf4bcfa12012-06-27 21:19:48 +0000335 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000336 return R;
337 }
Eli Friedman51f51202011-12-03 03:08:40 +0000338
339 RValue asAggregateRValue() const {
340 // FIMXE: Alignment
341 return RValue::getAggregate(getAddress(), isVolatileQualified());
342 }
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000343};
344
John McCall558d2ab2010-09-15 10:14:12 +0000345/// An aggregate value slot.
346class AggValueSlot {
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000347 /// The address.
John McCall6fa29162010-09-16 03:16:41 +0000348 llvm::Value *Addr;
John McCallf85e1932011-06-15 23:02:42 +0000349
350 // Qualifiers
351 Qualifiers Quals;
John McCall7c2349b2011-08-25 20:40:09 +0000352
Chad Rosier649b4a12012-03-29 17:37:10 +0000353 unsigned short Alignment;
Eli Friedmanf3940782011-12-03 00:54:26 +0000354
John McCallfd71fb82011-08-26 08:02:37 +0000355 /// DestructedFlag - This is set to true if some external code is
356 /// responsible for setting up a destructor for the slot. Otherwise
357 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier649b4a12012-03-29 17:37:10 +0000358 bool DestructedFlag : 1;
John McCallfd71fb82011-08-26 08:02:37 +0000359
360 /// ObjCGCFlag - This is set to true if writing to the memory in the
361 /// slot might require calling an appropriate Objective-C GC
362 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier649b4a12012-03-29 17:37:10 +0000363 bool ObjCGCFlag : 1;
Chris Lattner1b726772010-12-02 07:07:26 +0000364
John McCallfd71fb82011-08-26 08:02:37 +0000365 /// ZeroedFlag - This is set to true if the memory in the slot is
366 /// known to be zero before the assignment into it. This means that
367 /// zero fields don't need to be set.
Chad Rosier649b4a12012-03-29 17:37:10 +0000368 bool ZeroedFlag : 1;
John McCall558d2ab2010-09-15 10:14:12 +0000369
John McCallfd71fb82011-08-26 08:02:37 +0000370 /// AliasedFlag - This is set to true if the slot might be aliased
371 /// and it's not undefined behavior to access it through such an
372 /// alias. Note that it's always undefined behavior to access a C++
373 /// object that's under construction through an alias derived from
374 /// outside the construction process.
375 ///
376 /// This flag controls whether calls that produce the aggregate
377 /// value may be evaluated directly into the slot, or whether they
378 /// must be evaluated into an unaliased temporary and then memcpy'ed
379 /// over. Since it's invalid in general to memcpy a non-POD C++
380 /// object, it's important that this flag never be set when
381 /// evaluating an expression which constructs such an object.
Chad Rosier649b4a12012-03-29 17:37:10 +0000382 bool AliasedFlag : 1;
John McCall410ffb22011-08-25 23:04:34 +0000383
John McCall9eda3ab2013-03-07 21:37:17 +0000384 /// ValueOfAtomicFlag - This is set to true if the slot is the value
385 /// subobject of an object the size of an _Atomic(T). The specific
386 /// guarantees this makes are:
387 /// - the address is guaranteed to be a getelementptr into the
388 /// padding struct and
389 /// - it is okay to store something the width of an _Atomic(T)
390 /// into the address.
391 /// Tracking this allows us to avoid some obviously unnecessary
392 /// memcpys.
393 bool ValueOfAtomicFlag : 1;
394
John McCall558d2ab2010-09-15 10:14:12 +0000395public:
John McCall410ffb22011-08-25 23:04:34 +0000396 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall7c2349b2011-08-25 20:40:09 +0000397 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCall410ffb22011-08-25 23:04:34 +0000398 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall7c2349b2011-08-25 20:40:09 +0000399 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
John McCall9eda3ab2013-03-07 21:37:17 +0000400 enum IsValueOfAtomic_t { IsNotValueOfAtomic, IsValueOfAtomic };
John McCall7c2349b2011-08-25 20:40:09 +0000401
John McCall558d2ab2010-09-15 10:14:12 +0000402 /// ignored - Returns an aggregate value slot indicating that the
403 /// aggregate value is being ignored.
404 static AggValueSlot ignored() {
Benjamin Kramer9f32a922011-12-11 16:34:24 +0000405 return forAddr(0, CharUnits(), Qualifiers(), IsNotDestructed,
Chad Rosier649b4a12012-03-29 17:37:10 +0000406 DoesNotNeedGCBarriers, IsNotAliased);
John McCall558d2ab2010-09-15 10:14:12 +0000407 }
408
409 /// forAddr - Make a slot for an aggregate value.
410 ///
John McCall7c2349b2011-08-25 20:40:09 +0000411 /// \param quals - The qualifiers that dictate how the slot should
412 /// be initialied. Only 'volatile' and the Objective-C lifetime
413 /// qualifiers matter.
John McCallf85e1932011-06-15 23:02:42 +0000414 ///
John McCall7c2349b2011-08-25 20:40:09 +0000415 /// \param isDestructed - true if something else is responsible
416 /// for calling destructors on this object
417 /// \param needsGC - true if the slot is potentially located
John McCalld1a5f132010-09-16 03:13:23 +0000418 /// somewhere that ObjC GC calls should be emitted for
Eli Friedmand7722d92011-12-03 02:13:40 +0000419 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
Eli Friedmanf3940782011-12-03 00:54:26 +0000420 Qualifiers quals,
John McCall7c2349b2011-08-25 20:40:09 +0000421 IsDestructed_t isDestructed,
422 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000423 IsAliased_t isAliased,
John McCall9eda3ab2013-03-07 21:37:17 +0000424 IsZeroed_t isZeroed = IsNotZeroed,
425 IsValueOfAtomic_t isValueOfAtomic
426 = IsNotValueOfAtomic) {
John McCall558d2ab2010-09-15 10:14:12 +0000427 AggValueSlot AV;
John McCall7c2349b2011-08-25 20:40:09 +0000428 AV.Addr = addr;
Eli Friedmand7722d92011-12-03 02:13:40 +0000429 AV.Alignment = align.getQuantity();
John McCall7c2349b2011-08-25 20:40:09 +0000430 AV.Quals = quals;
John McCallfd71fb82011-08-26 08:02:37 +0000431 AV.DestructedFlag = isDestructed;
432 AV.ObjCGCFlag = needsGC;
John McCall7c2349b2011-08-25 20:40:09 +0000433 AV.ZeroedFlag = isZeroed;
John McCall410ffb22011-08-25 23:04:34 +0000434 AV.AliasedFlag = isAliased;
John McCall9eda3ab2013-03-07 21:37:17 +0000435 AV.ValueOfAtomicFlag = isValueOfAtomic;
John McCall558d2ab2010-09-15 10:14:12 +0000436 return AV;
437 }
438
John McCalle0c11682012-07-02 23:58:38 +0000439 static AggValueSlot forLValue(const LValue &LV,
440 IsDestructed_t isDestructed,
John McCall7c2349b2011-08-25 20:40:09 +0000441 NeedsGCBarriers_t needsGC,
John McCall44184392011-08-26 07:31:35 +0000442 IsAliased_t isAliased,
John McCall9eda3ab2013-03-07 21:37:17 +0000443 IsZeroed_t isZeroed = IsNotZeroed,
444 IsValueOfAtomic_t isValueOfAtomic
445 = IsNotValueOfAtomic) {
Eli Friedman6da2c712011-12-03 04:14:32 +0000446 return forAddr(LV.getAddress(), LV.getAlignment(),
John McCall9eda3ab2013-03-07 21:37:17 +0000447 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed,
448 isValueOfAtomic);
John McCall558d2ab2010-09-15 10:14:12 +0000449 }
Fariborz Jahanian8a970052010-10-22 22:05:03 +0000450
John McCallfd71fb82011-08-26 08:02:37 +0000451 IsDestructed_t isExternallyDestructed() const {
452 return IsDestructed_t(DestructedFlag);
John McCall558d2ab2010-09-15 10:14:12 +0000453 }
John McCallfd71fb82011-08-26 08:02:37 +0000454 void setExternallyDestructed(bool destructed = true) {
455 DestructedFlag = destructed;
John McCall558d2ab2010-09-15 10:14:12 +0000456 }
457
John McCallf85e1932011-06-15 23:02:42 +0000458 Qualifiers getQualifiers() const { return Quals; }
459
John McCall558d2ab2010-09-15 10:14:12 +0000460 bool isVolatile() const {
John McCallf85e1932011-06-15 23:02:42 +0000461 return Quals.hasVolatile();
462 }
463
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000464 void setVolatile(bool flag) {
465 Quals.setVolatile(flag);
466 }
467
John McCallf85e1932011-06-15 23:02:42 +0000468 Qualifiers::ObjCLifetime getObjCLifetime() const {
469 return Quals.getObjCLifetime();
John McCall558d2ab2010-09-15 10:14:12 +0000470 }
471
John McCall7c2349b2011-08-25 20:40:09 +0000472 NeedsGCBarriers_t requiresGCollection() const {
John McCallfd71fb82011-08-26 08:02:37 +0000473 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000474 }
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000475
John McCall558d2ab2010-09-15 10:14:12 +0000476 llvm::Value *getAddr() const {
John McCall6fa29162010-09-16 03:16:41 +0000477 return Addr;
John McCall558d2ab2010-09-15 10:14:12 +0000478 }
479
John McCall9eda3ab2013-03-07 21:37:17 +0000480 IsValueOfAtomic_t isValueOfAtomic() const {
481 return IsValueOfAtomic_t(ValueOfAtomicFlag);
482 }
483
484 llvm::Value *getPaddedAtomicAddr() const;
485
John McCall558d2ab2010-09-15 10:14:12 +0000486 bool isIgnored() const {
John McCall6fa29162010-09-16 03:16:41 +0000487 return Addr == 0;
John McCall558d2ab2010-09-15 10:14:12 +0000488 }
489
Eli Friedmand7722d92011-12-03 02:13:40 +0000490 CharUnits getAlignment() const {
491 return CharUnits::fromQuantity(Alignment);
Eli Friedmanf3940782011-12-03 00:54:26 +0000492 }
493
John McCall410ffb22011-08-25 23:04:34 +0000494 IsAliased_t isPotentiallyAliased() const {
495 return IsAliased_t(AliasedFlag);
496 }
497
Eli Friedmanf3940782011-12-03 00:54:26 +0000498 // FIXME: Alignment?
John McCall558d2ab2010-09-15 10:14:12 +0000499 RValue asRValue() const {
500 return RValue::getAggregate(getAddr(), isVolatile());
501 }
John McCall4b9c2d22011-11-06 09:01:30 +0000502
John McCall7c2349b2011-08-25 20:40:09 +0000503 void setZeroed(bool V = true) { ZeroedFlag = V; }
504 IsZeroed_t isZeroed() const {
505 return IsZeroed_t(ZeroedFlag);
Chris Lattner1b726772010-12-02 07:07:26 +0000506 }
John McCall558d2ab2010-09-15 10:14:12 +0000507};
508
Daniel Dunbar2eecaab2008-08-23 03:10:25 +0000509} // end namespace CodeGen
510} // end namespace clang
511
512#endif