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/Type.h"
20 #include "llvm/IR/Value.h"
21 #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 // The shift to make to an aggregate's alignment to make it look
43 enum { AggAlignShift = 4 };
45 // Stores first value and flavor.
46 llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
47 // Stores second value and volatility.
48 llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
51 bool isScalar() const { return V1.getInt() == Scalar; }
52 bool isComplex() const { return V1.getInt() == Complex; }
53 bool isAggregate() const { return V1.getInt() == Aggregate; }
55 bool isVolatileQualified() const { return V2.getInt(); }
57 /// getScalarVal() - Return the Value* of this scalar value.
58 llvm::Value *getScalarVal() const {
59 assert(isScalar() && "Not a scalar!");
60 return V1.getPointer();
63 /// getComplexVal - Return the real/imag components of this complex value.
65 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
66 return std::make_pair(V1.getPointer(), V2.getPointer());
69 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
70 Address getAggregateAddress() const {
71 assert(isAggregate() && "Not an aggregate!");
72 auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
73 return Address(V1.getPointer(), CharUnits::fromQuantity(align));
75 llvm::Value *getAggregatePointer() const {
76 assert(isAggregate() && "Not an aggregate!");
77 return V1.getPointer();
80 static RValue getIgnored() {
81 // FIXME: should we make this a more explicit state?
85 static RValue get(llvm::Value *V) {
92 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
96 ER.V1.setInt(Complex);
100 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
101 return getComplex(C.first, C.second);
103 // FIXME: Aggregate rvalues need to retain information about whether they are
104 // volatile or not. Remove default to find all places that probably get this
106 static RValue getAggregate(Address addr, bool isVolatile = false) {
108 ER.V1.setPointer(addr.getPointer());
109 ER.V1.setInt(Aggregate);
111 auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
112 ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
113 ER.V2.setInt(isVolatile);
118 /// Does an ARC strong l-value have precise lifetime?
119 enum ARCPreciseLifetime_t {
120 ARCImpreciseLifetime, ARCPreciseLifetime
123 /// The source of the alignment of an l-value; an expression of
124 /// confidence in the alignment actually matching the estimate.
125 enum class AlignmentSource {
126 /// The l-value was an access to a declared entity or something
127 /// equivalently strong, like the address of an array allocated by a
128 /// language runtime.
131 /// The l-value was considered opaque, so the alignment was
132 /// determined from a type, but that type was an explicitly-aligned
136 /// The l-value was considered opaque, so the alignment was
137 /// determined from a type.
141 /// Given that the base address has the given alignment source, what's
142 /// our confidence in the alignment of the field?
143 static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
144 // For now, we don't distinguish fields of opaque pointers from
145 // top-level declarations, but maybe we should.
146 return AlignmentSource::Decl;
149 /// LValue - This represents an lvalue references. Because C/C++ allow
150 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
154 Simple, // This is a normal l-value, use getAddress().
155 VectorElt, // This is a vector element l-value (V[i]), use getVector*
156 BitField, // This is a bitfield l-value, use getBitfield*.
157 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
158 GlobalReg // This is a register l-value, use getGlobalReg()
164 // Index into a vector subscript: V[i]
165 llvm::Value *VectorIdx;
167 // ExtVector element subset: V.xyx
168 llvm::Constant *VectorElts;
170 // BitField start bit and size
171 const CGBitFieldInfo *BitFieldInfo;
176 // 'const' is unused here
179 // The alignment to use when accessing this lvalue. (For vector elements,
180 // this is the alignment of the whole vector.)
183 // objective-c's ivar
186 // objective-c's ivar is an array
189 // LValue is non-gc'able for any reason, including being a parameter or local
193 // Lvalue is a global reference of an objective-c object
194 bool GlobalObjCRef : 1;
196 // Lvalue is a thread local reference
197 bool ThreadLocalRef : 1;
199 // Lvalue has ARC imprecise lifetime. We store this inverted to try
200 // to make the default bitfield pattern all-zeroes.
201 bool ImpreciseLifetime : 1;
203 unsigned AlignSource : 2;
205 // This flag shows if a nontemporal load/stores should be used when accessing
207 bool Nontemporal : 1;
211 /// Used by struct-path-aware TBAA.
212 QualType TBAABaseType;
213 /// Offset relative to the base type.
216 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
217 llvm::MDNode *TBAAInfo;
220 void Initialize(QualType Type, Qualifiers Quals,
221 CharUnits Alignment, AlignmentSource AlignSource,
222 llvm::MDNode *TBAAInfo = nullptr) {
223 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
224 "initializing l-value with zero alignment!");
227 this->Alignment = Alignment.getQuantity();
228 assert(this->Alignment == Alignment.getQuantity() &&
229 "Alignment exceeds allowed max!");
230 this->AlignSource = unsigned(AlignSource);
232 // Initialize Objective-C flags.
233 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
234 this->ImpreciseLifetime = false;
235 this->Nontemporal = false;
236 this->ThreadLocalRef = false;
237 this->BaseIvarExp = nullptr;
239 // Initialize fields for TBAA.
240 this->TBAABaseType = Type;
241 this->TBAAOffset = 0;
242 this->TBAAInfo = TBAAInfo;
246 bool isSimple() const { return LVType == Simple; }
247 bool isVectorElt() const { return LVType == VectorElt; }
248 bool isBitField() const { return LVType == BitField; }
249 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
250 bool isGlobalReg() const { return LVType == GlobalReg; }
252 bool isVolatileQualified() const { return Quals.hasVolatile(); }
253 bool isRestrictQualified() const { return Quals.hasRestrict(); }
254 unsigned getVRQualifiers() const {
255 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
258 QualType getType() const { return Type; }
260 Qualifiers::ObjCLifetime getObjCLifetime() const {
261 return Quals.getObjCLifetime();
264 bool isObjCIvar() const { return Ivar; }
265 void setObjCIvar(bool Value) { Ivar = Value; }
267 bool isObjCArray() const { return ObjIsArray; }
268 void setObjCArray(bool Value) { ObjIsArray = Value; }
270 bool isNonGC () const { return NonGC; }
271 void setNonGC(bool Value) { NonGC = Value; }
273 bool isGlobalObjCRef() const { return GlobalObjCRef; }
274 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
276 bool isThreadLocalRef() const { return ThreadLocalRef; }
277 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
279 ARCPreciseLifetime_t isARCPreciseLifetime() const {
280 return ARCPreciseLifetime_t(!ImpreciseLifetime);
282 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
283 ImpreciseLifetime = (value == ARCImpreciseLifetime);
285 bool isNontemporal() const { return Nontemporal; }
286 void setNontemporal(bool Value) { Nontemporal = Value; }
288 bool isObjCWeak() const {
289 return Quals.getObjCGCAttr() == Qualifiers::Weak;
291 bool isObjCStrong() const {
292 return Quals.getObjCGCAttr() == Qualifiers::Strong;
295 bool isVolatile() const {
296 return Quals.hasVolatile();
299 Expr *getBaseIvarExp() const { return BaseIvarExp; }
300 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
302 QualType getTBAABaseType() const { return TBAABaseType; }
303 void setTBAABaseType(QualType T) { TBAABaseType = T; }
305 uint64_t getTBAAOffset() const { return TBAAOffset; }
306 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
308 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
309 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
311 const Qualifiers &getQuals() const { return Quals; }
312 Qualifiers &getQuals() { return Quals; }
314 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
316 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
317 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
319 AlignmentSource getAlignmentSource() const {
320 return AlignmentSource(AlignSource);
322 void setAlignmentSource(AlignmentSource Source) {
323 AlignSource = unsigned(Source);
327 llvm::Value *getPointer() const {
331 Address getAddress() const { return Address(getPointer(), getAlignment()); }
332 void setAddress(Address address) {
334 V = address.getPointer();
335 Alignment = address.getAlignment().getQuantity();
339 Address getVectorAddress() const {
340 return Address(getVectorPointer(), getAlignment());
342 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
343 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
345 // extended vector elements.
346 Address getExtVectorAddress() const {
347 return Address(getExtVectorPointer(), getAlignment());
349 llvm::Value *getExtVectorPointer() const {
350 assert(isExtVectorElt());
353 llvm::Constant *getExtVectorElts() const {
354 assert(isExtVectorElt());
359 Address getBitFieldAddress() const {
360 return Address(getBitFieldPointer(), getAlignment());
362 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
363 const CGBitFieldInfo &getBitFieldInfo() const {
364 assert(isBitField());
365 return *BitFieldInfo;
368 // global register lvalue
369 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
371 static LValue MakeAddr(Address address, QualType type,
373 AlignmentSource alignSource,
374 llvm::MDNode *TBAAInfo = nullptr) {
375 Qualifiers qs = type.getQualifiers();
376 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
380 assert(address.getPointer()->getType()->isPointerTy());
381 R.V = address.getPointer();
382 R.Initialize(type, qs, address.getAlignment(), alignSource, TBAAInfo);
386 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
387 QualType type, AlignmentSource alignSource) {
389 R.LVType = VectorElt;
390 R.V = vecAddress.getPointer();
392 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
397 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
398 QualType type, AlignmentSource alignSource) {
400 R.LVType = ExtVectorElt;
401 R.V = vecAddress.getPointer();
403 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
408 /// \brief Create a new object to represent a bit-field access.
410 /// \param Addr - The base address of the bit-field sequence this
411 /// bit-field refers to.
412 /// \param Info - The information describing how to perform the bit-field
414 static LValue MakeBitfield(Address Addr,
415 const CGBitFieldInfo &Info,
417 AlignmentSource alignSource) {
420 R.V = Addr.getPointer();
421 R.BitFieldInfo = &Info;
422 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), alignSource);
426 static LValue MakeGlobalReg(Address Reg, QualType type) {
428 R.LVType = GlobalReg;
429 R.V = Reg.getPointer();
430 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
431 AlignmentSource::Decl);
435 RValue asAggregateRValue() const {
436 return RValue::getAggregate(getAddress(), isVolatileQualified());
440 /// An aggregate value slot.
448 unsigned short Alignment;
450 /// DestructedFlag - This is set to true if some external code is
451 /// responsible for setting up a destructor for the slot. Otherwise
452 /// the code which constructs it should push the appropriate cleanup.
453 bool DestructedFlag : 1;
455 /// ObjCGCFlag - This is set to true if writing to the memory in the
456 /// slot might require calling an appropriate Objective-C GC
457 /// barrier. The exact interaction here is unnecessarily mysterious.
460 /// ZeroedFlag - This is set to true if the memory in the slot is
461 /// known to be zero before the assignment into it. This means that
462 /// zero fields don't need to be set.
465 /// AliasedFlag - This is set to true if the slot might be aliased
466 /// and it's not undefined behavior to access it through such an
467 /// alias. Note that it's always undefined behavior to access a C++
468 /// object that's under construction through an alias derived from
469 /// outside the construction process.
471 /// This flag controls whether calls that produce the aggregate
472 /// value may be evaluated directly into the slot, or whether they
473 /// must be evaluated into an unaliased temporary and then memcpy'ed
474 /// over. Since it's invalid in general to memcpy a non-POD C++
475 /// object, it's important that this flag never be set when
476 /// evaluating an expression which constructs such an object.
477 bool AliasedFlag : 1;
480 enum IsAliased_t { IsNotAliased, IsAliased };
481 enum IsDestructed_t { IsNotDestructed, IsDestructed };
482 enum IsZeroed_t { IsNotZeroed, IsZeroed };
483 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
485 /// ignored - Returns an aggregate value slot indicating that the
486 /// aggregate value is being ignored.
487 static AggValueSlot ignored() {
488 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
489 DoesNotNeedGCBarriers, IsNotAliased);
492 /// forAddr - Make a slot for an aggregate value.
494 /// \param quals - The qualifiers that dictate how the slot should
495 /// be initialied. Only 'volatile' and the Objective-C lifetime
496 /// qualifiers matter.
498 /// \param isDestructed - true if something else is responsible
499 /// for calling destructors on this object
500 /// \param needsGC - true if the slot is potentially located
501 /// somewhere that ObjC GC calls should be emitted for
502 static AggValueSlot forAddr(Address addr,
504 IsDestructed_t isDestructed,
505 NeedsGCBarriers_t needsGC,
506 IsAliased_t isAliased,
507 IsZeroed_t isZeroed = IsNotZeroed) {
509 if (addr.isValid()) {
510 AV.Addr = addr.getPointer();
511 AV.Alignment = addr.getAlignment().getQuantity();
517 AV.DestructedFlag = isDestructed;
518 AV.ObjCGCFlag = needsGC;
519 AV.ZeroedFlag = isZeroed;
520 AV.AliasedFlag = isAliased;
524 static AggValueSlot forLValue(const LValue &LV,
525 IsDestructed_t isDestructed,
526 NeedsGCBarriers_t needsGC,
527 IsAliased_t isAliased,
528 IsZeroed_t isZeroed = IsNotZeroed) {
529 return forAddr(LV.getAddress(),
530 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
533 IsDestructed_t isExternallyDestructed() const {
534 return IsDestructed_t(DestructedFlag);
536 void setExternallyDestructed(bool destructed = true) {
537 DestructedFlag = destructed;
540 Qualifiers getQualifiers() const { return Quals; }
542 bool isVolatile() const {
543 return Quals.hasVolatile();
546 void setVolatile(bool flag) {
547 Quals.setVolatile(flag);
550 Qualifiers::ObjCLifetime getObjCLifetime() const {
551 return Quals.getObjCLifetime();
554 NeedsGCBarriers_t requiresGCollection() const {
555 return NeedsGCBarriers_t(ObjCGCFlag);
558 llvm::Value *getPointer() const {
562 Address getAddress() const {
563 return Address(Addr, getAlignment());
566 bool isIgnored() const {
567 return Addr == nullptr;
570 CharUnits getAlignment() const {
571 return CharUnits::fromQuantity(Alignment);
574 IsAliased_t isPotentiallyAliased() const {
575 return IsAliased_t(AliasedFlag);
578 RValue asRValue() const {
580 return RValue::getIgnored();
582 return RValue::getAggregate(getAddress(), isVolatile());
586 void setZeroed(bool V = true) { ZeroedFlag = V; }
587 IsZeroed_t isZeroed() const {
588 return IsZeroed_t(ZeroedFlag);
592 } // end namespace CodeGen
593 } // end namespace clang