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