1 //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // These classes implement wrappers around llvm::Value in order to
10 // fully represent the range of values for C L- and R- values.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
18 #include "CGPointerAuthInfo.h"
19 #include "CodeGenTBAA.h"
20 #include "EHScopeStack.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/Type.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Value.h"
35 class CodeGenFunction
;
36 struct CGBitFieldInfo
;
38 /// RValue - This trivial value class is used to represent the result of an
39 /// expression that is evaluated. It can be one of three things: either a
40 /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
41 /// address of an aggregate value in memory.
43 friend struct DominatingValue
<RValue
>;
45 enum FlavorEnum
{ Scalar
, Complex
, Aggregate
};
48 // Stores first and second value.
54 // Stores aggregate address.
55 Address AggregateAddr
;
58 unsigned IsVolatile
: 1;
62 RValue() : Vals
{nullptr, nullptr}, Flavor(Scalar
) {}
64 bool isScalar() const { return Flavor
== Scalar
; }
65 bool isComplex() const { return Flavor
== Complex
; }
66 bool isAggregate() const { return Flavor
== Aggregate
; }
68 bool isVolatileQualified() const { return IsVolatile
; }
70 /// getScalarVal() - Return the Value* of this scalar value.
71 llvm::Value
*getScalarVal() const {
72 assert(isScalar() && "Not a scalar!");
76 /// getComplexVal - Return the real/imag components of this complex value.
78 std::pair
<llvm::Value
*, llvm::Value
*> getComplexVal() const {
79 return std::make_pair(Vals
.first
, Vals
.second
);
82 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
83 Address
getAggregateAddress() const {
84 assert(isAggregate() && "Not an aggregate!");
88 llvm::Value
*getAggregatePointer(QualType PointeeType
,
89 CodeGenFunction
&CGF
) const {
90 return getAggregateAddress().getBasePointer();
93 static RValue
getIgnored() {
94 // FIXME: should we make this a more explicit state?
98 static RValue
get(llvm::Value
*V
) {
102 ER
.IsVolatile
= false;
105 static RValue
get(Address Addr
, CodeGenFunction
&CGF
) {
106 return RValue::get(Addr
.emitRawPointer(CGF
));
108 static RValue
getComplex(llvm::Value
*V1
, llvm::Value
*V2
) {
112 ER
.IsVolatile
= false;
115 static RValue
getComplex(const std::pair
<llvm::Value
*, llvm::Value
*> &C
) {
116 return getComplex(C
.first
, C
.second
);
118 // FIXME: Aggregate rvalues need to retain information about whether they are
119 // volatile or not. Remove default to find all places that probably get this
122 /// Convert an Address to an RValue. If the Address is not
123 /// signed, create an RValue using the unsigned address. Otherwise, resign the
124 /// address using the provided type.
125 static RValue
getAggregate(Address addr
, bool isVolatile
= false) {
127 ER
.AggregateAddr
= addr
;
128 ER
.Flavor
= Aggregate
;
129 ER
.IsVolatile
= isVolatile
;
134 /// Does an ARC strong l-value have precise lifetime?
135 enum ARCPreciseLifetime_t
{
136 ARCImpreciseLifetime
, ARCPreciseLifetime
139 /// The source of the alignment of an l-value; an expression of
140 /// confidence in the alignment actually matching the estimate.
141 enum class AlignmentSource
{
142 /// The l-value was an access to a declared entity or something
143 /// equivalently strong, like the address of an array allocated by a
144 /// language runtime.
147 /// The l-value was considered opaque, so the alignment was
148 /// determined from a type, but that type was an explicitly-aligned
152 /// The l-value was considered opaque, so the alignment was
153 /// determined from a type.
157 /// Given that the base address has the given alignment source, what's
158 /// our confidence in the alignment of the field?
159 static inline AlignmentSource
getFieldAlignmentSource(AlignmentSource Source
) {
160 // For now, we don't distinguish fields of opaque pointers from
161 // top-level declarations, but maybe we should.
162 return AlignmentSource::Decl
;
165 class LValueBaseInfo
{
166 AlignmentSource AlignSource
;
169 explicit LValueBaseInfo(AlignmentSource Source
= AlignmentSource::Type
)
170 : AlignSource(Source
) {}
171 AlignmentSource
getAlignmentSource() const { return AlignSource
; }
172 void setAlignmentSource(AlignmentSource Source
) { AlignSource
= Source
; }
174 void mergeForCast(const LValueBaseInfo
&Info
) {
175 setAlignmentSource(Info
.getAlignmentSource());
179 /// LValue - This represents an lvalue references. Because C/C++ allow
180 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
184 Simple
, // This is a normal l-value, use getAddress().
185 VectorElt
, // This is a vector element l-value (V[i]), use getVector*
186 BitField
, // This is a bitfield l-value, use getBitfield*.
187 ExtVectorElt
, // This is an extended vector subset, use getExtVectorComp
188 GlobalReg
, // This is a register l-value, use getGlobalReg()
189 MatrixElt
// This is a matrix element, use getVector*
193 Address Addr
= Address::invalid();
198 // Index into a vector subscript: V[i]
199 llvm::Value
*VectorIdx
;
201 // ExtVector element subset: V.xyx
202 llvm::Constant
*VectorElts
;
204 // BitField start bit and size
205 const CGBitFieldInfo
*BitFieldInfo
;
210 // 'const' is unused here
213 // objective-c's ivar
216 // objective-c's ivar is an array
219 // LValue is non-gc'able for any reason, including being a parameter or local
223 // Lvalue is a global reference of an objective-c object
224 bool GlobalObjCRef
: 1;
226 // Lvalue is a thread local reference
227 bool ThreadLocalRef
: 1;
229 // Lvalue has ARC imprecise lifetime. We store this inverted to try
230 // to make the default bitfield pattern all-zeroes.
231 bool ImpreciseLifetime
: 1;
233 // This flag shows if a nontemporal load/stores should be used when accessing
235 bool Nontemporal
: 1;
237 LValueBaseInfo BaseInfo
;
238 TBAAAccessInfo TBAAInfo
;
243 void Initialize(QualType Type
, Qualifiers Quals
, Address Addr
,
244 LValueBaseInfo BaseInfo
, TBAAAccessInfo TBAAInfo
) {
247 const unsigned MaxAlign
= 1U << 31;
248 CharUnits Alignment
= Addr
.getAlignment();
249 assert((isGlobalReg() || !Alignment
.isZero() || Type
->isIncompleteType()) &&
250 "initializing l-value with zero alignment!");
251 if (Alignment
.getQuantity() > MaxAlign
) {
252 assert(false && "Alignment exceeds allowed max!");
253 Alignment
= CharUnits::fromQuantity(MaxAlign
);
256 this->BaseInfo
= BaseInfo
;
257 this->TBAAInfo
= TBAAInfo
;
259 // Initialize Objective-C flags.
260 this->Ivar
= this->ObjIsArray
= this->NonGC
= this->GlobalObjCRef
= false;
261 this->ImpreciseLifetime
= false;
262 this->Nontemporal
= false;
263 this->ThreadLocalRef
= false;
264 this->BaseIvarExp
= nullptr;
267 void initializeSimpleLValue(Address Addr
, QualType Type
,
268 LValueBaseInfo BaseInfo
, TBAAAccessInfo TBAAInfo
,
269 ASTContext
&Context
) {
270 Qualifiers QS
= Type
.getQualifiers();
271 QS
.setObjCGCAttr(Context
.getObjCGCAttrKind(Type
));
273 Initialize(Type
, QS
, Addr
, BaseInfo
, TBAAInfo
);
274 assert(Addr
.getBasePointer()->getType()->isPointerTy());
278 bool isSimple() const { return LVType
== Simple
; }
279 bool isVectorElt() const { return LVType
== VectorElt
; }
280 bool isBitField() const { return LVType
== BitField
; }
281 bool isExtVectorElt() const { return LVType
== ExtVectorElt
; }
282 bool isGlobalReg() const { return LVType
== GlobalReg
; }
283 bool isMatrixElt() const { return LVType
== MatrixElt
; }
285 bool isVolatileQualified() const { return Quals
.hasVolatile(); }
286 bool isRestrictQualified() const { return Quals
.hasRestrict(); }
287 unsigned getVRQualifiers() const {
288 return Quals
.getCVRQualifiers() & ~Qualifiers::Const
;
291 QualType
getType() const { return Type
; }
293 Qualifiers::ObjCLifetime
getObjCLifetime() const {
294 return Quals
.getObjCLifetime();
297 bool isObjCIvar() const { return Ivar
; }
298 void setObjCIvar(bool Value
) { Ivar
= Value
; }
300 bool isObjCArray() const { return ObjIsArray
; }
301 void setObjCArray(bool Value
) { ObjIsArray
= Value
; }
303 bool isNonGC () const { return NonGC
; }
304 void setNonGC(bool Value
) { NonGC
= Value
; }
306 bool isGlobalObjCRef() const { return GlobalObjCRef
; }
307 void setGlobalObjCRef(bool Value
) { GlobalObjCRef
= Value
; }
309 bool isThreadLocalRef() const { return ThreadLocalRef
; }
310 void setThreadLocalRef(bool Value
) { ThreadLocalRef
= Value
;}
312 ARCPreciseLifetime_t
isARCPreciseLifetime() const {
313 return ARCPreciseLifetime_t(!ImpreciseLifetime
);
315 void setARCPreciseLifetime(ARCPreciseLifetime_t value
) {
316 ImpreciseLifetime
= (value
== ARCImpreciseLifetime
);
318 bool isNontemporal() const { return Nontemporal
; }
319 void setNontemporal(bool Value
) { Nontemporal
= Value
; }
321 bool isObjCWeak() const {
322 return Quals
.getObjCGCAttr() == Qualifiers::Weak
;
324 bool isObjCStrong() const {
325 return Quals
.getObjCGCAttr() == Qualifiers::Strong
;
328 bool isVolatile() const {
329 return Quals
.hasVolatile();
332 Expr
*getBaseIvarExp() const { return BaseIvarExp
; }
333 void setBaseIvarExp(Expr
*V
) { BaseIvarExp
= V
; }
335 TBAAAccessInfo
getTBAAInfo() const { return TBAAInfo
; }
336 void setTBAAInfo(TBAAAccessInfo Info
) { TBAAInfo
= Info
; }
338 const Qualifiers
&getQuals() const { return Quals
; }
339 Qualifiers
&getQuals() { return Quals
; }
341 LangAS
getAddressSpace() const { return Quals
.getAddressSpace(); }
343 CharUnits
getAlignment() const { return Addr
.getAlignment(); }
344 void setAlignment(CharUnits A
) { Addr
.setAlignment(A
); }
346 LValueBaseInfo
getBaseInfo() const { return BaseInfo
; }
347 void setBaseInfo(LValueBaseInfo Info
) { BaseInfo
= Info
; }
349 KnownNonNull_t
isKnownNonNull() const { return Addr
.isKnownNonNull(); }
350 LValue
setKnownNonNull() {
351 Addr
.setKnownNonNull();
356 llvm::Value
*getPointer(CodeGenFunction
&CGF
) const;
357 llvm::Value
*emitResignedPointer(QualType PointeeTy
,
358 CodeGenFunction
&CGF
) const;
359 llvm::Value
*emitRawPointer(CodeGenFunction
&CGF
) const;
361 Address
getAddress() const { return Addr
; }
363 void setAddress(Address address
) { Addr
= address
; }
365 CGPointerAuthInfo
getPointerAuthInfo() const {
366 return Addr
.getPointerAuthInfo();
370 Address
getVectorAddress() const {
371 assert(isVectorElt());
374 llvm::Value
*getRawVectorPointer(CodeGenFunction
&CGF
) const {
375 assert(isVectorElt());
376 return Addr
.emitRawPointer(CGF
);
378 llvm::Value
*getVectorPointer() const {
379 assert(isVectorElt());
380 return Addr
.getBasePointer();
382 llvm::Value
*getVectorIdx() const {
383 assert(isVectorElt());
387 Address
getMatrixAddress() const {
388 assert(isMatrixElt());
391 llvm::Value
*getMatrixPointer() const {
392 assert(isMatrixElt());
393 return Addr
.getBasePointer();
395 llvm::Value
*getMatrixIdx() const {
396 assert(isMatrixElt());
400 // extended vector elements.
401 Address
getExtVectorAddress() const {
402 assert(isExtVectorElt());
405 llvm::Value
*getRawExtVectorPointer(CodeGenFunction
&CGF
) const {
406 assert(isExtVectorElt());
407 return Addr
.emitRawPointer(CGF
);
409 llvm::Constant
*getExtVectorElts() const {
410 assert(isExtVectorElt());
415 Address
getBitFieldAddress() const {
416 assert(isBitField());
419 llvm::Value
*getRawBitFieldPointer(CodeGenFunction
&CGF
) const {
420 assert(isBitField());
421 return Addr
.emitRawPointer(CGF
);
424 const CGBitFieldInfo
&getBitFieldInfo() const {
425 assert(isBitField());
426 return *BitFieldInfo
;
429 // global register lvalue
430 llvm::Value
*getGlobalReg() const { assert(isGlobalReg()); return V
; }
432 static LValue
MakeAddr(Address Addr
, QualType type
, ASTContext
&Context
,
433 LValueBaseInfo BaseInfo
, TBAAAccessInfo TBAAInfo
) {
436 R
.initializeSimpleLValue(Addr
, type
, BaseInfo
, TBAAInfo
, Context
);
438 assert(Addr
.getType()->isPointerTy());
442 static LValue
MakeVectorElt(Address vecAddress
, llvm::Value
*Idx
,
443 QualType type
, LValueBaseInfo BaseInfo
,
444 TBAAAccessInfo TBAAInfo
) {
446 R
.LVType
= VectorElt
;
448 R
.Initialize(type
, type
.getQualifiers(), vecAddress
, BaseInfo
, TBAAInfo
);
452 static LValue
MakeExtVectorElt(Address Addr
, llvm::Constant
*Elts
,
453 QualType type
, LValueBaseInfo BaseInfo
,
454 TBAAAccessInfo TBAAInfo
) {
456 R
.LVType
= ExtVectorElt
;
458 R
.Initialize(type
, type
.getQualifiers(), Addr
, BaseInfo
, TBAAInfo
);
462 /// Create a new object to represent a bit-field access.
464 /// \param Addr - The base address of the bit-field sequence this
465 /// bit-field refers to.
466 /// \param Info - The information describing how to perform the bit-field
468 static LValue
MakeBitfield(Address Addr
, const CGBitFieldInfo
&Info
,
469 QualType type
, LValueBaseInfo BaseInfo
,
470 TBAAAccessInfo TBAAInfo
) {
473 R
.BitFieldInfo
= &Info
;
474 R
.Initialize(type
, type
.getQualifiers(), Addr
, BaseInfo
, TBAAInfo
);
478 static LValue
MakeGlobalReg(llvm::Value
*V
, CharUnits alignment
,
481 R
.LVType
= GlobalReg
;
482 R
.Initialize(type
, type
.getQualifiers(), Address::invalid(),
483 LValueBaseInfo(AlignmentSource::Decl
), TBAAAccessInfo());
488 static LValue
MakeMatrixElt(Address matAddress
, llvm::Value
*Idx
,
489 QualType type
, LValueBaseInfo BaseInfo
,
490 TBAAAccessInfo TBAAInfo
) {
492 R
.LVType
= MatrixElt
;
494 R
.Initialize(type
, type
.getQualifiers(), matAddress
, BaseInfo
, TBAAInfo
);
498 RValue
asAggregateRValue() const {
499 return RValue::getAggregate(getAddress(), isVolatileQualified());
503 /// An aggregate value slot.
511 /// DestructedFlag - This is set to true if some external code is
512 /// responsible for setting up a destructor for the slot. Otherwise
513 /// the code which constructs it should push the appropriate cleanup.
514 bool DestructedFlag
: 1;
516 /// ObjCGCFlag - This is set to true if writing to the memory in the
517 /// slot might require calling an appropriate Objective-C GC
518 /// barrier. The exact interaction here is unnecessarily mysterious.
521 /// ZeroedFlag - This is set to true if the memory in the slot is
522 /// known to be zero before the assignment into it. This means that
523 /// zero fields don't need to be set.
526 /// AliasedFlag - This is set to true if the slot might be aliased
527 /// and it's not undefined behavior to access it through such an
528 /// alias. Note that it's always undefined behavior to access a C++
529 /// object that's under construction through an alias derived from
530 /// outside the construction process.
532 /// This flag controls whether calls that produce the aggregate
533 /// value may be evaluated directly into the slot, or whether they
534 /// must be evaluated into an unaliased temporary and then memcpy'ed
535 /// over. Since it's invalid in general to memcpy a non-POD C++
536 /// object, it's important that this flag never be set when
537 /// evaluating an expression which constructs such an object.
538 bool AliasedFlag
: 1;
540 /// This is set to true if the tail padding of this slot might overlap
541 /// another object that may have already been initialized (and whose
542 /// value must be preserved by this initialization). If so, we may only
543 /// store up to the dsize of the type. Otherwise we can widen stores to
544 /// the size of the type.
545 bool OverlapFlag
: 1;
547 /// If is set to true, sanitizer checks are already generated for this address
548 /// or not required. For instance, if this address represents an object
549 /// created in 'new' expression, sanitizer checks for memory is made as a part
550 /// of 'operator new' emission and object constructor should not generate
552 bool SanitizerCheckedFlag
: 1;
554 AggValueSlot(Address Addr
, Qualifiers Quals
, bool DestructedFlag
,
555 bool ObjCGCFlag
, bool ZeroedFlag
, bool AliasedFlag
,
556 bool OverlapFlag
, bool SanitizerCheckedFlag
)
557 : Addr(Addr
), Quals(Quals
), DestructedFlag(DestructedFlag
),
558 ObjCGCFlag(ObjCGCFlag
), ZeroedFlag(ZeroedFlag
),
559 AliasedFlag(AliasedFlag
), OverlapFlag(OverlapFlag
),
560 SanitizerCheckedFlag(SanitizerCheckedFlag
) {}
563 enum IsAliased_t
{ IsNotAliased
, IsAliased
};
564 enum IsDestructed_t
{ IsNotDestructed
, IsDestructed
};
565 enum IsZeroed_t
{ IsNotZeroed
, IsZeroed
};
566 enum Overlap_t
{ DoesNotOverlap
, MayOverlap
};
567 enum NeedsGCBarriers_t
{ DoesNotNeedGCBarriers
, NeedsGCBarriers
};
568 enum IsSanitizerChecked_t
{ IsNotSanitizerChecked
, IsSanitizerChecked
};
570 /// ignored - Returns an aggregate value slot indicating that the
571 /// aggregate value is being ignored.
572 static AggValueSlot
ignored() {
573 return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed
,
574 DoesNotNeedGCBarriers
, IsNotAliased
, DoesNotOverlap
);
577 /// forAddr - Make a slot for an aggregate value.
579 /// \param quals - The qualifiers that dictate how the slot should
580 /// be initialied. Only 'volatile' and the Objective-C lifetime
581 /// qualifiers matter.
583 /// \param isDestructed - true if something else is responsible
584 /// for calling destructors on this object
585 /// \param needsGC - true if the slot is potentially located
586 /// somewhere that ObjC GC calls should be emitted for
587 static AggValueSlot
forAddr(Address addr
,
589 IsDestructed_t isDestructed
,
590 NeedsGCBarriers_t needsGC
,
591 IsAliased_t isAliased
,
592 Overlap_t mayOverlap
,
593 IsZeroed_t isZeroed
= IsNotZeroed
,
594 IsSanitizerChecked_t isChecked
= IsNotSanitizerChecked
) {
596 addr
.setKnownNonNull();
597 return AggValueSlot(addr
, quals
, isDestructed
, needsGC
, isZeroed
, isAliased
,
598 mayOverlap
, isChecked
);
602 forLValue(const LValue
&LV
, IsDestructed_t isDestructed
,
603 NeedsGCBarriers_t needsGC
, IsAliased_t isAliased
,
604 Overlap_t mayOverlap
, IsZeroed_t isZeroed
= IsNotZeroed
,
605 IsSanitizerChecked_t isChecked
= IsNotSanitizerChecked
) {
606 return forAddr(LV
.getAddress(), LV
.getQuals(), isDestructed
, needsGC
,
607 isAliased
, mayOverlap
, isZeroed
, isChecked
);
610 IsDestructed_t
isExternallyDestructed() const {
611 return IsDestructed_t(DestructedFlag
);
613 void setExternallyDestructed(bool destructed
= true) {
614 DestructedFlag
= destructed
;
617 Qualifiers
getQualifiers() const { return Quals
; }
619 bool isVolatile() const {
620 return Quals
.hasVolatile();
623 void setVolatile(bool flag
) {
627 Quals
.removeVolatile();
630 Qualifiers::ObjCLifetime
getObjCLifetime() const {
631 return Quals
.getObjCLifetime();
634 NeedsGCBarriers_t
requiresGCollection() const {
635 return NeedsGCBarriers_t(ObjCGCFlag
);
638 llvm::Value
*getPointer(QualType PointeeTy
, CodeGenFunction
&CGF
) const;
640 llvm::Value
*emitRawPointer(CodeGenFunction
&CGF
) const {
641 return Addr
.isValid() ? Addr
.emitRawPointer(CGF
) : nullptr;
644 Address
getAddress() const {
648 bool isIgnored() const { return !Addr
.isValid(); }
650 CharUnits
getAlignment() const {
651 return Addr
.getAlignment();
654 IsAliased_t
isPotentiallyAliased() const {
655 return IsAliased_t(AliasedFlag
);
658 Overlap_t
mayOverlap() const {
659 return Overlap_t(OverlapFlag
);
662 bool isSanitizerChecked() const {
663 return SanitizerCheckedFlag
;
666 RValue
asRValue() const {
668 return RValue::getIgnored();
670 return RValue::getAggregate(getAddress(), isVolatile());
674 void setZeroed(bool V
= true) { ZeroedFlag
= V
; }
675 IsZeroed_t
isZeroed() const {
676 return IsZeroed_t(ZeroedFlag
);
679 /// Get the preferred size to use when storing a value to this slot. This
680 /// is the type size unless that might overlap another object, in which
681 /// case it's the dsize.
682 CharUnits
getPreferredSize(ASTContext
&Ctx
, QualType Type
) const {
683 return mayOverlap() ? Ctx
.getTypeInfoDataSizeInChars(Type
).Width
684 : Ctx
.getTypeSizeInChars(Type
);
688 } // end namespace CodeGen
689 } // end namespace clang