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