xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h (revision 09e516c54b0c5ec3fe6b3ef6c70d9e09e89abc95)
1 //===- VPlanValue.h - Represent Values in Vectorizer Plan -----------------===//
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 /// \file
11 /// This file contains the declarations of the entities induced by Vectorization
12 /// Plans, e.g. the instructions the VPlan intends to generate if executed.
13 /// VPlan models the following entities:
14 /// VPValue
15 ///  |-- VPUser
16 ///  |    |-- VPInstruction
17 /// These are documented in docs/VectorizationPlan.rst.
18 ///
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
22 #define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
23 
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/IR/Value.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 
30 namespace llvm {
31 
32 // Forward declarations.
33 class VPUser;
34 
35 // This is the base class of the VPlan Def/Use graph, used for modeling the data
36 // flow into, within and out of the VPlan. VPValues can stand for live-ins
37 // coming from the input IR, instructions which VPlan will generate if executed
38 // and live-outs which the VPlan will need to fix accordingly.
39 class VPValue {
40   friend class VPBuilder;
41   friend class VPlanHCFGTransforms;
42   friend class VPBasicBlock;
43   friend class VPInterleavedAccessInfo;
44 
45 private:
46   const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
47 
48   SmallVector<VPUser *, 1> Users;
49 
50 protected:
51   // Hold the underlying Value, if any, attached to this VPValue.
52   Value *UnderlyingVal;
53 
54   VPValue(const unsigned char SC, Value *UV = nullptr)
55       : SubclassID(SC), UnderlyingVal(UV) {}
56 
57   // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
58   // the front-end and back-end of VPlan so that the middle-end is as
59   // independent as possible of the underlying IR. We grant access to the
60   // underlying IR using friendship. In that way, we should be able to use VPlan
61   // for multiple underlying IRs (Polly?) by providing a new VPlan front-end,
62   // back-end and analysis information for the new IR.
63 
64   /// Return the underlying Value attached to this VPValue.
65   Value *getUnderlyingValue() { return UnderlyingVal; }
66 
67   // Set \p Val as the underlying Value of this VPValue.
68   void setUnderlyingValue(Value *Val) {
69     assert(!UnderlyingVal && "Underlying Value is already set.");
70     UnderlyingVal = Val;
71   }
72 
73 public:
74   /// An enumeration for keeping track of the concrete subclass of VPValue that
75   /// are actually instantiated. Values of this enumeration are kept in the
76   /// SubclassID field of the VPValue objects. They are used for concrete
77   /// type identification.
78   enum { VPValueSC, VPUserSC, VPInstructionSC };
79 
80   VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
81   VPValue(const VPValue &) = delete;
82   VPValue &operator=(const VPValue &) = delete;
83 
84   /// \return an ID for the concrete type of this object.
85   /// This is used to implement the classof checks. This should not be used
86   /// for any other purpose, as the values may change as LLVM evolves.
87   unsigned getVPValueID() const { return SubclassID; }
88 
89   void printAsOperand(raw_ostream &OS) const {
90     OS << "%vp" << (unsigned short)(unsigned long long)this;
91   }
92 
93   unsigned getNumUsers() const { return Users.size(); }
94   void addUser(VPUser &User) { Users.push_back(&User); }
95 
96   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
97   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
98   typedef iterator_range<user_iterator> user_range;
99   typedef iterator_range<const_user_iterator> const_user_range;
100 
101   user_iterator user_begin() { return Users.begin(); }
102   const_user_iterator user_begin() const { return Users.begin(); }
103   user_iterator user_end() { return Users.end(); }
104   const_user_iterator user_end() const { return Users.end(); }
105   user_range users() { return user_range(user_begin(), user_end()); }
106   const_user_range users() const {
107     return const_user_range(user_begin(), user_end());
108   }
109 
110   /// Returns true if the value has more than one unique user.
111   bool hasMoreThanOneUniqueUser() {
112     if (getNumUsers() == 0)
113       return false;
114 
115     // Check if all users match the first user.
116     auto Current = std::next(user_begin());
117     while (Current != user_end() && *user_begin() == *Current)
118       Current++;
119     return Current != user_end();
120   }
121 
122   void replaceAllUsesWith(VPValue *New);
123 };
124 
125 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
126 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
127 
128 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
129 
130 /// This class augments VPValue with operands which provide the inverse def-use
131 /// edges from VPValue's users to their defs.
132 class VPUser : public VPValue {
133 private:
134   SmallVector<VPValue *, 2> Operands;
135 
136 protected:
137   VPUser(const unsigned char SC) : VPValue(SC) {}
138   VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) {
139     for (VPValue *Operand : Operands)
140       addOperand(Operand);
141   }
142 
143 public:
144   VPUser() : VPValue(VPValue::VPUserSC) {}
145   VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {}
146   VPUser(std::initializer_list<VPValue *> Operands)
147       : VPUser(ArrayRef<VPValue *>(Operands)) {}
148   VPUser(const VPUser &) = delete;
149   VPUser &operator=(const VPUser &) = delete;
150 
151   /// Method to support type inquiry through isa, cast, and dyn_cast.
152   static inline bool classof(const VPValue *V) {
153     return V->getVPValueID() >= VPUserSC &&
154            V->getVPValueID() <= VPInstructionSC;
155   }
156 
157   void addOperand(VPValue *Operand) {
158     Operands.push_back(Operand);
159     Operand->addUser(*this);
160   }
161 
162   unsigned getNumOperands() const { return Operands.size(); }
163   inline VPValue *getOperand(unsigned N) const {
164     assert(N < Operands.size() && "Operand index out of bounds");
165     return Operands[N];
166   }
167 
168   void setOperand(unsigned I, VPValue *New) { Operands[I] = New; }
169 
170   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
171   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
172   typedef iterator_range<operand_iterator> operand_range;
173   typedef iterator_range<const_operand_iterator> const_operand_range;
174 
175   operand_iterator op_begin() { return Operands.begin(); }
176   const_operand_iterator op_begin() const { return Operands.begin(); }
177   operand_iterator op_end() { return Operands.end(); }
178   const_operand_iterator op_end() const { return Operands.end(); }
179   operand_range operands() { return operand_range(op_begin(), op_end()); }
180   const_operand_range operands() const {
181     return const_operand_range(op_begin(), op_end());
182   }
183 };
184 
185 } // namespace llvm
186 
187 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
188