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 21 namespace llvm { 22 class Constant; 23 class Value; 24 } 25 26 namespace clang { 27 class ObjCPropertyRefExpr; 28 29 namespace CodeGen { 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 PropertyRef // This is an Objective-C property reference, use 110 // getPropertyRefExpr 111 } LVType; 112 113 llvm::Value *V; 114 115 union { 116 // Index into a vector subscript: V[i] 117 llvm::Value *VectorIdx; 118 119 // ExtVector element subset: V.xyx 120 llvm::Constant *VectorElts; 121 122 // BitField start bit and size 123 const CGBitFieldInfo *BitFieldInfo; 124 125 // Obj-C property reference expression 126 const ObjCPropertyRefExpr *PropertyRefExpr; 127 }; 128 129 QualType Type; 130 131 // 'const' is unused here 132 Qualifiers Quals; 133 134 /// The alignment to use when accessing this lvalue. 135 unsigned short Alignment; 136 137 // objective-c's ivar 138 bool Ivar:1; 139 140 // objective-c's ivar is an array 141 bool ObjIsArray:1; 142 143 // LValue is non-gc'able for any reason, including being a parameter or local 144 // variable. 145 bool NonGC: 1; 146 147 // Lvalue is a global reference of an objective-c object 148 bool GlobalObjCRef : 1; 149 150 // Lvalue is a thread local reference 151 bool ThreadLocalRef : 1; 152 153 Expr *BaseIvarExp; 154 155 /// TBAAInfo - TBAA information to attach to dereferences of this LValue. 156 llvm::MDNode *TBAAInfo; 157 158 private: 159 void Initialize(QualType Type, Qualifiers Quals, unsigned Alignment = 0, 160 llvm::MDNode *TBAAInfo = 0) { 161 this->Type = Type; 162 this->Quals = Quals; 163 this->Alignment = Alignment; 164 assert(this->Alignment == Alignment && "Alignment exceeds allowed max!"); 165 166 // Initialize Objective-C flags. 167 this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false; 168 this->ThreadLocalRef = false; 169 this->BaseIvarExp = 0; 170 this->TBAAInfo = TBAAInfo; 171 } 172 173 public: 174 bool isSimple() const { return LVType == Simple; } 175 bool isVectorElt() const { return LVType == VectorElt; } 176 bool isBitField() const { return LVType == BitField; } 177 bool isExtVectorElt() const { return LVType == ExtVectorElt; } 178 bool isPropertyRef() const { return LVType == PropertyRef; } 179 180 bool isVolatileQualified() const { return Quals.hasVolatile(); } 181 bool isRestrictQualified() const { return Quals.hasRestrict(); } 182 unsigned getVRQualifiers() const { 183 return Quals.getCVRQualifiers() & ~Qualifiers::Const; 184 } 185 186 QualType getType() const { return Type; } 187 188 Qualifiers::ObjCLifetime getObjCLifetime() const { 189 return Quals.getObjCLifetime(); 190 } 191 192 bool isObjCIvar() const { return Ivar; } 193 void setObjCIvar(bool Value) { Ivar = Value; } 194 195 bool isObjCArray() const { return ObjIsArray; } 196 void setObjCArray(bool Value) { ObjIsArray = Value; } 197 198 bool isNonGC () const { return NonGC; } 199 void setNonGC(bool Value) { NonGC = Value; } 200 201 bool isGlobalObjCRef() const { return GlobalObjCRef; } 202 void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; } 203 204 bool isThreadLocalRef() const { return ThreadLocalRef; } 205 void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;} 206 207 bool isObjCWeak() const { 208 return Quals.getObjCGCAttr() == Qualifiers::Weak; 209 } 210 bool isObjCStrong() const { 211 return Quals.getObjCGCAttr() == Qualifiers::Strong; 212 } 213 214 bool isVolatile() const { 215 return Quals.hasVolatile(); 216 } 217 218 Expr *getBaseIvarExp() const { return BaseIvarExp; } 219 void setBaseIvarExp(Expr *V) { BaseIvarExp = V; } 220 221 llvm::MDNode *getTBAAInfo() const { return TBAAInfo; } 222 void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; } 223 224 const Qualifiers &getQuals() const { return Quals; } 225 Qualifiers &getQuals() { return Quals; } 226 227 unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 228 229 unsigned getAlignment() const { return Alignment; } 230 231 // simple lvalue 232 llvm::Value *getAddress() const { assert(isSimple()); return V; } 233 void setAddress(llvm::Value *address) { 234 assert(isSimple()); 235 V = address; 236 } 237 238 // vector elt lvalue 239 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } 240 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } 241 242 // extended vector elements. 243 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } 244 llvm::Constant *getExtVectorElts() const { 245 assert(isExtVectorElt()); 246 return VectorElts; 247 } 248 249 // bitfield lvalue 250 llvm::Value *getBitFieldBaseAddr() const { 251 assert(isBitField()); 252 return V; 253 } 254 const CGBitFieldInfo &getBitFieldInfo() const { 255 assert(isBitField()); 256 return *BitFieldInfo; 257 } 258 259 // property ref lvalue 260 llvm::Value *getPropertyRefBaseAddr() const { 261 assert(isPropertyRef()); 262 return V; 263 } 264 const ObjCPropertyRefExpr *getPropertyRefExpr() const { 265 assert(isPropertyRef()); 266 return PropertyRefExpr; 267 } 268 269 static LValue MakeAddr(llvm::Value *address, QualType type, 270 unsigned alignment, ASTContext &Context, 271 llvm::MDNode *TBAAInfo = 0) { 272 Qualifiers qs = type.getQualifiers(); 273 qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); 274 275 LValue R; 276 R.LVType = Simple; 277 R.V = address; 278 R.Initialize(type, qs, alignment, TBAAInfo); 279 return R; 280 } 281 282 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, 283 QualType type) { 284 LValue R; 285 R.LVType = VectorElt; 286 R.V = Vec; 287 R.VectorIdx = Idx; 288 R.Initialize(type, type.getQualifiers()); 289 return R; 290 } 291 292 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, 293 QualType type) { 294 LValue R; 295 R.LVType = ExtVectorElt; 296 R.V = Vec; 297 R.VectorElts = Elts; 298 R.Initialize(type, type.getQualifiers()); 299 return R; 300 } 301 302 /// \brief Create a new object to represent a bit-field access. 303 /// 304 /// \param BaseValue - The base address of the structure containing the 305 /// bit-field. 306 /// \param Info - The information describing how to perform the bit-field 307 /// access. 308 static LValue MakeBitfield(llvm::Value *BaseValue, 309 const CGBitFieldInfo &Info, 310 QualType type) { 311 LValue R; 312 R.LVType = BitField; 313 R.V = BaseValue; 314 R.BitFieldInfo = &Info; 315 R.Initialize(type, type.getQualifiers()); 316 return R; 317 } 318 319 // FIXME: It is probably bad that we aren't emitting the target when we build 320 // the lvalue. However, this complicates the code a bit, and I haven't figured 321 // out how to make it go wrong yet. 322 static LValue MakePropertyRef(const ObjCPropertyRefExpr *E, 323 llvm::Value *Base) { 324 LValue R; 325 R.LVType = PropertyRef; 326 R.V = Base; 327 R.PropertyRefExpr = E; 328 R.Initialize(QualType(), Qualifiers()); 329 return R; 330 } 331 }; 332 333 /// An aggregate value slot. 334 class AggValueSlot { 335 /// The address. 336 llvm::Value *Addr; 337 338 // Qualifiers 339 Qualifiers Quals; 340 341 // Associated flags. 342 bool LifetimeFlag : 1; 343 bool RequiresGCollection : 1; 344 345 /// IsZeroed - This is set to true if the destination is known to be zero 346 /// before the assignment into it. This means that zero fields don't need to 347 /// be set. 348 bool IsZeroed : 1; 349 350 public: 351 /// ignored - Returns an aggregate value slot indicating that the 352 /// aggregate value is being ignored. 353 static AggValueSlot ignored() { 354 AggValueSlot AV; 355 AV.Addr = 0; 356 AV.Quals = Qualifiers(); 357 AV.LifetimeFlag = AV.RequiresGCollection = AV.IsZeroed =0; 358 return AV; 359 } 360 361 /// forAddr - Make a slot for an aggregate value. 362 /// 363 /// \param Volatile - true if the slot should be volatile-initialized 364 /// 365 /// \param Qualifiers - The qualifiers that dictate how the slot 366 /// should be initialied. Only 'volatile' and the Objective-C 367 /// lifetime qualifiers matter. 368 /// 369 /// \param LifetimeExternallyManaged - true if the slot's lifetime 370 /// is being externally managed; false if a destructor should be 371 /// registered for any temporaries evaluated into the slot 372 /// \param RequiresGCollection - true if the slot is located 373 /// somewhere that ObjC GC calls should be emitted for 374 static AggValueSlot forAddr(llvm::Value *Addr, Qualifiers Quals, 375 bool LifetimeExternallyManaged, 376 bool RequiresGCollection = false, 377 bool IsZeroed = false) { 378 AggValueSlot AV; 379 AV.Addr = Addr; 380 AV.Quals = Quals; 381 AV.LifetimeFlag = LifetimeExternallyManaged; 382 AV.RequiresGCollection = RequiresGCollection; 383 AV.IsZeroed = IsZeroed; 384 return AV; 385 } 386 387 static AggValueSlot forLValue(LValue LV, bool LifetimeExternallyManaged, 388 bool RequiresGCollection = false, 389 bool IsZeroed = false) { 390 return forAddr(LV.getAddress(), LV.getQuals(), 391 LifetimeExternallyManaged, RequiresGCollection, IsZeroed); 392 } 393 394 bool isLifetimeExternallyManaged() const { 395 return LifetimeFlag; 396 } 397 void setLifetimeExternallyManaged(bool Managed = true) { 398 LifetimeFlag = Managed; 399 } 400 401 Qualifiers getQualifiers() const { return Quals; } 402 403 bool isVolatile() const { 404 return Quals.hasVolatile(); 405 } 406 407 Qualifiers::ObjCLifetime getObjCLifetime() const { 408 return Quals.getObjCLifetime(); 409 } 410 411 bool requiresGCollection() const { 412 return RequiresGCollection; 413 } 414 415 llvm::Value *getAddr() const { 416 return Addr; 417 } 418 419 bool isIgnored() const { 420 return Addr == 0; 421 } 422 423 RValue asRValue() const { 424 return RValue::getAggregate(getAddr(), isVolatile()); 425 } 426 427 void setZeroed(bool V = true) { IsZeroed = V; } 428 bool isZeroed() const { 429 return IsZeroed; 430 } 431 }; 432 433 } // end namespace CodeGen 434 } // end namespace clang 435 436 #endif 437