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 class LValueBaseInfo {
150 AlignmentSource AlignSource;
154 explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type,
156 : AlignSource(Source), MayAlias(Alias) {}
157 AlignmentSource getAlignmentSource() const { return AlignSource; }
158 void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
159 bool getMayAlias() const { return MayAlias; }
160 void setMayAlias(bool Alias) { MayAlias = Alias; }
162 void mergeForCast(const LValueBaseInfo &Info) {
163 setAlignmentSource(Info.getAlignmentSource());
164 setMayAlias(getMayAlias() || Info.getMayAlias());
168 /// LValue - This represents an lvalue references. Because C/C++ allow
169 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
173 Simple, // This is a normal l-value, use getAddress().
174 VectorElt, // This is a vector element l-value (V[i]), use getVector*
175 BitField, // This is a bitfield l-value, use getBitfield*.
176 ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
177 GlobalReg // This is a register l-value, use getGlobalReg()
183 // Index into a vector subscript: V[i]
184 llvm::Value *VectorIdx;
186 // ExtVector element subset: V.xyx
187 llvm::Constant *VectorElts;
189 // BitField start bit and size
190 const CGBitFieldInfo *BitFieldInfo;
195 // 'const' is unused here
198 // The alignment to use when accessing this lvalue. (For vector elements,
199 // this is the alignment of the whole vector.)
202 // objective-c's ivar
205 // objective-c's ivar is an array
208 // LValue is non-gc'able for any reason, including being a parameter or local
212 // Lvalue is a global reference of an objective-c object
213 bool GlobalObjCRef : 1;
215 // Lvalue is a thread local reference
216 bool ThreadLocalRef : 1;
218 // Lvalue has ARC imprecise lifetime. We store this inverted to try
219 // to make the default bitfield pattern all-zeroes.
220 bool ImpreciseLifetime : 1;
222 LValueBaseInfo BaseInfo;
224 // This flag shows if a nontemporal load/stores should be used when accessing
226 bool Nontemporal : 1;
230 /// Used by struct-path-aware TBAA.
231 QualType TBAABaseType;
232 /// Offset relative to the base type.
235 /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
236 llvm::MDNode *TBAAInfo;
239 void Initialize(QualType Type, Qualifiers Quals,
240 CharUnits Alignment, LValueBaseInfo BaseInfo,
241 llvm::MDNode *TBAAInfo = nullptr) {
242 assert((!Alignment.isZero() || Type->isIncompleteType()) &&
243 "initializing l-value with zero alignment!");
246 this->Alignment = Alignment.getQuantity();
247 assert(this->Alignment == Alignment.getQuantity() &&
248 "Alignment exceeds allowed max!");
249 this->BaseInfo = BaseInfo;
251 // Initialize Objective-C flags.
252 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
253 this->ImpreciseLifetime = false;
254 this->Nontemporal = false;
255 this->ThreadLocalRef = false;
256 this->BaseIvarExp = nullptr;
258 // Initialize fields for TBAA.
259 this->TBAABaseType = Type;
260 this->TBAAOffset = 0;
261 this->TBAAInfo = TBAAInfo;
265 bool isSimple() const { return LVType == Simple; }
266 bool isVectorElt() const { return LVType == VectorElt; }
267 bool isBitField() const { return LVType == BitField; }
268 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
269 bool isGlobalReg() const { return LVType == GlobalReg; }
271 bool isVolatileQualified() const { return Quals.hasVolatile(); }
272 bool isRestrictQualified() const { return Quals.hasRestrict(); }
273 unsigned getVRQualifiers() const {
274 return Quals.getCVRQualifiers() & ~Qualifiers::Const;
277 QualType getType() const { return Type; }
279 Qualifiers::ObjCLifetime getObjCLifetime() const {
280 return Quals.getObjCLifetime();
283 bool isObjCIvar() const { return Ivar; }
284 void setObjCIvar(bool Value) { Ivar = Value; }
286 bool isObjCArray() const { return ObjIsArray; }
287 void setObjCArray(bool Value) { ObjIsArray = Value; }
289 bool isNonGC () const { return NonGC; }
290 void setNonGC(bool Value) { NonGC = Value; }
292 bool isGlobalObjCRef() const { return GlobalObjCRef; }
293 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
295 bool isThreadLocalRef() const { return ThreadLocalRef; }
296 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
298 ARCPreciseLifetime_t isARCPreciseLifetime() const {
299 return ARCPreciseLifetime_t(!ImpreciseLifetime);
301 void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
302 ImpreciseLifetime = (value == ARCImpreciseLifetime);
304 bool isNontemporal() const { return Nontemporal; }
305 void setNontemporal(bool Value) { Nontemporal = Value; }
307 bool isObjCWeak() const {
308 return Quals.getObjCGCAttr() == Qualifiers::Weak;
310 bool isObjCStrong() const {
311 return Quals.getObjCGCAttr() == Qualifiers::Strong;
314 bool isVolatile() const {
315 return Quals.hasVolatile();
318 Expr *getBaseIvarExp() const { return BaseIvarExp; }
319 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
321 QualType getTBAABaseType() const { return TBAABaseType; }
322 void setTBAABaseType(QualType T) { TBAABaseType = T; }
324 uint64_t getTBAAOffset() const { return TBAAOffset; }
325 void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
327 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
328 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
330 const Qualifiers &getQuals() const { return Quals; }
331 Qualifiers &getQuals() { return Quals; }
333 unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
335 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
336 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
338 LValueBaseInfo getBaseInfo() const { return BaseInfo; }
339 void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
342 llvm::Value *getPointer() const {
346 Address getAddress() const { return Address(getPointer(), getAlignment()); }
347 void setAddress(Address address) {
349 V = address.getPointer();
350 Alignment = address.getAlignment().getQuantity();
354 Address getVectorAddress() const {
355 return Address(getVectorPointer(), getAlignment());
357 llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
358 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
360 // extended vector elements.
361 Address getExtVectorAddress() const {
362 return Address(getExtVectorPointer(), getAlignment());
364 llvm::Value *getExtVectorPointer() const {
365 assert(isExtVectorElt());
368 llvm::Constant *getExtVectorElts() const {
369 assert(isExtVectorElt());
374 Address getBitFieldAddress() const {
375 return Address(getBitFieldPointer(), getAlignment());
377 llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
378 const CGBitFieldInfo &getBitFieldInfo() const {
379 assert(isBitField());
380 return *BitFieldInfo;
383 // global register lvalue
384 llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
386 static LValue MakeAddr(Address address, QualType type,
388 LValueBaseInfo BaseInfo,
389 llvm::MDNode *TBAAInfo = nullptr) {
390 Qualifiers qs = type.getQualifiers();
391 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
395 assert(address.getPointer()->getType()->isPointerTy());
396 R.V = address.getPointer();
397 R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
401 static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
402 QualType type, LValueBaseInfo BaseInfo) {
404 R.LVType = VectorElt;
405 R.V = vecAddress.getPointer();
407 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
412 static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
413 QualType type, LValueBaseInfo BaseInfo) {
415 R.LVType = ExtVectorElt;
416 R.V = vecAddress.getPointer();
418 R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
423 /// \brief Create a new object to represent a bit-field access.
425 /// \param Addr - The base address of the bit-field sequence this
426 /// bit-field refers to.
427 /// \param Info - The information describing how to perform the bit-field
429 static LValue MakeBitfield(Address Addr,
430 const CGBitFieldInfo &Info,
432 LValueBaseInfo BaseInfo) {
435 R.V = Addr.getPointer();
436 R.BitFieldInfo = &Info;
437 R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo);
441 static LValue MakeGlobalReg(Address Reg, QualType type) {
443 R.LVType = GlobalReg;
444 R.V = Reg.getPointer();
445 R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
446 LValueBaseInfo(AlignmentSource::Decl, false));
450 RValue asAggregateRValue() const {
451 return RValue::getAggregate(getAddress(), isVolatileQualified());
455 /// An aggregate value slot.
465 /// DestructedFlag - This is set to true if some external code is
466 /// responsible for setting up a destructor for the slot. Otherwise
467 /// the code which constructs it should push the appropriate cleanup.
468 bool DestructedFlag : 1;
470 /// ObjCGCFlag - This is set to true if writing to the memory in the
471 /// slot might require calling an appropriate Objective-C GC
472 /// barrier. The exact interaction here is unnecessarily mysterious.
475 /// ZeroedFlag - This is set to true if the memory in the slot is
476 /// known to be zero before the assignment into it. This means that
477 /// zero fields don't need to be set.
480 /// AliasedFlag - This is set to true if the slot might be aliased
481 /// and it's not undefined behavior to access it through such an
482 /// alias. Note that it's always undefined behavior to access a C++
483 /// object that's under construction through an alias derived from
484 /// outside the construction process.
486 /// This flag controls whether calls that produce the aggregate
487 /// value may be evaluated directly into the slot, or whether they
488 /// must be evaluated into an unaliased temporary and then memcpy'ed
489 /// over. Since it's invalid in general to memcpy a non-POD C++
490 /// object, it's important that this flag never be set when
491 /// evaluating an expression which constructs such an object.
492 bool AliasedFlag : 1;
495 enum IsAliased_t { IsNotAliased, IsAliased };
496 enum IsDestructed_t { IsNotDestructed, IsDestructed };
497 enum IsZeroed_t { IsNotZeroed, IsZeroed };
498 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
500 /// ignored - Returns an aggregate value slot indicating that the
501 /// aggregate value is being ignored.
502 static AggValueSlot ignored() {
503 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
504 DoesNotNeedGCBarriers, IsNotAliased);
507 /// forAddr - Make a slot for an aggregate value.
509 /// \param quals - The qualifiers that dictate how the slot should
510 /// be initialied. Only 'volatile' and the Objective-C lifetime
511 /// qualifiers matter.
513 /// \param isDestructed - true if something else is responsible
514 /// for calling destructors on this object
515 /// \param needsGC - true if the slot is potentially located
516 /// somewhere that ObjC GC calls should be emitted for
517 static AggValueSlot forAddr(Address addr,
519 IsDestructed_t isDestructed,
520 NeedsGCBarriers_t needsGC,
521 IsAliased_t isAliased,
522 IsZeroed_t isZeroed = IsNotZeroed) {
524 if (addr.isValid()) {
525 AV.Addr = addr.getPointer();
526 AV.Alignment = addr.getAlignment().getQuantity();
532 AV.DestructedFlag = isDestructed;
533 AV.ObjCGCFlag = needsGC;
534 AV.ZeroedFlag = isZeroed;
535 AV.AliasedFlag = isAliased;
539 static AggValueSlot forLValue(const LValue &LV,
540 IsDestructed_t isDestructed,
541 NeedsGCBarriers_t needsGC,
542 IsAliased_t isAliased,
543 IsZeroed_t isZeroed = IsNotZeroed) {
544 return forAddr(LV.getAddress(),
545 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
548 IsDestructed_t isExternallyDestructed() const {
549 return IsDestructed_t(DestructedFlag);
551 void setExternallyDestructed(bool destructed = true) {
552 DestructedFlag = destructed;
555 Qualifiers getQualifiers() const { return Quals; }
557 bool isVolatile() const {
558 return Quals.hasVolatile();
561 void setVolatile(bool flag) {
562 Quals.setVolatile(flag);
565 Qualifiers::ObjCLifetime getObjCLifetime() const {
566 return Quals.getObjCLifetime();
569 NeedsGCBarriers_t requiresGCollection() const {
570 return NeedsGCBarriers_t(ObjCGCFlag);
573 llvm::Value *getPointer() const {
577 Address getAddress() const {
578 return Address(Addr, getAlignment());
581 bool isIgnored() const {
582 return Addr == nullptr;
585 CharUnits getAlignment() const {
586 return CharUnits::fromQuantity(Alignment);
589 IsAliased_t isPotentiallyAliased() const {
590 return IsAliased_t(AliasedFlag);
593 RValue asRValue() const {
595 return RValue::getIgnored();
597 return RValue::getAggregate(getAddress(), isVolatile());
601 void setZeroed(bool V = true) { ZeroedFlag = V; }
602 IsZeroed_t isZeroed() const {
603 return IsZeroed_t(ZeroedFlag);
607 } // end namespace CodeGen
608 } // end namespace clang