xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h (revision f58ad3129c874aeda40ce720a0aede59480e02d6)
1 //===- VPlanValue.h - Represent Values in Vectorizer Plan -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains the declarations of the entities induced by Vectorization
12 /// Plans, e.g. the instructions the VPlan intends to generate if executed.
13 /// VPlan models the following entities:
14 /// VPValue
15 ///  |-- VPUser
16 ///  |    |-- VPInstruction
17 /// These are documented in docs/VectorizationPlan.rst.
18 ///
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
22 #define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
23 
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallVector.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 // 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 private:
42   const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
43 
44   SmallVector<VPUser *, 1> Users;
45 
46 protected:
47   // Hold the underlying Value, if any, attached to this VPValue.
48   Value *UnderlyingVal;
49 
50   VPValue(const unsigned char SC, Value *UV = nullptr)
51       : SubclassID(SC), UnderlyingVal(UV) {}
52 
53   // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to
54   // the front-end and back-end of VPlan so that the middle-end is as
55   // independent as possible of the underlying IR. We grant access to the
56   // underlying IR using friendship. In that way, we should be able to use VPlan
57   // for multiple underlying IRs (Polly?) by providing a new VPlan front-end,
58   // back-end and analysis information for the new IR.
59 
60   /// Return the underlying Value attached to this VPValue.
61   Value *getUnderlyingValue() { return UnderlyingVal; }
62 
63   // Set \p Val as the underlying Value of this VPValue.
64   void setUnderlyingValue(Value *Val) {
65     assert(!UnderlyingVal && "Underlying Value is already set.");
66     UnderlyingVal = Val;
67   }
68 
69 public:
70   /// An enumeration for keeping track of the concrete subclass of VPValue that
71   /// are actually instantiated. Values of this enumeration are kept in the
72   /// SubclassID field of the VPValue objects. They are used for concrete
73   /// type identification.
74   enum { VPValueSC, VPUserSC, VPInstructionSC };
75 
76   VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
77   VPValue(const VPValue &) = delete;
78   VPValue &operator=(const VPValue &) = delete;
79 
80   /// \return an ID for the concrete type of this object.
81   /// This is used to implement the classof checks. This should not be used
82   /// for any other purpose, as the values may change as LLVM evolves.
83   unsigned getVPValueID() const { return SubclassID; }
84 
85   void printAsOperand(raw_ostream &OS) const {
86     OS << "%vp" << (unsigned short)(unsigned long long)this;
87   }
88 
89   unsigned getNumUsers() const { return Users.size(); }
90   void addUser(VPUser &User) { Users.push_back(&User); }
91 
92   typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
93   typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
94   typedef iterator_range<user_iterator> user_range;
95   typedef iterator_range<const_user_iterator> const_user_range;
96 
97   user_iterator user_begin() { return Users.begin(); }
98   const_user_iterator user_begin() const { return Users.begin(); }
99   user_iterator user_end() { return Users.end(); }
100   const_user_iterator user_end() const { return Users.end(); }
101   user_range users() { return user_range(user_begin(), user_end()); }
102   const_user_range users() const {
103     return const_user_range(user_begin(), user_end());
104   }
105 };
106 
107 typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
108 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
109 
110 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
111 
112 /// This class augments VPValue with operands which provide the inverse def-use
113 /// edges from VPValue's users to their defs.
114 class VPUser : public VPValue {
115 private:
116   SmallVector<VPValue *, 2> Operands;
117 
118 protected:
119   VPUser(const unsigned char SC) : VPValue(SC) {}
120   VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) {
121     for (VPValue *Operand : Operands)
122       addOperand(Operand);
123   }
124 
125 public:
126   VPUser() : VPValue(VPValue::VPUserSC) {}
127   VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {}
128   VPUser(std::initializer_list<VPValue *> Operands)
129       : VPUser(ArrayRef<VPValue *>(Operands)) {}
130   VPUser(const VPUser &) = delete;
131   VPUser &operator=(const VPUser &) = delete;
132 
133   /// Method to support type inquiry through isa, cast, and dyn_cast.
134   static inline bool classof(const VPValue *V) {
135     return V->getVPValueID() >= VPUserSC &&
136            V->getVPValueID() <= VPInstructionSC;
137   }
138 
139   void addOperand(VPValue *Operand) {
140     Operands.push_back(Operand);
141     Operand->addUser(*this);
142   }
143 
144   unsigned getNumOperands() const { return Operands.size(); }
145   inline VPValue *getOperand(unsigned N) const {
146     assert(N < Operands.size() && "Operand index out of bounds");
147     return Operands[N];
148   }
149 
150   typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
151   typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
152   typedef iterator_range<operand_iterator> operand_range;
153   typedef iterator_range<const_operand_iterator> const_operand_range;
154 
155   operand_iterator op_begin() { return Operands.begin(); }
156   const_operand_iterator op_begin() const { return Operands.begin(); }
157   operand_iterator op_end() { return Operands.end(); }
158   const_operand_iterator op_end() const { return Operands.end(); }
159   operand_range operands() { return operand_range(op_begin(), op_end()); }
160   const_operand_range operands() const {
161     return const_operand_range(op_begin(), op_end());
162   }
163 };
164 
165 } // namespace llvm
166 
167 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
168