Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 5cf8bfe | 2010-08-21 02:53:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
| 21 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | class Constant; |
| 24 | class Value; |
| 25 | } |
| 26 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 27 | namespace clang { |
| 28 | namespace CodeGen { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 29 | class AggValueSlot; |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 30 | class CGBitFieldInfo; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 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 { |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 37 | enum Flavor { Scalar, Complex, Aggregate }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 44 | public: |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 45 | bool isScalar() const { return V1.getInt() == Scalar; } |
| 46 | bool isComplex() const { return V1.getInt() == Complex; } |
| 47 | bool isAggregate() const { return V1.getInt() == Aggregate; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 49 | bool isVolatileQualified() const { return V2.getInt(); } |
Mike Stump | 8b3d93a | 2009-05-23 20:21:36 +0000 | [diff] [blame] | 50 | |
Mike Stump | 519202d | 2009-11-03 16:11:57 +0000 | [diff] [blame] | 51 | /// getScalarVal() - Return the Value* of this scalar value. |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 52 | llvm::Value *getScalarVal() const { |
| 53 | assert(isScalar() && "Not a scalar!"); |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 54 | return V1.getPointer(); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// getComplexVal - Return the real/imag components of this complex value. |
| 58 | /// |
| 59 | std::pair<llvm::Value *, llvm::Value *> getComplexVal() const { |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 60 | return std::make_pair(V1.getPointer(), V2.getPointer()); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 61 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 63 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 64 | llvm::Value *getAggregateAddr() const { |
| 65 | assert(isAggregate() && "Not an aggregate!"); |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 66 | return V1.getPointer(); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 67 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 69 | static RValue get(llvm::Value *V) { |
| 70 | RValue ER; |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 71 | ER.V1.setPointer(V); |
| 72 | ER.V1.setInt(Scalar); |
| 73 | ER.V2.setInt(false); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 74 | return ER; |
| 75 | } |
| 76 | static RValue getComplex(llvm::Value *V1, llvm::Value *V2) { |
| 77 | RValue ER; |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 78 | ER.V1.setPointer(V1); |
| 79 | ER.V2.setPointer(V2); |
| 80 | ER.V1.setInt(Complex); |
| 81 | ER.V2.setInt(false); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 82 | return ER; |
| 83 | } |
| 84 | static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) { |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 85 | return getComplex(C.first, C.second); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 8b3d93a | 2009-05-23 20:21:36 +0000 | [diff] [blame] | 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. |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 90 | static RValue getAggregate(llvm::Value *V, bool Volatile = false) { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 91 | RValue ER; |
Benjamin Kramer | 81bf3b3 | 2010-05-02 14:59:10 +0000 | [diff] [blame] | 92 | ER.V1.setPointer(V); |
| 93 | ER.V1.setInt(Aggregate); |
| 94 | ER.V2.setInt(Volatile); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 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 { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 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*. |
John McCall | db45806 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 108 | ExtVectorElt // This is an extended vector subset, use getExtVectorComp |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 109 | } LVType; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 111 | llvm::Value *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 120 | // BitField start bit and size |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 121 | const CGBitFieldInfo *BitFieldInfo; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 124 | QualType Type; |
| 125 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 126 | // 'const' is unused here |
| 127 | Qualifiers Quals; |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 128 | |
Eli Friedman | e5a8aeb | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 129 | // The alignment to use when accessing this lvalue. (For vector elements, |
| 130 | // this is the alignment of the whole vector.) |
Daniel Dunbar | 77c0590 | 2010-08-26 06:02:12 +0000 | [diff] [blame] | 131 | unsigned short Alignment; |
Daniel Dunbar | 9f4f7cf | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 132 | |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 133 | // objective-c's ivar |
| 134 | bool Ivar:1; |
Fariborz Jahanian | 1c1afc4 | 2009-09-18 00:04:00 +0000 | [diff] [blame] | 135 | |
| 136 | // objective-c's ivar is an array |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 137 | bool ObjIsArray:1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 139 | // LValue is non-gc'able for any reason, including being a parameter or local |
| 140 | // variable. |
| 141 | bool NonGC: 1; |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 142 | |
Fariborz Jahanian | bf63b87 | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 143 | // Lvalue is a global reference of an objective-c object |
| 144 | bool GlobalObjCRef : 1; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 145 | |
| 146 | // Lvalue is a thread local reference |
| 147 | bool ThreadLocalRef : 1; |
Fariborz Jahanian | bf63b87 | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 148 | |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 149 | Expr *BaseIvarExp; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 150 | |
| 151 | /// TBAAInfo - TBAA information to attach to dereferences of this LValue. |
| 152 | llvm::MDNode *TBAAInfo; |
| 153 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 154 | private: |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 155 | void Initialize(QualType Type, Qualifiers Quals, |
| 156 | CharUnits Alignment = CharUnits(), |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 157 | llvm::MDNode *TBAAInfo = 0) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 158 | this->Type = Type; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 159 | this->Quals = Quals; |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 160 | this->Alignment = Alignment.getQuantity(); |
| 161 | assert(this->Alignment == Alignment.getQuantity() && |
| 162 | "Alignment exceeds allowed max!"); |
Daniel Dunbar | 9f4f7cf | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 163 | |
| 164 | // Initialize Objective-C flags. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 165 | this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 166 | this->ThreadLocalRef = false; |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 167 | this->BaseIvarExp = 0; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 168 | this->TBAAInfo = TBAAInfo; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 171 | public: |
| 172 | bool isSimple() const { return LVType == Simple; } |
| 173 | bool isVectorElt() const { return LVType == VectorElt; } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 174 | bool isBitField() const { return LVType == BitField; } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 175 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 176 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 177 | bool isVolatileQualified() const { return Quals.hasVolatile(); } |
| 178 | bool isRestrictQualified() const { return Quals.hasRestrict(); } |
| 179 | unsigned getVRQualifiers() const { |
| 180 | return Quals.getCVRQualifiers() & ~Qualifiers::Const; |
Chris Lattner | 1bd885e | 2009-02-16 22:25:49 +0000 | [diff] [blame] | 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 183 | QualType getType() const { return Type; } |
| 184 | |
| 185 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
| 186 | return Quals.getObjCLifetime(); |
| 187 | } |
| 188 | |
Fariborz Jahanian | d1cc804 | 2008-11-20 20:53:20 +0000 | [diff] [blame] | 189 | bool isObjCIvar() const { return Ivar; } |
Daniel Dunbar | 3491b3d | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 190 | void setObjCIvar(bool Value) { Ivar = Value; } |
| 191 | |
Fariborz Jahanian | fd02ed7 | 2009-09-21 18:54:29 +0000 | [diff] [blame] | 192 | bool isObjCArray() const { return ObjIsArray; } |
Daniel Dunbar | 3491b3d | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 193 | void setObjCArray(bool Value) { ObjIsArray = Value; } |
Daniel Dunbar | ea61917 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 194 | |
Fariborz Jahanian | 4f676ed | 2009-02-21 00:30:43 +0000 | [diff] [blame] | 195 | bool isNonGC () const { return NonGC; } |
Daniel Dunbar | ea61917 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 196 | void setNonGC(bool Value) { NonGC = Value; } |
| 197 | |
Fariborz Jahanian | bf63b87 | 2009-05-04 23:27:20 +0000 | [diff] [blame] | 198 | bool isGlobalObjCRef() const { return GlobalObjCRef; } |
Daniel Dunbar | 3491b3d | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 199 | void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; } |
| 200 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 201 | bool isThreadLocalRef() const { return ThreadLocalRef; } |
Daniel Dunbar | 3491b3d | 2010-08-21 03:51:29 +0000 | [diff] [blame] | 202 | void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;} |
| 203 | |
| 204 | bool isObjCWeak() const { |
| 205 | return Quals.getObjCGCAttr() == Qualifiers::Weak; |
| 206 | } |
| 207 | bool isObjCStrong() const { |
| 208 | return Quals.getObjCGCAttr() == Qualifiers::Strong; |
| 209 | } |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 210 | |
| 211 | bool isVolatile() const { |
| 212 | return Quals.hasVolatile(); |
| 213 | } |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 214 | |
| 215 | Expr *getBaseIvarExp() const { return BaseIvarExp; } |
| 216 | void setBaseIvarExp(Expr *V) { BaseIvarExp = V; } |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 217 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 218 | llvm::MDNode *getTBAAInfo() const { return TBAAInfo; } |
| 219 | void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; } |
| 220 | |
Daniel Dunbar | 99ad7df | 2010-08-21 03:29:54 +0000 | [diff] [blame] | 221 | const Qualifiers &getQuals() const { return Quals; } |
| 222 | Qualifiers &getQuals() { return Quals; } |
| 223 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 224 | unsigned getAddressSpace() const { return Quals.getAddressSpace(); } |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 225 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 226 | CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); } |
| 227 | void setAlignment(CharUnits A) { Alignment = A.getQuantity(); } |
Daniel Dunbar | 9f4f7cf | 2010-08-21 02:39:23 +0000 | [diff] [blame] | 228 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 229 | // simple lvalue |
| 230 | llvm::Value *getAddress() const { assert(isSimple()); return V; } |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 231 | void setAddress(llvm::Value *address) { |
| 232 | assert(isSimple()); |
| 233 | V = address; |
| 234 | } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 235 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 236 | // vector elt lvalue |
| 237 | llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } |
| 238 | llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 239 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 240 | // extended vector elements. |
| 241 | llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } |
| 242 | llvm::Constant *getExtVectorElts() const { |
| 243 | assert(isExtVectorElt()); |
| 244 | return VectorElts; |
| 245 | } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 246 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 247 | // bitfield lvalue |
Daniel Dunbar | 7f28964 | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 248 | llvm::Value *getBitFieldBaseAddr() const { |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 249 | assert(isBitField()); |
| 250 | return V; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 251 | } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 252 | const CGBitFieldInfo &getBitFieldInfo() const { |
| 253 | assert(isBitField()); |
| 254 | return *BitFieldInfo; |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 255 | } |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 256 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 257 | static LValue MakeAddr(llvm::Value *address, QualType type, |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 258 | CharUnits alignment, ASTContext &Context, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 259 | llvm::MDNode *TBAAInfo = 0) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 260 | Qualifiers qs = type.getQualifiers(); |
| 261 | qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); |
Daniel Dunbar | f1fbda3 | 2010-08-21 03:58:45 +0000 | [diff] [blame] | 262 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 263 | LValue R; |
| 264 | R.LVType = Simple; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 265 | R.V = address; |
| 266 | R.Initialize(type, qs, alignment, TBAAInfo); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 267 | return R; |
| 268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 270 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, |
Eli Friedman | e5a8aeb | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 271 | QualType type, CharUnits Alignment) { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 272 | LValue R; |
| 273 | R.LVType = VectorElt; |
| 274 | R.V = Vec; |
| 275 | R.VectorIdx = Idx; |
Eli Friedman | e5a8aeb | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 276 | R.Initialize(type, type.getQualifiers(), Alignment); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 277 | return R; |
| 278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 280 | static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, |
Eli Friedman | e5a8aeb | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 281 | QualType type, CharUnits Alignment) { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 282 | LValue R; |
| 283 | R.LVType = ExtVectorElt; |
| 284 | R.V = Vec; |
| 285 | R.VectorElts = Elts; |
Eli Friedman | e5a8aeb | 2012-03-22 22:36:39 +0000 | [diff] [blame] | 286 | R.Initialize(type, type.getQualifiers(), Alignment); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 287 | return R; |
| 288 | } |
| 289 | |
Daniel Dunbar | 7f28964 | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 290 | /// \brief Create a new object to represent a bit-field access. |
| 291 | /// |
| 292 | /// \param BaseValue - The base address of the structure containing the |
| 293 | /// bit-field. |
| 294 | /// \param Info - The information describing how to perform the bit-field |
| 295 | /// access. |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 296 | static LValue MakeBitfield(llvm::Value *BaseValue, |
| 297 | const CGBitFieldInfo &Info, |
| 298 | QualType type) { |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 299 | LValue R; |
| 300 | R.LVType = BitField; |
Daniel Dunbar | 7f28964 | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 301 | R.V = BaseValue; |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 302 | R.BitFieldInfo = &Info; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 303 | R.Initialize(type, type.getQualifiers()); |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 304 | return R; |
| 305 | } |
Eli Friedman | 51f5120 | 2011-12-03 03:08:40 +0000 | [diff] [blame] | 306 | |
| 307 | RValue asAggregateRValue() const { |
| 308 | // FIMXE: Alignment |
| 309 | return RValue::getAggregate(getAddress(), isVolatileQualified()); |
| 310 | } |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 313 | /// An aggregate value slot. |
| 314 | class AggValueSlot { |
Fariborz Jahanian | 474e2fe | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 315 | /// The address. |
John McCall | 6fa2916 | 2010-09-16 03:16:41 +0000 | [diff] [blame] | 316 | llvm::Value *Addr; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 317 | |
| 318 | // Qualifiers |
| 319 | Qualifiers Quals; |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 320 | |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 321 | unsigned Alignment : 16; |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 322 | |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 323 | /// DestructedFlag - This is set to true if some external code is |
| 324 | /// responsible for setting up a destructor for the slot. Otherwise |
| 325 | /// the code which constructs it should push the appropriate cleanup. |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 326 | unsigned DestructedFlag : 1; |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 327 | |
| 328 | /// ObjCGCFlag - This is set to true if writing to the memory in the |
| 329 | /// slot might require calling an appropriate Objective-C GC |
| 330 | /// barrier. The exact interaction here is unnecessarily mysterious. |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 331 | unsigned ObjCGCFlag : 1; |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 332 | |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 333 | /// ZeroedFlag - This is set to true if the memory in the slot is |
| 334 | /// known to be zero before the assignment into it. This means that |
| 335 | /// zero fields don't need to be set. |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 336 | unsigned ZeroedFlag : 1; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 337 | |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 338 | /// AliasedFlag - This is set to true if the slot might be aliased |
| 339 | /// and it's not undefined behavior to access it through such an |
| 340 | /// alias. Note that it's always undefined behavior to access a C++ |
| 341 | /// object that's under construction through an alias derived from |
| 342 | /// outside the construction process. |
| 343 | /// |
| 344 | /// This flag controls whether calls that produce the aggregate |
| 345 | /// value may be evaluated directly into the slot, or whether they |
| 346 | /// must be evaluated into an unaliased temporary and then memcpy'ed |
| 347 | /// over. Since it's invalid in general to memcpy a non-POD C++ |
| 348 | /// object, it's important that this flag never be set when |
| 349 | /// evaluating an expression which constructs such an object. |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 350 | unsigned AliasedFlag : 1; |
| 351 | |
| 352 | /// CompleteObjectFlag - This is set to true if the slot is known to |
| 353 | /// be a complete object. When emitting an aggregate copy of a |
| 354 | /// non-POD C++ struct to a location which may not be a complete |
| 355 | /// object, only the data size of the type can be copied in order to |
| 356 | /// prevent unrelated fields from being overwritten. |
| 357 | unsigned CompleteObjectFlag : 1; |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 358 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 359 | public: |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 360 | enum IsAliased_t { IsNotAliased, IsAliased }; |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 361 | enum IsDestructed_t { IsNotDestructed, IsDestructed }; |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 362 | enum IsZeroed_t { IsNotZeroed, IsZeroed }; |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 363 | enum IsCompleteObject_t { |
| 364 | IsNotCompleteObject, |
| 365 | MayNotBeCompleteObject = IsNotCompleteObject, |
| 366 | IsCompleteObject |
| 367 | }; |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 368 | enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers }; |
| 369 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 370 | /// ignored - Returns an aggregate value slot indicating that the |
| 371 | /// aggregate value is being ignored. |
| 372 | static AggValueSlot ignored() { |
Benjamin Kramer | 9f32a92 | 2011-12-11 16:34:24 +0000 | [diff] [blame] | 373 | return forAddr(0, CharUnits(), Qualifiers(), IsNotDestructed, |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 374 | DoesNotNeedGCBarriers, IsNotAliased, |
| 375 | IsCompleteObject); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /// forAddr - Make a slot for an aggregate value. |
| 379 | /// |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 380 | /// \param quals - The qualifiers that dictate how the slot should |
| 381 | /// be initialied. Only 'volatile' and the Objective-C lifetime |
| 382 | /// qualifiers matter. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 383 | /// |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 384 | /// \param isDestructed - true if something else is responsible |
| 385 | /// for calling destructors on this object |
| 386 | /// \param needsGC - true if the slot is potentially located |
John McCall | d1a5f13 | 2010-09-16 03:13:23 +0000 | [diff] [blame] | 387 | /// somewhere that ObjC GC calls should be emitted for |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 388 | static AggValueSlot forAddr(llvm::Value *addr, CharUnits align, |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 389 | Qualifiers quals, |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 390 | IsDestructed_t isDestructed, |
| 391 | NeedsGCBarriers_t needsGC, |
John McCall | 4418439 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 392 | IsAliased_t isAliased, |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 393 | IsCompleteObject_t isCompleteObject, |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 394 | IsZeroed_t isZeroed = IsNotZeroed) { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 395 | AggValueSlot AV; |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 396 | AV.Addr = addr; |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 397 | AV.Alignment = align.getQuantity(); |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 398 | AV.Quals = quals; |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 399 | AV.DestructedFlag = isDestructed; |
| 400 | AV.ObjCGCFlag = needsGC; |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 401 | AV.ZeroedFlag = isZeroed; |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 402 | AV.AliasedFlag = isAliased; |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 403 | AV.CompleteObjectFlag = isCompleteObject; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 404 | return AV; |
| 405 | } |
| 406 | |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 407 | static AggValueSlot forLValue(LValue LV, IsDestructed_t isDestructed, |
| 408 | NeedsGCBarriers_t needsGC, |
John McCall | 4418439 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 409 | IsAliased_t isAliased, |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 410 | IsCompleteObject_t isCompleteObject, |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 411 | IsZeroed_t isZeroed = IsNotZeroed) { |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 412 | return forAddr(LV.getAddress(), LV.getAlignment(), |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 413 | LV.getQuals(), isDestructed, needsGC, isAliased, |
| 414 | isCompleteObject, isZeroed); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 415 | } |
Fariborz Jahanian | 8a97005 | 2010-10-22 22:05:03 +0000 | [diff] [blame] | 416 | |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 417 | IsDestructed_t isExternallyDestructed() const { |
| 418 | return IsDestructed_t(DestructedFlag); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 419 | } |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 420 | void setExternallyDestructed(bool destructed = true) { |
| 421 | DestructedFlag = destructed; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 424 | Qualifiers getQualifiers() const { return Quals; } |
| 425 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 426 | bool isVolatile() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 427 | return Quals.hasVolatile(); |
| 428 | } |
| 429 | |
| 430 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
| 431 | return Quals.getObjCLifetime(); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 432 | } |
| 433 | |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 434 | NeedsGCBarriers_t requiresGCollection() const { |
John McCall | fd71fb8 | 2011-08-26 08:02:37 +0000 | [diff] [blame] | 435 | return NeedsGCBarriers_t(ObjCGCFlag); |
Fariborz Jahanian | 474e2fe | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 436 | } |
Fariborz Jahanian | 474e2fe | 2010-09-16 00:20:07 +0000 | [diff] [blame] | 437 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 438 | llvm::Value *getAddr() const { |
John McCall | 6fa2916 | 2010-09-16 03:16:41 +0000 | [diff] [blame] | 439 | return Addr; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | bool isIgnored() const { |
John McCall | 6fa2916 | 2010-09-16 03:16:41 +0000 | [diff] [blame] | 443 | return Addr == 0; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 446 | CharUnits getAlignment() const { |
| 447 | return CharUnits::fromQuantity(Alignment); |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 448 | } |
| 449 | |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 450 | IsAliased_t isPotentiallyAliased() const { |
| 451 | return IsAliased_t(AliasedFlag); |
| 452 | } |
| 453 | |
John McCall | 57cd1b8 | 2012-03-28 23:30:44 +0000 | [diff] [blame] | 454 | IsCompleteObject_t isCompleteObject() const { |
| 455 | return IsCompleteObject_t(CompleteObjectFlag); |
| 456 | } |
| 457 | |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 458 | // FIXME: Alignment? |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 459 | RValue asRValue() const { |
| 460 | return RValue::getAggregate(getAddr(), isVolatile()); |
| 461 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 462 | |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 463 | void setZeroed(bool V = true) { ZeroedFlag = V; } |
| 464 | IsZeroed_t isZeroed() const { |
| 465 | return IsZeroed_t(ZeroedFlag); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 466 | } |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
Daniel Dunbar | 2eecaab | 2008-08-23 03:10:25 +0000 | [diff] [blame] | 469 | } // end namespace CodeGen |
| 470 | } // end namespace clang |
| 471 | |
| 472 | #endif |