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