xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h (revision ca38652b9a58fc4e15b12e6f3914bfdb124cb6cd)
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
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/SmallVector.h"
25 #include "llvm/ADT/iterator_range.h"
26 
27 namespace llvm {
28 
29 // Forward declarations.
30 class raw_ostream;
31 class Value;
32 class VPSlotTracker;
33 class VPUser;
34 class VPRecipeBase;
35 
36 // This is the base class of the VPlan Def/Use graph, used for modeling the data
37 // flow into, within and out of the VPlan. VPValues can stand for live-ins
38 // coming from the input IR, instructions which VPlan will generate if executed
39 // and live-outs which the VPlan will need to fix accordingly.
40 class VPValue {
41   friend class VPBuilder;
42   friend struct VPlanTransforms;
43   friend class VPBasicBlock;
44   friend class VPInterleavedAccessInfo;
45   friend class VPSlotTracker;
46   friend class VPRecipeBase;
47 
48   const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
49 
50   SmallVector<VPUser *, 1> Users;
51 
52 protected:
53   // Hold the underlying Value, if any, attached to this VPValue.
54   Value *UnderlyingVal;
55 
56   VPValue(const unsigned char SC, Value *UV = nullptr)
57       : SubclassID(SC), UnderlyingVal(UV) {}
58 
59   // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
60   // the front-end and back-end of VPlan so that the middle-end is as
61   // independent as possible of the underlying IR. We grant access to the
62   // underlying IR using friendship. In that way, we should be able to use VPlan
63   // for multiple underlying IRs (Polly?) by providing a new VPlan front-end,
64   // back-end and analysis information for the new IR.
65 
66   /// Return the underlying Value attached to this VPValue.
67   Value *getUnderlyingValue() { return UnderlyingVal; }
68   const Value *getUnderlyingValue() const { return UnderlyingVal; }
69 
70   // Set \p Val as the underlying Value of this VPValue.
71   void setUnderlyingValue(Value *Val) {
72     assert(!UnderlyingVal && "Underlying Value is already set.");
73     UnderlyingVal = Val;
74   }
75 
76 public:
77   /// An enumeration for keeping track of the concrete subclass of VPValue that
78   /// are actually instantiated. Values of this enumeration are kept in the
79   /// SubclassID field of the VPValue objects. They are used for concrete
80   /// type identification.
81   enum { VPValueSC, VPInstructionSC, VPMemoryInstructionSC };
82 
83   VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
84   VPValue(const VPValue &) = delete;
85   VPValue &operator=(const VPValue &) = delete;
86 
87   virtual ~VPValue() {
88     assert(Users.empty() && "trying to delete a VPValue with remaining users");
89   }
90 
91   /// \return an ID for the concrete type of this object.
92   /// This is used to implement the classof checks. This should not be used
93   /// for any other purpose, as the values may change as LLVM evolves.
94   unsigned getVPValueID() const { return SubclassID; }
95 
96   void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;
97   void print(raw_ostream &OS, VPSlotTracker &Tracker) const;
98 
99   /// Dump the value to stderr (for debugging).
100   void dump() const;
101 
102   unsigned getNumUsers() const { return Users.size(); }
103   void addUser(VPUser &User) { Users.push_back(&User); }
104 
105   /// Remove a single \p User from the list of users.
106   void removeUser(VPUser &User) {
107     bool Found = false;
108     // The same user can be added multiple times, e.g. because the same VPValue
109     // is used twice by the same VPUser. Remove a single one.
110     erase_if(Users, [&User, &Found](VPUser *Other) {
111       if (Found)
112         return false;
113       if (Other == &User) {
114         Found = true;
115         return true;
116       }
117       return false;
118     });
119   }
120 
121   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
122   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
123   typedef iterator_range<user_iterator> user_range;
124   typedef iterator_range<const_user_iterator> const_user_range;
125 
126   user_iterator user_begin() { return Users.begin(); }
127   const_user_iterator user_begin() const { return Users.begin(); }
128   user_iterator user_end() { return Users.end(); }
129   const_user_iterator user_end() const { return Users.end(); }
130   user_range users() { return user_range(user_begin(), user_end()); }
131   const_user_range users() const {
132     return const_user_range(user_begin(), user_end());
133   }
134 
135   /// Returns true if the value has more than one unique user.
136   bool hasMoreThanOneUniqueUser() {
137     if (getNumUsers() == 0)
138       return false;
139 
140     // Check if all users match the first user.
141     auto Current = std::next(user_begin());
142     while (Current != user_end() && *user_begin() == *Current)
143       Current++;
144     return Current != user_end();
145   }
146 
147   void replaceAllUsesWith(VPValue *New);
148 };
149 
150 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
151 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
152 
153 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
154 
155 /// This class augments VPValue with operands which provide the inverse def-use
156 /// edges from VPValue's users to their defs.
157 class VPUser {
158   SmallVector<VPValue *, 2> Operands;
159 
160 public:
161   VPUser() {}
162   VPUser(ArrayRef<VPValue *> Operands) {
163     for (VPValue *Operand : Operands)
164       addOperand(Operand);
165   }
166 
167   VPUser(std::initializer_list<VPValue *> Operands)
168       : VPUser(ArrayRef<VPValue *>(Operands)) {}
169   template <typename IterT> VPUser(iterator_range<IterT> Operands) {
170     for (VPValue *Operand : Operands)
171       addOperand(Operand);
172   }
173 
174   VPUser(const VPUser &) = delete;
175   VPUser &operator=(const VPUser &) = delete;
176   virtual ~VPUser() {
177     for (VPValue *Op : operands())
178       Op->removeUser(*this);
179   }
180 
181   void addOperand(VPValue *Operand) {
182     Operands.push_back(Operand);
183     Operand->addUser(*this);
184   }
185 
186   unsigned getNumOperands() const { return Operands.size(); }
187   inline VPValue *getOperand(unsigned N) const {
188     assert(N < Operands.size() && "Operand index out of bounds");
189     return Operands[N];
190   }
191 
192   void setOperand(unsigned I, VPValue *New) {
193     Operands[I]->removeUser(*this);
194     Operands[I] = New;
195     New->addUser(*this);
196   }
197 
198   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
199   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
200   typedef iterator_range<operand_iterator> operand_range;
201   typedef iterator_range<const_operand_iterator> const_operand_range;
202 
203   operand_iterator op_begin() { return Operands.begin(); }
204   const_operand_iterator op_begin() const { return Operands.begin(); }
205   operand_iterator op_end() { return Operands.end(); }
206   const_operand_iterator op_end() const { return Operands.end(); }
207   operand_range operands() { return operand_range(op_begin(), op_end()); }
208   const_operand_range operands() const {
209     return const_operand_range(op_begin(), op_end());
210   }
211 
212   /// Method to support type inquiry through isa, cast, and dyn_cast.
213   static inline bool classof(const VPRecipeBase *Recipe);
214 };
215 class VPlan;
216 class VPBasicBlock;
217 class VPRegionBlock;
218 
219 /// This class can be used to assign consecutive numbers to all VPValues in a
220 /// VPlan and allows querying the numbering for printing, similar to the
221 /// ModuleSlotTracker for IR values.
222 class VPSlotTracker {
223   DenseMap<const VPValue *, unsigned> Slots;
224   unsigned NextSlot = 0;
225 
226   void assignSlots(const VPBlockBase *VPBB);
227   void assignSlots(const VPRegionBlock *Region);
228   void assignSlots(const VPBasicBlock *VPBB);
229   void assignSlot(const VPValue *V);
230 
231   void assignSlots(const VPlan &Plan);
232 
233 public:
234   VPSlotTracker(const VPlan *Plan) {
235     if (Plan)
236       assignSlots(*Plan);
237   }
238 
239   unsigned getSlot(const VPValue *V) const {
240     auto I = Slots.find(V);
241     if (I == Slots.end())
242       return -1;
243     return I->second;
244   }
245 };
246 
247 } // namespace llvm
248 
249 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
250