xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h (revision 52f3714dae7b23ba734a449623cb1570c6c1b700)
1 //===- VPlanValue.h - Represent Values in Vectorizer Plan -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the declarations of the entities induced by Vectorization
11 /// Plans, e.g. the instructions the VPlan intends to generate if executed.
12 /// VPlan models the following entities:
13 /// VPValue   VPUser   VPDef
14 ///    |        |
15 ///   VPInstruction
16 /// These are documented in docs/VectorizationPlan.rst.
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
21 #define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
22 
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/TinyPtrVector.h"
27 #include "llvm/ADT/iterator_range.h"
28 
29 namespace llvm {
30 
31 // Forward declarations.
32 class raw_ostream;
33 class Value;
34 class VPDef;
35 class VPSlotTracker;
36 class VPUser;
37 class VPRecipeBase;
38 
39 // This is the base class of the VPlan Def/Use graph, used for modeling the data
40 // flow into, within and out of the VPlan. VPValues can stand for live-ins
41 // coming from the input IR, instructions which VPlan will generate if executed
42 // and live-outs which the VPlan will need to fix accordingly.
43 class VPValue {
44   friend class VPBuilder;
45   friend class VPDef;
46   friend struct VPlanTransforms;
47   friend class VPBasicBlock;
48   friend class VPInterleavedAccessInfo;
49   friend class VPSlotTracker;
50   friend class VPRecipeBase;
51 
52   const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
53 
54   SmallVector<VPUser *, 1> Users;
55 
56 protected:
57   // Hold the underlying Value, if any, attached to this VPValue.
58   Value *UnderlyingVal;
59 
60   /// Pointer to the VPDef that defines this VPValue. If it is nullptr, the
61   /// VPValue is not defined by any recipe modeled in VPlan.
62   VPDef *Def;
63 
64   VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr);
65 
66   // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
67   // the front-end and back-end of VPlan so that the middle-end is as
68   // independent as possible of the underlying IR. We grant access to the
69   // underlying IR using friendship. In that way, we should be able to use VPlan
70   // for multiple underlying IRs (Polly?) by providing a new VPlan front-end,
71   // back-end and analysis information for the new IR.
72 
73   /// Return the underlying Value attached to this VPValue.
74   Value *getUnderlyingValue() { return UnderlyingVal; }
75   const Value *getUnderlyingValue() const { return UnderlyingVal; }
76 
77   // Set \p Val as the underlying Value of this VPValue.
78   void setUnderlyingValue(Value *Val) {
79     assert(!UnderlyingVal && "Underlying Value is already set.");
80     UnderlyingVal = Val;
81   }
82 
83 public:
84   /// An enumeration for keeping track of the concrete subclass of VPValue that
85   /// are actually instantiated. Values of this enumeration are kept in the
86   /// SubclassID field of the VPValue objects. They are used for concrete
87   /// type identification.
88   enum {
89     VPValueSC,
90     VPInstructionSC,
91     VPMemoryInstructionSC,
92     VPVWidenCallSC,
93     VPVWidenSelectSC,
94     VPVWidenGEPSC
95   };
96 
97   VPValue(Value *UV = nullptr, VPDef *Def = nullptr)
98       : VPValue(VPValueSC, UV, Def) {}
99   VPValue(const VPValue &) = delete;
100   VPValue &operator=(const VPValue &) = delete;
101 
102   virtual ~VPValue();
103 
104   /// \return an ID for the concrete type of this object.
105   /// This is used to implement the classof checks. This should not be used
106   /// for any other purpose, as the values may change as LLVM evolves.
107   unsigned getVPValueID() const { return SubclassID; }
108 
109   void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;
110   void print(raw_ostream &OS, VPSlotTracker &Tracker) const;
111 
112   /// Dump the value to stderr (for debugging).
113   void dump() const;
114 
115   unsigned getNumUsers() const { return Users.size(); }
116   void addUser(VPUser &User) { Users.push_back(&User); }
117 
118   /// Remove a single \p User from the list of users.
119   void removeUser(VPUser &User) {
120     bool Found = false;
121     // The same user can be added multiple times, e.g. because the same VPValue
122     // is used twice by the same VPUser. Remove a single one.
123     erase_if(Users, [&User, &Found](VPUser *Other) {
124       if (Found)
125         return false;
126       if (Other == &User) {
127         Found = true;
128         return true;
129       }
130       return false;
131     });
132   }
133 
134   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
135   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
136   typedef iterator_range<user_iterator> user_range;
137   typedef iterator_range<const_user_iterator> const_user_range;
138 
139   user_iterator user_begin() { return Users.begin(); }
140   const_user_iterator user_begin() const { return Users.begin(); }
141   user_iterator user_end() { return Users.end(); }
142   const_user_iterator user_end() const { return Users.end(); }
143   user_range users() { return user_range(user_begin(), user_end()); }
144   const_user_range users() const {
145     return const_user_range(user_begin(), user_end());
146   }
147 
148   /// Returns true if the value has more than one unique user.
149   bool hasMoreThanOneUniqueUser() {
150     if (getNumUsers() == 0)
151       return false;
152 
153     // Check if all users match the first user.
154     auto Current = std::next(user_begin());
155     while (Current != user_end() && *user_begin() == *Current)
156       Current++;
157     return Current != user_end();
158   }
159 
160   void replaceAllUsesWith(VPValue *New);
161 
162   VPDef *getDef() { return Def; }
163 };
164 
165 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
166 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
167 
168 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
169 
170 /// This class augments VPValue with operands which provide the inverse def-use
171 /// edges from VPValue's users to their defs.
172 class VPUser {
173   SmallVector<VPValue *, 2> Operands;
174 
175 protected:
176   /// Print the operands to \p O.
177   void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const;
178 
179 public:
180   VPUser() {}
181   VPUser(ArrayRef<VPValue *> Operands) {
182     for (VPValue *Operand : Operands)
183       addOperand(Operand);
184   }
185 
186   VPUser(std::initializer_list<VPValue *> Operands)
187       : VPUser(ArrayRef<VPValue *>(Operands)) {}
188   template <typename IterT> VPUser(iterator_range<IterT> Operands) {
189     for (VPValue *Operand : Operands)
190       addOperand(Operand);
191   }
192 
193   VPUser(const VPUser &) = delete;
194   VPUser &operator=(const VPUser &) = delete;
195   virtual ~VPUser() {
196     for (VPValue *Op : operands())
197       Op->removeUser(*this);
198   }
199 
200   void addOperand(VPValue *Operand) {
201     Operands.push_back(Operand);
202     Operand->addUser(*this);
203   }
204 
205   unsigned getNumOperands() const { return Operands.size(); }
206   inline VPValue *getOperand(unsigned N) const {
207     assert(N < Operands.size() && "Operand index out of bounds");
208     return Operands[N];
209   }
210 
211   void setOperand(unsigned I, VPValue *New) {
212     Operands[I]->removeUser(*this);
213     Operands[I] = New;
214     New->addUser(*this);
215   }
216 
217   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
218   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
219   typedef iterator_range<operand_iterator> operand_range;
220   typedef iterator_range<const_operand_iterator> const_operand_range;
221 
222   operand_iterator op_begin() { return Operands.begin(); }
223   const_operand_iterator op_begin() const { return Operands.begin(); }
224   operand_iterator op_end() { return Operands.end(); }
225   const_operand_iterator op_end() const { return Operands.end(); }
226   operand_range operands() { return operand_range(op_begin(), op_end()); }
227   const_operand_range operands() const {
228     return const_operand_range(op_begin(), op_end());
229   }
230 
231   /// Method to support type inquiry through isa, cast, and dyn_cast.
232   static inline bool classof(const VPRecipeBase *Recipe);
233 };
234 
235 /// This class augments a recipe with a set of VPValues defined by the recipe.
236 /// It allows recipes to define zero, one or multiple VPValues. A VPDef owns
237 /// the VPValues it defines and is responsible for deleting its defined values.
238 /// Single-value VPDefs that also inherit from VPValue must make sure to inherit
239 /// from VPDef before VPValue.
240 class VPDef {
241   friend class VPValue;
242 
243   /// The VPValues defined by this VPDef.
244   TinyPtrVector<VPValue *> DefinedValues;
245 
246   /// Add \p V as a defined value by this VPDef.
247   void addDefinedValue(VPValue *V) {
248     assert(V->getDef() == this &&
249            "can only add VPValue already linked with this VPDef");
250     DefinedValues.push_back(V);
251   }
252 
253   /// Remove \p V from the values defined by this VPDef. \p V must be a defined
254   /// value of this VPDef.
255   void removeDefinedValue(VPValue *V) {
256     assert(V->getDef() == this &&
257            "can only remove VPValue linked with this VPDef");
258     assert(find(DefinedValues, V) != DefinedValues.end() &&
259            "VPValue to remove must be in DefinedValues");
260     erase_value(DefinedValues, V);
261     V->Def = nullptr;
262   }
263 
264 public:
265   virtual ~VPDef() {
266     for (VPValue *D : make_early_inc_range(DefinedValues)) {
267       assert(D->Def == this &&
268              "all defined VPValues should point to the containing VPDef");
269       assert(D->getNumUsers() == 0 &&
270              "all defined VPValues should have no more users");
271       D->Def = nullptr;
272       delete D;
273     }
274   }
275 
276   /// Returns the VPValue with index \p I defined by the VPDef.
277   VPValue *getVPValue(unsigned I = 0) {
278     assert(DefinedValues[I] && "defined value must be non-null");
279     return DefinedValues[I];
280   }
281   const VPValue *getVPValue(unsigned I = 0) const {
282     assert(DefinedValues[I] && "defined value must be non-null");
283     return DefinedValues[I];
284   }
285 
286   /// Returns an ArrayRef of the values defined by the VPDef.
287   ArrayRef<VPValue *> definedValues() { return DefinedValues; }
288 
289   /// Returns the number of values defined by the VPDef.
290   unsigned getNumDefinedValues() const { return DefinedValues.size(); }
291 };
292 
293 class VPlan;
294 class VPBasicBlock;
295 class VPRegionBlock;
296 
297 /// This class can be used to assign consecutive numbers to all VPValues in a
298 /// VPlan and allows querying the numbering for printing, similar to the
299 /// ModuleSlotTracker for IR values.
300 class VPSlotTracker {
301   DenseMap<const VPValue *, unsigned> Slots;
302   unsigned NextSlot = 0;
303 
304   void assignSlots(const VPBlockBase *VPBB);
305   void assignSlots(const VPRegionBlock *Region);
306   void assignSlots(const VPBasicBlock *VPBB);
307   void assignSlot(const VPValue *V);
308 
309   void assignSlots(const VPlan &Plan);
310 
311 public:
312   VPSlotTracker(const VPlan *Plan) {
313     if (Plan)
314       assignSlots(*Plan);
315   }
316 
317   unsigned getSlot(const VPValue *V) const {
318     auto I = Slots.find(V);
319     if (I == Slots.end())
320       return -1;
321     return I->second;
322   }
323 };
324 
325 } // namespace llvm
326 
327 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
328