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