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