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