CGValue.h 14 KB

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