]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGValue.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / CodeGen / CGValue.h
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   /// DestructedFlag - This is set to true if some external code is
342   /// responsible for setting up a destructor for the slot.  Otherwise
343   /// the code which constructs it should push the appropriate cleanup.
344   bool DestructedFlag : 1;
345
346   /// ObjCGCFlag - This is set to true if writing to the memory in the
347   /// slot might require calling an appropriate Objective-C GC
348   /// barrier.  The exact interaction here is unnecessarily mysterious.
349   bool ObjCGCFlag : 1;
350   
351   /// ZeroedFlag - This is set to true if the memory in the slot is
352   /// known to be zero before the assignment into it.  This means that
353   /// zero fields don't need to be set.
354   bool ZeroedFlag : 1;
355
356   /// AliasedFlag - This is set to true if the slot might be aliased
357   /// and it's not undefined behavior to access it through such an
358   /// alias.  Note that it's always undefined behavior to access a C++
359   /// object that's under construction through an alias derived from
360   /// outside the construction process.
361   ///
362   /// This flag controls whether calls that produce the aggregate
363   /// value may be evaluated directly into the slot, or whether they
364   /// must be evaluated into an unaliased temporary and then memcpy'ed
365   /// over.  Since it's invalid in general to memcpy a non-POD C++
366   /// object, it's important that this flag never be set when
367   /// evaluating an expression which constructs such an object.
368   bool AliasedFlag : 1;
369
370 public:
371   enum IsAliased_t { IsNotAliased, IsAliased };
372   enum IsDestructed_t { IsNotDestructed, IsDestructed };
373   enum IsZeroed_t { IsNotZeroed, IsZeroed };
374   enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
375
376   /// ignored - Returns an aggregate value slot indicating that the
377   /// aggregate value is being ignored.
378   static AggValueSlot ignored() {
379     AggValueSlot AV;
380     AV.Addr = 0;
381     AV.Quals = Qualifiers();
382     AV.DestructedFlag = AV.ObjCGCFlag = AV.ZeroedFlag = AV.AliasedFlag = false;
383     return AV;
384   }
385
386   /// forAddr - Make a slot for an aggregate value.
387   ///
388   /// \param quals - The qualifiers that dictate how the slot should
389   /// be initialied. Only 'volatile' and the Objective-C lifetime
390   /// qualifiers matter.
391   ///
392   /// \param isDestructed - true if something else is responsible
393   ///   for calling destructors on this object
394   /// \param needsGC - true if the slot is potentially located
395   ///   somewhere that ObjC GC calls should be emitted for
396   static AggValueSlot forAddr(llvm::Value *addr, Qualifiers quals,
397                               IsDestructed_t isDestructed,
398                               NeedsGCBarriers_t needsGC,
399                               IsAliased_t isAliased,
400                               IsZeroed_t isZeroed = IsNotZeroed) {
401     AggValueSlot AV;
402     AV.Addr = addr;
403     AV.Quals = quals;
404     AV.DestructedFlag = isDestructed;
405     AV.ObjCGCFlag = needsGC;
406     AV.ZeroedFlag = isZeroed;
407     AV.AliasedFlag = isAliased;
408     return AV;
409   }
410
411   static AggValueSlot forLValue(LValue LV, IsDestructed_t isDestructed,
412                                 NeedsGCBarriers_t needsGC,
413                                 IsAliased_t isAliased,
414                                 IsZeroed_t isZeroed = IsNotZeroed) {
415     return forAddr(LV.getAddress(), LV.getQuals(),
416                    isDestructed, needsGC, isAliased, isZeroed);
417   }
418
419   IsDestructed_t isExternallyDestructed() const {
420     return IsDestructed_t(DestructedFlag);
421   }
422   void setExternallyDestructed(bool destructed = true) {
423     DestructedFlag = destructed;
424   }
425
426   Qualifiers getQualifiers() const { return Quals; }
427
428   bool isVolatile() const {
429     return Quals.hasVolatile();
430   }
431
432   Qualifiers::ObjCLifetime getObjCLifetime() const {
433     return Quals.getObjCLifetime();
434   }
435
436   NeedsGCBarriers_t requiresGCollection() const {
437     return NeedsGCBarriers_t(ObjCGCFlag);
438   }
439   
440   llvm::Value *getAddr() const {
441     return Addr;
442   }
443
444   bool isIgnored() const {
445     return Addr == 0;
446   }
447
448   IsAliased_t isPotentiallyAliased() const {
449     return IsAliased_t(AliasedFlag);
450   }
451
452   RValue asRValue() const {
453     return RValue::getAggregate(getAddr(), isVolatile());
454   }
455   
456   void setZeroed(bool V = true) { ZeroedFlag = V; }
457   IsZeroed_t isZeroed() const {
458     return IsZeroed_t(ZeroedFlag);
459   }
460 };
461
462 }  // end namespace CodeGen
463 }  // end namespace clang
464
465 #endif