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