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 class VPWidenMemoryInstructionRecipe; 39 40 // This is the base class of the VPlan Def/Use graph, used for modeling the data 41 // flow into, within and out of the VPlan. VPValues can stand for live-ins 42 // coming from the input IR, instructions which VPlan will generate if executed 43 // and live-outs which the VPlan will need to fix accordingly. 44 class VPValue { 45 friend class VPBuilder; 46 friend class VPDef; 47 friend class VPInstruction; 48 friend struct VPlanTransforms; 49 friend class VPBasicBlock; 50 friend class VPInterleavedAccessInfo; 51 friend class VPSlotTracker; 52 friend class VPRecipeBase; 53 friend class VPWidenMemoryInstructionRecipe; 54 55 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast). 56 57 SmallVector<VPUser *, 1> Users; 58 59 protected: 60 // Hold the underlying Value, if any, attached to this VPValue. 61 Value *UnderlyingVal; 62 63 /// Pointer to the VPDef that defines this VPValue. If it is nullptr, the 64 /// VPValue is not defined by any recipe modeled in VPlan. 65 VPDef *Def; 66 67 VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr); 68 69 // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to 70 // the front-end and back-end of VPlan so that the middle-end is as 71 // independent as possible of the underlying IR. We grant access to the 72 // underlying IR using friendship. In that way, we should be able to use VPlan 73 // for multiple underlying IRs (Polly?) by providing a new VPlan front-end, 74 // back-end and analysis information for the new IR. 75 76 // Set \p Val as the underlying Value of this VPValue. 77 void setUnderlyingValue(Value *Val) { 78 assert(!UnderlyingVal && "Underlying Value is already set."); 79 UnderlyingVal = Val; 80 } 81 82 public: 83 /// Return the underlying Value attached to this VPValue. 84 Value *getUnderlyingValue() { return UnderlyingVal; } 85 const Value *getUnderlyingValue() const { return UnderlyingVal; } 86 87 /// An enumeration for keeping track of the concrete subclass of VPValue that 88 /// are actually instantiated. Values of this enumeration are kept in the 89 /// SubclassID field of the VPValue objects. They are used for concrete 90 /// type identification. 91 enum { 92 VPValueSC, 93 VPVBlendSC, 94 VPVInstructionSC, 95 VPVMemoryInstructionSC, 96 VPVPredInstPHI, 97 VPVReductionSC, 98 VPVReplicateSC, 99 VPVWidenSC, 100 VPVWidenCallSC, 101 VPVWidenGEPSC, 102 VPVWidenIntOrFpIndcutionSC, 103 VPVWidenPHISC, 104 VPVWidenSelectSC, 105 }; 106 107 VPValue(Value *UV = nullptr, VPDef *Def = nullptr) 108 : VPValue(VPValueSC, UV, Def) {} 109 VPValue(const VPValue &) = delete; 110 VPValue &operator=(const VPValue &) = delete; 111 112 virtual ~VPValue(); 113 114 /// \return an ID for the concrete type of this object. 115 /// This is used to implement the classof checks. This should not be used 116 /// for any other purpose, as the values may change as LLVM evolves. 117 unsigned getVPValueID() const { return SubclassID; } 118 119 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 120 void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const; 121 void print(raw_ostream &OS, VPSlotTracker &Tracker) const; 122 123 /// Dump the value to stderr (for debugging). 124 void dump() const; 125 #endif 126 127 unsigned getNumUsers() const { return Users.size(); } 128 void addUser(VPUser &User) { Users.push_back(&User); } 129 130 /// Remove a single \p User from the list of users. 131 void removeUser(VPUser &User) { 132 bool Found = false; 133 // The same user can be added multiple times, e.g. because the same VPValue 134 // is used twice by the same VPUser. Remove a single one. 135 erase_if(Users, [&User, &Found](VPUser *Other) { 136 if (Found) 137 return false; 138 if (Other == &User) { 139 Found = true; 140 return true; 141 } 142 return false; 143 }); 144 } 145 146 typedef SmallVectorImpl<VPUser *>::iterator user_iterator; 147 typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator; 148 typedef iterator_range<user_iterator> user_range; 149 typedef iterator_range<const_user_iterator> const_user_range; 150 151 user_iterator user_begin() { return Users.begin(); } 152 const_user_iterator user_begin() const { return Users.begin(); } 153 user_iterator user_end() { return Users.end(); } 154 const_user_iterator user_end() const { return Users.end(); } 155 user_range users() { return user_range(user_begin(), user_end()); } 156 const_user_range users() const { 157 return const_user_range(user_begin(), user_end()); 158 } 159 160 /// Returns true if the value has more than one unique user. 161 bool hasMoreThanOneUniqueUser() { 162 if (getNumUsers() == 0) 163 return false; 164 165 // Check if all users match the first user. 166 auto Current = std::next(user_begin()); 167 while (Current != user_end() && *user_begin() == *Current) 168 Current++; 169 return Current != user_end(); 170 } 171 172 void replaceAllUsesWith(VPValue *New); 173 174 VPDef *getDef() { return Def; } 175 176 /// Returns the underlying IR value, if this VPValue is defined outside the 177 /// scope of VPlan. Returns nullptr if the VPValue is defined by a VPDef 178 /// inside a VPlan. 179 Value *getLiveInIRValue() { 180 assert(!getDef() && 181 "VPValue is not a live-in; it is defined by a VPDef inside a VPlan"); 182 return getUnderlyingValue(); 183 } 184 }; 185 186 typedef DenseMap<Value *, VPValue *> Value2VPValueTy; 187 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy; 188 189 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V); 190 191 /// This class augments VPValue with operands which provide the inverse def-use 192 /// edges from VPValue's users to their defs. 193 class VPUser { 194 public: 195 /// Subclass identifier (for isa/dyn_cast). 196 enum class VPUserID { 197 Recipe, 198 // TODO: Currently VPUsers are used in VPBlockBase, but in the future the 199 // only VPUsers should either be recipes or live-outs. 200 Block 201 }; 202 203 private: 204 SmallVector<VPValue *, 2> Operands; 205 206 VPUserID ID; 207 208 protected: 209 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 210 /// Print the operands to \p O. 211 void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const; 212 #endif 213 214 VPUser(ArrayRef<VPValue *> Operands, VPUserID ID) : ID(ID) { 215 for (VPValue *Operand : Operands) 216 addOperand(Operand); 217 } 218 219 VPUser(std::initializer_list<VPValue *> Operands, VPUserID ID) 220 : VPUser(ArrayRef<VPValue *>(Operands), ID) {} 221 222 template <typename IterT> 223 VPUser(iterator_range<IterT> Operands, VPUserID ID) : ID(ID) { 224 for (VPValue *Operand : Operands) 225 addOperand(Operand); 226 } 227 228 public: 229 VPUser() = delete; 230 VPUser(const VPUser &) = delete; 231 VPUser &operator=(const VPUser &) = delete; 232 virtual ~VPUser() { 233 for (VPValue *Op : operands()) 234 Op->removeUser(*this); 235 } 236 237 VPUserID getVPUserID() const { return ID; } 238 239 void addOperand(VPValue *Operand) { 240 Operands.push_back(Operand); 241 Operand->addUser(*this); 242 } 243 244 unsigned getNumOperands() const { return Operands.size(); } 245 inline VPValue *getOperand(unsigned N) const { 246 assert(N < Operands.size() && "Operand index out of bounds"); 247 return Operands[N]; 248 } 249 250 void setOperand(unsigned I, VPValue *New) { 251 Operands[I]->removeUser(*this); 252 Operands[I] = New; 253 New->addUser(*this); 254 } 255 256 void removeLastOperand() { 257 VPValue *Op = Operands.pop_back_val(); 258 Op->removeUser(*this); 259 } 260 261 typedef SmallVectorImpl<VPValue *>::iterator operand_iterator; 262 typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator; 263 typedef iterator_range<operand_iterator> operand_range; 264 typedef iterator_range<const_operand_iterator> const_operand_range; 265 266 operand_iterator op_begin() { return Operands.begin(); } 267 const_operand_iterator op_begin() const { return Operands.begin(); } 268 operand_iterator op_end() { return Operands.end(); } 269 const_operand_iterator op_end() const { return Operands.end(); } 270 operand_range operands() { return operand_range(op_begin(), op_end()); } 271 const_operand_range operands() const { 272 return const_operand_range(op_begin(), op_end()); 273 } 274 275 /// Method to support type inquiry through isa, cast, and dyn_cast. 276 static inline bool classof(const VPDef *Recipe); 277 }; 278 279 /// This class augments a recipe with a set of VPValues defined by the recipe. 280 /// It allows recipes to define zero, one or multiple VPValues. A VPDef owns 281 /// the VPValues it defines and is responsible for deleting its defined values. 282 /// Single-value VPDefs that also inherit from VPValue must make sure to inherit 283 /// from VPDef before VPValue. 284 class VPDef { 285 friend class VPValue; 286 287 /// Subclass identifier (for isa/dyn_cast). 288 const unsigned char SubclassID; 289 290 /// The VPValues defined by this VPDef. 291 TinyPtrVector<VPValue *> DefinedValues; 292 293 /// Add \p V as a defined value by this VPDef. 294 void addDefinedValue(VPValue *V) { 295 assert(V->getDef() == this && 296 "can only add VPValue already linked with this VPDef"); 297 DefinedValues.push_back(V); 298 } 299 300 /// Remove \p V from the values defined by this VPDef. \p V must be a defined 301 /// value of this VPDef. 302 void removeDefinedValue(VPValue *V) { 303 assert(V->getDef() == this && 304 "can only remove VPValue linked with this VPDef"); 305 assert(is_contained(DefinedValues, V) && 306 "VPValue to remove must be in DefinedValues"); 307 erase_value(DefinedValues, V); 308 V->Def = nullptr; 309 } 310 311 public: 312 /// An enumeration for keeping track of the concrete subclass of VPRecipeBase 313 /// that is actually instantiated. Values of this enumeration are kept in the 314 /// SubclassID field of the VPRecipeBase objects. They are used for concrete 315 /// type identification. 316 using VPRecipeTy = enum { 317 VPBlendSC, 318 VPBranchOnMaskSC, 319 VPInstructionSC, 320 VPInterleaveSC, 321 VPPredInstPHISC, 322 VPReductionSC, 323 VPReplicateSC, 324 VPWidenCallSC, 325 VPWidenCanonicalIVSC, 326 VPWidenGEPSC, 327 VPWidenIntOrFpInductionSC, 328 VPWidenMemoryInstructionSC, 329 VPWidenPHISC, 330 VPWidenSC, 331 VPWidenSelectSC 332 }; 333 334 VPDef(const unsigned char SC) : SubclassID(SC) {} 335 336 virtual ~VPDef() { 337 for (VPValue *D : make_early_inc_range(DefinedValues)) { 338 assert(D->Def == this && 339 "all defined VPValues should point to the containing VPDef"); 340 assert(D->getNumUsers() == 0 && 341 "all defined VPValues should have no more users"); 342 D->Def = nullptr; 343 delete D; 344 } 345 } 346 347 /// Returns the only VPValue defined by the VPDef. Can only be called for 348 /// VPDefs with a single defined value. 349 VPValue *getVPSingleValue() { 350 assert(DefinedValues.size() == 1 && "must have exactly one defined value"); 351 assert(DefinedValues[0] && "defined value must be non-null"); 352 return DefinedValues[0]; 353 } 354 const VPValue *getVPSingleValue() const { 355 assert(DefinedValues.size() == 1 && "must have exactly one defined value"); 356 assert(DefinedValues[0] && "defined value must be non-null"); 357 return DefinedValues[0]; 358 } 359 360 /// Returns the VPValue with index \p I defined by the VPDef. 361 VPValue *getVPValue(unsigned I) { 362 assert(DefinedValues[I] && "defined value must be non-null"); 363 return DefinedValues[I]; 364 } 365 const VPValue *getVPValue(unsigned I = 0) const { 366 assert(DefinedValues[I] && "defined value must be non-null"); 367 return DefinedValues[I]; 368 } 369 370 /// Returns an ArrayRef of the values defined by the VPDef. 371 ArrayRef<VPValue *> definedValues() { return DefinedValues; } 372 /// Returns an ArrayRef of the values defined by the VPDef. 373 ArrayRef<VPValue *> definedValues() const { return DefinedValues; } 374 375 /// Returns the number of values defined by the VPDef. 376 unsigned getNumDefinedValues() const { return DefinedValues.size(); } 377 378 /// \return an ID for the concrete type of this object. 379 /// This is used to implement the classof checks. This should not be used 380 /// for any other purpose, as the values may change as LLVM evolves. 381 unsigned getVPDefID() const { return SubclassID; } 382 383 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 384 /// Dump the VPDef to stderr (for debugging). 385 void dump() const; 386 387 /// Each concrete VPDef prints itself. 388 virtual void print(raw_ostream &O, const Twine &Indent, 389 VPSlotTracker &SlotTracker) const = 0; 390 #endif 391 }; 392 393 class VPlan; 394 class VPBasicBlock; 395 class VPRegionBlock; 396 397 /// This class can be used to assign consecutive numbers to all VPValues in a 398 /// VPlan and allows querying the numbering for printing, similar to the 399 /// ModuleSlotTracker for IR values. 400 class VPSlotTracker { 401 DenseMap<const VPValue *, unsigned> Slots; 402 unsigned NextSlot = 0; 403 404 void assignSlot(const VPValue *V); 405 void assignSlots(const VPlan &Plan); 406 407 public: 408 VPSlotTracker(const VPlan *Plan = nullptr) { 409 if (Plan) 410 assignSlots(*Plan); 411 } 412 413 unsigned getSlot(const VPValue *V) const { 414 auto I = Slots.find(V); 415 if (I == Slots.end()) 416 return -1; 417 return I->second; 418 } 419 }; 420 421 } // namespace llvm 422 423 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H 424