clang API Documentation

CGValue.h
Go to the documentation of this file.
00001 //===-- CGValue.h - LLVM CodeGen wrappers for llvm::Value* ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // These classes implement wrappers around llvm::Value in order to
00011 // fully represent the range of values for C L- and R- values.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
00016 #define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
00017 
00018 #include "clang/AST/ASTContext.h"
00019 #include "clang/AST/CharUnits.h"
00020 #include "clang/AST/Type.h"
00021 #include "llvm/IR/Value.h"
00022 
00023 namespace llvm {
00024   class Constant;
00025   class MDNode;
00026 }
00027 
00028 namespace clang {
00029 namespace CodeGen {
00030   class AggValueSlot;
00031   struct CGBitFieldInfo;
00032 
00033 /// RValue - This trivial value class is used to represent the result of an
00034 /// expression that is evaluated.  It can be one of three things: either a
00035 /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
00036 /// address of an aggregate value in memory.
00037 class RValue {
00038   enum Flavor { Scalar, Complex, Aggregate };
00039 
00040   // Stores first value and flavor.
00041   llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
00042   // Stores second value and volatility.
00043   llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
00044 
00045 public:
00046   bool isScalar() const { return V1.getInt() == Scalar; }
00047   bool isComplex() const { return V1.getInt() == Complex; }
00048   bool isAggregate() const { return V1.getInt() == Aggregate; }
00049 
00050   bool isVolatileQualified() const { return V2.getInt(); }
00051 
00052   /// getScalarVal() - Return the Value* of this scalar value.
00053   llvm::Value *getScalarVal() const {
00054     assert(isScalar() && "Not a scalar!");
00055     return V1.getPointer();
00056   }
00057 
00058   /// getComplexVal - Return the real/imag components of this complex value.
00059   ///
00060   std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
00061     return std::make_pair(V1.getPointer(), V2.getPointer());
00062   }
00063 
00064   /// getAggregateAddr() - Return the Value* of the address of the aggregate.
00065   llvm::Value *getAggregateAddr() const {
00066     assert(isAggregate() && "Not an aggregate!");
00067     return V1.getPointer();
00068   }
00069 
00070   static RValue get(llvm::Value *V) {
00071     RValue ER;
00072     ER.V1.setPointer(V);
00073     ER.V1.setInt(Scalar);
00074     ER.V2.setInt(false);
00075     return ER;
00076   }
00077   static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
00078     RValue ER;
00079     ER.V1.setPointer(V1);
00080     ER.V2.setPointer(V2);
00081     ER.V1.setInt(Complex);
00082     ER.V2.setInt(false);
00083     return ER;
00084   }
00085   static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
00086     return getComplex(C.first, C.second);
00087   }
00088   // FIXME: Aggregate rvalues need to retain information about whether they are
00089   // volatile or not.  Remove default to find all places that probably get this
00090   // wrong.
00091   static RValue getAggregate(llvm::Value *V, bool Volatile = false) {
00092     RValue ER;
00093     ER.V1.setPointer(V);
00094     ER.V1.setInt(Aggregate);
00095     ER.V2.setInt(Volatile);
00096     return ER;
00097   }
00098 };
00099 
00100 /// Does an ARC strong l-value have precise lifetime?
00101 enum ARCPreciseLifetime_t {
00102   ARCImpreciseLifetime, ARCPreciseLifetime
00103 };
00104 
00105 /// LValue - This represents an lvalue references.  Because C/C++ allow
00106 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
00107 /// bitrange.
00108 class LValue {
00109   enum {
00110     Simple,       // This is a normal l-value, use getAddress().
00111     VectorElt,    // This is a vector element l-value (V[i]), use getVector*
00112     BitField,     // This is a bitfield l-value, use getBitfield*.
00113     ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
00114     GlobalReg     // This is a register l-value, use getGlobalReg()
00115   } LVType;
00116 
00117   llvm::Value *V;
00118 
00119   union {
00120     // Index into a vector subscript: V[i]
00121     llvm::Value *VectorIdx;
00122 
00123     // ExtVector element subset: V.xyx
00124     llvm::Constant *VectorElts;
00125 
00126     // BitField start bit and size
00127     const CGBitFieldInfo *BitFieldInfo;
00128   };
00129 
00130   QualType Type;
00131 
00132   // 'const' is unused here
00133   Qualifiers Quals;
00134 
00135   // The alignment to use when accessing this lvalue.  (For vector elements,
00136   // this is the alignment of the whole vector.)
00137   int64_t Alignment;
00138 
00139   // objective-c's ivar
00140   bool Ivar:1;
00141   
00142   // objective-c's ivar is an array
00143   bool ObjIsArray:1;
00144 
00145   // LValue is non-gc'able for any reason, including being a parameter or local
00146   // variable.
00147   bool NonGC: 1;
00148 
00149   // Lvalue is a global reference of an objective-c object
00150   bool GlobalObjCRef : 1;
00151   
00152   // Lvalue is a thread local reference
00153   bool ThreadLocalRef : 1;
00154 
00155   // Lvalue has ARC imprecise lifetime.  We store this inverted to try
00156   // to make the default bitfield pattern all-zeroes.
00157   bool ImpreciseLifetime : 1;
00158 
00159   Expr *BaseIvarExp;
00160 
00161   /// Used by struct-path-aware TBAA.
00162   QualType TBAABaseType;
00163   /// Offset relative to the base type.
00164   uint64_t TBAAOffset;
00165 
00166   /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
00167   llvm::MDNode *TBAAInfo;
00168 
00169 private:
00170   void Initialize(QualType Type, Qualifiers Quals,
00171                   CharUnits Alignment,
00172                   llvm::MDNode *TBAAInfo = nullptr) {
00173     this->Type = Type;
00174     this->Quals = Quals;
00175     this->Alignment = Alignment.getQuantity();
00176     assert(this->Alignment == Alignment.getQuantity() &&
00177            "Alignment exceeds allowed max!");
00178 
00179     // Initialize Objective-C flags.
00180     this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
00181     this->ImpreciseLifetime = false;
00182     this->ThreadLocalRef = false;
00183     this->BaseIvarExp = nullptr;
00184 
00185     // Initialize fields for TBAA.
00186     this->TBAABaseType = Type;
00187     this->TBAAOffset = 0;
00188     this->TBAAInfo = TBAAInfo;
00189   }
00190 
00191 public:
00192   bool isSimple() const { return LVType == Simple; }
00193   bool isVectorElt() const { return LVType == VectorElt; }
00194   bool isBitField() const { return LVType == BitField; }
00195   bool isExtVectorElt() const { return LVType == ExtVectorElt; }
00196   bool isGlobalReg() const { return LVType == GlobalReg; }
00197 
00198   bool isVolatileQualified() const { return Quals.hasVolatile(); }
00199   bool isRestrictQualified() const { return Quals.hasRestrict(); }
00200   unsigned getVRQualifiers() const {
00201     return Quals.getCVRQualifiers() & ~Qualifiers::Const;
00202   }
00203 
00204   QualType getType() const { return Type; }
00205 
00206   Qualifiers::ObjCLifetime getObjCLifetime() const {
00207     return Quals.getObjCLifetime();
00208   }
00209 
00210   bool isObjCIvar() const { return Ivar; }
00211   void setObjCIvar(bool Value) { Ivar = Value; }
00212 
00213   bool isObjCArray() const { return ObjIsArray; }
00214   void setObjCArray(bool Value) { ObjIsArray = Value; }
00215 
00216   bool isNonGC () const { return NonGC; }
00217   void setNonGC(bool Value) { NonGC = Value; }
00218 
00219   bool isGlobalObjCRef() const { return GlobalObjCRef; }
00220   void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
00221 
00222   bool isThreadLocalRef() const { return ThreadLocalRef; }
00223   void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
00224 
00225   ARCPreciseLifetime_t isARCPreciseLifetime() const {
00226     return ARCPreciseLifetime_t(!ImpreciseLifetime);
00227   }
00228   void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
00229     ImpreciseLifetime = (value == ARCImpreciseLifetime);
00230   }
00231 
00232   bool isObjCWeak() const {
00233     return Quals.getObjCGCAttr() == Qualifiers::Weak;
00234   }
00235   bool isObjCStrong() const {
00236     return Quals.getObjCGCAttr() == Qualifiers::Strong;
00237   }
00238 
00239   bool isVolatile() const {
00240     return Quals.hasVolatile();
00241   }
00242   
00243   Expr *getBaseIvarExp() const { return BaseIvarExp; }
00244   void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
00245 
00246   QualType getTBAABaseType() const { return TBAABaseType; }
00247   void setTBAABaseType(QualType T) { TBAABaseType = T; }
00248 
00249   uint64_t getTBAAOffset() const { return TBAAOffset; }
00250   void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
00251 
00252   llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
00253   void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
00254 
00255   const Qualifiers &getQuals() const { return Quals; }
00256   Qualifiers &getQuals() { return Quals; }
00257 
00258   unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
00259 
00260   CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
00261   void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
00262 
00263   // simple lvalue
00264   llvm::Value *getAddress() const { assert(isSimple()); return V; }
00265   void setAddress(llvm::Value *address) {
00266     assert(isSimple());
00267     V = address;
00268   }
00269 
00270   // vector elt lvalue
00271   llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
00272   llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
00273 
00274   // extended vector elements.
00275   llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
00276   llvm::Constant *getExtVectorElts() const {
00277     assert(isExtVectorElt());
00278     return VectorElts;
00279   }
00280 
00281   // bitfield lvalue
00282   llvm::Value *getBitFieldAddr() const {
00283     assert(isBitField());
00284     return V;
00285   }
00286   const CGBitFieldInfo &getBitFieldInfo() const {
00287     assert(isBitField());
00288     return *BitFieldInfo;
00289   }
00290 
00291   // global register lvalue
00292   llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
00293 
00294   static LValue MakeAddr(llvm::Value *address, QualType type,
00295                          CharUnits alignment, ASTContext &Context,
00296                          llvm::MDNode *TBAAInfo = nullptr) {
00297     Qualifiers qs = type.getQualifiers();
00298     qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
00299 
00300     LValue R;
00301     R.LVType = Simple;
00302     R.V = address;
00303     R.Initialize(type, qs, alignment, TBAAInfo);
00304     return R;
00305   }
00306 
00307   static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
00308                               QualType type, CharUnits Alignment) {
00309     LValue R;
00310     R.LVType = VectorElt;
00311     R.V = Vec;
00312     R.VectorIdx = Idx;
00313     R.Initialize(type, type.getQualifiers(), Alignment);
00314     return R;
00315   }
00316 
00317   static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
00318                                  QualType type, CharUnits Alignment) {
00319     LValue R;
00320     R.LVType = ExtVectorElt;
00321     R.V = Vec;
00322     R.VectorElts = Elts;
00323     R.Initialize(type, type.getQualifiers(), Alignment);
00324     return R;
00325   }
00326 
00327   /// \brief Create a new object to represent a bit-field access.
00328   ///
00329   /// \param Addr - The base address of the bit-field sequence this
00330   /// bit-field refers to.
00331   /// \param Info - The information describing how to perform the bit-field
00332   /// access.
00333   static LValue MakeBitfield(llvm::Value *Addr,
00334                              const CGBitFieldInfo &Info,
00335                              QualType type, CharUnits Alignment) {
00336     LValue R;
00337     R.LVType = BitField;
00338     R.V = Addr;
00339     R.BitFieldInfo = &Info;
00340     R.Initialize(type, type.getQualifiers(), Alignment);
00341     return R;
00342   }
00343 
00344   static LValue MakeGlobalReg(llvm::Value *Reg,
00345                               QualType type,
00346                               CharUnits Alignment) {
00347     LValue R;
00348     R.LVType = GlobalReg;
00349     R.V = Reg;
00350     R.Initialize(type, type.getQualifiers(), Alignment);
00351     return R;
00352   }
00353 
00354   RValue asAggregateRValue() const {
00355     // FIMXE: Alignment
00356     return RValue::getAggregate(getAddress(), isVolatileQualified());
00357   }
00358 };
00359 
00360 /// An aggregate value slot.
00361 class AggValueSlot {
00362   /// The address.
00363   llvm::Value *Addr;
00364 
00365   // Qualifiers
00366   Qualifiers Quals;
00367 
00368   unsigned short Alignment;
00369 
00370   /// DestructedFlag - This is set to true if some external code is
00371   /// responsible for setting up a destructor for the slot.  Otherwise
00372   /// the code which constructs it should push the appropriate cleanup.
00373   bool DestructedFlag : 1;
00374 
00375   /// ObjCGCFlag - This is set to true if writing to the memory in the
00376   /// slot might require calling an appropriate Objective-C GC
00377   /// barrier.  The exact interaction here is unnecessarily mysterious.
00378   bool ObjCGCFlag : 1;
00379   
00380   /// ZeroedFlag - This is set to true if the memory in the slot is
00381   /// known to be zero before the assignment into it.  This means that
00382   /// zero fields don't need to be set.
00383   bool ZeroedFlag : 1;
00384 
00385   /// AliasedFlag - This is set to true if the slot might be aliased
00386   /// and it's not undefined behavior to access it through such an
00387   /// alias.  Note that it's always undefined behavior to access a C++
00388   /// object that's under construction through an alias derived from
00389   /// outside the construction process.
00390   ///
00391   /// This flag controls whether calls that produce the aggregate
00392   /// value may be evaluated directly into the slot, or whether they
00393   /// must be evaluated into an unaliased temporary and then memcpy'ed
00394   /// over.  Since it's invalid in general to memcpy a non-POD C++
00395   /// object, it's important that this flag never be set when
00396   /// evaluating an expression which constructs such an object.
00397   bool AliasedFlag : 1;
00398 
00399 public:
00400   enum IsAliased_t { IsNotAliased, IsAliased };
00401   enum IsDestructed_t { IsNotDestructed, IsDestructed };
00402   enum IsZeroed_t { IsNotZeroed, IsZeroed };
00403   enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
00404 
00405   /// ignored - Returns an aggregate value slot indicating that the
00406   /// aggregate value is being ignored.
00407   static AggValueSlot ignored() {
00408     return forAddr(nullptr, CharUnits(), Qualifiers(), IsNotDestructed,
00409                    DoesNotNeedGCBarriers, IsNotAliased);
00410   }
00411 
00412   /// forAddr - Make a slot for an aggregate value.
00413   ///
00414   /// \param quals - The qualifiers that dictate how the slot should
00415   /// be initialied. Only 'volatile' and the Objective-C lifetime
00416   /// qualifiers matter.
00417   ///
00418   /// \param isDestructed - true if something else is responsible
00419   ///   for calling destructors on this object
00420   /// \param needsGC - true if the slot is potentially located
00421   ///   somewhere that ObjC GC calls should be emitted for
00422   static AggValueSlot forAddr(llvm::Value *addr, CharUnits align,
00423                               Qualifiers quals,
00424                               IsDestructed_t isDestructed,
00425                               NeedsGCBarriers_t needsGC,
00426                               IsAliased_t isAliased,
00427                               IsZeroed_t isZeroed = IsNotZeroed) {
00428     AggValueSlot AV;
00429     AV.Addr = addr;
00430     AV.Alignment = align.getQuantity();
00431     AV.Quals = quals;
00432     AV.DestructedFlag = isDestructed;
00433     AV.ObjCGCFlag = needsGC;
00434     AV.ZeroedFlag = isZeroed;
00435     AV.AliasedFlag = isAliased;
00436     return AV;
00437   }
00438 
00439   static AggValueSlot forLValue(const LValue &LV,
00440                                 IsDestructed_t isDestructed,
00441                                 NeedsGCBarriers_t needsGC,
00442                                 IsAliased_t isAliased,
00443                                 IsZeroed_t isZeroed = IsNotZeroed) {
00444     return forAddr(LV.getAddress(), LV.getAlignment(),
00445                    LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
00446   }
00447 
00448   IsDestructed_t isExternallyDestructed() const {
00449     return IsDestructed_t(DestructedFlag);
00450   }
00451   void setExternallyDestructed(bool destructed = true) {
00452     DestructedFlag = destructed;
00453   }
00454 
00455   Qualifiers getQualifiers() const { return Quals; }
00456 
00457   bool isVolatile() const {
00458     return Quals.hasVolatile();
00459   }
00460 
00461   void setVolatile(bool flag) {
00462     Quals.setVolatile(flag);
00463   }
00464   
00465   Qualifiers::ObjCLifetime getObjCLifetime() const {
00466     return Quals.getObjCLifetime();
00467   }
00468 
00469   NeedsGCBarriers_t requiresGCollection() const {
00470     return NeedsGCBarriers_t(ObjCGCFlag);
00471   }
00472   
00473   llvm::Value *getAddr() const {
00474     return Addr;
00475   }
00476 
00477   bool isIgnored() const {
00478     return Addr == nullptr;
00479   }
00480 
00481   CharUnits getAlignment() const {
00482     return CharUnits::fromQuantity(Alignment);
00483   }
00484 
00485   IsAliased_t isPotentiallyAliased() const {
00486     return IsAliased_t(AliasedFlag);
00487   }
00488 
00489   // FIXME: Alignment?
00490   RValue asRValue() const {
00491     return RValue::getAggregate(getAddr(), isVolatile());
00492   }
00493 
00494   void setZeroed(bool V = true) { ZeroedFlag = V; }
00495   IsZeroed_t isZeroed() const {
00496     return IsZeroed_t(ZeroedFlag);
00497   }
00498 };
00499 
00500 }  // end namespace CodeGen
00501 }  // end namespace clang
00502 
00503 #endif