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/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 } LVType; 115 116 llvm::Value *V; 117 118 union { 119 // Index into a vector subscript: V[i] 120 llvm::Value *VectorIdx; 121 122 // ExtVector element subset: V.xyx 123 llvm::Constant *VectorElts; 124 125 // BitField start bit and size 126 const CGBitFieldInfo *BitFieldInfo; 127 }; 128 129 QualType Type; 130 131 // 'const' is unused here 132 Qualifiers Quals; 133 134 // The alignment to use when accessing this lvalue. (For vector elements, 135 // this is the alignment of the whole vector.) 136 int64_t Alignment; 137 138 // objective-c's ivar 139 bool Ivar:1; 140 141 // objective-c's ivar is an array 142 bool ObjIsArray:1; 143 144 // LValue is non-gc'able for any reason, including being a parameter or local 145 // variable. 146 bool NonGC: 1; 147 148 // Lvalue is a global reference of an objective-c object 149 bool GlobalObjCRef : 1; 150 151 // Lvalue is a thread local reference 152 bool ThreadLocalRef : 1; 153 154 // Lvalue has ARC imprecise lifetime. We store this inverted to try 155 // to make the default bitfield pattern all-zeroes. 156 bool ImpreciseLifetime : 1; 157 158 Expr *BaseIvarExp; 159 160 /// TBAAInfo - TBAA information to attach to dereferences of this LValue. 161 llvm::MDNode *TBAAInfo; 162 163 private: 164 void Initialize(QualType Type, Qualifiers Quals, 165 CharUnits Alignment, 166 llvm::MDNode *TBAAInfo = 0) { 167 this->Type = Type; 168 this->Quals = Quals; 169 this->Alignment = Alignment.getQuantity(); 170 assert(this->Alignment == Alignment.getQuantity() && 171 "Alignment exceeds allowed max!"); 172 173 // Initialize Objective-C flags. 174 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false; 175 this->ImpreciseLifetime = false; 176 this->ThreadLocalRef = false; 177 this->BaseIvarExp = 0; 178 this->TBAAInfo = TBAAInfo; 179 } 180 181 public: isSimple()182 bool isSimple() const { return LVType == Simple; } isVectorElt()183 bool isVectorElt() const { return LVType == VectorElt; } isBitField()184 bool isBitField() const { return LVType == BitField; } isExtVectorElt()185 bool isExtVectorElt() const { return LVType == ExtVectorElt; } 186 isVolatileQualified()187 bool isVolatileQualified() const { return Quals.hasVolatile(); } isRestrictQualified()188 bool isRestrictQualified() const { return Quals.hasRestrict(); } getVRQualifiers()189 unsigned getVRQualifiers() const { 190 return Quals.getCVRQualifiers() & ~Qualifiers::Const; 191 } 192 getType()193 QualType getType() const { return Type; } 194 getObjCLifetime()195 Qualifiers::ObjCLifetime getObjCLifetime() const { 196 return Quals.getObjCLifetime(); 197 } 198 isObjCIvar()199 bool isObjCIvar() const { return Ivar; } setObjCIvar(bool Value)200 void setObjCIvar(bool Value) { Ivar = Value; } 201 isObjCArray()202 bool isObjCArray() const { return ObjIsArray; } setObjCArray(bool Value)203 void setObjCArray(bool Value) { ObjIsArray = Value; } 204 isNonGC()205 bool isNonGC () const { return NonGC; } setNonGC(bool Value)206 void setNonGC(bool Value) { NonGC = Value; } 207 isGlobalObjCRef()208 bool isGlobalObjCRef() const { return GlobalObjCRef; } setGlobalObjCRef(bool Value)209 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; } 210 isThreadLocalRef()211 bool isThreadLocalRef() const { return ThreadLocalRef; } setThreadLocalRef(bool Value)212 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;} 213 isARCPreciseLifetime()214 ARCPreciseLifetime_t isARCPreciseLifetime() const { 215 return ARCPreciseLifetime_t(!ImpreciseLifetime); 216 } setARCPreciseLifetime(ARCPreciseLifetime_t value)217 void setARCPreciseLifetime(ARCPreciseLifetime_t value) { 218 ImpreciseLifetime = (value == ARCImpreciseLifetime); 219 } 220 isObjCWeak()221 bool isObjCWeak() const { 222 return Quals.getObjCGCAttr() == Qualifiers::Weak; 223 } isObjCStrong()224 bool isObjCStrong() const { 225 return Quals.getObjCGCAttr() == Qualifiers::Strong; 226 } 227 isVolatile()228 bool isVolatile() const { 229 return Quals.hasVolatile(); 230 } 231 getBaseIvarExp()232 Expr *getBaseIvarExp() const { return BaseIvarExp; } setBaseIvarExp(Expr * V)233 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; } 234 getTBAAInfo()235 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; } setTBAAInfo(llvm::MDNode * N)236 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; } 237 getQuals()238 const Qualifiers &getQuals() const { return Quals; } getQuals()239 Qualifiers &getQuals() { return Quals; } 240 getAddressSpace()241 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 242 getAlignment()243 CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); } setAlignment(CharUnits A)244 void setAlignment(CharUnits A) { Alignment = A.getQuantity(); } 245 246 // simple lvalue getAddress()247 llvm::Value *getAddress() const { assert(isSimple()); return V; } setAddress(llvm::Value * address)248 void setAddress(llvm::Value *address) { 249 assert(isSimple()); 250 V = address; 251 } 252 253 // vector elt lvalue getVectorAddr()254 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } getVectorIdx()255 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } 256 257 // extended vector elements. getExtVectorAddr()258 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } getExtVectorElts()259 llvm::Constant *getExtVectorElts() const { 260 assert(isExtVectorElt()); 261 return VectorElts; 262 } 263 264 // bitfield lvalue getBitFieldAddr()265 llvm::Value *getBitFieldAddr() const { 266 assert(isBitField()); 267 return V; 268 } getBitFieldInfo()269 const CGBitFieldInfo &getBitFieldInfo() const { 270 assert(isBitField()); 271 return *BitFieldInfo; 272 } 273 274 static LValue MakeAddr(llvm::Value *address, QualType type, 275 CharUnits alignment, ASTContext &Context, 276 llvm::MDNode *TBAAInfo = 0) { 277 Qualifiers qs = type.getQualifiers(); 278 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); 279 280 LValue R; 281 R.LVType = Simple; 282 R.V = address; 283 R.Initialize(type, qs, alignment, TBAAInfo); 284 return R; 285 } 286 MakeVectorElt(llvm::Value * Vec,llvm::Value * Idx,QualType type,CharUnits Alignment)287 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, 288 QualType type, CharUnits Alignment) { 289 LValue R; 290 R.LVType = VectorElt; 291 R.V = Vec; 292 R.VectorIdx = Idx; 293 R.Initialize(type, type.getQualifiers(), Alignment); 294 return R; 295 } 296 MakeExtVectorElt(llvm::Value * Vec,llvm::Constant * Elts,QualType type,CharUnits Alignment)297 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, 298 QualType type, CharUnits Alignment) { 299 LValue R; 300 R.LVType = ExtVectorElt; 301 R.V = Vec; 302 R.VectorElts = Elts; 303 R.Initialize(type, type.getQualifiers(), Alignment); 304 return R; 305 } 306 307 /// \brief Create a new object to represent a bit-field access. 308 /// 309 /// \param Addr - The base address of the bit-field sequence this 310 /// bit-field refers to. 311 /// \param Info - The information describing how to perform the bit-field 312 /// access. MakeBitfield(llvm::Value * Addr,const CGBitFieldInfo & Info,QualType type,CharUnits Alignment)313 static LValue MakeBitfield(llvm::Value *Addr, 314 const CGBitFieldInfo &Info, 315 QualType type, CharUnits Alignment) { 316 LValue R; 317 R.LVType = BitField; 318 R.V = Addr; 319 R.BitFieldInfo = &Info; 320 R.Initialize(type, type.getQualifiers(), Alignment); 321 return R; 322 } 323 asAggregateRValue()324 RValue asAggregateRValue() const { 325 // FIMXE: Alignment 326 return RValue::getAggregate(getAddress(), isVolatileQualified()); 327 } 328 }; 329 330 /// An aggregate value slot. 331 class AggValueSlot { 332 /// The address. 333 llvm::Value *Addr; 334 335 // Qualifiers 336 Qualifiers Quals; 337 338 unsigned short Alignment; 339 340 /// DestructedFlag - This is set to true if some external code is 341 /// responsible for setting up a destructor for the slot. Otherwise 342 /// the code which constructs it should push the appropriate cleanup. 343 bool DestructedFlag : 1; 344 345 /// ObjCGCFlag - This is set to true if writing to the memory in the 346 /// slot might require calling an appropriate Objective-C GC 347 /// barrier. The exact interaction here is unnecessarily mysterious. 348 bool ObjCGCFlag : 1; 349 350 /// ZeroedFlag - This is set to true if the memory in the slot is 351 /// known to be zero before the assignment into it. This means that 352 /// zero fields don't need to be set. 353 bool ZeroedFlag : 1; 354 355 /// AliasedFlag - This is set to true if the slot might be aliased 356 /// and it's not undefined behavior to access it through such an 357 /// alias. Note that it's always undefined behavior to access a C++ 358 /// object that's under construction through an alias derived from 359 /// outside the construction process. 360 /// 361 /// This flag controls whether calls that produce the aggregate 362 /// value may be evaluated directly into the slot, or whether they 363 /// must be evaluated into an unaliased temporary and then memcpy'ed 364 /// over. Since it's invalid in general to memcpy a non-POD C++ 365 /// object, it's important that this flag never be set when 366 /// evaluating an expression which constructs such an object. 367 bool AliasedFlag : 1; 368 369 /// ValueOfAtomicFlag - This is set to true if the slot is the value 370 /// subobject of an object the size of an _Atomic(T). The specific 371 /// guarantees this makes are: 372 /// - the address is guaranteed to be a getelementptr into the 373 /// padding struct and 374 /// - it is okay to store something the width of an _Atomic(T) 375 /// into the address. 376 /// Tracking this allows us to avoid some obviously unnecessary 377 /// memcpys. 378 bool ValueOfAtomicFlag : 1; 379 380 public: 381 enum IsAliased_t { IsNotAliased, IsAliased }; 382 enum IsDestructed_t { IsNotDestructed, IsDestructed }; 383 enum IsZeroed_t { IsNotZeroed, IsZeroed }; 384 enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers }; 385 enum IsValueOfAtomic_t { IsNotValueOfAtomic, IsValueOfAtomic }; 386 387 /// ignored - Returns an aggregate value slot indicating that the 388 /// aggregate value is being ignored. ignored()389 static AggValueSlot ignored() { 390 return forAddr(0, CharUnits(), Qualifiers(), IsNotDestructed, 391 DoesNotNeedGCBarriers, IsNotAliased); 392 } 393 394 /// forAddr - Make a slot for an aggregate value. 395 /// 396 /// \param quals - The qualifiers that dictate how the slot should 397 /// be initialied. Only 'volatile' and the Objective-C lifetime 398 /// qualifiers matter. 399 /// 400 /// \param isDestructed - true if something else is responsible 401 /// for calling destructors on this object 402 /// \param needsGC - true if the slot is potentially located 403 /// somewhere that ObjC GC calls should be emitted for 404 static AggValueSlot forAddr(llvm::Value *addr, CharUnits align, 405 Qualifiers quals, 406 IsDestructed_t isDestructed, 407 NeedsGCBarriers_t needsGC, 408 IsAliased_t isAliased, 409 IsZeroed_t isZeroed = IsNotZeroed, 410 IsValueOfAtomic_t isValueOfAtomic 411 = IsNotValueOfAtomic) { 412 AggValueSlot AV; 413 AV.Addr = addr; 414 AV.Alignment = align.getQuantity(); 415 AV.Quals = quals; 416 AV.DestructedFlag = isDestructed; 417 AV.ObjCGCFlag = needsGC; 418 AV.ZeroedFlag = isZeroed; 419 AV.AliasedFlag = isAliased; 420 AV.ValueOfAtomicFlag = isValueOfAtomic; 421 return AV; 422 } 423 424 static AggValueSlot forLValue(const LValue &LV, 425 IsDestructed_t isDestructed, 426 NeedsGCBarriers_t needsGC, 427 IsAliased_t isAliased, 428 IsZeroed_t isZeroed = IsNotZeroed, 429 IsValueOfAtomic_t isValueOfAtomic 430 = IsNotValueOfAtomic) { 431 return forAddr(LV.getAddress(), LV.getAlignment(), 432 LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed, 433 isValueOfAtomic); 434 } 435 isExternallyDestructed()436 IsDestructed_t isExternallyDestructed() const { 437 return IsDestructed_t(DestructedFlag); 438 } 439 void setExternallyDestructed(bool destructed = true) { 440 DestructedFlag = destructed; 441 } 442 getQualifiers()443 Qualifiers getQualifiers() const { return Quals; } 444 isVolatile()445 bool isVolatile() const { 446 return Quals.hasVolatile(); 447 } 448 setVolatile(bool flag)449 void setVolatile(bool flag) { 450 Quals.setVolatile(flag); 451 } 452 getObjCLifetime()453 Qualifiers::ObjCLifetime getObjCLifetime() const { 454 return Quals.getObjCLifetime(); 455 } 456 requiresGCollection()457 NeedsGCBarriers_t requiresGCollection() const { 458 return NeedsGCBarriers_t(ObjCGCFlag); 459 } 460 getAddr()461 llvm::Value *getAddr() const { 462 return Addr; 463 } 464 isValueOfAtomic()465 IsValueOfAtomic_t isValueOfAtomic() const { 466 return IsValueOfAtomic_t(ValueOfAtomicFlag); 467 } 468 469 llvm::Value *getPaddedAtomicAddr() const; 470 isIgnored()471 bool isIgnored() const { 472 return Addr == 0; 473 } 474 getAlignment()475 CharUnits getAlignment() const { 476 return CharUnits::fromQuantity(Alignment); 477 } 478 isPotentiallyAliased()479 IsAliased_t isPotentiallyAliased() const { 480 return IsAliased_t(AliasedFlag); 481 } 482 483 // FIXME: Alignment? asRValue()484 RValue asRValue() const { 485 return RValue::getAggregate(getAddr(), isVolatile()); 486 } 487 488 void setZeroed(bool V = true) { ZeroedFlag = V; } isZeroed()489 IsZeroed_t isZeroed() const { 490 return IsZeroed_t(ZeroedFlag); 491 } 492 }; 493 494 } // end namespace CodeGen 495 } // end namespace clang 496 497 #endif 498