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