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 LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
16 #define LLVM_CLANG_LIB_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
114 GlobalReg // This is a register l-value, use getGlobalReg()
120 // Index into a vector subscript: V[i]
121 llvm::Value *VectorIdx;
123 // ExtVector element subset: V.xyx
124 llvm::Constant *VectorElts;
126 // BitField start bit and size
127 const CGBitFieldInfo *BitFieldInfo;
132 // 'const' is unused here
135 // The alignment to use when accessing this lvalue. (For vector elements,
136 // this is the alignment of the whole vector.)
139 // objective-c's ivar
142 // objective-c's ivar is an array
145 // LValue is non-gc'able for any reason, including being a parameter or local
149 // Lvalue is a global reference of an objective-c object
150 bool GlobalObjCRef : 1;
152 // Lvalue is a thread local reference
153 bool ThreadLocalRef : 1;
155 // Lvalue has ARC imprecise lifetime. We store this inverted to try
156 // to make the default bitfield pattern all-zeroes.
157 bool ImpreciseLifetime : 1;
161 /// Used by struct-path-aware TBAA.
162 QualType TBAABaseType;
163 /// Offset relative to the base type.
166 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
167 llvm::MDNode *TBAAInfo;
170 void Initialize(QualType Type, Qualifiers Quals,
172 llvm::MDNode *TBAAInfo = nullptr) {
175 this->Alignment = Alignment.getQuantity();
176 assert(this->Alignment == Alignment.getQuantity() &&
177 "Alignment exceeds allowed max!");
179 // Initialize Objective-C flags.
180 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
181 this->ImpreciseLifetime = false;
182 this->ThreadLocalRef = false;
183 this->BaseIvarExp = nullptr;
185 // Initialize fields for TBAA.
186 this->TBAABaseType = Type;
187 this->TBAAOffset = 0;
188 this->TBAAInfo = TBAAInfo;
192 bool isSimple() const { return LVType == Simple; }
193 bool isVectorElt() const { return LVType == VectorElt; }
194 bool isBitField() const { return LVType == BitField; }
195 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
196 bool isGlobalReg() const { return LVType == GlobalReg; }
198 bool isVolatileQualified() const { return Quals.hasVolatile(); }
199 bool isRestrictQualified() const { return Quals.hasRestrict(); }
200 unsigned getVRQualifiers() const {
201 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
204 QualType getType() const { return Type; }
206 Qualifiers::ObjCLifetime getObjCLifetime() const {
207 return Quals.getObjCLifetime();
210 bool isObjCIvar() const { return Ivar; }
211 void setObjCIvar(bool Value) { Ivar = Value; }
213 bool isObjCArray() const { return ObjIsArray; }
214 void setObjCArray(bool Value) { ObjIsArray = Value; }
216 bool isNonGC () const { return NonGC; }
217 void setNonGC(bool Value) { NonGC = Value; }
219 bool isGlobalObjCRef() const { return GlobalObjCRef; }
220 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
222 bool isThreadLocalRef() const { return ThreadLocalRef; }
223 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
225 ARCPreciseLifetime_t isARCPreciseLifetime() const {
226 return ARCPreciseLifetime_t(!ImpreciseLifetime);
228 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
229 ImpreciseLifetime = (value == ARCImpreciseLifetime);
232 bool isObjCWeak() const {
233 return Quals.getObjCGCAttr() == Qualifiers::Weak;
235 bool isObjCStrong() const {
236 return Quals.getObjCGCAttr() == Qualifiers::Strong;
239 bool isVolatile() const {
240 return Quals.hasVolatile();
243 Expr *getBaseIvarExp() const { return BaseIvarExp; }
244 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
246 QualType getTBAABaseType() const { return TBAABaseType; }
247 void setTBAABaseType(QualType T) { TBAABaseType = T; }
249 uint64_t getTBAAOffset() const { return TBAAOffset; }
250 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
252 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
253 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
255 const Qualifiers &getQuals() const { return Quals; }
256 Qualifiers &getQuals() { return Quals; }
258 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
260 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
261 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
264 llvm::Value *getAddress() const { assert(isSimple()); return V; }
265 void setAddress(llvm::Value *address) {
271 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
272 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
274 // extended vector elements.
275 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
276 llvm::Constant *getExtVectorElts() const {
277 assert(isExtVectorElt());
282 llvm::Value *getBitFieldAddr() const {
283 assert(isBitField());
286 const CGBitFieldInfo &getBitFieldInfo() const {
287 assert(isBitField());
288 return *BitFieldInfo;
291 // global register lvalue
292 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
294 static LValue MakeAddr(llvm::Value *address, QualType type,
295 CharUnits alignment, ASTContext &Context,
296 llvm::MDNode *TBAAInfo = nullptr) {
297 Qualifiers qs = type.getQualifiers();
298 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
303 R.Initialize(type, qs, alignment, TBAAInfo);
307 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
308 QualType type, CharUnits Alignment) {
310 R.LVType = VectorElt;
313 R.Initialize(type, type.getQualifiers(), Alignment);
317 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
318 QualType type, CharUnits Alignment) {
320 R.LVType = ExtVectorElt;
323 R.Initialize(type, type.getQualifiers(), Alignment);
327 /// \brief Create a new object to represent a bit-field access.
329 /// \param Addr - The base address of the bit-field sequence this
330 /// bit-field refers to.
331 /// \param Info - The information describing how to perform the bit-field
333 static LValue MakeBitfield(llvm::Value *Addr,
334 const CGBitFieldInfo &Info,
335 QualType type, CharUnits Alignment) {
339 R.BitFieldInfo = &Info;
340 R.Initialize(type, type.getQualifiers(), Alignment);
344 static LValue MakeGlobalReg(llvm::Value *Reg,
346 CharUnits Alignment) {
348 R.LVType = GlobalReg;
350 R.Initialize(type, type.getQualifiers(), Alignment);
354 RValue asAggregateRValue() const {
356 return RValue::getAggregate(getAddress(), isVolatileQualified());
360 /// An aggregate value slot.
368 unsigned short Alignment;
370 /// DestructedFlag - This is set to true if some external code is
371 /// responsible for setting up a destructor for the slot. Otherwise
372 /// the code which constructs it should push the appropriate cleanup.
373 bool DestructedFlag : 1;
375 /// ObjCGCFlag - This is set to true if writing to the memory in the
376 /// slot might require calling an appropriate Objective-C GC
377 /// barrier. The exact interaction here is unnecessarily mysterious.
380 /// ZeroedFlag - This is set to true if the memory in the slot is
381 /// known to be zero before the assignment into it. This means that
382 /// zero fields don't need to be set.
385 /// AliasedFlag - This is set to true if the slot might be aliased
386 /// and it's not undefined behavior to access it through such an
387 /// alias. Note that it's always undefined behavior to access a C++
388 /// object that's under construction through an alias derived from
389 /// outside the construction process.
391 /// This flag controls whether calls that produce the aggregate
392 /// value may be evaluated directly into the slot, or whether they
393 /// must be evaluated into an unaliased temporary and then memcpy'ed
394 /// over. Since it's invalid in general to memcpy a non-POD C++
395 /// object, it's important that this flag never be set when
396 /// evaluating an expression which constructs such an object.
397 bool AliasedFlag : 1;
400 enum IsAliased_t { IsNotAliased, IsAliased };
401 enum IsDestructed_t { IsNotDestructed, IsDestructed };
402 enum IsZeroed_t { IsNotZeroed, IsZeroed };
403 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
405 /// ignored - Returns an aggregate value slot indicating that the
406 /// aggregate value is being ignored.
407 static AggValueSlot ignored() {
408 return forAddr(nullptr, CharUnits(), Qualifiers(), IsNotDestructed,
409 DoesNotNeedGCBarriers, IsNotAliased);
412 /// forAddr - Make a slot for an aggregate value.
414 /// \param quals - The qualifiers that dictate how the slot should
415 /// be initialied. Only 'volatile' and the Objective-C lifetime
416 /// qualifiers matter.
418 /// \param isDestructed - true if something else is responsible
419 /// for calling destructors on this object
420 /// \param needsGC - true if the slot is potentially located
421 /// somewhere that ObjC GC calls should be emitted for
422 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
424 IsDestructed_t isDestructed,
425 NeedsGCBarriers_t needsGC,
426 IsAliased_t isAliased,
427 IsZeroed_t isZeroed = IsNotZeroed) {
430 AV.Alignment = align.getQuantity();
432 AV.DestructedFlag = isDestructed;
433 AV.ObjCGCFlag = needsGC;
434 AV.ZeroedFlag = isZeroed;
435 AV.AliasedFlag = isAliased;
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 return forAddr(LV.getAddress(), LV.getAlignment(),
445 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
448 IsDestructed_t isExternallyDestructed() const {
449 return IsDestructed_t(DestructedFlag);
451 void setExternallyDestructed(bool destructed = true) {
452 DestructedFlag = destructed;
455 Qualifiers getQualifiers() const { return Quals; }
457 bool isVolatile() const {
458 return Quals.hasVolatile();
461 void setVolatile(bool flag) {
462 Quals.setVolatile(flag);
465 Qualifiers::ObjCLifetime getObjCLifetime() const {
466 return Quals.getObjCLifetime();
469 NeedsGCBarriers_t requiresGCollection() const {
470 return NeedsGCBarriers_t(ObjCGCFlag);
473 llvm::Value *getAddr() const {
477 bool isIgnored() const {
478 return Addr == nullptr;
481 CharUnits getAlignment() const {
482 return CharUnits::fromQuantity(Alignment);
485 IsAliased_t isPotentiallyAliased() const {
486 return IsAliased_t(AliasedFlag);
490 RValue asRValue() const {
491 return RValue::getAggregate(getAddr(), isVolatile());
494 void setZeroed(bool V = true) { ZeroedFlag = V; }
495 IsZeroed_t isZeroed() const {
496 return IsZeroed_t(ZeroedFlag);
500 } // end namespace CodeGen
501 } // end namespace clang