blob: 972f0fa7da66202583fc92b7e972a87c8fba671d [file] [log] [blame]
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001//===- 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
30namespace llvm {
31
32// Forward declarations.
33class 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.
39class VPValue {
Diego Caballero168d04d2018-05-21 18:14:23 +000040 friend class VPBuilder;
Hideki Saitoea7f3032018-09-14 00:36:00 +000041 friend class VPlanHCFGTransforms;
42 friend class VPBasicBlock;
Florian Hahna4dc7fe2018-11-13 15:58:18 +000043 friend class VPInterleavedAccessInfo;
Hideki Saitoea7f3032018-09-14 00:36:00 +000044
Gil Rapaport8b9d1f32017-11-20 12:01:47 +000045private:
46 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
47
48 SmallVector<VPUser *, 1> Users;
49
50protected:
Diego Caballero168d04d2018-05-21 18:14:23 +000051 // 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
67 // Set \p Val as the underlying Value of this VPValue.
68 void setUnderlyingValue(Value *Val) {
69 assert(!UnderlyingVal && "Underlying Value is already set.");
70 UnderlyingVal = Val;
71 }
Gil Rapaport8b9d1f32017-11-20 12:01:47 +000072
73public:
74 /// An enumeration for keeping track of the concrete subclass of VPValue that
75 /// are actually instantiated. Values of this enumeration are kept in the
76 /// SubclassID field of the VPValue objects. They are used for concrete
77 /// type identification.
78 enum { VPValueSC, VPUserSC, VPInstructionSC };
79
Diego Caballero168d04d2018-05-21 18:14:23 +000080 VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {}
Gil Rapaport8b9d1f32017-11-20 12:01:47 +000081 VPValue(const VPValue &) = delete;
82 VPValue &operator=(const VPValue &) = delete;
83
84 /// \return an ID for the concrete type of this object.
85 /// This is used to implement the classof checks. This should not be used
86 /// for any other purpose, as the values may change as LLVM evolves.
87 unsigned getVPValueID() const { return SubclassID; }
88
89 void printAsOperand(raw_ostream &OS) const {
90 OS << "%vp" << (unsigned short)(unsigned long long)this;
91 }
92
93 unsigned getNumUsers() const { return Users.size(); }
94 void addUser(VPUser &User) { Users.push_back(&User); }
95
96 typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
97 typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
98 typedef iterator_range<user_iterator> user_range;
99 typedef iterator_range<const_user_iterator> const_user_range;
100
101 user_iterator user_begin() { return Users.begin(); }
102 const_user_iterator user_begin() const { return Users.begin(); }
103 user_iterator user_end() { return Users.end(); }
104 const_user_iterator user_end() const { return Users.end(); }
105 user_range users() { return user_range(user_begin(), user_end()); }
106 const_user_range users() const {
107 return const_user_range(user_begin(), user_end());
108 }
109};
110
111typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
112typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
113
114raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
115
116/// This class augments VPValue with operands which provide the inverse def-use
117/// edges from VPValue's users to their defs.
118class VPUser : public VPValue {
119private:
120 SmallVector<VPValue *, 2> Operands;
121
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000122protected:
123 VPUser(const unsigned char SC) : VPValue(SC) {}
124 VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) {
125 for (VPValue *Operand : Operands)
126 addOperand(Operand);
127 }
128
129public:
130 VPUser() : VPValue(VPValue::VPUserSC) {}
131 VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {}
132 VPUser(std::initializer_list<VPValue *> Operands)
133 : VPUser(ArrayRef<VPValue *>(Operands)) {}
134 VPUser(const VPUser &) = delete;
135 VPUser &operator=(const VPUser &) = delete;
136
137 /// Method to support type inquiry through isa, cast, and dyn_cast.
138 static inline bool classof(const VPValue *V) {
139 return V->getVPValueID() >= VPUserSC &&
140 V->getVPValueID() <= VPInstructionSC;
141 }
142
Diego Caballero168d04d2018-05-21 18:14:23 +0000143 void addOperand(VPValue *Operand) {
144 Operands.push_back(Operand);
145 Operand->addUser(*this);
146 }
147
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000148 unsigned getNumOperands() const { return Operands.size(); }
149 inline VPValue *getOperand(unsigned N) const {
150 assert(N < Operands.size() && "Operand index out of bounds");
151 return Operands[N];
152 }
153
154 typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
155 typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
156 typedef iterator_range<operand_iterator> operand_range;
157 typedef iterator_range<const_operand_iterator> const_operand_range;
158
159 operand_iterator op_begin() { return Operands.begin(); }
160 const_operand_iterator op_begin() const { return Operands.begin(); }
161 operand_iterator op_end() { return Operands.end(); }
162 const_operand_iterator op_end() const { return Operands.end(); }
163 operand_range operands() { return operand_range(op_begin(), op_end()); }
164 const_operand_range operands() const {
165 return const_operand_range(op_begin(), op_end());
166 }
167};
168
169} // namespace llvm
170
171#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H