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"
22 #include "llvm/IR/Type.h"
32 struct CGBitFieldInfo;
34 /// RValue - This trivial value class is used to represent the result of an
35 /// expression that is evaluated. It can be one of three things: either a
36 /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
37 /// address of an aggregate value in memory.
39 enum Flavor { Scalar, Complex, Aggregate };
41 // Stores first value and flavor.
42 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
43 // Stores second value and volatility.
44 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
47 bool isScalar() const { return V1.getInt() == Scalar; }
48 bool isComplex() const { return V1.getInt() == Complex; }
49 bool isAggregate() const { return V1.getInt() == Aggregate; }
51 bool isVolatileQualified() const { return V2.getInt(); }
53 /// getScalarVal() - Return the Value* of this scalar value.
54 llvm::Value *getScalarVal() const {
55 assert(isScalar() && "Not a scalar!");
56 return V1.getPointer();
59 /// getComplexVal - Return the real/imag components of this complex value.
61 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
62 return std::make_pair(V1.getPointer(), V2.getPointer());
65 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
66 llvm::Value *getAggregateAddr() const {
67 assert(isAggregate() && "Not an aggregate!");
68 return V1.getPointer();
71 static RValue get(llvm::Value *V) {
78 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
82 ER.V1.setInt(Complex);
86 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
87 return getComplex(C.first, C.second);
89 // FIXME: Aggregate rvalues need to retain information about whether they are
90 // volatile or not. Remove default to find all places that probably get this
92 static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
95 ER.V1.setInt(Aggregate);
96 ER.V2.setInt(Volatile);
101 /// Does an ARC strong l-value have precise lifetime?
102 enum ARCPreciseLifetime_t {
103 ARCImpreciseLifetime, ARCPreciseLifetime
106 /// LValue - This represents an lvalue references. Because C/C++ allow
107 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
111 Simple, // This is a normal l-value, use getAddress().
112 VectorElt, // This is a vector element l-value (V[i]), use getVector*
113 BitField, // This is a bitfield l-value, use getBitfield*.
114 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
115 GlobalReg // This is a register l-value, use getGlobalReg()
121 // Index into a vector subscript: V[i]
122 llvm::Value *VectorIdx;
124 // ExtVector element subset: V.xyx
125 llvm::Constant *VectorElts;
127 // BitField start bit and size
128 const CGBitFieldInfo *BitFieldInfo;
133 // 'const' is unused here
136 // The alignment to use when accessing this lvalue. (For vector elements,
137 // this is the alignment of the whole vector.)
140 // objective-c's ivar
143 // objective-c's ivar is an array
146 // LValue is non-gc'able for any reason, including being a parameter or local
150 // Lvalue is a global reference of an objective-c object
151 bool GlobalObjCRef : 1;
153 // Lvalue is a thread local reference
154 bool ThreadLocalRef : 1;
156 // Lvalue has ARC imprecise lifetime. We store this inverted to try
157 // to make the default bitfield pattern all-zeroes.
158 bool ImpreciseLifetime : 1;
162 /// Used by struct-path-aware TBAA.
163 QualType TBAABaseType;
164 /// Offset relative to the base type.
167 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
168 llvm::MDNode *TBAAInfo;
171 void Initialize(QualType Type, Qualifiers Quals,
173 llvm::MDNode *TBAAInfo = nullptr) {
176 this->Alignment = Alignment.getQuantity();
177 assert(this->Alignment == Alignment.getQuantity() &&
178 "Alignment exceeds allowed max!");
180 // Initialize Objective-C flags.
181 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
182 this->ImpreciseLifetime = false;
183 this->ThreadLocalRef = false;
184 this->BaseIvarExp = nullptr;
186 // Initialize fields for TBAA.
187 this->TBAABaseType = Type;
188 this->TBAAOffset = 0;
189 this->TBAAInfo = TBAAInfo;
193 bool isSimple() const { return LVType == Simple; }
194 bool isVectorElt() const { return LVType == VectorElt; }
195 bool isBitField() const { return LVType == BitField; }
196 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
197 bool isGlobalReg() const { return LVType == GlobalReg; }
199 bool isVolatileQualified() const { return Quals.hasVolatile(); }
200 bool isRestrictQualified() const { return Quals.hasRestrict(); }
201 unsigned getVRQualifiers() const {
202 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
205 QualType getType() const { return Type; }
207 Qualifiers::ObjCLifetime getObjCLifetime() const {
208 return Quals.getObjCLifetime();
211 bool isObjCIvar() const { return Ivar; }
212 void setObjCIvar(bool Value) { Ivar = Value; }
214 bool isObjCArray() const { return ObjIsArray; }
215 void setObjCArray(bool Value) { ObjIsArray = Value; }
217 bool isNonGC () const { return NonGC; }
218 void setNonGC(bool Value) { NonGC = Value; }
220 bool isGlobalObjCRef() const { return GlobalObjCRef; }
221 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
223 bool isThreadLocalRef() const { return ThreadLocalRef; }
224 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
226 ARCPreciseLifetime_t isARCPreciseLifetime() const {
227 return ARCPreciseLifetime_t(!ImpreciseLifetime);
229 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
230 ImpreciseLifetime = (value == ARCImpreciseLifetime);
233 bool isObjCWeak() const {
234 return Quals.getObjCGCAttr() == Qualifiers::Weak;
236 bool isObjCStrong() const {
237 return Quals.getObjCGCAttr() == Qualifiers::Strong;
240 bool isVolatile() const {
241 return Quals.hasVolatile();
244 Expr *getBaseIvarExp() const { return BaseIvarExp; }
245 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
247 QualType getTBAABaseType() const { return TBAABaseType; }
248 void setTBAABaseType(QualType T) { TBAABaseType = T; }
250 uint64_t getTBAAOffset() const { return TBAAOffset; }
251 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
253 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
254 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
256 const Qualifiers &getQuals() const { return Quals; }
257 Qualifiers &getQuals() { return Quals; }
259 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
261 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
262 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
265 llvm::Value *getAddress() const { assert(isSimple()); return V; }
266 void setAddress(llvm::Value *address) {
272 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
273 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
275 // extended vector elements.
276 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
277 llvm::Constant *getExtVectorElts() const {
278 assert(isExtVectorElt());
283 llvm::Value *getBitFieldAddr() const {
284 assert(isBitField());
287 const CGBitFieldInfo &getBitFieldInfo() const {
288 assert(isBitField());
289 return *BitFieldInfo;
292 // global register lvalue
293 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
295 static LValue MakeAddr(llvm::Value *address, QualType type,
296 CharUnits alignment, ASTContext &Context,
297 llvm::MDNode *TBAAInfo = nullptr) {
298 Qualifiers qs = type.getQualifiers();
299 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
303 assert(address->getType()->isPointerTy());
305 R.Initialize(type, qs, alignment, TBAAInfo);
309 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
310 QualType type, CharUnits Alignment) {
312 R.LVType = VectorElt;
315 R.Initialize(type, type.getQualifiers(), Alignment);
319 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
320 QualType type, CharUnits Alignment) {
322 R.LVType = ExtVectorElt;
325 R.Initialize(type, type.getQualifiers(), Alignment);
329 /// \brief Create a new object to represent a bit-field access.
331 /// \param Addr - The base address of the bit-field sequence this
332 /// bit-field refers to.
333 /// \param Info - The information describing how to perform the bit-field
335 static LValue MakeBitfield(llvm::Value *Addr,
336 const CGBitFieldInfo &Info,
337 QualType type, CharUnits Alignment) {
341 R.BitFieldInfo = &Info;
342 R.Initialize(type, type.getQualifiers(), Alignment);
346 static LValue MakeGlobalReg(llvm::Value *Reg,
348 CharUnits Alignment) {
350 R.LVType = GlobalReg;
352 R.Initialize(type, type.getQualifiers(), Alignment);
356 RValue asAggregateRValue() const {
358 return RValue::getAggregate(getAddress(), isVolatileQualified());
362 /// An aggregate value slot.
370 unsigned short Alignment;
372 /// DestructedFlag - This is set to true if some external code is
373 /// responsible for setting up a destructor for the slot. Otherwise
374 /// the code which constructs it should push the appropriate cleanup.
375 bool DestructedFlag : 1;
377 /// ObjCGCFlag - This is set to true if writing to the memory in the
378 /// slot might require calling an appropriate Objective-C GC
379 /// barrier. The exact interaction here is unnecessarily mysterious.
382 /// ZeroedFlag - This is set to true if the memory in the slot is
383 /// known to be zero before the assignment into it. This means that
384 /// zero fields don't need to be set.
387 /// AliasedFlag - This is set to true if the slot might be aliased
388 /// and it's not undefined behavior to access it through such an
389 /// alias. Note that it's always undefined behavior to access a C++
390 /// object that's under construction through an alias derived from
391 /// outside the construction process.
393 /// This flag controls whether calls that produce the aggregate
394 /// value may be evaluated directly into the slot, or whether they
395 /// must be evaluated into an unaliased temporary and then memcpy'ed
396 /// over. Since it's invalid in general to memcpy a non-POD C++
397 /// object, it's important that this flag never be set when
398 /// evaluating an expression which constructs such an object.
399 bool AliasedFlag : 1;
402 enum IsAliased_t { IsNotAliased, IsAliased };
403 enum IsDestructed_t { IsNotDestructed, IsDestructed };
404 enum IsZeroed_t { IsNotZeroed, IsZeroed };
405 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
407 /// ignored - Returns an aggregate value slot indicating that the
408 /// aggregate value is being ignored.
409 static AggValueSlot ignored() {
410 return forAddr(nullptr, CharUnits(), Qualifiers(), IsNotDestructed,
411 DoesNotNeedGCBarriers, IsNotAliased);
414 /// forAddr - Make a slot for an aggregate value.
416 /// \param quals - The qualifiers that dictate how the slot should
417 /// be initialied. Only 'volatile' and the Objective-C lifetime
418 /// qualifiers matter.
420 /// \param isDestructed - true if something else is responsible
421 /// for calling destructors on this object
422 /// \param needsGC - true if the slot is potentially located
423 /// somewhere that ObjC GC calls should be emitted for
424 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
426 IsDestructed_t isDestructed,
427 NeedsGCBarriers_t needsGC,
428 IsAliased_t isAliased,
429 IsZeroed_t isZeroed = IsNotZeroed) {
432 AV.Alignment = align.getQuantity();
434 AV.DestructedFlag = isDestructed;
435 AV.ObjCGCFlag = needsGC;
436 AV.ZeroedFlag = isZeroed;
437 AV.AliasedFlag = isAliased;
441 static AggValueSlot forLValue(const LValue &LV,
442 IsDestructed_t isDestructed,
443 NeedsGCBarriers_t needsGC,
444 IsAliased_t isAliased,
445 IsZeroed_t isZeroed = IsNotZeroed) {
446 return forAddr(LV.getAddress(), LV.getAlignment(),
447 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
450 IsDestructed_t isExternallyDestructed() const {
451 return IsDestructed_t(DestructedFlag);
453 void setExternallyDestructed(bool destructed = true) {
454 DestructedFlag = destructed;
457 Qualifiers getQualifiers() const { return Quals; }
459 bool isVolatile() const {
460 return Quals.hasVolatile();
463 void setVolatile(bool flag) {
464 Quals.setVolatile(flag);
467 Qualifiers::ObjCLifetime getObjCLifetime() const {
468 return Quals.getObjCLifetime();
471 NeedsGCBarriers_t requiresGCollection() const {
472 return NeedsGCBarriers_t(ObjCGCFlag);
475 llvm::Value *getAddr() const {
479 bool isIgnored() const {
480 return Addr == nullptr;
483 CharUnits getAlignment() const {
484 return CharUnits::fromQuantity(Alignment);
487 IsAliased_t isPotentiallyAliased() const {
488 return IsAliased_t(AliasedFlag);
492 RValue asRValue() const {
493 return RValue::getAggregate(getAddr(), isVolatile());
496 void setZeroed(bool V = true) { ZeroedFlag = V; }
497 IsZeroed_t isZeroed() const {
498 return IsZeroed_t(ZeroedFlag);
502 } // end namespace CodeGen
503 } // end namespace clang