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