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