1 //===- VPlanValue.h - Represent Values in Vectorizer Plan -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// This file contains the declarations of the entities induced by Vectorization 12 /// Plans, e.g. the instructions the VPlan intends to generate if executed. 13 /// VPlan models the following entities: 14 /// VPValue 15 /// |-- VPUser 16 /// | |-- VPInstruction 17 /// These are documented in docs/VectorizationPlan.rst. 18 /// 19 //===----------------------------------------------------------------------===// 20 21 #ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H 22 #define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H 23 24 #include "llvm/ADT/DenseMap.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/IR/Value.h" 27 #include "llvm/Support/Debug.h" 28 #include "llvm/Support/raw_ostream.h" 29 30 namespace llvm { 31 32 // Forward declarations. 33 class VPUser; 34 35 // This is the base class of the VPlan Def/Use graph, used for modeling the data 36 // flow into, within and out of the VPlan. VPValues can stand for live-ins 37 // coming from the input IR, instructions which VPlan will generate if executed 38 // and live-outs which the VPlan will need to fix accordingly. 39 class VPValue { 40 friend class VPBuilder; 41 friend class VPlanHCFGTransforms; 42 friend class VPBasicBlock; 43 44 private: 45 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast). 46 47 SmallVector<VPUser *, 1> Users; 48 49 protected: 50 // Hold the underlying Value, if any, attached to this VPValue. 51 Value *UnderlyingVal; 52 53 VPValue(const unsigned char SC, Value *UV = nullptr) 54 : SubclassID(SC), UnderlyingVal(UV) {} 55 56 // DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to 57 // the front-end and back-end of VPlan so that the middle-end is as 58 // independent as possible of the underlying IR. We grant access to the 59 // underlying IR using friendship. In that way, we should be able to use VPlan 60 // for multiple underlying IRs (Polly?) by providing a new VPlan front-end, 61 // back-end and analysis information for the new IR. 62 63 /// Return the underlying Value attached to this VPValue. 64 Value *getUnderlyingValue() { return UnderlyingVal; } 65 66 // Set \p Val as the underlying Value of this VPValue. 67 void setUnderlyingValue(Value *Val) { 68 assert(!UnderlyingVal && "Underlying Value is already set."); 69 UnderlyingVal = Val; 70 } 71 72 public: 73 /// An enumeration for keeping track of the concrete subclass of VPValue that 74 /// are actually instantiated. Values of this enumeration are kept in the 75 /// SubclassID field of the VPValue objects. They are used for concrete 76 /// type identification. 77 enum { VPValueSC, VPUserSC, VPInstructionSC }; 78 79 VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV) {} 80 VPValue(const VPValue &) = delete; 81 VPValue &operator=(const VPValue &) = delete; 82 83 /// \return an ID for the concrete type of this object. 84 /// This is used to implement the classof checks. This should not be used 85 /// for any other purpose, as the values may change as LLVM evolves. 86 unsigned getVPValueID() const { return SubclassID; } 87 88 void printAsOperand(raw_ostream &OS) const { 89 OS << "%vp" << (unsigned short)(unsigned long long)this; 90 } 91 92 unsigned getNumUsers() const { return Users.size(); } 93 void addUser(VPUser &User) { Users.push_back(&User); } 94 95 typedef SmallVectorImpl<VPUser *>::iterator user_iterator; 96 typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator; 97 typedef iterator_range<user_iterator> user_range; 98 typedef iterator_range<const_user_iterator> const_user_range; 99 100 user_iterator user_begin() { return Users.begin(); } 101 const_user_iterator user_begin() const { return Users.begin(); } 102 user_iterator user_end() { return Users.end(); } 103 const_user_iterator user_end() const { return Users.end(); } 104 user_range users() { return user_range(user_begin(), user_end()); } 105 const_user_range users() const { 106 return const_user_range(user_begin(), user_end()); 107 } 108 }; 109 110 typedef DenseMap<Value *, VPValue *> Value2VPValueTy; 111 typedef DenseMap<VPValue *, Value *> VPValue2ValueTy; 112 113 raw_ostream &operator<<(raw_ostream &OS, const VPValue &V); 114 115 /// This class augments VPValue with operands which provide the inverse def-use 116 /// edges from VPValue's users to their defs. 117 class VPUser : public VPValue { 118 private: 119 SmallVector<VPValue *, 2> Operands; 120 121 protected: 122 VPUser(const unsigned char SC) : VPValue(SC) {} 123 VPUser(const unsigned char SC, ArrayRef<VPValue *> Operands) : VPValue(SC) { 124 for (VPValue *Operand : Operands) 125 addOperand(Operand); 126 } 127 128 public: 129 VPUser() : VPValue(VPValue::VPUserSC) {} 130 VPUser(ArrayRef<VPValue *> Operands) : VPUser(VPValue::VPUserSC, Operands) {} 131 VPUser(std::initializer_list<VPValue *> Operands) 132 : VPUser(ArrayRef<VPValue *>(Operands)) {} 133 VPUser(const VPUser &) = delete; 134 VPUser &operator=(const VPUser &) = delete; 135 136 /// Method to support type inquiry through isa, cast, and dyn_cast. 137 static inline bool classof(const VPValue *V) { 138 return V->getVPValueID() >= VPUserSC && 139 V->getVPValueID() <= VPInstructionSC; 140 } 141 142 void addOperand(VPValue *Operand) { 143 Operands.push_back(Operand); 144 Operand->addUser(*this); 145 } 146 147 unsigned getNumOperands() const { return Operands.size(); } 148 inline VPValue *getOperand(unsigned N) const { 149 assert(N < Operands.size() && "Operand index out of bounds"); 150 return Operands[N]; 151 } 152 153 typedef SmallVectorImpl<VPValue *>::iterator operand_iterator; 154 typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator; 155 typedef iterator_range<operand_iterator> operand_range; 156 typedef iterator_range<const_operand_iterator> const_operand_range; 157 158 operand_iterator op_begin() { return Operands.begin(); } 159 const_operand_iterator op_begin() const { return Operands.begin(); } 160 operand_iterator op_end() { return Operands.end(); } 161 const_operand_iterator op_end() const { return Operands.end(); } 162 operand_range operands() { return operand_range(op_begin(), op_end()); } 163 const_operand_range operands() const { 164 return const_operand_range(op_begin(), op_end()); 165 } 166 }; 167 168 } // namespace llvm 169 170 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H 171