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/CharUnits.h"
20 #include "clang/AST/Type.h"
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
114 // Index into a vector subscript: V[i]
115 llvm::Value *VectorIdx;
117 // ExtVector element subset: V.xyx
118 llvm::Constant *VectorElts;
120 // BitField start bit and size
121 const CGBitFieldInfo *BitFieldInfo;
126 // 'const' is unused here
129 // The alignment to use when accessing this lvalue. (For vector elements,
130 // this is the alignment of the whole vector.)
133 // objective-c's ivar
136 // objective-c's ivar is an array
139 // LValue is non-gc'able for any reason, including being a parameter or local
143 // Lvalue is a global reference of an objective-c object
144 bool GlobalObjCRef : 1;
146 // Lvalue is a thread local reference
147 bool ThreadLocalRef : 1;
151 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
152 llvm::MDNode *TBAAInfo;
155 void Initialize(QualType Type, Qualifiers Quals,
157 llvm::MDNode *TBAAInfo = 0) {
160 this->Alignment = Alignment.getQuantity();
161 assert(this->Alignment == Alignment.getQuantity() &&
162 "Alignment exceeds allowed max!");
164 // Initialize Objective-C flags.
165 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
166 this->ThreadLocalRef = false;
167 this->BaseIvarExp = 0;
168 this->TBAAInfo = TBAAInfo;
172 bool isSimple() const { return LVType == Simple; }
173 bool isVectorElt() const { return LVType == VectorElt; }
174 bool isBitField() const { return LVType == BitField; }
175 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
177 bool isVolatileQualified() const { return Quals.hasVolatile(); }
178 bool isRestrictQualified() const { return Quals.hasRestrict(); }
179 unsigned getVRQualifiers() const {
180 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
183 QualType getType() const { return Type; }
185 Qualifiers::ObjCLifetime getObjCLifetime() const {
186 return Quals.getObjCLifetime();
189 bool isObjCIvar() const { return Ivar; }
190 void setObjCIvar(bool Value) { Ivar = Value; }
192 bool isObjCArray() const { return ObjIsArray; }
193 void setObjCArray(bool Value) { ObjIsArray = Value; }
195 bool isNonGC () const { return NonGC; }
196 void setNonGC(bool Value) { NonGC = Value; }
198 bool isGlobalObjCRef() const { return GlobalObjCRef; }
199 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
201 bool isThreadLocalRef() const { return ThreadLocalRef; }
202 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
204 bool isObjCWeak() const {
205 return Quals.getObjCGCAttr() == Qualifiers::Weak;
207 bool isObjCStrong() const {
208 return Quals.getObjCGCAttr() == Qualifiers::Strong;
211 bool isVolatile() const {
212 return Quals.hasVolatile();
215 Expr *getBaseIvarExp() const { return BaseIvarExp; }
216 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
218 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
219 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
221 const Qualifiers &getQuals() const { return Quals; }
222 Qualifiers &getQuals() { return Quals; }
224 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
226 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
227 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
230 llvm::Value *getAddress() const { assert(isSimple()); return V; }
231 void setAddress(llvm::Value *address) {
237 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
238 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
240 // extended vector elements.
241 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
242 llvm::Constant *getExtVectorElts() const {
243 assert(isExtVectorElt());
248 llvm::Value *getBitFieldBaseAddr() const {
249 assert(isBitField());
252 const CGBitFieldInfo &getBitFieldInfo() const {
253 assert(isBitField());
254 return *BitFieldInfo;
257 static LValue MakeAddr(llvm::Value *address, QualType type,
258 CharUnits alignment, ASTContext &Context,
259 llvm::MDNode *TBAAInfo = 0) {
260 Qualifiers qs = type.getQualifiers();
261 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
266 R.Initialize(type, qs, alignment, TBAAInfo);
270 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
271 QualType type, CharUnits Alignment) {
273 R.LVType = VectorElt;
276 R.Initialize(type, type.getQualifiers(), Alignment);
280 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
281 QualType type, CharUnits Alignment) {
283 R.LVType = ExtVectorElt;
286 R.Initialize(type, type.getQualifiers(), Alignment);
290 /// \brief Create a new object to represent a bit-field access.
292 /// \param BaseValue - The base address of the structure containing the
294 /// \param Info - The information describing how to perform the bit-field
296 static LValue MakeBitfield(llvm::Value *BaseValue,
297 const CGBitFieldInfo &Info,
298 QualType type, CharUnits Alignment) {
302 R.BitFieldInfo = &Info;
303 R.Initialize(type, type.getQualifiers(), Alignment);
307 RValue asAggregateRValue() const {
309 return RValue::getAggregate(getAddress(), isVolatileQualified());
313 /// An aggregate value slot.
321 unsigned short Alignment;
323 /// DestructedFlag - This is set to true if some external code is
324 /// responsible for setting up a destructor for the slot. Otherwise
325 /// the code which constructs it should push the appropriate cleanup.
326 bool DestructedFlag : 1;
328 /// ObjCGCFlag - This is set to true if writing to the memory in the
329 /// slot might require calling an appropriate Objective-C GC
330 /// barrier. The exact interaction here is unnecessarily mysterious.
333 /// ZeroedFlag - This is set to true if the memory in the slot is
334 /// known to be zero before the assignment into it. This means that
335 /// zero fields don't need to be set.
338 /// AliasedFlag - This is set to true if the slot might be aliased
339 /// and it's not undefined behavior to access it through such an
340 /// alias. Note that it's always undefined behavior to access a C++
341 /// object that's under construction through an alias derived from
342 /// outside the construction process.
344 /// This flag controls whether calls that produce the aggregate
345 /// value may be evaluated directly into the slot, or whether they
346 /// must be evaluated into an unaliased temporary and then memcpy'ed
347 /// over. Since it's invalid in general to memcpy a non-POD C++
348 /// object, it's important that this flag never be set when
349 /// evaluating an expression which constructs such an object.
350 bool AliasedFlag : 1;
353 enum IsAliased_t { IsNotAliased, IsAliased };
354 enum IsDestructed_t { IsNotDestructed, IsDestructed };
355 enum IsZeroed_t { IsNotZeroed, IsZeroed };
356 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
358 /// ignored - Returns an aggregate value slot indicating that the
359 /// aggregate value is being ignored.
360 static AggValueSlot ignored() {
361 return forAddr(0, CharUnits(), Qualifiers(), IsNotDestructed,
362 DoesNotNeedGCBarriers, IsNotAliased);
365 /// forAddr - Make a slot for an aggregate value.
367 /// \param quals - The qualifiers that dictate how the slot should
368 /// be initialied. Only 'volatile' and the Objective-C lifetime
369 /// qualifiers matter.
371 /// \param isDestructed - true if something else is responsible
372 /// for calling destructors on this object
373 /// \param needsGC - true if the slot is potentially located
374 /// somewhere that ObjC GC calls should be emitted for
375 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
377 IsDestructed_t isDestructed,
378 NeedsGCBarriers_t needsGC,
379 IsAliased_t isAliased,
380 IsZeroed_t isZeroed = IsNotZeroed) {
383 AV.Alignment = align.getQuantity();
385 AV.DestructedFlag = isDestructed;
386 AV.ObjCGCFlag = needsGC;
387 AV.ZeroedFlag = isZeroed;
388 AV.AliasedFlag = isAliased;
392 static AggValueSlot forLValue(const LValue &LV,
393 IsDestructed_t isDestructed,
394 NeedsGCBarriers_t needsGC,
395 IsAliased_t isAliased,
396 IsZeroed_t isZeroed = IsNotZeroed) {
397 return forAddr(LV.getAddress(), LV.getAlignment(),
398 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
401 IsDestructed_t isExternallyDestructed() const {
402 return IsDestructed_t(DestructedFlag);
404 void setExternallyDestructed(bool destructed = true) {
405 DestructedFlag = destructed;
408 Qualifiers getQualifiers() const { return Quals; }
410 bool isVolatile() const {
411 return Quals.hasVolatile();
414 Qualifiers::ObjCLifetime getObjCLifetime() const {
415 return Quals.getObjCLifetime();
418 NeedsGCBarriers_t requiresGCollection() const {
419 return NeedsGCBarriers_t(ObjCGCFlag);
422 llvm::Value *getAddr() const {
426 bool isIgnored() const {
430 CharUnits getAlignment() const {
431 return CharUnits::fromQuantity(Alignment);
434 IsAliased_t isPotentiallyAliased() const {
435 return IsAliased_t(AliasedFlag);
439 RValue asRValue() const {
440 return RValue::getAggregate(getAddr(), isVolatile());
443 void setZeroed(bool V = true) { ZeroedFlag = V; }
444 IsZeroed_t isZeroed() const {
445 return IsZeroed_t(ZeroedFlag);
449 } // end namespace CodeGen
450 } // end namespace clang