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