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