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