blob: 50966891e0eb39ed9bb2ef12ccabf800ee436adb [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 {
40private:
41 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
42
43 SmallVector<VPUser *, 1> Users;
44
45protected:
46 VPValue(const unsigned char SC) : SubclassID(SC) {}
47
48public:
49 /// An enumeration for keeping track of the concrete subclass of VPValue that
50 /// are actually instantiated. Values of this enumeration are kept in the
51 /// SubclassID field of the VPValue objects. They are used for concrete
52 /// type identification.
53 enum { VPValueSC, VPUserSC, VPInstructionSC };
54
55 VPValue() : SubclassID(VPValueSC) {}
56 VPValue(const VPValue &) = delete;
57 VPValue &operator=(const VPValue &) = delete;
58
59 /// \return an ID for the concrete type of this object.
60 /// This is used to implement the classof checks. This should not be used
61 /// for any other purpose, as the values may change as LLVM evolves.
62 unsigned getVPValueID() const { return SubclassID; }
63
64 void printAsOperand(raw_ostream &OS) const {
65 OS << "%vp" << (unsigned short)(unsigned long long)this;
66 }
67
68 unsigned getNumUsers() const { return Users.size(); }
69 void addUser(VPUser &User) { Users.push_back(&User); }
70
71 typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
72 typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
73 typedef iterator_range<user_iterator> user_range;
74 typedef iterator_range<const_user_iterator> const_user_range;
75
76 user_iterator user_begin() { return Users.begin(); }
77 const_user_iterator user_begin() const { return Users.begin(); }
78 user_iterator user_end() { return Users.end(); }
79 const_user_iterator user_end() const { return Users.end(); }
80 user_range users() { return user_range(user_begin(), user_end()); }
81 const_user_range users() const {
82 return const_user_range(user_begin(), user_end());
83 }
84};
85
86typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
87typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
88
89raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
90
91/// This class augments VPValue with operands which provide the inverse def-use
92/// edges from VPValue's users to their defs.
93class VPUser : public VPValue {
94private:
95 SmallVector<VPValue *, 2> Operands;
96
97 void addOperand(VPValue *Operand) {
98 Operands.push_back(Operand);
99 Operand->addUser(*this);
100 }
101
102protected:
103 VPUser(const unsigned char SC) : VPValue(SC) {}
104 VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) {
105 for (VPValue *Operand : Operands)
106 addOperand(Operand);
107 }
108
109public:
110 VPUser() : VPValue(VPValue::VPUserSC) {}
111 VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {}
112 VPUser(std::initializer_list<VPValue *> Operands)
113 : VPUser(ArrayRef<VPValue *>(Operands)) {}
114 VPUser(const VPUser &) = delete;
115 VPUser &operator=(const VPUser &) = delete;
116
117 /// Method to support type inquiry through isa, cast, and dyn_cast.
118 static inline bool classof(const VPValue *V) {
119 return V->getVPValueID() >= VPUserSC &&
120 V->getVPValueID() <= VPInstructionSC;
121 }
122
123 unsigned getNumOperands() const { return Operands.size(); }
124 inline VPValue *getOperand(unsigned N) const {
125 assert(N < Operands.size() && "Operand index out of bounds");
126 return Operands[N];
127 }
128
129 typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
130 typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
131 typedef iterator_range<operand_iterator> operand_range;
132 typedef iterator_range<const_operand_iterator> const_operand_range;
133
134 operand_iterator op_begin() { return Operands.begin(); }
135 const_operand_iterator op_begin() const { return Operands.begin(); }
136 operand_iterator op_end() { return Operands.end(); }
137 const_operand_iterator op_end() const { return Operands.end(); }
138 operand_range operands() { return operand_range(op_begin(), op_end()); }
139 const_operand_range operands() const {
140 return const_operand_range(op_begin(), op_end());
141 }
142};
143
144} // namespace llvm
145
146#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H