CGValue.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These classes implement wrappers around llvm::Value in order to
  10. // fully represent the range of values for C L- and R- values.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Type.h"
  17. #include "llvm/IR/Value.h"
  18. #include "llvm/IR/Type.h"
  19. #include "Address.h"
  20. #include "CodeGenTBAA.h"
  21. namespace llvm {
  22. class Constant;
  23. class MDNode;
  24. }
  25. namespace clang {
  26. namespace CodeGen {
  27. class AggValueSlot;
  28. class CodeGenFunction;
  29. struct CGBitFieldInfo;
  30. /// RValue - This trivial value class is used to represent the result of an
  31. /// expression that is evaluated. It can be one of three things: either a
  32. /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
  33. /// address of an aggregate value in memory.
  34. class RValue {
  35. enum Flavor { Scalar, Complex, Aggregate };
  36. // The shift to make to an aggregate's alignment to make it look
  37. // like a pointer.
  38. enum { AggAlignShift = 4 };
  39. // Stores first value and flavor.
  40. llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
  41. // Stores second value and volatility.
  42. llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
  43. // Stores element type for aggregate values.
  44. llvm::Type *ElementType;
  45. public:
  46. bool isScalar() const { return V1.getInt() == Scalar; }
  47. bool isComplex() const { return V1.getInt() == Complex; }
  48. bool isAggregate() const { return V1.getInt() == Aggregate; }
  49. bool isVolatileQualified() const { return V2.getInt(); }
  50. /// getScalarVal() - Return the Value* of this scalar value.
  51. llvm::Value *getScalarVal() const {
  52. assert(isScalar() && "Not a scalar!");
  53. return V1.getPointer();
  54. }
  55. /// getComplexVal - Return the real/imag components of this complex value.
  56. ///
  57. std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
  58. return std::make_pair(V1.getPointer(), V2.getPointer());
  59. }
  60. /// getAggregateAddr() - Return the Value* of the address of the aggregate.
  61. Address getAggregateAddress() const {
  62. assert(isAggregate() && "Not an aggregate!");
  63. auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
  64. return Address(
  65. V1.getPointer(), ElementType, CharUnits::fromQuantity(align));
  66. }
  67. llvm::Value *getAggregatePointer() const {
  68. assert(isAggregate() && "Not an aggregate!");
  69. return V1.getPointer();
  70. }
  71. static RValue getIgnored() {
  72. // FIXME: should we make this a more explicit state?
  73. return get(nullptr);
  74. }
  75. static RValue get(llvm::Value *V) {
  76. RValue ER;
  77. ER.V1.setPointer(V);
  78. ER.V1.setInt(Scalar);
  79. ER.V2.setInt(false);
  80. return ER;
  81. }
  82. static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
  83. RValue ER;
  84. ER.V1.setPointer(V1);
  85. ER.V2.setPointer(V2);
  86. ER.V1.setInt(Complex);
  87. ER.V2.setInt(false);
  88. return ER;
  89. }
  90. static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
  91. return getComplex(C.first, C.second);
  92. }
  93. // FIXME: Aggregate rvalues need to retain information about whether they are
  94. // volatile or not. Remove default to find all places that probably get this
  95. // wrong.
  96. static RValue getAggregate(Address addr, bool isVolatile = false) {
  97. RValue ER;
  98. ER.V1.setPointer(addr.getPointer());
  99. ER.V1.setInt(Aggregate);
  100. ER.ElementType = addr.getElementType();
  101. auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
  102. ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
  103. ER.V2.setInt(isVolatile);
  104. return ER;
  105. }
  106. };
  107. /// Does an ARC strong l-value have precise lifetime?
  108. enum ARCPreciseLifetime_t {
  109. ARCImpreciseLifetime, ARCPreciseLifetime
  110. };
  111. /// The source of the alignment of an l-value; an expression of
  112. /// confidence in the alignment actually matching the estimate.
  113. enum class AlignmentSource {
  114. /// The l-value was an access to a declared entity or something
  115. /// equivalently strong, like the address of an array allocated by a
  116. /// language runtime.
  117. Decl,
  118. /// The l-value was considered opaque, so the alignment was
  119. /// determined from a type, but that type was an explicitly-aligned
  120. /// typedef.
  121. AttributedType,
  122. /// The l-value was considered opaque, so the alignment was
  123. /// determined from a type.
  124. Type
  125. };
  126. /// Given that the base address has the given alignment source, what's
  127. /// our confidence in the alignment of the field?
  128. static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
  129. // For now, we don't distinguish fields of opaque pointers from
  130. // top-level declarations, but maybe we should.
  131. return AlignmentSource::Decl;
  132. }
  133. class LValueBaseInfo {
  134. AlignmentSource AlignSource;
  135. public:
  136. explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
  137. : AlignSource(Source) {}
  138. AlignmentSource getAlignmentSource() const { return AlignSource; }
  139. void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
  140. void mergeForCast(const LValueBaseInfo &Info) {
  141. setAlignmentSource(Info.getAlignmentSource());
  142. }
  143. };
  144. /// LValue - This represents an lvalue references. Because C/C++ allow
  145. /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
  146. /// bitrange.
  147. class LValue {
  148. enum {
  149. Simple, // This is a normal l-value, use getAddress().
  150. VectorElt, // This is a vector element l-value (V[i]), use getVector*
  151. BitField, // This is a bitfield l-value, use getBitfield*.
  152. ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
  153. GlobalReg, // This is a register l-value, use getGlobalReg()
  154. MatrixElt // This is a matrix element, use getVector*
  155. } LVType;
  156. llvm::Value *V;
  157. llvm::Type *ElementType;
  158. union {
  159. // Index into a vector subscript: V[i]
  160. llvm::Value *VectorIdx;
  161. // ExtVector element subset: V.xyx
  162. llvm::Constant *VectorElts;
  163. // BitField start bit and size
  164. const CGBitFieldInfo *BitFieldInfo;
  165. };
  166. QualType Type;
  167. // 'const' is unused here
  168. Qualifiers Quals;
  169. // The alignment to use when accessing this lvalue. (For vector elements,
  170. // this is the alignment of the whole vector.)
  171. unsigned Alignment;
  172. // objective-c's ivar
  173. bool Ivar:1;
  174. // objective-c's ivar is an array
  175. bool ObjIsArray:1;
  176. // LValue is non-gc'able for any reason, including being a parameter or local
  177. // variable.
  178. bool NonGC: 1;
  179. // Lvalue is a global reference of an objective-c object
  180. bool GlobalObjCRef : 1;
  181. // Lvalue is a thread local reference
  182. bool ThreadLocalRef : 1;
  183. // Lvalue has ARC imprecise lifetime. We store this inverted to try
  184. // to make the default bitfield pattern all-zeroes.
  185. bool ImpreciseLifetime : 1;
  186. // This flag shows if a nontemporal load/stores should be used when accessing
  187. // this lvalue.
  188. bool Nontemporal : 1;
  189. LValueBaseInfo BaseInfo;
  190. TBAAAccessInfo TBAAInfo;
  191. Expr *BaseIvarExp;
  192. private:
  193. void Initialize(QualType Type, Qualifiers Quals, CharUnits Alignment,
  194. LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
  195. assert((!Alignment.isZero() || Type->isIncompleteType()) &&
  196. "initializing l-value with zero alignment!");
  197. if (isGlobalReg())
  198. assert(ElementType == nullptr && "Global reg does not store elem type");
  199. else
  200. assert(llvm::cast<llvm::PointerType>(V->getType())
  201. ->isOpaqueOrPointeeTypeMatches(ElementType) &&
  202. "Pointer element type mismatch");
  203. this->Type = Type;
  204. this->Quals = Quals;
  205. const unsigned MaxAlign = 1U << 31;
  206. this->Alignment = Alignment.getQuantity() <= MaxAlign
  207. ? Alignment.getQuantity()
  208. : MaxAlign;
  209. assert(this->Alignment == Alignment.getQuantity() &&
  210. "Alignment exceeds allowed max!");
  211. this->BaseInfo = BaseInfo;
  212. this->TBAAInfo = TBAAInfo;
  213. // Initialize Objective-C flags.
  214. this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
  215. this->ImpreciseLifetime = false;
  216. this->Nontemporal = false;
  217. this->ThreadLocalRef = false;
  218. this->BaseIvarExp = nullptr;
  219. }
  220. public:
  221. bool isSimple() const { return LVType == Simple; }
  222. bool isVectorElt() const { return LVType == VectorElt; }
  223. bool isBitField() const { return LVType == BitField; }
  224. bool isExtVectorElt() const { return LVType == ExtVectorElt; }
  225. bool isGlobalReg() const { return LVType == GlobalReg; }
  226. bool isMatrixElt() const { return LVType == MatrixElt; }
  227. bool isVolatileQualified() const { return Quals.hasVolatile(); }
  228. bool isRestrictQualified() const { return Quals.hasRestrict(); }
  229. unsigned getVRQualifiers() const {
  230. return Quals.getCVRQualifiers() & ~Qualifiers::Const;
  231. }
  232. QualType getType() const { return Type; }
  233. Qualifiers::ObjCLifetime getObjCLifetime() const {
  234. return Quals.getObjCLifetime();
  235. }
  236. bool isObjCIvar() const { return Ivar; }
  237. void setObjCIvar(bool Value) { Ivar = Value; }
  238. bool isObjCArray() const { return ObjIsArray; }
  239. void setObjCArray(bool Value) { ObjIsArray = Value; }
  240. bool isNonGC () const { return NonGC; }
  241. void setNonGC(bool Value) { NonGC = Value; }
  242. bool isGlobalObjCRef() const { return GlobalObjCRef; }
  243. void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
  244. bool isThreadLocalRef() const { return ThreadLocalRef; }
  245. void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
  246. ARCPreciseLifetime_t isARCPreciseLifetime() const {
  247. return ARCPreciseLifetime_t(!ImpreciseLifetime);
  248. }
  249. void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
  250. ImpreciseLifetime = (value == ARCImpreciseLifetime);
  251. }
  252. bool isNontemporal() const { return Nontemporal; }
  253. void setNontemporal(bool Value) { Nontemporal = Value; }
  254. bool isObjCWeak() const {
  255. return Quals.getObjCGCAttr() == Qualifiers::Weak;
  256. }
  257. bool isObjCStrong() const {
  258. return Quals.getObjCGCAttr() == Qualifiers::Strong;
  259. }
  260. bool isVolatile() const {
  261. return Quals.hasVolatile();
  262. }
  263. Expr *getBaseIvarExp() const { return BaseIvarExp; }
  264. void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
  265. TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
  266. void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
  267. const Qualifiers &getQuals() const { return Quals; }
  268. Qualifiers &getQuals() { return Quals; }
  269. LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
  270. CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
  271. void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
  272. LValueBaseInfo getBaseInfo() const { return BaseInfo; }
  273. void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
  274. // simple lvalue
  275. llvm::Value *getPointer(CodeGenFunction &CGF) const {
  276. assert(isSimple());
  277. return V;
  278. }
  279. Address getAddress(CodeGenFunction &CGF) const {
  280. return Address(getPointer(CGF), ElementType, getAlignment());
  281. }
  282. void setAddress(Address address) {
  283. assert(isSimple());
  284. V = address.getPointer();
  285. ElementType = address.getElementType();
  286. Alignment = address.getAlignment().getQuantity();
  287. }
  288. // vector elt lvalue
  289. Address getVectorAddress() const {
  290. return Address(getVectorPointer(), ElementType, getAlignment());
  291. }
  292. llvm::Value *getVectorPointer() const {
  293. assert(isVectorElt());
  294. return V;
  295. }
  296. llvm::Value *getVectorIdx() const {
  297. assert(isVectorElt());
  298. return VectorIdx;
  299. }
  300. Address getMatrixAddress() const {
  301. return Address(getMatrixPointer(), ElementType, getAlignment());
  302. }
  303. llvm::Value *getMatrixPointer() const {
  304. assert(isMatrixElt());
  305. return V;
  306. }
  307. llvm::Value *getMatrixIdx() const {
  308. assert(isMatrixElt());
  309. return VectorIdx;
  310. }
  311. // extended vector elements.
  312. Address getExtVectorAddress() const {
  313. return Address(getExtVectorPointer(), ElementType, getAlignment());
  314. }
  315. llvm::Value *getExtVectorPointer() const {
  316. assert(isExtVectorElt());
  317. return V;
  318. }
  319. llvm::Constant *getExtVectorElts() const {
  320. assert(isExtVectorElt());
  321. return VectorElts;
  322. }
  323. // bitfield lvalue
  324. Address getBitFieldAddress() const {
  325. return Address(getBitFieldPointer(), ElementType, getAlignment());
  326. }
  327. llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
  328. const CGBitFieldInfo &getBitFieldInfo() const {
  329. assert(isBitField());
  330. return *BitFieldInfo;
  331. }
  332. // global register lvalue
  333. llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
  334. static LValue MakeAddr(Address address, QualType type, ASTContext &Context,
  335. LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
  336. Qualifiers qs = type.getQualifiers();
  337. qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
  338. LValue R;
  339. R.LVType = Simple;
  340. assert(address.getPointer()->getType()->isPointerTy());
  341. R.V = address.getPointer();
  342. R.ElementType = address.getElementType();
  343. R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
  344. return R;
  345. }
  346. static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
  347. QualType type, LValueBaseInfo BaseInfo,
  348. TBAAAccessInfo TBAAInfo) {
  349. LValue R;
  350. R.LVType = VectorElt;
  351. R.V = vecAddress.getPointer();
  352. R.ElementType = vecAddress.getElementType();
  353. R.VectorIdx = Idx;
  354. R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
  355. BaseInfo, TBAAInfo);
  356. return R;
  357. }
  358. static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
  359. QualType type, LValueBaseInfo BaseInfo,
  360. TBAAAccessInfo TBAAInfo) {
  361. LValue R;
  362. R.LVType = ExtVectorElt;
  363. R.V = vecAddress.getPointer();
  364. R.ElementType = vecAddress.getElementType();
  365. R.VectorElts = Elts;
  366. R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
  367. BaseInfo, TBAAInfo);
  368. return R;
  369. }
  370. /// Create a new object to represent a bit-field access.
  371. ///
  372. /// \param Addr - The base address of the bit-field sequence this
  373. /// bit-field refers to.
  374. /// \param Info - The information describing how to perform the bit-field
  375. /// access.
  376. static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
  377. QualType type, LValueBaseInfo BaseInfo,
  378. TBAAAccessInfo TBAAInfo) {
  379. LValue R;
  380. R.LVType = BitField;
  381. R.V = Addr.getPointer();
  382. R.ElementType = Addr.getElementType();
  383. R.BitFieldInfo = &Info;
  384. R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
  385. TBAAInfo);
  386. return R;
  387. }
  388. static LValue MakeGlobalReg(llvm::Value *V, CharUnits alignment,
  389. QualType type) {
  390. LValue R;
  391. R.LVType = GlobalReg;
  392. R.V = V;
  393. R.ElementType = nullptr;
  394. R.Initialize(type, type.getQualifiers(), alignment,
  395. LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
  396. return R;
  397. }
  398. static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx,
  399. QualType type, LValueBaseInfo BaseInfo,
  400. TBAAAccessInfo TBAAInfo) {
  401. LValue R;
  402. R.LVType = MatrixElt;
  403. R.V = matAddress.getPointer();
  404. R.ElementType = matAddress.getElementType();
  405. R.VectorIdx = Idx;
  406. R.Initialize(type, type.getQualifiers(), matAddress.getAlignment(),
  407. BaseInfo, TBAAInfo);
  408. return R;
  409. }
  410. RValue asAggregateRValue(CodeGenFunction &CGF) const {
  411. return RValue::getAggregate(getAddress(CGF), isVolatileQualified());
  412. }
  413. };
  414. /// An aggregate value slot.
  415. class AggValueSlot {
  416. /// The address.
  417. Address Addr;
  418. // Qualifiers
  419. Qualifiers Quals;
  420. /// DestructedFlag - This is set to true if some external code is
  421. /// responsible for setting up a destructor for the slot. Otherwise
  422. /// the code which constructs it should push the appropriate cleanup.
  423. bool DestructedFlag : 1;
  424. /// ObjCGCFlag - This is set to true if writing to the memory in the
  425. /// slot might require calling an appropriate Objective-C GC
  426. /// barrier. The exact interaction here is unnecessarily mysterious.
  427. bool ObjCGCFlag : 1;
  428. /// ZeroedFlag - This is set to true if the memory in the slot is
  429. /// known to be zero before the assignment into it. This means that
  430. /// zero fields don't need to be set.
  431. bool ZeroedFlag : 1;
  432. /// AliasedFlag - This is set to true if the slot might be aliased
  433. /// and it's not undefined behavior to access it through such an
  434. /// alias. Note that it's always undefined behavior to access a C++
  435. /// object that's under construction through an alias derived from
  436. /// outside the construction process.
  437. ///
  438. /// This flag controls whether calls that produce the aggregate
  439. /// value may be evaluated directly into the slot, or whether they
  440. /// must be evaluated into an unaliased temporary and then memcpy'ed
  441. /// over. Since it's invalid in general to memcpy a non-POD C++
  442. /// object, it's important that this flag never be set when
  443. /// evaluating an expression which constructs such an object.
  444. bool AliasedFlag : 1;
  445. /// This is set to true if the tail padding of this slot might overlap
  446. /// another object that may have already been initialized (and whose
  447. /// value must be preserved by this initialization). If so, we may only
  448. /// store up to the dsize of the type. Otherwise we can widen stores to
  449. /// the size of the type.
  450. bool OverlapFlag : 1;
  451. /// If is set to true, sanitizer checks are already generated for this address
  452. /// or not required. For instance, if this address represents an object
  453. /// created in 'new' expression, sanitizer checks for memory is made as a part
  454. /// of 'operator new' emission and object constructor should not generate
  455. /// them.
  456. bool SanitizerCheckedFlag : 1;
  457. AggValueSlot(Address Addr, Qualifiers Quals, bool DestructedFlag,
  458. bool ObjCGCFlag, bool ZeroedFlag, bool AliasedFlag,
  459. bool OverlapFlag, bool SanitizerCheckedFlag)
  460. : Addr(Addr), Quals(Quals), DestructedFlag(DestructedFlag),
  461. ObjCGCFlag(ObjCGCFlag), ZeroedFlag(ZeroedFlag),
  462. AliasedFlag(AliasedFlag), OverlapFlag(OverlapFlag),
  463. SanitizerCheckedFlag(SanitizerCheckedFlag) {}
  464. public:
  465. enum IsAliased_t { IsNotAliased, IsAliased };
  466. enum IsDestructed_t { IsNotDestructed, IsDestructed };
  467. enum IsZeroed_t { IsNotZeroed, IsZeroed };
  468. enum Overlap_t { DoesNotOverlap, MayOverlap };
  469. enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
  470. enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
  471. /// ignored - Returns an aggregate value slot indicating that the
  472. /// aggregate value is being ignored.
  473. static AggValueSlot ignored() {
  474. return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
  475. DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
  476. }
  477. /// forAddr - Make a slot for an aggregate value.
  478. ///
  479. /// \param quals - The qualifiers that dictate how the slot should
  480. /// be initialied. Only 'volatile' and the Objective-C lifetime
  481. /// qualifiers matter.
  482. ///
  483. /// \param isDestructed - true if something else is responsible
  484. /// for calling destructors on this object
  485. /// \param needsGC - true if the slot is potentially located
  486. /// somewhere that ObjC GC calls should be emitted for
  487. static AggValueSlot forAddr(Address addr,
  488. Qualifiers quals,
  489. IsDestructed_t isDestructed,
  490. NeedsGCBarriers_t needsGC,
  491. IsAliased_t isAliased,
  492. Overlap_t mayOverlap,
  493. IsZeroed_t isZeroed = IsNotZeroed,
  494. IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
  495. return AggValueSlot(addr, quals, isDestructed, needsGC, isZeroed, isAliased,
  496. mayOverlap, isChecked);
  497. }
  498. static AggValueSlot
  499. forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed,
  500. NeedsGCBarriers_t needsGC, IsAliased_t isAliased,
  501. Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed,
  502. IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
  503. return forAddr(LV.getAddress(CGF), LV.getQuals(), isDestructed, needsGC,
  504. isAliased, mayOverlap, isZeroed, isChecked);
  505. }
  506. IsDestructed_t isExternallyDestructed() const {
  507. return IsDestructed_t(DestructedFlag);
  508. }
  509. void setExternallyDestructed(bool destructed = true) {
  510. DestructedFlag = destructed;
  511. }
  512. Qualifiers getQualifiers() const { return Quals; }
  513. bool isVolatile() const {
  514. return Quals.hasVolatile();
  515. }
  516. void setVolatile(bool flag) {
  517. if (flag)
  518. Quals.addVolatile();
  519. else
  520. Quals.removeVolatile();
  521. }
  522. Qualifiers::ObjCLifetime getObjCLifetime() const {
  523. return Quals.getObjCLifetime();
  524. }
  525. NeedsGCBarriers_t requiresGCollection() const {
  526. return NeedsGCBarriers_t(ObjCGCFlag);
  527. }
  528. llvm::Value *getPointer() const {
  529. return Addr.getPointer();
  530. }
  531. Address getAddress() const {
  532. return Addr;
  533. }
  534. bool isIgnored() const {
  535. return !Addr.isValid();
  536. }
  537. CharUnits getAlignment() const {
  538. return Addr.getAlignment();
  539. }
  540. IsAliased_t isPotentiallyAliased() const {
  541. return IsAliased_t(AliasedFlag);
  542. }
  543. Overlap_t mayOverlap() const {
  544. return Overlap_t(OverlapFlag);
  545. }
  546. bool isSanitizerChecked() const {
  547. return SanitizerCheckedFlag;
  548. }
  549. RValue asRValue() const {
  550. if (isIgnored()) {
  551. return RValue::getIgnored();
  552. } else {
  553. return RValue::getAggregate(getAddress(), isVolatile());
  554. }
  555. }
  556. void setZeroed(bool V = true) { ZeroedFlag = V; }
  557. IsZeroed_t isZeroed() const {
  558. return IsZeroed_t(ZeroedFlag);
  559. }
  560. /// Get the preferred size to use when storing a value to this slot. This
  561. /// is the type size unless that might overlap another object, in which
  562. /// case it's the dsize.
  563. CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
  564. return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).Width
  565. : Ctx.getTypeSizeInChars(Type);
  566. }
  567. };
  568. } // end namespace CodeGen
  569. } // end namespace clang
  570. #endif