CGValue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // These classes implement wrappers around llvm::Value in order to
  11. // fully represent the range of values for C L- and R- values.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
  15. #define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/AST/Type.h"
  19. #include "llvm/IR/Value.h"
  20. #include "llvm/IR/Type.h"
  21. namespace llvm {
  22. class Constant;
  23. class MDNode;
  24. }
  25. namespace clang {
  26. namespace CodeGen {
  27. class AggValueSlot;
  28. struct CGBitFieldInfo;
  29. /// RValue - This trivial value class is used to represent the result of an
  30. /// expression that is evaluated. It can be one of three things: either a
  31. /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
  32. /// address of an aggregate value in memory.
  33. class RValue {
  34. enum Flavor { Scalar, Complex, Aggregate };
  35. // Stores first value and flavor.
  36. llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
  37. // Stores second value and volatility.
  38. llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
  39. public:
  40. bool isScalar() const { return V1.getInt() == Scalar; }
  41. bool isComplex() const { return V1.getInt() == Complex; }
  42. bool isAggregate() const { return V1.getInt() == Aggregate; }
  43. bool isVolatileQualified() const { return V2.getInt(); }
  44. /// getScalarVal() - Return the Value* of this scalar value.
  45. llvm::Value *getScalarVal() const {
  46. assert(isScalar() && "Not a scalar!");
  47. return V1.getPointer();
  48. }
  49. /// getComplexVal - Return the real/imag components of this complex value.
  50. ///
  51. std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
  52. return std::make_pair(V1.getPointer(), V2.getPointer());
  53. }
  54. /// getAggregateAddr() - Return the Value* of the address of the aggregate.
  55. llvm::Value *getAggregateAddr() const {
  56. assert(isAggregate() && "Not an aggregate!");
  57. return V1.getPointer();
  58. }
  59. static RValue get(llvm::Value *V) {
  60. RValue ER;
  61. ER.V1.setPointer(V);
  62. ER.V1.setInt(Scalar);
  63. ER.V2.setInt(false);
  64. return ER;
  65. }
  66. static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
  67. RValue ER;
  68. ER.V1.setPointer(V1);
  69. ER.V2.setPointer(V2);
  70. ER.V1.setInt(Complex);
  71. ER.V2.setInt(false);
  72. return ER;
  73. }
  74. static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
  75. return getComplex(C.first, C.second);
  76. }
  77. // FIXME: Aggregate rvalues need to retain information about whether they are
  78. // volatile or not. Remove default to find all places that probably get this
  79. // wrong.
  80. static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
  81. RValue ER;
  82. ER.V1.setPointer(V);
  83. ER.V1.setInt(Aggregate);
  84. ER.V2.setInt(Volatile);
  85. return ER;
  86. }
  87. };
  88. /// Does an ARC strong l-value have precise lifetime?
  89. enum ARCPreciseLifetime_t {
  90. ARCImpreciseLifetime, ARCPreciseLifetime
  91. };
  92. /// LValue - This represents an lvalue references. Because C/C++ allow
  93. /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
  94. /// bitrange.
  95. class LValue {
  96. enum {
  97. Simple, // This is a normal l-value, use getAddress().
  98. VectorElt, // This is a vector element l-value (V[i]), use getVector*
  99. BitField, // This is a bitfield l-value, use getBitfield*.
  100. ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
  101. ExtMatrixElt, // This is an extended matrix subset, use getExtMatrixComp - HLSL Change
  102. GlobalReg // This is a register l-value, use getGlobalReg()
  103. } LVType;
  104. llvm::Value *V;
  105. union {
  106. // Index into a vector subscript: V[i]
  107. llvm::Value *VectorIdx;
  108. // ExtVector element subset: V.xyx
  109. llvm::Constant *VectorElts;
  110. // BitField start bit and size
  111. const CGBitFieldInfo *BitFieldInfo;
  112. };
  113. QualType Type;
  114. // 'const' is unused here
  115. Qualifiers Quals;
  116. // The alignment to use when accessing this lvalue. (For vector elements,
  117. // this is the alignment of the whole vector.)
  118. int64_t Alignment;
  119. // objective-c's ivar
  120. bool Ivar:1;
  121. // objective-c's ivar is an array
  122. bool ObjIsArray:1;
  123. // LValue is non-gc'able for any reason, including being a parameter or local
  124. // variable.
  125. bool NonGC: 1;
  126. // Lvalue is a global reference of an objective-c object
  127. bool GlobalObjCRef : 1;
  128. // Lvalue is a thread local reference
  129. bool ThreadLocalRef : 1;
  130. // Lvalue has ARC imprecise lifetime. We store this inverted to try
  131. // to make the default bitfield pattern all-zeroes.
  132. bool ImpreciseLifetime : 1;
  133. Expr *BaseIvarExp;
  134. /// Used by struct-path-aware TBAA.
  135. QualType TBAABaseType;
  136. /// Offset relative to the base type.
  137. uint64_t TBAAOffset;
  138. /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
  139. llvm::MDNode *TBAAInfo;
  140. private:
  141. void Initialize(QualType Type, Qualifiers Quals,
  142. CharUnits Alignment,
  143. llvm::MDNode *TBAAInfo = nullptr) {
  144. this->Type = Type;
  145. this->Quals = Quals;
  146. this->Alignment = Alignment.getQuantity();
  147. assert(this->Alignment == Alignment.getQuantity() &&
  148. "Alignment exceeds allowed max!");
  149. // Initialize Objective-C flags.
  150. this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
  151. this->ImpreciseLifetime = false;
  152. this->ThreadLocalRef = false;
  153. this->BaseIvarExp = nullptr;
  154. // Initialize fields for TBAA.
  155. this->TBAABaseType = Type;
  156. this->TBAAOffset = 0;
  157. this->TBAAInfo = TBAAInfo;
  158. }
  159. public:
  160. bool isSimple() const { return LVType == Simple; }
  161. bool isVectorElt() const { return LVType == VectorElt; }
  162. bool isBitField() const { return LVType == BitField; }
  163. bool isExtVectorElt() const { return LVType == ExtVectorElt; }
  164. bool isGlobalReg() const { return LVType == GlobalReg; }
  165. bool isExtMatrixElt() const { return LVType == ExtMatrixElt; } // HLSL Change
  166. bool isVolatileQualified() const { return Quals.hasVolatile(); }
  167. bool isRestrictQualified() const { return Quals.hasRestrict(); }
  168. unsigned getVRQualifiers() const {
  169. return Quals.getCVRQualifiers() & ~Qualifiers::Const;
  170. }
  171. QualType getType() const { return Type; }
  172. Qualifiers::ObjCLifetime getObjCLifetime() const {
  173. return Quals.getObjCLifetime();
  174. }
  175. bool isObjCIvar() const { return Ivar; }
  176. void setObjCIvar(bool Value) { Ivar = Value; }
  177. bool isObjCArray() const { return ObjIsArray; }
  178. void setObjCArray(bool Value) { ObjIsArray = Value; }
  179. bool isNonGC () const { return NonGC; }
  180. void setNonGC(bool Value) { NonGC = Value; }
  181. bool isGlobalObjCRef() const { return GlobalObjCRef; }
  182. void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
  183. bool isThreadLocalRef() const { return ThreadLocalRef; }
  184. void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
  185. ARCPreciseLifetime_t isARCPreciseLifetime() const {
  186. return ARCPreciseLifetime_t(!ImpreciseLifetime);
  187. }
  188. void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
  189. ImpreciseLifetime = (value == ARCImpreciseLifetime);
  190. }
  191. bool isObjCWeak() const {
  192. return Quals.getObjCGCAttr() == Qualifiers::Weak;
  193. }
  194. bool isObjCStrong() const {
  195. return Quals.getObjCGCAttr() == Qualifiers::Strong;
  196. }
  197. bool isVolatile() const {
  198. return Quals.hasVolatile();
  199. }
  200. Expr *getBaseIvarExp() const { return BaseIvarExp; }
  201. void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
  202. QualType getTBAABaseType() const { return TBAABaseType; }
  203. void setTBAABaseType(QualType T) { TBAABaseType = T; }
  204. uint64_t getTBAAOffset() const { return TBAAOffset; }
  205. void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
  206. llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
  207. void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
  208. const Qualifiers &getQuals() const { return Quals; }
  209. Qualifiers &getQuals() { return Quals; }
  210. unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
  211. CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
  212. void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
  213. // simple lvalue
  214. llvm::Value *getAddress() const { assert(isSimple()); return V; }
  215. void setAddress(llvm::Value *address) {
  216. assert(isSimple());
  217. V = address;
  218. }
  219. // vector elt lvalue
  220. llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
  221. llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
  222. // extended vector elements.
  223. llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
  224. llvm::Constant *getExtVectorElts() const {
  225. assert(isExtVectorElt());
  226. return VectorElts;
  227. }
  228. // bitfield lvalue
  229. llvm::Value *getBitFieldAddr() const {
  230. assert(isBitField());
  231. return V;
  232. }
  233. const CGBitFieldInfo &getBitFieldInfo() const {
  234. assert(isBitField());
  235. return *BitFieldInfo;
  236. }
  237. // global register lvalue
  238. llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
  239. static LValue MakeAddr(llvm::Value *address, QualType type,
  240. CharUnits alignment, ASTContext &Context,
  241. llvm::MDNode *TBAAInfo = nullptr) {
  242. Qualifiers qs = type.getQualifiers();
  243. qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
  244. LValue R;
  245. R.LVType = Simple;
  246. assert(address->getType()->isPointerTy());
  247. R.V = address;
  248. R.Initialize(type, qs, alignment, TBAAInfo);
  249. return R;
  250. }
  251. static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
  252. QualType type, CharUnits Alignment) {
  253. LValue R;
  254. R.LVType = VectorElt;
  255. R.V = Vec;
  256. R.VectorIdx = Idx;
  257. R.Initialize(type, type.getQualifiers(), Alignment);
  258. return R;
  259. }
  260. static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
  261. QualType type, CharUnits Alignment) {
  262. LValue R;
  263. R.LVType = ExtVectorElt;
  264. R.V = Vec;
  265. R.VectorElts = Elts;
  266. R.Initialize(type, type.getQualifiers(), Alignment);
  267. return R;
  268. }
  269. /// \brief Create a new object to represent a bit-field access.
  270. ///
  271. /// \param Addr - The base address of the bit-field sequence this
  272. /// bit-field refers to.
  273. /// \param Info - The information describing how to perform the bit-field
  274. /// access.
  275. static LValue MakeBitfield(llvm::Value *Addr,
  276. const CGBitFieldInfo &Info,
  277. QualType type, CharUnits Alignment) {
  278. LValue R;
  279. R.LVType = BitField;
  280. R.V = Addr;
  281. R.BitFieldInfo = &Info;
  282. R.Initialize(type, type.getQualifiers(), Alignment);
  283. return R;
  284. }
  285. static LValue MakeGlobalReg(llvm::Value *Reg,
  286. QualType type,
  287. CharUnits Alignment) {
  288. LValue R;
  289. R.LVType = GlobalReg;
  290. R.V = Reg;
  291. R.Initialize(type, type.getQualifiers(), Alignment);
  292. return R;
  293. }
  294. RValue asAggregateRValue() const {
  295. // FIMXE: Alignment
  296. return RValue::getAggregate(getAddress(), isVolatileQualified());
  297. }
  298. };
  299. /// An aggregate value slot.
  300. class AggValueSlot {
  301. /// The address.
  302. llvm::Value *Addr;
  303. // Qualifiers
  304. Qualifiers Quals;
  305. unsigned short Alignment;
  306. /// DestructedFlag - This is set to true if some external code is
  307. /// responsible for setting up a destructor for the slot. Otherwise
  308. /// the code which constructs it should push the appropriate cleanup.
  309. bool DestructedFlag : 1;
  310. /// ObjCGCFlag - This is set to true if writing to the memory in the
  311. /// slot might require calling an appropriate Objective-C GC
  312. /// barrier. The exact interaction here is unnecessarily mysterious.
  313. bool ObjCGCFlag : 1;
  314. /// ZeroedFlag - This is set to true if the memory in the slot is
  315. /// known to be zero before the assignment into it. This means that
  316. /// zero fields don't need to be set.
  317. bool ZeroedFlag : 1;
  318. /// AliasedFlag - This is set to true if the slot might be aliased
  319. /// and it's not undefined behavior to access it through such an
  320. /// alias. Note that it's always undefined behavior to access a C++
  321. /// object that's under construction through an alias derived from
  322. /// outside the construction process.
  323. ///
  324. /// This flag controls whether calls that produce the aggregate
  325. /// value may be evaluated directly into the slot, or whether they
  326. /// must be evaluated into an unaliased temporary and then memcpy'ed
  327. /// over. Since it's invalid in general to memcpy a non-POD C++
  328. /// object, it's important that this flag never be set when
  329. /// evaluating an expression which constructs such an object.
  330. bool AliasedFlag : 1;
  331. public:
  332. enum IsAliased_t { IsNotAliased, IsAliased };
  333. enum IsDestructed_t { IsNotDestructed, IsDestructed };
  334. enum IsZeroed_t { IsNotZeroed, IsZeroed };
  335. enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
  336. /// ignored - Returns an aggregate value slot indicating that the
  337. /// aggregate value is being ignored.
  338. static AggValueSlot ignored() {
  339. return forAddr(nullptr, CharUnits(), Qualifiers(), IsNotDestructed,
  340. DoesNotNeedGCBarriers, IsNotAliased);
  341. }
  342. /// forAddr - Make a slot for an aggregate value.
  343. ///
  344. /// \param quals - The qualifiers that dictate how the slot should
  345. /// be initialied. Only 'volatile' and the Objective-C lifetime
  346. /// qualifiers matter.
  347. ///
  348. /// \param isDestructed - true if something else is responsible
  349. /// for calling destructors on this object
  350. /// \param needsGC - true if the slot is potentially located
  351. /// somewhere that ObjC GC calls should be emitted for
  352. static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
  353. Qualifiers quals,
  354. IsDestructed_t isDestructed,
  355. NeedsGCBarriers_t needsGC,
  356. IsAliased_t isAliased,
  357. IsZeroed_t isZeroed = IsNotZeroed) {
  358. AggValueSlot AV;
  359. AV.Addr = addr;
  360. AV.Alignment = align.getQuantity();
  361. AV.Quals = quals;
  362. AV.DestructedFlag = isDestructed;
  363. AV.ObjCGCFlag = needsGC;
  364. AV.ZeroedFlag = isZeroed;
  365. AV.AliasedFlag = isAliased;
  366. return AV;
  367. }
  368. static AggValueSlot forLValue(const LValue &LV,
  369. IsDestructed_t isDestructed,
  370. NeedsGCBarriers_t needsGC,
  371. IsAliased_t isAliased,
  372. IsZeroed_t isZeroed = IsNotZeroed) {
  373. return forAddr(LV.getAddress(), LV.getAlignment(),
  374. LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
  375. }
  376. IsDestructed_t isExternallyDestructed() const {
  377. return IsDestructed_t(DestructedFlag);
  378. }
  379. void setExternallyDestructed(bool destructed = true) {
  380. DestructedFlag = destructed;
  381. }
  382. Qualifiers getQualifiers() const { return Quals; }
  383. bool isVolatile() const {
  384. return Quals.hasVolatile();
  385. }
  386. void setVolatile(bool flag) {
  387. Quals.setVolatile(flag);
  388. }
  389. Qualifiers::ObjCLifetime getObjCLifetime() const {
  390. return Quals.getObjCLifetime();
  391. }
  392. NeedsGCBarriers_t requiresGCollection() const {
  393. return NeedsGCBarriers_t(ObjCGCFlag);
  394. }
  395. llvm::Value *getAddr() const {
  396. return Addr;
  397. }
  398. bool isIgnored() const {
  399. return Addr == nullptr;
  400. }
  401. CharUnits getAlignment() const {
  402. return CharUnits::fromQuantity(Alignment);
  403. }
  404. IsAliased_t isPotentiallyAliased() const {
  405. return IsAliased_t(AliasedFlag);
  406. }
  407. // FIXME: Alignment?
  408. RValue asRValue() const {
  409. return RValue::getAggregate(getAddr(), isVolatile());
  410. }
  411. void setZeroed(bool V = true) { ZeroedFlag = V; }
  412. IsZeroed_t isZeroed() const {
  413. return IsZeroed_t(ZeroedFlag);
  414. }
  415. };
  416. } // end namespace CodeGen
  417. } // end namespace clang
  418. #endif