]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGValue.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[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/Type.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/IR/Type.h"
22 #include "Address.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   // The shift to make to an aggregate's alignment to make it look
42   // like a pointer.
43   enum { AggAlignShift = 4 };
44
45   // Stores first value and flavor.
46   llvm::PointerIntPair<llvm::Value *, 2, Flavor> V1;
47   // Stores second value and volatility.
48   llvm::PointerIntPair<llvm::Value *, 1, bool> V2;
49
50 public:
51   bool isScalar() const { return V1.getInt() == Scalar; }
52   bool isComplex() const { return V1.getInt() == Complex; }
53   bool isAggregate() const { return V1.getInt() == Aggregate; }
54
55   bool isVolatileQualified() const { return V2.getInt(); }
56
57   /// getScalarVal() - Return the Value* of this scalar value.
58   llvm::Value *getScalarVal() const {
59     assert(isScalar() && "Not a scalar!");
60     return V1.getPointer();
61   }
62
63   /// getComplexVal - Return the real/imag components of this complex value.
64   ///
65   std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
66     return std::make_pair(V1.getPointer(), V2.getPointer());
67   }
68
69   /// getAggregateAddr() - Return the Value* of the address of the aggregate.
70   Address getAggregateAddress() const {
71     assert(isAggregate() && "Not an aggregate!");
72     auto align = reinterpret_cast<uintptr_t>(V2.getPointer()) >> AggAlignShift;
73     return Address(V1.getPointer(), CharUnits::fromQuantity(align));
74   }
75   llvm::Value *getAggregatePointer() const {
76     assert(isAggregate() && "Not an aggregate!");
77     return V1.getPointer();
78   }
79
80   static RValue getIgnored() {
81     // FIXME: should we make this a more explicit state?
82     return get(nullptr);
83   }
84
85   static RValue get(llvm::Value *V) {
86     RValue ER;
87     ER.V1.setPointer(V);
88     ER.V1.setInt(Scalar);
89     ER.V2.setInt(false);
90     return ER;
91   }
92   static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
93     RValue ER;
94     ER.V1.setPointer(V1);
95     ER.V2.setPointer(V2);
96     ER.V1.setInt(Complex);
97     ER.V2.setInt(false);
98     return ER;
99   }
100   static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
101     return getComplex(C.first, C.second);
102   }
103   // FIXME: Aggregate rvalues need to retain information about whether they are
104   // volatile or not.  Remove default to find all places that probably get this
105   // wrong.
106   static RValue getAggregate(Address addr, bool isVolatile = false) {
107     RValue ER;
108     ER.V1.setPointer(addr.getPointer());
109     ER.V1.setInt(Aggregate);
110
111     auto align = static_cast<uintptr_t>(addr.getAlignment().getQuantity());
112     ER.V2.setPointer(reinterpret_cast<llvm::Value*>(align << AggAlignShift));
113     ER.V2.setInt(isVolatile);
114     return ER;
115   }
116 };
117
118 /// Does an ARC strong l-value have precise lifetime?
119 enum ARCPreciseLifetime_t {
120   ARCImpreciseLifetime, ARCPreciseLifetime
121 };
122
123 /// The source of the alignment of an l-value; an expression of
124 /// confidence in the alignment actually matching the estimate.
125 enum class AlignmentSource {
126   /// The l-value was an access to a declared entity or something
127   /// equivalently strong, like the address of an array allocated by a
128   /// language runtime.
129   Decl,
130
131   /// The l-value was considered opaque, so the alignment was
132   /// determined from a type, but that type was an explicitly-aligned
133   /// typedef.
134   AttributedType,
135
136   /// The l-value was considered opaque, so the alignment was
137   /// determined from a type.
138   Type
139 };
140
141 /// Given that the base address has the given alignment source, what's
142 /// our confidence in the alignment of the field?
143 static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
144   // For now, we don't distinguish fields of opaque pointers from
145   // top-level declarations, but maybe we should.
146   return AlignmentSource::Decl;
147 }
148
149 class LValueBaseInfo {
150   AlignmentSource AlignSource;
151   bool MayAlias;
152
153 public:
154   explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type,
155                  bool Alias = false)
156     : AlignSource(Source), MayAlias(Alias) {}
157   AlignmentSource getAlignmentSource() const { return AlignSource; }
158   void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
159   bool getMayAlias() const { return MayAlias; }
160   void setMayAlias(bool Alias) { MayAlias = Alias; }
161
162   void mergeForCast(const LValueBaseInfo &Info) {
163     setAlignmentSource(Info.getAlignmentSource());
164     setMayAlias(getMayAlias() || Info.getMayAlias());
165   }
166 };
167
168 /// LValue - This represents an lvalue references.  Because C/C++ allow
169 /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
170 /// bitrange.
171 class LValue {
172   enum {
173     Simple,       // This is a normal l-value, use getAddress().
174     VectorElt,    // This is a vector element l-value (V[i]), use getVector*
175     BitField,     // This is a bitfield l-value, use getBitfield*.
176     ExtVectorElt, // This is an extended vector subset, use getExtVectorComp
177     GlobalReg     // This is a register l-value, use getGlobalReg()
178   } LVType;
179
180   llvm::Value *V;
181
182   union {
183     // Index into a vector subscript: V[i]
184     llvm::Value *VectorIdx;
185
186     // ExtVector element subset: V.xyx
187     llvm::Constant *VectorElts;
188
189     // BitField start bit and size
190     const CGBitFieldInfo *BitFieldInfo;
191   };
192
193   QualType Type;
194
195   // 'const' is unused here
196   Qualifiers Quals;
197
198   // The alignment to use when accessing this lvalue.  (For vector elements,
199   // this is the alignment of the whole vector.)
200   int64_t Alignment;
201
202   // objective-c's ivar
203   bool Ivar:1;
204   
205   // objective-c's ivar is an array
206   bool ObjIsArray:1;
207
208   // LValue is non-gc'able for any reason, including being a parameter or local
209   // variable.
210   bool NonGC: 1;
211
212   // Lvalue is a global reference of an objective-c object
213   bool GlobalObjCRef : 1;
214   
215   // Lvalue is a thread local reference
216   bool ThreadLocalRef : 1;
217
218   // Lvalue has ARC imprecise lifetime.  We store this inverted to try
219   // to make the default bitfield pattern all-zeroes.
220   bool ImpreciseLifetime : 1;
221
222   LValueBaseInfo BaseInfo;
223
224   // This flag shows if a nontemporal load/stores should be used when accessing
225   // this lvalue.
226   bool Nontemporal : 1;
227
228   Expr *BaseIvarExp;
229
230   /// Used by struct-path-aware TBAA.
231   QualType TBAABaseType;
232   /// Offset relative to the base type.
233   uint64_t TBAAOffset;
234
235   /// TBAAInfo - TBAA information to attach to dereferences of this LValue.
236   llvm::MDNode *TBAAInfo;
237
238 private:
239   void Initialize(QualType Type, Qualifiers Quals,
240                   CharUnits Alignment, LValueBaseInfo BaseInfo,
241                   llvm::MDNode *TBAAInfo = nullptr) {
242     assert((!Alignment.isZero() || Type->isIncompleteType()) &&
243            "initializing l-value with zero alignment!");
244     this->Type = Type;
245     this->Quals = Quals;
246     this->Alignment = Alignment.getQuantity();
247     assert(this->Alignment == Alignment.getQuantity() &&
248            "Alignment exceeds allowed max!");
249     this->BaseInfo = BaseInfo;
250
251     // Initialize Objective-C flags.
252     this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
253     this->ImpreciseLifetime = false;
254     this->Nontemporal = false;
255     this->ThreadLocalRef = false;
256     this->BaseIvarExp = nullptr;
257
258     // Initialize fields for TBAA.
259     this->TBAABaseType = Type;
260     this->TBAAOffset = 0;
261     this->TBAAInfo = TBAAInfo;
262   }
263
264 public:
265   bool isSimple() const { return LVType == Simple; }
266   bool isVectorElt() const { return LVType == VectorElt; }
267   bool isBitField() const { return LVType == BitField; }
268   bool isExtVectorElt() const { return LVType == ExtVectorElt; }
269   bool isGlobalReg() const { return LVType == GlobalReg; }
270
271   bool isVolatileQualified() const { return Quals.hasVolatile(); }
272   bool isRestrictQualified() const { return Quals.hasRestrict(); }
273   unsigned getVRQualifiers() const {
274     return Quals.getCVRQualifiers() & ~Qualifiers::Const;
275   }
276
277   QualType getType() const { return Type; }
278
279   Qualifiers::ObjCLifetime getObjCLifetime() const {
280     return Quals.getObjCLifetime();
281   }
282
283   bool isObjCIvar() const { return Ivar; }
284   void setObjCIvar(bool Value) { Ivar = Value; }
285
286   bool isObjCArray() const { return ObjIsArray; }
287   void setObjCArray(bool Value) { ObjIsArray = Value; }
288
289   bool isNonGC () const { return NonGC; }
290   void setNonGC(bool Value) { NonGC = Value; }
291
292   bool isGlobalObjCRef() const { return GlobalObjCRef; }
293   void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
294
295   bool isThreadLocalRef() const { return ThreadLocalRef; }
296   void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
297
298   ARCPreciseLifetime_t isARCPreciseLifetime() const {
299     return ARCPreciseLifetime_t(!ImpreciseLifetime);
300   }
301   void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
302     ImpreciseLifetime = (value == ARCImpreciseLifetime);
303   }
304   bool isNontemporal() const { return Nontemporal; }
305   void setNontemporal(bool Value) { Nontemporal = Value; }
306
307   bool isObjCWeak() const {
308     return Quals.getObjCGCAttr() == Qualifiers::Weak;
309   }
310   bool isObjCStrong() const {
311     return Quals.getObjCGCAttr() == Qualifiers::Strong;
312   }
313
314   bool isVolatile() const {
315     return Quals.hasVolatile();
316   }
317   
318   Expr *getBaseIvarExp() const { return BaseIvarExp; }
319   void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
320
321   QualType getTBAABaseType() const { return TBAABaseType; }
322   void setTBAABaseType(QualType T) { TBAABaseType = T; }
323
324   uint64_t getTBAAOffset() const { return TBAAOffset; }
325   void setTBAAOffset(uint64_t O) { TBAAOffset = O; }
326
327   llvm::MDNode *getTBAAInfo() const { return TBAAInfo; }
328   void setTBAAInfo(llvm::MDNode *N) { TBAAInfo = N; }
329
330   const Qualifiers &getQuals() const { return Quals; }
331   Qualifiers &getQuals() { return Quals; }
332
333   unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
334
335   CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); }
336   void setAlignment(CharUnits A) { Alignment = A.getQuantity(); }
337
338   LValueBaseInfo getBaseInfo() const { return BaseInfo; }
339   void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
340
341   // simple lvalue
342   llvm::Value *getPointer() const {
343     assert(isSimple());
344     return V;
345   }
346   Address getAddress() const { return Address(getPointer(), getAlignment()); }
347   void setAddress(Address address) {
348     assert(isSimple());
349     V = address.getPointer();
350     Alignment = address.getAlignment().getQuantity();
351   }
352
353   // vector elt lvalue
354   Address getVectorAddress() const {
355     return Address(getVectorPointer(), getAlignment());
356   }
357   llvm::Value *getVectorPointer() const { assert(isVectorElt()); return V; }
358   llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
359
360   // extended vector elements.
361   Address getExtVectorAddress() const {
362     return Address(getExtVectorPointer(), getAlignment());
363   }
364   llvm::Value *getExtVectorPointer() const {
365     assert(isExtVectorElt());
366     return V;
367   }
368   llvm::Constant *getExtVectorElts() const {
369     assert(isExtVectorElt());
370     return VectorElts;
371   }
372
373   // bitfield lvalue
374   Address getBitFieldAddress() const {
375     return Address(getBitFieldPointer(), getAlignment());
376   }
377   llvm::Value *getBitFieldPointer() const { assert(isBitField()); return V; }
378   const CGBitFieldInfo &getBitFieldInfo() const {
379     assert(isBitField());
380     return *BitFieldInfo;
381   }
382
383   // global register lvalue
384   llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
385
386   static LValue MakeAddr(Address address, QualType type,
387                          ASTContext &Context,
388                          LValueBaseInfo BaseInfo,
389                          llvm::MDNode *TBAAInfo = nullptr) {
390     Qualifiers qs = type.getQualifiers();
391     qs.setObjCGCAttr(Context.getObjCGCAttrKind(type));
392
393     LValue R;
394     R.LVType = Simple;
395     assert(address.getPointer()->getType()->isPointerTy());
396     R.V = address.getPointer();
397     R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
398     return R;
399   }
400
401   static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
402                               QualType type, LValueBaseInfo BaseInfo) {
403     LValue R;
404     R.LVType = VectorElt;
405     R.V = vecAddress.getPointer();
406     R.VectorIdx = Idx;
407     R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
408                  BaseInfo);
409     return R;
410   }
411
412   static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts,
413                                  QualType type, LValueBaseInfo BaseInfo) {
414     LValue R;
415     R.LVType = ExtVectorElt;
416     R.V = vecAddress.getPointer();
417     R.VectorElts = Elts;
418     R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
419                  BaseInfo);
420     return R;
421   }
422
423   /// \brief Create a new object to represent a bit-field access.
424   ///
425   /// \param Addr - The base address of the bit-field sequence this
426   /// bit-field refers to.
427   /// \param Info - The information describing how to perform the bit-field
428   /// access.
429   static LValue MakeBitfield(Address Addr,
430                              const CGBitFieldInfo &Info,
431                              QualType type,
432                              LValueBaseInfo BaseInfo) {
433     LValue R;
434     R.LVType = BitField;
435     R.V = Addr.getPointer();
436     R.BitFieldInfo = &Info;
437     R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo);
438     return R;
439   }
440
441   static LValue MakeGlobalReg(Address Reg, QualType type) {
442     LValue R;
443     R.LVType = GlobalReg;
444     R.V = Reg.getPointer();
445     R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
446                  LValueBaseInfo(AlignmentSource::Decl, false));
447     return R;
448   }
449
450   RValue asAggregateRValue() const {
451     return RValue::getAggregate(getAddress(), isVolatileQualified());
452   }
453 };
454
455 /// An aggregate value slot.
456 class AggValueSlot {
457   /// The address.
458   llvm::Value *Addr;
459
460   // Qualifiers
461   Qualifiers Quals;
462
463   unsigned Alignment;
464
465   /// DestructedFlag - This is set to true if some external code is
466   /// responsible for setting up a destructor for the slot.  Otherwise
467   /// the code which constructs it should push the appropriate cleanup.
468   bool DestructedFlag : 1;
469
470   /// ObjCGCFlag - This is set to true if writing to the memory in the
471   /// slot might require calling an appropriate Objective-C GC
472   /// barrier.  The exact interaction here is unnecessarily mysterious.
473   bool ObjCGCFlag : 1;
474   
475   /// ZeroedFlag - This is set to true if the memory in the slot is
476   /// known to be zero before the assignment into it.  This means that
477   /// zero fields don't need to be set.
478   bool ZeroedFlag : 1;
479
480   /// AliasedFlag - This is set to true if the slot might be aliased
481   /// and it's not undefined behavior to access it through such an
482   /// alias.  Note that it's always undefined behavior to access a C++
483   /// object that's under construction through an alias derived from
484   /// outside the construction process.
485   ///
486   /// This flag controls whether calls that produce the aggregate
487   /// value may be evaluated directly into the slot, or whether they
488   /// must be evaluated into an unaliased temporary and then memcpy'ed
489   /// over.  Since it's invalid in general to memcpy a non-POD C++
490   /// object, it's important that this flag never be set when
491   /// evaluating an expression which constructs such an object.
492   bool AliasedFlag : 1;
493
494 public:
495   enum IsAliased_t { IsNotAliased, IsAliased };
496   enum IsDestructed_t { IsNotDestructed, IsDestructed };
497   enum IsZeroed_t { IsNotZeroed, IsZeroed };
498   enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
499
500   /// ignored - Returns an aggregate value slot indicating that the
501   /// aggregate value is being ignored.
502   static AggValueSlot ignored() {
503     return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
504                    DoesNotNeedGCBarriers, IsNotAliased);
505   }
506
507   /// forAddr - Make a slot for an aggregate value.
508   ///
509   /// \param quals - The qualifiers that dictate how the slot should
510   /// be initialied. Only 'volatile' and the Objective-C lifetime
511   /// qualifiers matter.
512   ///
513   /// \param isDestructed - true if something else is responsible
514   ///   for calling destructors on this object
515   /// \param needsGC - true if the slot is potentially located
516   ///   somewhere that ObjC GC calls should be emitted for
517   static AggValueSlot forAddr(Address addr,
518                               Qualifiers quals,
519                               IsDestructed_t isDestructed,
520                               NeedsGCBarriers_t needsGC,
521                               IsAliased_t isAliased,
522                               IsZeroed_t isZeroed = IsNotZeroed) {
523     AggValueSlot AV;
524     if (addr.isValid()) {
525       AV.Addr = addr.getPointer();
526       AV.Alignment = addr.getAlignment().getQuantity();
527     } else {
528       AV.Addr = nullptr;
529       AV.Alignment = 0;
530     }
531     AV.Quals = quals;
532     AV.DestructedFlag = isDestructed;
533     AV.ObjCGCFlag = needsGC;
534     AV.ZeroedFlag = isZeroed;
535     AV.AliasedFlag = isAliased;
536     return AV;
537   }
538
539   static AggValueSlot forLValue(const LValue &LV,
540                                 IsDestructed_t isDestructed,
541                                 NeedsGCBarriers_t needsGC,
542                                 IsAliased_t isAliased,
543                                 IsZeroed_t isZeroed = IsNotZeroed) {
544     return forAddr(LV.getAddress(),
545                    LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed);
546   }
547
548   IsDestructed_t isExternallyDestructed() const {
549     return IsDestructed_t(DestructedFlag);
550   }
551   void setExternallyDestructed(bool destructed = true) {
552     DestructedFlag = destructed;
553   }
554
555   Qualifiers getQualifiers() const { return Quals; }
556
557   bool isVolatile() const {
558     return Quals.hasVolatile();
559   }
560
561   void setVolatile(bool flag) {
562     Quals.setVolatile(flag);
563   }
564   
565   Qualifiers::ObjCLifetime getObjCLifetime() const {
566     return Quals.getObjCLifetime();
567   }
568
569   NeedsGCBarriers_t requiresGCollection() const {
570     return NeedsGCBarriers_t(ObjCGCFlag);
571   }
572
573   llvm::Value *getPointer() const {
574     return Addr;
575   }
576
577   Address getAddress() const {
578     return Address(Addr, getAlignment());
579   }
580
581   bool isIgnored() const {
582     return Addr == nullptr;
583   }
584
585   CharUnits getAlignment() const {
586     return CharUnits::fromQuantity(Alignment);
587   }
588
589   IsAliased_t isPotentiallyAliased() const {
590     return IsAliased_t(AliasedFlag);
591   }
592
593   RValue asRValue() const {
594     if (isIgnored()) {
595       return RValue::getIgnored();
596     } else {
597       return RValue::getAggregate(getAddress(), isVolatile());
598     }
599   }
600
601   void setZeroed(bool V = true) { ZeroedFlag = V; }
602   IsZeroed_t isZeroed() const {
603     return IsZeroed_t(ZeroedFlag);
604   }
605 };
606
607 }  // end namespace CodeGen
608 }  // end namespace clang
609
610 #endif