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