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