xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h (revision ae1e353a258a396022b8eff763cb0a168f05314c)
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
14 ///  |-- VPUser
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 #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 class VPSlotTracker;
36 
37 // This is the base class of the VPlan Def/Use graph, used for modeling the data
38 // flow into, within and out of the VPlan. VPValues can stand for live-ins
39 // coming from the input IR, instructions which VPlan will generate if executed
40 // and live-outs which the VPlan will need to fix accordingly.
41 class VPValue {
42   friend class VPBuilder;
43   friend struct VPlanTransforms;
44   friend class VPBasicBlock;
45   friend class VPInterleavedAccessInfo;
46   friend class VPSlotTracker;
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, VPUserSC, VPInstructionSC };
82 
83   VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
84   VPValue(const VPValue &) = delete;
85   VPValue &operator=(const VPValue &) = delete;
86 
87   /// \return an ID for the concrete type of this object.
88   /// This is used to implement the classof checks. This should not be used
89   /// for any other purpose, as the values may change as LLVM evolves.
90   unsigned getVPValueID() const { return SubclassID; }
91 
92   void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;
93   void print(raw_ostream &OS, VPSlotTracker &Tracker) const;
94 
95   unsigned getNumUsers() const { return Users.size(); }
96   void addUser(VPUser &User) { Users.push_back(&User); }
97 
98   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
99   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
100   typedef iterator_range<user_iterator> user_range;
101   typedef iterator_range<const_user_iterator> const_user_range;
102 
103   user_iterator user_begin() { return Users.begin(); }
104   const_user_iterator user_begin() const { return Users.begin(); }
105   user_iterator user_end() { return Users.end(); }
106   const_user_iterator user_end() const { return Users.end(); }
107   user_range users() { return user_range(user_begin(), user_end()); }
108   const_user_range users() const {
109     return const_user_range(user_begin(), user_end());
110   }
111 
112   /// Returns true if the value has more than one unique user.
113   bool hasMoreThanOneUniqueUser() {
114     if (getNumUsers() == 0)
115       return false;
116 
117     // Check if all users match the first user.
118     auto Current = std::next(user_begin());
119     while (Current != user_end() && *user_begin() == *Current)
120       Current++;
121     return Current != user_end();
122   }
123 
124   void replaceAllUsesWith(VPValue *New);
125 };
126 
127 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
128 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
129 
130 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
131 
132 /// This class augments VPValue with operands which provide the inverse def-use
133 /// edges from VPValue's users to their defs.
134 class VPUser : public VPValue {
135   SmallVector<VPValue *, 2> Operands;
136 
137 protected:
138   VPUser(const unsigned char SC) : VPValue(SC) {}
139   VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) {
140     for (VPValue *Operand : Operands)
141       addOperand(Operand);
142   }
143 
144 public:
145   VPUser() : VPValue(VPValue::VPUserSC) {}
146   VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {}
147   VPUser(std::initializer_list<VPValue *> Operands)
148       : VPUser(ArrayRef<VPValue *>(Operands)) {}
149   template <typename IterT>
150   VPUser(iterator_range<IterT> Operands) : VPValue(VPValue::VPUserSC) {
151     for (VPValue *Operand : Operands)
152       addOperand(Operand);
153   }
154 
155   VPUser(const VPUser &) = delete;
156   VPUser &operator=(const VPUser &) = delete;
157 
158   /// Method to support type inquiry through isa, cast, and dyn_cast.
159   static inline bool classof(const VPValue *V) {
160     return V->getVPValueID() >= VPUserSC &&
161            V->getVPValueID() <= VPInstructionSC;
162   }
163 
164   void addOperand(VPValue *Operand) {
165     Operands.push_back(Operand);
166     Operand->addUser(*this);
167   }
168 
169   unsigned getNumOperands() const { return Operands.size(); }
170   inline VPValue *getOperand(unsigned N) const {
171     assert(N < Operands.size() && "Operand index out of bounds");
172     return Operands[N];
173   }
174 
175   void setOperand(unsigned I, VPValue *New) { Operands[I] = New; }
176 
177   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
178   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
179   typedef iterator_range<operand_iterator> operand_range;
180   typedef iterator_range<const_operand_iterator> const_operand_range;
181 
182   operand_iterator op_begin() { return Operands.begin(); }
183   const_operand_iterator op_begin() const { return Operands.begin(); }
184   operand_iterator op_end() { return Operands.end(); }
185   const_operand_iterator op_end() const { return Operands.end(); }
186   operand_range operands() { return operand_range(op_begin(), op_end()); }
187   const_operand_range operands() const {
188     return const_operand_range(op_begin(), op_end());
189   }
190 };
191 class VPlan;
192 class VPBasicBlock;
193 class VPRegionBlock;
194 
195 /// This class can be used to assign consecutive numbers to all VPValues in a
196 /// VPlan and allows querying the numbering for printing, similar to the
197 /// ModuleSlotTracker for IR values.
198 class VPSlotTracker {
199   DenseMap<const VPValue *, unsigned> Slots;
200   unsigned NextSlot = 0;
201 
202   void assignSlots(const VPBlockBase *VPBB);
203   void assignSlots(const VPRegionBlock *Region);
204   void assignSlots(const VPBasicBlock *VPBB);
205   void assignSlot(const VPValue *V);
206 
207   void assignSlots(const VPlan &Plan);
208 
209 public:
210   VPSlotTracker(const VPlan *Plan) {
211     if (Plan)
212       assignSlots(*Plan);
213   }
214 
215   unsigned getSlot(const VPValue *V) const {
216     auto I = Slots.find(V);
217     if (I == Slots.end())
218       return -1;
219     return I->second;
220   }
221 };
222 
223 } // namespace llvm
224 
225 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
226