blob: 92055917dba943a1d692f580cab77161dcdaeb39 [file] [log] [blame]
Daniel Dunbard4f616b2008-08-23 03:10:25 +00001//===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes implement wrappers around llvm::Value in order to
11// fully represent the range of values for C L- and R- values.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
16#define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
Daniel Dunbard4f616b2008-08-23 03:10:25 +000017
Daniel Dunbar93b00a92010-08-21 02:53:44 +000018#include "clang/AST/ASTContext.h"
Eli Friedman38cd36d2011-12-03 02:13:40 +000019#include "clang/AST/CharUnits.h"
Daniel Dunbard4f616b2008-08-23 03:10:25 +000020#include "clang/AST/Type.h"
John McCall47fb9502013-03-07 21:37:08 +000021#include "llvm/IR/Value.h"
David Blaikiee3b172a2015-04-02 18:55:21 +000022#include "llvm/IR/Type.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
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000041 // Stores first value and flavor.
42 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
43 // Stores second value and volatility.
44 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
45
Daniel Dunbard4f616b2008-08-23 03:10:25 +000046public:
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000047 bool isScalar() const { return V1.getInt() == Scalar; }
48 bool isComplex() const { return V1.getInt() == Complex; }
49 bool isAggregate() const { return V1.getInt() == Aggregate; }
Mike Stump11289f42009-09-09 15:08:12 +000050
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000051 bool isVolatileQualified() const { return V2.getInt(); }
Mike Stump93700fc2009-05-23 20:21:36 +000052
Mike Stump462a4aa2009-11-03 16:11:57 +000053 /// getScalarVal() - Return the Value* of this scalar value.
Daniel Dunbard4f616b2008-08-23 03:10:25 +000054 llvm::Value *getScalarVal() const {
55 assert(isScalar() && "Not a scalar!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000056 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000057 }
58
59 /// getComplexVal - Return the real/imag components of this complex value.
60 ///
61 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000062 return std::make_pair(V1.getPointer(), V2.getPointer());
Daniel Dunbard4f616b2008-08-23 03:10:25 +000063 }
Mike Stump11289f42009-09-09 15:08:12 +000064
Daniel Dunbard4f616b2008-08-23 03:10:25 +000065 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
66 llvm::Value *getAggregateAddr() const {
67 assert(isAggregate() && "Not an aggregate!");
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000068 return V1.getPointer();
Daniel Dunbard4f616b2008-08-23 03:10:25 +000069 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Daniel Dunbard4f616b2008-08-23 03:10:25 +000071 static RValue get(llvm::Value *V) {
72 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000073 ER.V1.setPointer(V);
74 ER.V1.setInt(Scalar);
75 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000076 return ER;
77 }
78 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
79 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000080 ER.V1.setPointer(V1);
81 ER.V2.setPointer(V2);
82 ER.V1.setInt(Complex);
83 ER.V2.setInt(false);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000084 return ER;
85 }
86 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000087 return getComplex(C.first, C.second);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000088 }
Mike Stump93700fc2009-05-23 20:21:36 +000089 // FIXME: Aggregate rvalues need to retain information about whether they are
90 // volatile or not. Remove default to find all places that probably get this
91 // wrong.
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000092 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +000093 RValue ER;
Benjamin Kramer2718b4d2010-05-02 14:59:10 +000094 ER.V1.setPointer(V);
95 ER.V1.setInt(Aggregate);
96 ER.V2.setInt(Volatile);
Daniel Dunbard4f616b2008-08-23 03:10:25 +000097 return ER;
98 }
99};
100
John McCallcdda29c2013-03-13 03:10:54 +0000101/// Does an ARC strong l-value have precise lifetime?
102enum ARCPreciseLifetime_t {
John McCall4d31b812013-03-13 05:02:21 +0000103 ARCImpreciseLifetime, ARCPreciseLifetime
John McCallcdda29c2013-03-13 03:10:54 +0000104};
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000105
106/// LValue - This represents an lvalue references. Because C/C++ allow
107/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
108/// bitrange.
109class LValue {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000110 enum {
111 Simple, // This is a normal l-value, use getAddress().
112 VectorElt, // This is a vector element l-value (V[i]), use getVector*
113 BitField, // This is a bitfield l-value, use getBitfield*.
Renato Golin230c5eb2014-05-19 18:15:42 +0000114 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
115 GlobalReg // This is a register l-value, use getGlobalReg()
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000116 } LVType;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000117
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000118 llvm::Value *V;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000120 union {
121 // Index into a vector subscript: V[i]
122 llvm::Value *VectorIdx;
123
124 // ExtVector element subset: V.xyx
125 llvm::Constant *VectorElts;
Mike Stump11289f42009-09-09 15:08:12 +0000126
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000127 // BitField start bit and size
Daniel Dunbardc406b82010-04-05 21:36:35 +0000128 const CGBitFieldInfo *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000129 };
130
John McCall1553b192011-06-16 04:16:24 +0000131 QualType Type;
132
John McCall8ccfcb52009-09-24 19:53:00 +0000133 // 'const' is unused here
134 Qualifiers Quals;
Fariborz Jahaniand7db9642008-11-19 00:59:10 +0000135
Eli Friedman610bb872012-03-22 22:36:39 +0000136 // The alignment to use when accessing this lvalue. (For vector elements,
137 // this is the alignment of the whole vector.)
John Criswelledc84502012-08-15 18:40:30 +0000138 int64_t Alignment;
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000139
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000140 // objective-c's ivar
141 bool Ivar:1;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000142
143 // objective-c's ivar is an array
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000144 bool ObjIsArray:1;
Mike Stump11289f42009-09-09 15:08:12 +0000145
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000146 // LValue is non-gc'able for any reason, including being a parameter or local
147 // variable.
148 bool NonGC: 1;
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000149
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000150 // Lvalue is a global reference of an objective-c object
151 bool GlobalObjCRef : 1;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000152
153 // Lvalue is a thread local reference
154 bool ThreadLocalRef : 1;
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000155
John McCallcdda29c2013-03-13 03:10:54 +0000156 // Lvalue has ARC imprecise lifetime. We store this inverted to try
157 // to make the default bitfield pattern all-zeroes.
158 bool ImpreciseLifetime : 1;
159
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000160 Expr *BaseIvarExp;
Dan Gohman947c9af2010-10-14 23:06:10 +0000161
Manman Renc451e572013-04-04 21:53:22 +0000162 /// Used by struct-path-aware TBAA.
163 QualType TBAABaseType;
164 /// Offset relative to the base type.
165 uint64_t TBAAOffset;
166
Dan Gohman947c9af2010-10-14 23:06:10 +0000167 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
168 llvm::MDNode *TBAAInfo;
169
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000170private:
Eli Friedmana0544d62011-12-03 04:14:32 +0000171 void Initialize(QualType Type, Qualifiers Quals,
Eli Friedmanc24e2fb2012-06-27 21:19:48 +0000172 CharUnits Alignment,
Craig Topper8a13c412014-05-21 05:09:00 +0000173 llvm::MDNode *TBAAInfo = nullptr) {
John McCall1553b192011-06-16 04:16:24 +0000174 this->Type = Type;
John McCall8ccfcb52009-09-24 19:53:00 +0000175 this->Quals = Quals;
Eli Friedmana0544d62011-12-03 04:14:32 +0000176 this->Alignment = Alignment.getQuantity();
177 assert(this->Alignment == Alignment.getQuantity() &&
178 "Alignment exceeds allowed max!");
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000179
180 // Initialize Objective-C flags.
John McCall8ccfcb52009-09-24 19:53:00 +0000181 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
John McCallcdda29c2013-03-13 03:10:54 +0000182 this->ImpreciseLifetime = false;
Fariborz Jahanian217af242010-07-20 20:30:03 +0000183 this->ThreadLocalRef = false;
Craig Topper8a13c412014-05-21 05:09:00 +0000184 this->BaseIvarExp = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000185
186 // Initialize fields for TBAA.
187 this->TBAABaseType = Type;
188 this->TBAAOffset = 0;
Dan Gohman947c9af2010-10-14 23:06:10 +0000189 this->TBAAInfo = TBAAInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000192public:
193 bool isSimple() const { return LVType == Simple; }
194 bool isVectorElt() const { return LVType == VectorElt; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000195 bool isBitField() const { return LVType == BitField; }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000196 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Renato Golin230c5eb2014-05-19 18:15:42 +0000197 bool isGlobalReg() const { return LVType == GlobalReg; }
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000198
John McCall8ccfcb52009-09-24 19:53:00 +0000199 bool isVolatileQualified() const { return Quals.hasVolatile(); }
200 bool isRestrictQualified() const { return Quals.hasRestrict(); }
201 unsigned getVRQualifiers() const {
202 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
Chris Lattnere084c012009-02-16 22:25:49 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
John McCall1553b192011-06-16 04:16:24 +0000205 QualType getType() const { return Type; }
206
207 Qualifiers::ObjCLifetime getObjCLifetime() const {
208 return Quals.getObjCLifetime();
209 }
210
Fariborz Jahanian75686a52008-11-20 20:53:20 +0000211 bool isObjCIvar() const { return Ivar; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000212 void setObjCIvar(bool Value) { Ivar = Value; }
213
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000214 bool isObjCArray() const { return ObjIsArray; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000215 void setObjCArray(bool Value) { ObjIsArray = Value; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000216
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000217 bool isNonGC () const { return NonGC; }
Daniel Dunbare50dda92010-08-21 03:22:38 +0000218 void setNonGC(bool Value) { NonGC = Value; }
219
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000220 bool isGlobalObjCRef() const { return GlobalObjCRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000221 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
222
Fariborz Jahanian217af242010-07-20 20:30:03 +0000223 bool isThreadLocalRef() const { return ThreadLocalRef; }
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000224 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
225
John McCallcdda29c2013-03-13 03:10:54 +0000226 ARCPreciseLifetime_t isARCPreciseLifetime() const {
227 return ARCPreciseLifetime_t(!ImpreciseLifetime);
228 }
229 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
230 ImpreciseLifetime = (value == ARCImpreciseLifetime);
231 }
232
Daniel Dunbar4bb04ce2010-08-21 03:51:29 +0000233 bool isObjCWeak() const {
234 return Quals.getObjCGCAttr() == Qualifiers::Weak;
235 }
236 bool isObjCStrong() const {
237 return Quals.getObjCGCAttr() == Qualifiers::Strong;
238 }
John McCall1553b192011-06-16 04:16:24 +0000239
240 bool isVolatile() const {
241 return Quals.hasVolatile();
242 }
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000243
244 Expr *getBaseIvarExp() const { return BaseIvarExp; }
245 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
Mon P Wangacedf772009-07-22 03:08:17 +0000246
Manman Renc451e572013-04-04 21:53:22 +0000247 QualType getTBAABaseType() const { return TBAABaseType; }
248 void setTBAABaseType(QualType T) { TBAABaseType = T; }
249
250 uint64_t getTBAAOffset() const { return TBAAOffset; }
251 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
252
Dan Gohman947c9af2010-10-14 23:06:10 +0000253 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
254 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
255
Daniel Dunbarb657ac52010-08-21 03:29:54 +0000256 const Qualifiers &getQuals() const { return Quals; }
257 Qualifiers &getQuals() { return Quals; }
258
John McCall8ccfcb52009-09-24 19:53:00 +0000259 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
Mon P Wangacedf772009-07-22 03:08:17 +0000260
Eli Friedmana0544d62011-12-03 04:14:32 +0000261 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
262 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
Daniel Dunbare3b8dd42010-08-21 02:39:23 +0000263
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000264 // simple lvalue
265 llvm::Value *getAddress() const { assert(isSimple()); return V; }
John McCall1553b192011-06-16 04:16:24 +0000266 void setAddress(llvm::Value *address) {
267 assert(isSimple());
268 V = address;
269 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000270
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000271 // vector elt lvalue
272 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
273 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000274
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000275 // extended vector elements.
276 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
277 llvm::Constant *getExtVectorElts() const {
278 assert(isExtVectorElt());
279 return VectorElts;
280 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000281
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000282 // bitfield lvalue
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000283 llvm::Value *getBitFieldAddr() const {
Daniel Dunbardc406b82010-04-05 21:36:35 +0000284 assert(isBitField());
285 return V;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000286 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000287 const CGBitFieldInfo &getBitFieldInfo() const {
288 assert(isBitField());
289 return *BitFieldInfo;
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000290 }
Daniel Dunbardc406b82010-04-05 21:36:35 +0000291
Renato Golin230c5eb2014-05-19 18:15:42 +0000292 // global register lvalue
293 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
294
John McCall1553b192011-06-16 04:16:24 +0000295 static LValue MakeAddr(llvm::Value *address, QualType type,
Eli Friedmana0544d62011-12-03 04:14:32 +0000296 CharUnits alignment, ASTContext &Context,
Craig Topper8a13c412014-05-21 05:09:00 +0000297 llvm::MDNode *TBAAInfo = nullptr) {
John McCall1553b192011-06-16 04:16:24 +0000298 Qualifiers qs = type.getQualifiers();
299 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
Daniel Dunbar226bdda2010-08-21 03:58:45 +0000300
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000301 LValue R;
302 R.LVType = Simple;
David Blaikiee3b172a2015-04-02 18:55:21 +0000303 assert(address->getType()->isPointerTy());
John McCall1553b192011-06-16 04:16:24 +0000304 R.V = address;
305 R.Initialize(type, qs, alignment, TBAAInfo);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000306 return R;
307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000309 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
Eli Friedman610bb872012-03-22 22:36:39 +0000310 QualType type, CharUnits Alignment) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000311 LValue R;
312 R.LVType = VectorElt;
313 R.V = Vec;
314 R.VectorIdx = Idx;
Eli Friedman610bb872012-03-22 22:36:39 +0000315 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000316 return R;
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000319 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
Eli Friedman610bb872012-03-22 22:36:39 +0000320 QualType type, CharUnits Alignment) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000321 LValue R;
322 R.LVType = ExtVectorElt;
323 R.V = Vec;
324 R.VectorElts = Elts;
Eli Friedman610bb872012-03-22 22:36:39 +0000325 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000326 return R;
327 }
328
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000329 /// \brief Create a new object to represent a bit-field access.
330 ///
NAKAMURA Takumi368d2ee2012-12-24 01:48:48 +0000331 /// \param Addr - The base address of the bit-field sequence this
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000332 /// bit-field refers to.
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000333 /// \param Info - The information describing how to perform the bit-field
334 /// access.
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000335 static LValue MakeBitfield(llvm::Value *Addr,
John McCall1553b192011-06-16 04:16:24 +0000336 const CGBitFieldInfo &Info,
Eli Friedmanc24e2fb2012-06-27 21:19:48 +0000337 QualType type, CharUnits Alignment) {
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000338 LValue R;
339 R.LVType = BitField;
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000340 R.V = Addr;
Daniel Dunbardc406b82010-04-05 21:36:35 +0000341 R.BitFieldInfo = &Info;
Eli Friedmanc24e2fb2012-06-27 21:19:48 +0000342 R.Initialize(type, type.getQualifiers(), Alignment);
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000343 return R;
344 }
Eli Friedman2869b5a2011-12-03 03:08:40 +0000345
Renato Golin230c5eb2014-05-19 18:15:42 +0000346 static LValue MakeGlobalReg(llvm::Value *Reg,
347 QualType type,
348 CharUnits Alignment) {
349 LValue R;
350 R.LVType = GlobalReg;
351 R.V = Reg;
352 R.Initialize(type, type.getQualifiers(), Alignment);
353 return R;
354 }
355
Eli Friedman2869b5a2011-12-03 03:08:40 +0000356 RValue asAggregateRValue() const {
357 // FIMXE: Alignment
358 return RValue::getAggregate(getAddress(), isVolatileQualified());
359 }
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000360};
361
John McCall7a626f62010-09-15 10:14:12 +0000362/// An aggregate value slot.
363class AggValueSlot {
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000364 /// The address.
John McCall2a8b9a32010-09-16 03:16:41 +0000365 llvm::Value *Addr;
John McCall31168b02011-06-15 23:02:42 +0000366
367 // Qualifiers
368 Qualifiers Quals;
John McCall8d6fc952011-08-25 20:40:09 +0000369
Chad Rosier615ed1a2012-03-29 17:37:10 +0000370 unsigned short Alignment;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000371
John McCallcac93852011-08-26 08:02:37 +0000372 /// DestructedFlag - This is set to true if some external code is
373 /// responsible for setting up a destructor for the slot. Otherwise
374 /// the code which constructs it should push the appropriate cleanup.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000375 bool DestructedFlag : 1;
John McCallcac93852011-08-26 08:02:37 +0000376
377 /// ObjCGCFlag - This is set to true if writing to the memory in the
378 /// slot might require calling an appropriate Objective-C GC
379 /// barrier. The exact interaction here is unnecessarily mysterious.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000380 bool ObjCGCFlag : 1;
Chris Lattner27a36312010-12-02 07:07:26 +0000381
John McCallcac93852011-08-26 08:02:37 +0000382 /// ZeroedFlag - This is set to true if the memory in the slot is
383 /// known to be zero before the assignment into it. This means that
384 /// zero fields don't need to be set.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000385 bool ZeroedFlag : 1;
John McCall7a626f62010-09-15 10:14:12 +0000386
John McCallcac93852011-08-26 08:02:37 +0000387 /// AliasedFlag - This is set to true if the slot might be aliased
388 /// and it's not undefined behavior to access it through such an
389 /// alias. Note that it's always undefined behavior to access a C++
390 /// object that's under construction through an alias derived from
391 /// outside the construction process.
392 ///
393 /// This flag controls whether calls that produce the aggregate
394 /// value may be evaluated directly into the slot, or whether they
395 /// must be evaluated into an unaliased temporary and then memcpy'ed
396 /// over. Since it's invalid in general to memcpy a non-POD C++
397 /// object, it's important that this flag never be set when
398 /// evaluating an expression which constructs such an object.
Chad Rosier615ed1a2012-03-29 17:37:10 +0000399 bool AliasedFlag : 1;
John McCalla5efa732011-08-25 23:04:34 +0000400
John McCall7a626f62010-09-15 10:14:12 +0000401public:
John McCalla5efa732011-08-25 23:04:34 +0000402 enum IsAliased_t { IsNotAliased, IsAliased };
John McCall8d6fc952011-08-25 20:40:09 +0000403 enum IsDestructed_t { IsNotDestructed, IsDestructed };
John McCalla5efa732011-08-25 23:04:34 +0000404 enum IsZeroed_t { IsNotZeroed, IsZeroed };
John McCall8d6fc952011-08-25 20:40:09 +0000405 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
406
John McCall7a626f62010-09-15 10:14:12 +0000407 /// ignored - Returns an aggregate value slot indicating that the
408 /// aggregate value is being ignored.
409 static AggValueSlot ignored() {
Craig Topper8a13c412014-05-21 05:09:00 +0000410 return forAddr(nullptr, CharUnits(), Qualifiers(), IsNotDestructed,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000411 DoesNotNeedGCBarriers, IsNotAliased);
John McCall7a626f62010-09-15 10:14:12 +0000412 }
413
414 /// forAddr - Make a slot for an aggregate value.
415 ///
John McCall8d6fc952011-08-25 20:40:09 +0000416 /// \param quals - The qualifiers that dictate how the slot should
417 /// be initialied. Only 'volatile' and the Objective-C lifetime
418 /// qualifiers matter.
John McCall31168b02011-06-15 23:02:42 +0000419 ///
John McCall8d6fc952011-08-25 20:40:09 +0000420 /// \param isDestructed - true if something else is responsible
421 /// for calling destructors on this object
422 /// \param needsGC - true if the slot is potentially located
John McCall58649dc2010-09-16 03:13:23 +0000423 /// somewhere that ObjC GC calls should be emitted for
Eli Friedman38cd36d2011-12-03 02:13:40 +0000424 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000425 Qualifiers quals,
John McCall8d6fc952011-08-25 20:40:09 +0000426 IsDestructed_t isDestructed,
427 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000428 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000429 IsZeroed_t isZeroed = IsNotZeroed) {
John McCall7a626f62010-09-15 10:14:12 +0000430 AggValueSlot AV;
John McCall8d6fc952011-08-25 20:40:09 +0000431 AV.Addr = addr;
Eli Friedman38cd36d2011-12-03 02:13:40 +0000432 AV.Alignment = align.getQuantity();
John McCall8d6fc952011-08-25 20:40:09 +0000433 AV.Quals = quals;
John McCallcac93852011-08-26 08:02:37 +0000434 AV.DestructedFlag = isDestructed;
435 AV.ObjCGCFlag = needsGC;
John McCall8d6fc952011-08-25 20:40:09 +0000436 AV.ZeroedFlag = isZeroed;
John McCalla5efa732011-08-25 23:04:34 +0000437 AV.AliasedFlag = isAliased;
John McCall7a626f62010-09-15 10:14:12 +0000438 return AV;
439 }
440
John McCall4e8ca4f2012-07-02 23:58:38 +0000441 static AggValueSlot forLValue(const LValue &LV,
442 IsDestructed_t isDestructed,
John McCall8d6fc952011-08-25 20:40:09 +0000443 NeedsGCBarriers_t needsGC,
John McCall46759f42011-08-26 07:31:35 +0000444 IsAliased_t isAliased,
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000445 IsZeroed_t isZeroed = IsNotZeroed) {
Eli Friedmana0544d62011-12-03 04:14:32 +0000446 return forAddr(LV.getAddress(), LV.getAlignment(),
Eli Friedmanbe4504d2013-07-11 01:32:21 +0000447 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
John McCall7a626f62010-09-15 10:14:12 +0000448 }
Fariborz Jahanianc1236232010-10-22 22:05:03 +0000449
John McCallcac93852011-08-26 08:02:37 +0000450 IsDestructed_t isExternallyDestructed() const {
451 return IsDestructed_t(DestructedFlag);
John McCall7a626f62010-09-15 10:14:12 +0000452 }
John McCallcac93852011-08-26 08:02:37 +0000453 void setExternallyDestructed(bool destructed = true) {
454 DestructedFlag = destructed;
John McCall7a626f62010-09-15 10:14:12 +0000455 }
456
John McCall31168b02011-06-15 23:02:42 +0000457 Qualifiers getQualifiers() const { return Quals; }
458
John McCall7a626f62010-09-15 10:14:12 +0000459 bool isVolatile() const {
John McCall31168b02011-06-15 23:02:42 +0000460 return Quals.hasVolatile();
461 }
462
Fariborz Jahanian78652202013-01-25 23:57:05 +0000463 void setVolatile(bool flag) {
464 Quals.setVolatile(flag);
465 }
466
John McCall31168b02011-06-15 23:02:42 +0000467 Qualifiers::ObjCLifetime getObjCLifetime() const {
468 return Quals.getObjCLifetime();
John McCall7a626f62010-09-15 10:14:12 +0000469 }
470
John McCall8d6fc952011-08-25 20:40:09 +0000471 NeedsGCBarriers_t requiresGCollection() const {
John McCallcac93852011-08-26 08:02:37 +0000472 return NeedsGCBarriers_t(ObjCGCFlag);
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000473 }
Fariborz Jahanianb60e70f2010-09-16 00:20:07 +0000474
John McCall7a626f62010-09-15 10:14:12 +0000475 llvm::Value *getAddr() const {
John McCall2a8b9a32010-09-16 03:16:41 +0000476 return Addr;
John McCall7a626f62010-09-15 10:14:12 +0000477 }
478
479 bool isIgnored() const {
Craig Topper8a13c412014-05-21 05:09:00 +0000480 return Addr == nullptr;
John McCall7a626f62010-09-15 10:14:12 +0000481 }
482
Eli Friedman38cd36d2011-12-03 02:13:40 +0000483 CharUnits getAlignment() const {
484 return CharUnits::fromQuantity(Alignment);
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000485 }
486
John McCalla5efa732011-08-25 23:04:34 +0000487 IsAliased_t isPotentiallyAliased() const {
488 return IsAliased_t(AliasedFlag);
489 }
490
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000491 // FIXME: Alignment?
John McCall7a626f62010-09-15 10:14:12 +0000492 RValue asRValue() const {
493 return RValue::getAggregate(getAddr(), isVolatile());
494 }
John McCallfe96e0b2011-11-06 09:01:30 +0000495
John McCall8d6fc952011-08-25 20:40:09 +0000496 void setZeroed(bool V = true) { ZeroedFlag = V; }
497 IsZeroed_t isZeroed() const {
498 return IsZeroed_t(ZeroedFlag);
Chris Lattner27a36312010-12-02 07:07:26 +0000499 }
John McCall7a626f62010-09-15 10:14:12 +0000500};
501
Daniel Dunbard4f616b2008-08-23 03:10:25 +0000502} // end namespace CodeGen
503} // end namespace clang
504
505#endif