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