1 //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes implement wrappers around llvm::Value in order to
11 // fully represent the range of values for C L- and R- values.
13 //===----------------------------------------------------------------------===//
15 #ifndef CLANG_CODEGEN_CGVALUE_H
16 #define CLANG_CODEGEN_CGVALUE_H
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Type.h"
27 class ObjCPropertyRefExpr;
32 /// RValue - This trivial value class is used to represent the result of an
33 /// expression that is evaluated. It can be one of three things: either a
34 /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
35 /// address of an aggregate value in memory.
37 enum Flavor { Scalar, Complex, Aggregate };
39 // Stores first value and flavor.
40 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
41 // Stores second value and volatility.
42 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
45 bool isScalar() const { return V1.getInt() == Scalar; }
46 bool isComplex() const { return V1.getInt() == Complex; }
47 bool isAggregate() const { return V1.getInt() == Aggregate; }
49 bool isVolatileQualified() const { return V2.getInt(); }
51 /// getScalarVal() - Return the Value* of this scalar value.
52 llvm::Value *getScalarVal() const {
53 assert(isScalar() && "Not a scalar!");
54 return V1.getPointer();
57 /// getComplexVal - Return the real/imag components of this complex value.
59 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
60 return std::make_pair(V1.getPointer(), V2.getPointer());
63 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
64 llvm::Value *getAggregateAddr() const {
65 assert(isAggregate() && "Not an aggregate!");
66 return V1.getPointer();
69 static RValue get(llvm::Value *V) {
76 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
80 ER.V1.setInt(Complex);
84 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
85 return getComplex(C.first, C.second);
87 // FIXME: Aggregate rvalues need to retain information about whether they are
88 // volatile or not. Remove default to find all places that probably get this
90 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
93 ER.V1.setInt(Aggregate);
94 ER.V2.setInt(Volatile);
100 /// LValue - This represents an lvalue references. Because C/C++ allow
101 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
105 Simple, // This is a normal l-value, use getAddress().
106 VectorElt, // This is a vector element l-value (V[i]), use getVector*
107 BitField, // This is a bitfield l-value, use getBitfield*.
108 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
109 PropertyRef // This is an Objective-C property reference, use
110 // getPropertyRefExpr
116 // Index into a vector subscript: V[i]
117 llvm::Value *VectorIdx;
119 // ExtVector element subset: V.xyx
120 llvm::Constant *VectorElts;
122 // BitField start bit and size
123 const CGBitFieldInfo *BitFieldInfo;
125 // Obj-C property reference expression
126 const ObjCPropertyRefExpr *PropertyRefExpr;
131 // 'const' is unused here
134 /// The alignment to use when accessing this lvalue.
135 unsigned short Alignment;
137 // objective-c's ivar
140 // objective-c's ivar is an array
143 // LValue is non-gc'able for any reason, including being a parameter or local
147 // Lvalue is a global reference of an objective-c object
148 bool GlobalObjCRef : 1;
150 // Lvalue is a thread local reference
151 bool ThreadLocalRef : 1;
155 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
156 llvm::MDNode *TBAAInfo;
159 void Initialize(QualType Type, Qualifiers Quals, unsigned Alignment = 0,
160 llvm::MDNode *TBAAInfo = 0) {
163 this->Alignment = Alignment;
164 assert(this->Alignment == Alignment && "Alignment exceeds allowed max!");
166 // Initialize Objective-C flags.
167 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
168 this->ThreadLocalRef = false;
169 this->BaseIvarExp = 0;
170 this->TBAAInfo = TBAAInfo;
174 bool isSimple() const { return LVType == Simple; }
175 bool isVectorElt() const { return LVType == VectorElt; }
176 bool isBitField() const { return LVType == BitField; }
177 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
178 bool isPropertyRef() const { return LVType == PropertyRef; }
180 bool isVolatileQualified() const { return Quals.hasVolatile(); }
181 bool isRestrictQualified() const { return Quals.hasRestrict(); }
182 unsigned getVRQualifiers() const {
183 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
186 QualType getType() const { return Type; }
188 Qualifiers::ObjCLifetime getObjCLifetime() const {
189 return Quals.getObjCLifetime();
192 bool isObjCIvar() const { return Ivar; }
193 void setObjCIvar(bool Value) { Ivar = Value; }
195 bool isObjCArray() const { return ObjIsArray; }
196 void setObjCArray(bool Value) { ObjIsArray = Value; }
198 bool isNonGC () const { return NonGC; }
199 void setNonGC(bool Value) { NonGC = Value; }
201 bool isGlobalObjCRef() const { return GlobalObjCRef; }
202 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
204 bool isThreadLocalRef() const { return ThreadLocalRef; }
205 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
207 bool isObjCWeak() const {
208 return Quals.getObjCGCAttr() == Qualifiers::Weak;
210 bool isObjCStrong() const {
211 return Quals.getObjCGCAttr() == Qualifiers::Strong;
214 bool isVolatile() const {
215 return Quals.hasVolatile();
218 Expr *getBaseIvarExp() const { return BaseIvarExp; }
219 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
221 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
222 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
224 const Qualifiers &getQuals() const { return Quals; }
225 Qualifiers &getQuals() { return Quals; }
227 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
229 unsigned getAlignment() const { return Alignment; }
232 llvm::Value *getAddress() const { assert(isSimple()); return V; }
233 void setAddress(llvm::Value *address) {
239 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
240 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
242 // extended vector elements.
243 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
244 llvm::Constant *getExtVectorElts() const {
245 assert(isExtVectorElt());
250 llvm::Value *getBitFieldBaseAddr() const {
251 assert(isBitField());
254 const CGBitFieldInfo &getBitFieldInfo() const {
255 assert(isBitField());
256 return *BitFieldInfo;
259 // property ref lvalue
260 llvm::Value *getPropertyRefBaseAddr() const {
261 assert(isPropertyRef());
264 const ObjCPropertyRefExpr *getPropertyRefExpr() const {
265 assert(isPropertyRef());
266 return PropertyRefExpr;
269 static LValue MakeAddr(llvm::Value *address, QualType type,
270 unsigned alignment, ASTContext &Context,
271 llvm::MDNode *TBAAInfo = 0) {
272 Qualifiers qs = type.getQualifiers();
273 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
278 R.Initialize(type, qs, alignment, TBAAInfo);
282 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
285 R.LVType = VectorElt;
288 R.Initialize(type, type.getQualifiers());
292 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
295 R.LVType = ExtVectorElt;
298 R.Initialize(type, type.getQualifiers());
302 /// \brief Create a new object to represent a bit-field access.
304 /// \param BaseValue - The base address of the structure containing the
306 /// \param Info - The information describing how to perform the bit-field
308 static LValue MakeBitfield(llvm::Value *BaseValue,
309 const CGBitFieldInfo &Info,
314 R.BitFieldInfo = &Info;
315 R.Initialize(type, type.getQualifiers());
319 // FIXME: It is probably bad that we aren't emitting the target when we build
320 // the lvalue. However, this complicates the code a bit, and I haven't figured
321 // out how to make it go wrong yet.
322 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E,
325 R.LVType = PropertyRef;
327 R.PropertyRefExpr = E;
328 R.Initialize(QualType(), Qualifiers());
333 /// An aggregate value slot.
342 bool LifetimeFlag : 1;
343 bool RequiresGCollection : 1;
345 /// IsZeroed - This is set to true if the destination is known to be zero
346 /// before the assignment into it. This means that zero fields don't need to
351 /// ignored - Returns an aggregate value slot indicating that the
352 /// aggregate value is being ignored.
353 static AggValueSlot ignored() {
356 AV.Quals = Qualifiers();
357 AV.LifetimeFlag = AV.RequiresGCollection = AV.IsZeroed =0;
361 /// forAddr - Make a slot for an aggregate value.
363 /// \param Volatile - true if the slot should be volatile-initialized
365 /// \param Qualifiers - The qualifiers that dictate how the slot
366 /// should be initialied. Only 'volatile' and the Objective-C
367 /// lifetime qualifiers matter.
369 /// \param LifetimeExternallyManaged - true if the slot's lifetime
370 /// is being externally managed; false if a destructor should be
371 /// registered for any temporaries evaluated into the slot
372 /// \param RequiresGCollection - true if the slot is located
373 /// somewhere that ObjC GC calls should be emitted for
374 static AggValueSlot forAddr(llvm::Value *Addr, Qualifiers Quals,
375 bool LifetimeExternallyManaged,
376 bool RequiresGCollection = false,
377 bool IsZeroed = false) {
381 AV.LifetimeFlag = LifetimeExternallyManaged;
382 AV.RequiresGCollection = RequiresGCollection;
383 AV.IsZeroed = IsZeroed;
387 static AggValueSlot forLValue(LValue LV, bool LifetimeExternallyManaged,
388 bool RequiresGCollection = false,
389 bool IsZeroed = false) {
390 return forAddr(LV.getAddress(), LV.getQuals(),
391 LifetimeExternallyManaged, RequiresGCollection, IsZeroed);
394 bool isLifetimeExternallyManaged() const {
397 void setLifetimeExternallyManaged(bool Managed = true) {
398 LifetimeFlag = Managed;
401 Qualifiers getQualifiers() const { return Quals; }
403 bool isVolatile() const {
404 return Quals.hasVolatile();
407 Qualifiers::ObjCLifetime getObjCLifetime() const {
408 return Quals.getObjCLifetime();
411 bool requiresGCollection() const {
412 return RequiresGCollection;
415 llvm::Value *getAddr() const {
419 bool isIgnored() const {
423 RValue asRValue() const {
424 return RValue::getAggregate(getAddr(), isVolatile());
427 void setZeroed(bool V = true) { IsZeroed = V; }
428 bool isZeroed() const {
433 } // end namespace CodeGen
434 } // end namespace clang