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 VPVInstructionSC, 91 VPVMemoryInstructionSC, 92 VPVReductionSC, 93 VPVWidenSC, 94 VPVWidenCallSC, 95 VPVWidenGEPSC, 96 VPVWidenSelectSC, 97 }; 98 99 VPValue(Value *UV = nullptr, VPDef *Def = nullptr) 100 : VPValue(VPValueSC, UV, Def) {} 101 VPValue(const VPValue &) = delete; 102 VPValue &operator=(const VPValue &) = delete; 103 104 virtual ~VPValue(); 105 106 /// \return an ID for the concrete type of this object. 107 /// This is used to implement the classof checks. This should not be used 108 /// for any other purpose, as the values may change as LLVM evolves. 109 unsigned getVPValueID() const { return SubclassID; } 110 111 void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const; 112 void print(raw_ostream &OS, VPSlotTracker &Tracker) const; 113 114 /// Dump the value to stderr (for debugging). 115 void dump() const; 116 117 unsigned getNumUsers() const { return Users.size(); } 118 void addUser(VPUser &User) { Users.push_back(&User); } 119 120 /// Remove a single \p User from the list of users. 121 void removeUser(VPUser &User) { 122 bool Found = false; 123 // The same user can be added multiple times, e.g. because the same VPValue 124 // is used twice by the same VPUser. Remove a single one. 125 erase_if(Users, [&User, &Found](VPUser *Other) { 126 if (Found) 127 return false; 128 if (Other == &User) { 129 Found = true; 130 return true; 131 } 132 return false; 133 }); 134 } 135 136 typedef SmallVectorImpl<VPUser *>::iterator user_iterator; 137 typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator; 138 typedef iterator_range<user_iterator> user_range; 139 typedef iterator_range<const_user_iterator> const_user_range; 140 141 user_iterator user_begin() { return Users.begin(); } 142 const_user_iterator user_begin() const { return Users.begin(); } 143 user_iterator user_end() { return Users.end(); } 144 const_user_iterator user_end() const { return Users.end(); } 145 user_range users() { return user_range(user_begin(), user_end()); } 146 const_user_range users() const { 147 return const_user_range(user_begin(), user_end()); 148 } 149 150 /// Returns true if the value has more than one unique user. 151 bool hasMoreThanOneUniqueUser() { 152 if (getNumUsers() == 0) 153 return false; 154 155 // Check if all users match the first user. 156 auto Current = std::next(user_begin()); 157 while (Current != user_end() && *user_begin() == *Current) 158 Current++; 159 return Current != user_end(); 160 } 161 162 void replaceAllUsesWith(VPValue *New); 163 164 VPDef *getDef() { return Def; } 165 }; 166 167 typedef DenseMap<Value *, VPValue *> Value2VPValueTy; 168 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy; 169 170 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V); 171 172 /// This class augments VPValue with operands which provide the inverse def-use 173 /// edges from VPValue's users to their defs. 174 class VPUser { 175 SmallVector<VPValue *, 2> Operands; 176 177 protected: 178 /// Print the operands to \p O. 179 void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const; 180 181 public: 182 VPUser() {} 183 VPUser(ArrayRef<VPValue *> Operands) { 184 for (VPValue *Operand : Operands) 185 addOperand(Operand); 186 } 187 188 VPUser(std::initializer_list<VPValue *> Operands) 189 : VPUser(ArrayRef<VPValue *>(Operands)) {} 190 template <typename IterT> VPUser(iterator_range<IterT> Operands) { 191 for (VPValue *Operand : Operands) 192 addOperand(Operand); 193 } 194 195 VPUser(const VPUser &) = delete; 196 VPUser &operator=(const VPUser &) = delete; 197 virtual ~VPUser() { 198 for (VPValue *Op : operands()) 199 Op->removeUser(*this); 200 } 201 202 void addOperand(VPValue *Operand) { 203 Operands.push_back(Operand); 204 Operand->addUser(*this); 205 } 206 207 unsigned getNumOperands() const { return Operands.size(); } 208 inline VPValue *getOperand(unsigned N) const { 209 assert(N < Operands.size() && "Operand index out of bounds"); 210 return Operands[N]; 211 } 212 213 void setOperand(unsigned I, VPValue *New) { 214 Operands[I]->removeUser(*this); 215 Operands[I] = New; 216 New->addUser(*this); 217 } 218 219 typedef SmallVectorImpl<VPValue *>::iterator operand_iterator; 220 typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator; 221 typedef iterator_range<operand_iterator> operand_range; 222 typedef iterator_range<const_operand_iterator> const_operand_range; 223 224 operand_iterator op_begin() { return Operands.begin(); } 225 const_operand_iterator op_begin() const { return Operands.begin(); } 226 operand_iterator op_end() { return Operands.end(); } 227 const_operand_iterator op_end() const { return Operands.end(); } 228 operand_range operands() { return operand_range(op_begin(), op_end()); } 229 const_operand_range operands() const { 230 return const_operand_range(op_begin(), op_end()); 231 } 232 233 /// Method to support type inquiry through isa, cast, and dyn_cast. 234 static inline bool classof(const VPRecipeBase *Recipe); 235 }; 236 237 /// This class augments a recipe with a set of VPValues defined by the recipe. 238 /// It allows recipes to define zero, one or multiple VPValues. A VPDef owns 239 /// the VPValues it defines and is responsible for deleting its defined values. 240 /// Single-value VPDefs that also inherit from VPValue must make sure to inherit 241 /// from VPDef before VPValue. 242 class VPDef { 243 friend class VPValue; 244 245 /// The VPValues defined by this VPDef. 246 TinyPtrVector<VPValue *> DefinedValues; 247 248 /// Add \p V as a defined value by this VPDef. 249 void addDefinedValue(VPValue *V) { 250 assert(V->getDef() == this && 251 "can only add VPValue already linked with this VPDef"); 252 DefinedValues.push_back(V); 253 } 254 255 /// Remove \p V from the values defined by this VPDef. \p V must be a defined 256 /// value of this VPDef. 257 void removeDefinedValue(VPValue *V) { 258 assert(V->getDef() == this && 259 "can only remove VPValue linked with this VPDef"); 260 assert(find(DefinedValues, V) != DefinedValues.end() && 261 "VPValue to remove must be in DefinedValues"); 262 erase_value(DefinedValues, V); 263 V->Def = nullptr; 264 } 265 266 public: 267 virtual ~VPDef() { 268 for (VPValue *D : make_early_inc_range(DefinedValues)) { 269 assert(D->Def == this && 270 "all defined VPValues should point to the containing VPDef"); 271 assert(D->getNumUsers() == 0 && 272 "all defined VPValues should have no more users"); 273 D->Def = nullptr; 274 delete D; 275 } 276 } 277 278 /// Returns the VPValue with index \p I defined by the VPDef. 279 VPValue *getVPValue(unsigned I = 0) { 280 assert(DefinedValues[I] && "defined value must be non-null"); 281 return DefinedValues[I]; 282 } 283 const VPValue *getVPValue(unsigned I = 0) const { 284 assert(DefinedValues[I] && "defined value must be non-null"); 285 return DefinedValues[I]; 286 } 287 288 /// Returns an ArrayRef of the values defined by the VPDef. 289 ArrayRef<VPValue *> definedValues() { return DefinedValues; } 290 291 /// Returns the number of values defined by the VPDef. 292 unsigned getNumDefinedValues() const { return DefinedValues.size(); } 293 }; 294 295 class VPlan; 296 class VPBasicBlock; 297 class VPRegionBlock; 298 299 /// This class can be used to assign consecutive numbers to all VPValues in a 300 /// VPlan and allows querying the numbering for printing, similar to the 301 /// ModuleSlotTracker for IR values. 302 class VPSlotTracker { 303 DenseMap<const VPValue *, unsigned> Slots; 304 unsigned NextSlot = 0; 305 306 void assignSlots(const VPBlockBase *VPBB); 307 void assignSlots(const VPRegionBlock *Region); 308 void assignSlots(const VPBasicBlock *VPBB); 309 void assignSlot(const VPValue *V); 310 311 void assignSlots(const VPlan &Plan); 312 313 public: 314 VPSlotTracker(const VPlan *Plan) { 315 if (Plan) 316 assignSlots(*Plan); 317 } 318 319 unsigned getSlot(const VPValue *V) const { 320 auto I = Slots.find(V); 321 if (I == Slots.end()) 322 return -1; 323 return I->second; 324 } 325 }; 326 327 } // namespace llvm 328 329 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H 330