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.
341 /// DestructedFlag - This is set to true if some external code is
342 /// responsible for setting up a destructor for the slot. Otherwise
343 /// the code which constructs it should push the appropriate cleanup.
344 bool DestructedFlag : 1;
346 /// ObjCGCFlag - This is set to true if writing to the memory in the
347 /// slot might require calling an appropriate Objective-C GC
348 /// barrier. The exact interaction here is unnecessarily mysterious.
351 /// ZeroedFlag - This is set to true if the memory in the slot is
352 /// known to be zero before the assignment into it. This means that
353 /// zero fields don't need to be set.
356 /// AliasedFlag - This is set to true if the slot might be aliased
357 /// and it's not undefined behavior to access it through such an
358 /// alias. Note that it's always undefined behavior to access a C++
359 /// object that's under construction through an alias derived from
360 /// outside the construction process.
362 /// This flag controls whether calls that produce the aggregate
363 /// value may be evaluated directly into the slot, or whether they
364 /// must be evaluated into an unaliased temporary and then memcpy'ed
365 /// over. Since it's invalid in general to memcpy a non-POD C++
366 /// object, it's important that this flag never be set when
367 /// evaluating an expression which constructs such an object.
368 bool AliasedFlag : 1;
371 enum IsAliased_t { IsNotAliased, IsAliased };
372 enum IsDestructed_t { IsNotDestructed, IsDestructed };
373 enum IsZeroed_t { IsNotZeroed, IsZeroed };
374 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
376 /// ignored - Returns an aggregate value slot indicating that the
377 /// aggregate value is being ignored.
378 static AggValueSlot ignored() {
381 AV.Quals = Qualifiers();
382 AV.DestructedFlag = AV.ObjCGCFlag = AV.ZeroedFlag = AV.AliasedFlag = false;
386 /// forAddr - Make a slot for an aggregate value.
388 /// \param quals - The qualifiers that dictate how the slot should
389 /// be initialied. Only 'volatile' and the Objective-C lifetime
390 /// qualifiers matter.
392 /// \param isDestructed - true if something else is responsible
393 /// for calling destructors on this object
394 /// \param needsGC - true if the slot is potentially located
395 /// somewhere that ObjC GC calls should be emitted for
396 static AggValueSlot forAddr(llvm::Value *addr, Qualifiers quals,
397 IsDestructed_t isDestructed,
398 NeedsGCBarriers_t needsGC,
399 IsAliased_t isAliased,
400 IsZeroed_t isZeroed = IsNotZeroed) {
404 AV.DestructedFlag = isDestructed;
405 AV.ObjCGCFlag = needsGC;
406 AV.ZeroedFlag = isZeroed;
407 AV.AliasedFlag = isAliased;
411 static AggValueSlot forLValue(LValue LV, IsDestructed_t isDestructed,
412 NeedsGCBarriers_t needsGC,
413 IsAliased_t isAliased,
414 IsZeroed_t isZeroed = IsNotZeroed) {
415 return forAddr(LV.getAddress(), LV.getQuals(),
416 isDestructed, needsGC, isAliased, isZeroed);
419 IsDestructed_t isExternallyDestructed() const {
420 return IsDestructed_t(DestructedFlag);
422 void setExternallyDestructed(bool destructed = true) {
423 DestructedFlag = destructed;
426 Qualifiers getQualifiers() const { return Quals; }
428 bool isVolatile() const {
429 return Quals.hasVolatile();
432 Qualifiers::ObjCLifetime getObjCLifetime() const {
433 return Quals.getObjCLifetime();
436 NeedsGCBarriers_t requiresGCollection() const {
437 return NeedsGCBarriers_t(ObjCGCFlag);
440 llvm::Value *getAddr() const {
444 bool isIgnored() const {
448 IsAliased_t isPotentiallyAliased() const {
449 return IsAliased_t(AliasedFlag);
452 RValue asRValue() const {
453 return RValue::getAggregate(getAddr(), isVolatile());
456 void setZeroed(bool V = true) { ZeroedFlag = V; }
457 IsZeroed_t isZeroed() const {
458 return IsZeroed_t(ZeroedFlag);
462 } // end namespace CodeGen
463 } // end namespace clang