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"
21 #include "llvm/IR/Value.h"
31 struct CGBitFieldInfo;
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.
38 enum Flavor { Scalar, Complex, Aggregate };
40 // 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;
46 bool isScalar() const { return V1.getInt() == Scalar; }
47 bool isComplex() const { return V1.getInt() == Complex; }
48 bool isAggregate() const { return V1.getInt() == Aggregate; }
50 bool isVolatileQualified() const { return V2.getInt(); }
52 /// getScalarVal() - Return the Value* of this scalar value.
53 llvm::Value *getScalarVal() const {
54 assert(isScalar() && "Not a scalar!");
55 return V1.getPointer();
58 /// getComplexVal - Return the real/imag components of this complex value.
60 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
61 return std::make_pair(V1.getPointer(), V2.getPointer());
64 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
65 llvm::Value *getAggregateAddr() const {
66 assert(isAggregate() && "Not an aggregate!");
67 return V1.getPointer();
70 static RValue get(llvm::Value *V) {
77 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
81 ER.V1.setInt(Complex);
85 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
86 return getComplex(C.first, C.second);
88 // 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
91 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
94 ER.V1.setInt(Aggregate);
95 ER.V2.setInt(Volatile);
100 /// Does an ARC strong l-value have precise lifetime?
101 enum ARCPreciseLifetime_t {
102 ARCImpreciseLifetime, ARCPreciseLifetime
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
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*.
113 ExtVectorElt // This is an extended vector subset, use getExtVectorComp
119 // Index into a vector subscript: V[i]
120 llvm::Value *VectorIdx;
122 // ExtVector element subset: V.xyx
123 llvm::Constant *VectorElts;
125 // BitField start bit and size
126 const CGBitFieldInfo *BitFieldInfo;
131 // 'const' is unused here
134 // The alignment to use when accessing this lvalue. (For vector elements,
135 // this is the alignment of the whole vector.)
138 // objective-c's ivar
141 // objective-c's ivar is an array
144 // LValue is non-gc'able for any reason, including being a parameter or local
148 // Lvalue is a global reference of an objective-c object
149 bool GlobalObjCRef : 1;
151 // Lvalue is a thread local reference
152 bool ThreadLocalRef : 1;
154 // Lvalue has ARC imprecise lifetime. We store this inverted to try
155 // to make the default bitfield pattern all-zeroes.
156 bool ImpreciseLifetime : 1;
160 /// Used by struct-path-aware TBAA.
161 QualType TBAABaseType;
162 /// Offset relative to the base type.
165 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
166 llvm::MDNode *TBAAInfo;
169 void Initialize(QualType Type, Qualifiers Quals,
171 llvm::MDNode *TBAAInfo = 0) {
174 this->Alignment = Alignment.getQuantity();
175 assert(this->Alignment == Alignment.getQuantity() &&
176 "Alignment exceeds allowed max!");
178 // Initialize Objective-C flags.
179 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
180 this->ImpreciseLifetime = false;
181 this->ThreadLocalRef = false;
182 this->BaseIvarExp = 0;
184 // Initialize fields for TBAA.
185 this->TBAABaseType = Type;
186 this->TBAAOffset = 0;
187 this->TBAAInfo = TBAAInfo;
191 bool isSimple() const { return LVType == Simple; }
192 bool isVectorElt() const { return LVType == VectorElt; }
193 bool isBitField() const { return LVType == BitField; }
194 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
196 bool isVolatileQualified() const { return Quals.hasVolatile(); }
197 bool isRestrictQualified() const { return Quals.hasRestrict(); }
198 unsigned getVRQualifiers() const {
199 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
202 QualType getType() const { return Type; }
204 Qualifiers::ObjCLifetime getObjCLifetime() const {
205 return Quals.getObjCLifetime();
208 bool isObjCIvar() const { return Ivar; }
209 void setObjCIvar(bool Value) { Ivar = Value; }
211 bool isObjCArray() const { return ObjIsArray; }
212 void setObjCArray(bool Value) { ObjIsArray = Value; }
214 bool isNonGC () const { return NonGC; }
215 void setNonGC(bool Value) { NonGC = Value; }
217 bool isGlobalObjCRef() const { return GlobalObjCRef; }
218 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
220 bool isThreadLocalRef() const { return ThreadLocalRef; }
221 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
223 ARCPreciseLifetime_t isARCPreciseLifetime() const {
224 return ARCPreciseLifetime_t(!ImpreciseLifetime);
226 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
227 ImpreciseLifetime = (value == ARCImpreciseLifetime);
230 bool isObjCWeak() const {
231 return Quals.getObjCGCAttr() == Qualifiers::Weak;
233 bool isObjCStrong() const {
234 return Quals.getObjCGCAttr() == Qualifiers::Strong;
237 bool isVolatile() const {
238 return Quals.hasVolatile();
241 Expr *getBaseIvarExp() const { return BaseIvarExp; }
242 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
244 QualType getTBAABaseType() const { return TBAABaseType; }
245 void setTBAABaseType(QualType T) { TBAABaseType = T; }
247 uint64_t getTBAAOffset() const { return TBAAOffset; }
248 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
250 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
251 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
253 const Qualifiers &getQuals() const { return Quals; }
254 Qualifiers &getQuals() { return Quals; }
256 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
258 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
259 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
262 llvm::Value *getAddress() const { assert(isSimple()); return V; }
263 void setAddress(llvm::Value *address) {
269 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
270 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
272 // extended vector elements.
273 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
274 llvm::Constant *getExtVectorElts() const {
275 assert(isExtVectorElt());
280 llvm::Value *getBitFieldAddr() const {
281 assert(isBitField());
284 const CGBitFieldInfo &getBitFieldInfo() const {
285 assert(isBitField());
286 return *BitFieldInfo;
289 static LValue MakeAddr(llvm::Value *address, QualType type,
290 CharUnits alignment, ASTContext &Context,
291 llvm::MDNode *TBAAInfo = 0) {
292 Qualifiers qs = type.getQualifiers();
293 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
298 R.Initialize(type, qs, alignment, TBAAInfo);
302 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
303 QualType type, CharUnits Alignment) {
305 R.LVType = VectorElt;
308 R.Initialize(type, type.getQualifiers(), Alignment);
312 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
313 QualType type, CharUnits Alignment) {
315 R.LVType = ExtVectorElt;
318 R.Initialize(type, type.getQualifiers(), Alignment);
322 /// \brief Create a new object to represent a bit-field access.
324 /// \param Addr - The base address of the bit-field sequence this
325 /// bit-field refers to.
326 /// \param Info - The information describing how to perform the bit-field
328 static LValue MakeBitfield(llvm::Value *Addr,
329 const CGBitFieldInfo &Info,
330 QualType type, CharUnits Alignment) {
334 R.BitFieldInfo = &Info;
335 R.Initialize(type, type.getQualifiers(), Alignment);
339 RValue asAggregateRValue() const {
341 return RValue::getAggregate(getAddress(), isVolatileQualified());
345 /// An aggregate value slot.
353 unsigned short Alignment;
355 /// 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.
358 bool DestructedFlag : 1;
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.
365 /// 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.
370 /// 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.
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.
382 bool AliasedFlag : 1;
384 /// 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
393 bool ValueOfAtomicFlag : 1;
396 enum IsAliased_t { IsNotAliased, IsAliased };
397 enum IsDestructed_t { IsNotDestructed, IsDestructed };
398 enum IsZeroed_t { IsNotZeroed, IsZeroed };
399 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
400 enum IsValueOfAtomic_t { IsNotValueOfAtomic, IsValueOfAtomic };
402 /// ignored - Returns an aggregate value slot indicating that the
403 /// aggregate value is being ignored.
404 static AggValueSlot ignored() {
405 return forAddr(0, CharUnits(), Qualifiers(), IsNotDestructed,
406 DoesNotNeedGCBarriers, IsNotAliased);
409 /// forAddr - Make a slot for an aggregate value.
411 /// \param quals - The qualifiers that dictate how the slot should
412 /// be initialied. Only 'volatile' and the Objective-C lifetime
413 /// qualifiers matter.
415 /// \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
418 /// somewhere that ObjC GC calls should be emitted for
419 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
421 IsDestructed_t isDestructed,
422 NeedsGCBarriers_t needsGC,
423 IsAliased_t isAliased,
424 IsZeroed_t isZeroed = IsNotZeroed,
425 IsValueOfAtomic_t isValueOfAtomic
426 = IsNotValueOfAtomic) {
429 AV.Alignment = align.getQuantity();
431 AV.DestructedFlag = isDestructed;
432 AV.ObjCGCFlag = needsGC;
433 AV.ZeroedFlag = isZeroed;
434 AV.AliasedFlag = isAliased;
435 AV.ValueOfAtomicFlag = isValueOfAtomic;
439 static AggValueSlot forLValue(const LValue &LV,
440 IsDestructed_t isDestructed,
441 NeedsGCBarriers_t needsGC,
442 IsAliased_t isAliased,
443 IsZeroed_t isZeroed = IsNotZeroed,
444 IsValueOfAtomic_t isValueOfAtomic
445 = IsNotValueOfAtomic) {
446 return forAddr(LV.getAddress(), LV.getAlignment(),
447 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed,
451 IsDestructed_t isExternallyDestructed() const {
452 return IsDestructed_t(DestructedFlag);
454 void setExternallyDestructed(bool destructed = true) {
455 DestructedFlag = destructed;
458 Qualifiers getQualifiers() const { return Quals; }
460 bool isVolatile() const {
461 return Quals.hasVolatile();
464 void setVolatile(bool flag) {
465 Quals.setVolatile(flag);
468 Qualifiers::ObjCLifetime getObjCLifetime() const {
469 return Quals.getObjCLifetime();
472 NeedsGCBarriers_t requiresGCollection() const {
473 return NeedsGCBarriers_t(ObjCGCFlag);
476 llvm::Value *getAddr() const {
480 IsValueOfAtomic_t isValueOfAtomic() const {
481 return IsValueOfAtomic_t(ValueOfAtomicFlag);
484 llvm::Value *getPaddedAtomicAddr() const;
486 bool isIgnored() const {
490 CharUnits getAlignment() const {
491 return CharUnits::fromQuantity(Alignment);
494 IsAliased_t isPotentiallyAliased() const {
495 return IsAliased_t(AliasedFlag);
499 RValue asRValue() const {
500 return RValue::getAggregate(getAddr(), isVolatile());
503 void setZeroed(bool V = true) { ZeroedFlag = V; }
504 IsZeroed_t isZeroed() const {
505 return IsZeroed_t(ZeroedFlag);
509 } // end namespace CodeGen
510 } // end namespace clang