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 class VPWidenMemoryInstructionRecipe;
39 
40 // This is the base class of the VPlan Def/Use graph, used for modeling the data
41 // flow into, within and out of the VPlan. VPValues can stand for live-ins
42 // coming from the input IR, instructions which VPlan will generate if executed
43 // and live-outs which the VPlan will need to fix accordingly.
44 class VPValue {
45   friend class VPBuilder;
46   friend class VPDef;
47   friend class VPInstruction;
48   friend struct VPlanTransforms;
49   friend class VPBasicBlock;
50   friend class VPInterleavedAccessInfo;
51   friend class VPSlotTracker;
52   friend class VPRecipeBase;
53   friend class VPWidenMemoryInstructionRecipe;
54 
55   const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
56 
57   SmallVector<VPUser *, 1> Users;
58 
59 protected:
60   // Hold the underlying Value, if any, attached to this VPValue.
61   Value *UnderlyingVal;
62 
63   /// Pointer to the VPDef that defines this VPValue. If it is nullptr, the
64   /// VPValue is not defined by any recipe modeled in VPlan.
65   VPDef *Def;
66 
67   VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr);
68 
69   // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
70   // the front-end and back-end of VPlan so that the middle-end is as
71   // independent as possible of the underlying IR. We grant access to the
72   // underlying IR using friendship. In that way, we should be able to use VPlan
73   // for multiple underlying IRs (Polly?) by providing a new VPlan front-end,
74   // back-end and analysis information for the new IR.
75 
76   // Set \p Val as the underlying Value of this VPValue.
77   void setUnderlyingValue(Value *Val) {
78     assert(!UnderlyingVal && "Underlying Value is already set.");
79     UnderlyingVal = Val;
80   }
81 
82 public:
83   /// Return the underlying Value attached to this VPValue.
84   Value *getUnderlyingValue() { return UnderlyingVal; }
85   const Value *getUnderlyingValue() const { return UnderlyingVal; }
86 
87   /// An enumeration for keeping track of the concrete subclass of VPValue that
88   /// are actually instantiated. Values of this enumeration are kept in the
89   /// SubclassID field of the VPValue objects. They are used for concrete
90   /// type identification.
91   enum {
92     VPValueSC,
93     VPVInstructionSC,
94     VPVMemoryInstructionSC,
95     VPVReductionSC,
96     VPVReplicateSC,
97     VPVWidenSC,
98     VPVWidenCallSC,
99     VPVWidenGEPSC,
100     VPVWidenSelectSC,
101   };
102 
103   VPValue(Value *UV = nullptr, VPDef *Def = nullptr)
104       : VPValue(VPValueSC, UV, Def) {}
105   VPValue(const VPValue &) = delete;
106   VPValue &operator=(const VPValue &) = delete;
107 
108   virtual ~VPValue();
109 
110   /// \return an ID for the concrete type of this object.
111   /// This is used to implement the classof checks. This should not be used
112   /// for any other purpose, as the values may change as LLVM evolves.
113   unsigned getVPValueID() const { return SubclassID; }
114 
115   void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;
116   void print(raw_ostream &OS, VPSlotTracker &Tracker) const;
117 
118   /// Dump the value to stderr (for debugging).
119   void dump() const;
120 
121   unsigned getNumUsers() const { return Users.size(); }
122   void addUser(VPUser &User) { Users.push_back(&User); }
123 
124   /// Remove a single \p User from the list of users.
125   void removeUser(VPUser &User) {
126     bool Found = false;
127     // The same user can be added multiple times, e.g. because the same VPValue
128     // is used twice by the same VPUser. Remove a single one.
129     erase_if(Users, [&User, &Found](VPUser *Other) {
130       if (Found)
131         return false;
132       if (Other == &User) {
133         Found = true;
134         return true;
135       }
136       return false;
137     });
138   }
139 
140   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
141   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
142   typedef iterator_range<user_iterator> user_range;
143   typedef iterator_range<const_user_iterator> const_user_range;
144 
145   user_iterator user_begin() { return Users.begin(); }
146   const_user_iterator user_begin() const { return Users.begin(); }
147   user_iterator user_end() { return Users.end(); }
148   const_user_iterator user_end() const { return Users.end(); }
149   user_range users() { return user_range(user_begin(), user_end()); }
150   const_user_range users() const {
151     return const_user_range(user_begin(), user_end());
152   }
153 
154   /// Returns true if the value has more than one unique user.
155   bool hasMoreThanOneUniqueUser() {
156     if (getNumUsers() == 0)
157       return false;
158 
159     // Check if all users match the first user.
160     auto Current = std::next(user_begin());
161     while (Current != user_end() && *user_begin() == *Current)
162       Current++;
163     return Current != user_end();
164   }
165 
166   void replaceAllUsesWith(VPValue *New);
167 
168   VPDef *getDef() { return Def; }
169 
170   /// Returns the underlying IR value, if this VPValue is defined outside the
171   /// scope of VPlan. Returns nullptr if the VPValue is defined by a VPDef
172   /// inside a VPlan.
173   Value *getLiveInIRValue() {
174     assert(!getDef() &&
175            "VPValue is not a live-in; it is defined by a VPDef inside a VPlan");
176     return getUnderlyingValue();
177   }
178 };
179 
180 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
181 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
182 
183 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
184 
185 /// This class augments VPValue with operands which provide the inverse def-use
186 /// edges from VPValue's users to their defs.
187 class VPUser {
188   SmallVector<VPValue *, 2> Operands;
189 
190 protected:
191   /// Print the operands to \p O.
192   void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const;
193 
194 public:
195   VPUser() {}
196   VPUser(ArrayRef<VPValue *> Operands) {
197     for (VPValue *Operand : Operands)
198       addOperand(Operand);
199   }
200 
201   VPUser(std::initializer_list<VPValue *> Operands)
202       : VPUser(ArrayRef<VPValue *>(Operands)) {}
203   template <typename IterT> VPUser(iterator_range<IterT> Operands) {
204     for (VPValue *Operand : Operands)
205       addOperand(Operand);
206   }
207 
208   VPUser(const VPUser &) = delete;
209   VPUser &operator=(const VPUser &) = delete;
210   virtual ~VPUser() {
211     for (VPValue *Op : operands())
212       Op->removeUser(*this);
213   }
214 
215   void addOperand(VPValue *Operand) {
216     Operands.push_back(Operand);
217     Operand->addUser(*this);
218   }
219 
220   unsigned getNumOperands() const { return Operands.size(); }
221   inline VPValue *getOperand(unsigned N) const {
222     assert(N < Operands.size() && "Operand index out of bounds");
223     return Operands[N];
224   }
225 
226   void setOperand(unsigned I, VPValue *New) {
227     Operands[I]->removeUser(*this);
228     Operands[I] = New;
229     New->addUser(*this);
230   }
231 
232   void removeLastOperand() {
233     VPValue *Op = Operands.pop_back_val();
234     Op->removeUser(*this);
235   }
236 
237   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
238   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
239   typedef iterator_range<operand_iterator> operand_range;
240   typedef iterator_range<const_operand_iterator> const_operand_range;
241 
242   operand_iterator op_begin() { return Operands.begin(); }
243   const_operand_iterator op_begin() const { return Operands.begin(); }
244   operand_iterator op_end() { return Operands.end(); }
245   const_operand_iterator op_end() const { return Operands.end(); }
246   operand_range operands() { return operand_range(op_begin(), op_end()); }
247   const_operand_range operands() const {
248     return const_operand_range(op_begin(), op_end());
249   }
250 
251   /// Method to support type inquiry through isa, cast, and dyn_cast.
252   static inline bool classof(const VPDef *Recipe);
253 };
254 
255 /// This class augments a recipe with a set of VPValues defined by the recipe.
256 /// It allows recipes to define zero, one or multiple VPValues. A VPDef owns
257 /// the VPValues it defines and is responsible for deleting its defined values.
258 /// Single-value VPDefs that also inherit from VPValue must make sure to inherit
259 /// from VPDef before VPValue.
260 class VPDef {
261   friend class VPValue;
262 
263   /// Subclass identifier (for isa/dyn_cast).
264   const unsigned char SubclassID;
265 
266   /// The VPValues defined by this VPDef.
267   TinyPtrVector<VPValue *> DefinedValues;
268 
269   /// Add \p V as a defined value by this VPDef.
270   void addDefinedValue(VPValue *V) {
271     assert(V->getDef() == this &&
272            "can only add VPValue already linked with this VPDef");
273     DefinedValues.push_back(V);
274   }
275 
276   /// Remove \p V from the values defined by this VPDef. \p V must be a defined
277   /// value of this VPDef.
278   void removeDefinedValue(VPValue *V) {
279     assert(V->getDef() == this &&
280            "can only remove VPValue linked with this VPDef");
281     assert(is_contained(DefinedValues, V) &&
282            "VPValue to remove must be in DefinedValues");
283     erase_value(DefinedValues, V);
284     V->Def = nullptr;
285   }
286 
287 public:
288   /// An enumeration for keeping track of the concrete subclass of VPRecipeBase
289   /// that is actually instantiated. Values of this enumeration are kept in the
290   /// SubclassID field of the VPRecipeBase objects. They are used for concrete
291   /// type identification.
292   using VPRecipeTy = enum {
293     VPBlendSC,
294     VPBranchOnMaskSC,
295     VPInstructionSC,
296     VPInterleaveSC,
297     VPPredInstPHISC,
298     VPReductionSC,
299     VPReplicateSC,
300     VPWidenCallSC,
301     VPWidenCanonicalIVSC,
302     VPWidenGEPSC,
303     VPWidenIntOrFpInductionSC,
304     VPWidenMemoryInstructionSC,
305     VPWidenPHISC,
306     VPWidenSC,
307     VPWidenSelectSC
308   };
309 
310   VPDef(const unsigned char SC) : SubclassID(SC) {}
311 
312   virtual ~VPDef() {
313     for (VPValue *D : make_early_inc_range(DefinedValues)) {
314       assert(D->Def == this &&
315              "all defined VPValues should point to the containing VPDef");
316       assert(D->getNumUsers() == 0 &&
317              "all defined VPValues should have no more users");
318       D->Def = nullptr;
319       delete D;
320     }
321   }
322 
323   /// Returns the VPValue with index \p I defined by the VPDef.
324   VPValue *getVPValue(unsigned I = 0) {
325     assert(DefinedValues[I] && "defined value must be non-null");
326     return DefinedValues[I];
327   }
328   const VPValue *getVPValue(unsigned I = 0) const {
329     assert(DefinedValues[I] && "defined value must be non-null");
330     return DefinedValues[I];
331   }
332 
333   /// Returns an ArrayRef of the values defined by the VPDef.
334   ArrayRef<VPValue *> definedValues() { return DefinedValues; }
335   /// Returns an ArrayRef of the values defined by the VPDef.
336   ArrayRef<VPValue *> definedValues() const { return DefinedValues; }
337 
338   /// Returns the number of values defined by the VPDef.
339   unsigned getNumDefinedValues() const { return DefinedValues.size(); }
340 
341   /// \return an ID for the concrete type of this object.
342   /// This is used to implement the classof checks. This should not be used
343   /// for any other purpose, as the values may change as LLVM evolves.
344   unsigned getVPDefID() const { return SubclassID; }
345 
346   /// Dump the VPDef to stderr (for debugging).
347   void dump() const;
348 
349   /// Each concrete VPDef prints itself.
350   virtual void print(raw_ostream &O, const Twine &Indent,
351                      VPSlotTracker &SlotTracker) const = 0;
352 };
353 
354 class VPlan;
355 class VPBasicBlock;
356 class VPRegionBlock;
357 
358 /// This class can be used to assign consecutive numbers to all VPValues in a
359 /// VPlan and allows querying the numbering for printing, similar to the
360 /// ModuleSlotTracker for IR values.
361 class VPSlotTracker {
362   DenseMap<const VPValue *, unsigned> Slots;
363   unsigned NextSlot = 0;
364 
365   void assignSlots(const VPBlockBase *VPBB);
366   void assignSlots(const VPRegionBlock *Region);
367   void assignSlots(const VPBasicBlock *VPBB);
368   void assignSlot(const VPValue *V);
369 
370   void assignSlots(const VPlan &Plan);
371 
372 public:
373   VPSlotTracker(const VPlan *Plan = nullptr) {
374     if (Plan)
375       assignSlots(*Plan);
376   }
377 
378   unsigned getSlot(const VPValue *V) const {
379     auto I = Slots.find(V);
380     if (I == Slots.end())
381       return -1;
382     return I->second;
383   }
384 };
385 
386 } // namespace llvm
387 
388 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
389