Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Vectorize/VPlanValue.h
35266 views
//===- VPlanValue.h - Represent Values in Vectorizer Plan -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file contains the declarations of the entities induced by Vectorization10/// Plans, e.g. the instructions the VPlan intends to generate if executed.11/// VPlan models the following entities:12/// VPValue VPUser VPDef13/// | |14/// VPInstruction15/// These are documented in docs/VectorizationPlan.rst.16///17//===----------------------------------------------------------------------===//1819#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H20#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H2122#include "llvm/ADT/DenseMap.h"23#include "llvm/ADT/STLExtras.h"24#include "llvm/ADT/SmallVector.h"25#include "llvm/ADT/StringMap.h"26#include "llvm/ADT/TinyPtrVector.h"27#include "llvm/ADT/iterator_range.h"2829namespace llvm {3031// Forward declarations.32class raw_ostream;33class Value;34class VPDef;35class VPSlotTracker;36class VPUser;37class VPRecipeBase;3839// This is the base class of the VPlan Def/Use graph, used for modeling the data40// flow into, within and out of the VPlan. VPValues can stand for live-ins41// coming from the input IR, instructions which VPlan will generate if executed42// and live-outs which the VPlan will need to fix accordingly.43class VPValue {44friend class VPBuilder;45friend class VPDef;46friend class VPInstruction;47friend struct VPlanTransforms;48friend class VPBasicBlock;49friend class VPInterleavedAccessInfo;50friend class VPSlotTracker;51friend class VPRecipeBase;5253const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).5455SmallVector<VPUser *, 1> Users;5657protected:58// Hold the underlying Value, if any, attached to this VPValue.59Value *UnderlyingVal;6061/// Pointer to the VPDef that defines this VPValue. If it is nullptr, the62/// VPValue is not defined by any recipe modeled in VPlan.63VPDef *Def;6465VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr);6667// DESIGN PRINCIPLE: Access to the underlying IR must be strictly limited to68// the front-end and back-end of VPlan so that the middle-end is as69// independent as possible of the underlying IR. We grant access to the70// underlying IR using friendship. In that way, we should be able to use VPlan71// for multiple underlying IRs (Polly?) by providing a new VPlan front-end,72// back-end and analysis information for the new IR.7374public:75/// Return the underlying Value attached to this VPValue.76Value *getUnderlyingValue() const { return UnderlyingVal; }7778/// An enumeration for keeping track of the concrete subclass of VPValue that79/// are actually instantiated.80enum {81VPValueSC, /// A generic VPValue, like live-in values or defined by a recipe82/// that defines multiple values.83VPVRecipeSC /// A VPValue sub-class that is a VPRecipeBase.84};8586/// Create a live-in VPValue.87VPValue(Value *UV = nullptr) : VPValue(VPValueSC, UV, nullptr) {}88/// Create a VPValue for a \p Def which is a subclass of VPValue.89VPValue(VPDef *Def, Value *UV = nullptr) : VPValue(VPVRecipeSC, UV, Def) {}90/// Create a VPValue for a \p Def which defines multiple values.91VPValue(Value *UV, VPDef *Def) : VPValue(VPValueSC, UV, Def) {}92VPValue(const VPValue &) = delete;93VPValue &operator=(const VPValue &) = delete;9495virtual ~VPValue();9697/// \return an ID for the concrete type of this object.98/// This is used to implement the classof checks. This should not be used99/// for any other purpose, as the values may change as LLVM evolves.100unsigned getVPValueID() const { return SubclassID; }101102#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)103void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;104void print(raw_ostream &OS, VPSlotTracker &Tracker) const;105106/// Dump the value to stderr (for debugging).107void dump() const;108#endif109110unsigned getNumUsers() const { return Users.size(); }111void addUser(VPUser &User) { Users.push_back(&User); }112113/// Remove a single \p User from the list of users.114void removeUser(VPUser &User) {115// The same user can be added multiple times, e.g. because the same VPValue116// is used twice by the same VPUser. Remove a single one.117auto *I = find(Users, &User);118if (I != Users.end())119Users.erase(I);120}121122typedef SmallVectorImpl<VPUser *>::iterator user_iterator;123typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;124typedef iterator_range<user_iterator> user_range;125typedef iterator_range<const_user_iterator> const_user_range;126127user_iterator user_begin() { return Users.begin(); }128const_user_iterator user_begin() const { return Users.begin(); }129user_iterator user_end() { return Users.end(); }130const_user_iterator user_end() const { return Users.end(); }131user_range users() { return user_range(user_begin(), user_end()); }132const_user_range users() const {133return const_user_range(user_begin(), user_end());134}135136/// Returns true if the value has more than one unique user.137bool hasMoreThanOneUniqueUser() {138if (getNumUsers() == 0)139return false;140141// Check if all users match the first user.142auto Current = std::next(user_begin());143while (Current != user_end() && *user_begin() == *Current)144Current++;145return Current != user_end();146}147148void replaceAllUsesWith(VPValue *New);149150/// Go through the uses list for this VPValue and make each use point to \p151/// New if the callback ShouldReplace returns true for the given use specified152/// by a pair of (VPUser, the use index).153void replaceUsesWithIf(154VPValue *New,155llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace);156157/// Returns the recipe defining this VPValue or nullptr if it is not defined158/// by a recipe, i.e. is a live-in.159VPRecipeBase *getDefiningRecipe();160const VPRecipeBase *getDefiningRecipe() const;161162/// Returns true if this VPValue is defined by a recipe.163bool hasDefiningRecipe() const { return getDefiningRecipe(); }164165/// Returns true if this VPValue is a live-in, i.e. defined outside the VPlan.166bool isLiveIn() const { return !hasDefiningRecipe(); }167168/// Returns the underlying IR value, if this VPValue is defined outside the169/// scope of VPlan. Returns nullptr if the VPValue is defined by a VPDef170/// inside a VPlan.171Value *getLiveInIRValue() {172assert(isLiveIn() &&173"VPValue is not a live-in; it is defined by a VPDef inside a VPlan");174return getUnderlyingValue();175}176const Value *getLiveInIRValue() const {177assert(isLiveIn() &&178"VPValue is not a live-in; it is defined by a VPDef inside a VPlan");179return getUnderlyingValue();180}181182/// Returns true if the VPValue is defined outside any vector regions, i.e. it183/// is a live-in value.184/// TODO: Also handle recipes defined in pre-header blocks.185bool isDefinedOutsideVectorRegions() const { return !hasDefiningRecipe(); }186187// Set \p Val as the underlying Value of this VPValue.188void setUnderlyingValue(Value *Val) {189assert(!UnderlyingVal && "Underlying Value is already set.");190UnderlyingVal = Val;191}192};193194typedef DenseMap<Value *, VPValue *> Value2VPValueTy;195typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;196197raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);198199/// This class augments VPValue with operands which provide the inverse def-use200/// edges from VPValue's users to their defs.201class VPUser {202public:203/// Subclass identifier (for isa/dyn_cast).204enum class VPUserID {205Recipe,206LiveOut,207};208209private:210SmallVector<VPValue *, 2> Operands;211212VPUserID ID;213214protected:215#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)216/// Print the operands to \p O.217void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const;218#endif219220VPUser(ArrayRef<VPValue *> Operands, VPUserID ID) : ID(ID) {221for (VPValue *Operand : Operands)222addOperand(Operand);223}224225VPUser(std::initializer_list<VPValue *> Operands, VPUserID ID)226: VPUser(ArrayRef<VPValue *>(Operands), ID) {}227228template <typename IterT>229VPUser(iterator_range<IterT> Operands, VPUserID ID) : ID(ID) {230for (VPValue *Operand : Operands)231addOperand(Operand);232}233234public:235VPUser() = delete;236VPUser(const VPUser &) = delete;237VPUser &operator=(const VPUser &) = delete;238virtual ~VPUser() {239for (VPValue *Op : operands())240Op->removeUser(*this);241}242243VPUserID getVPUserID() const { return ID; }244245void addOperand(VPValue *Operand) {246Operands.push_back(Operand);247Operand->addUser(*this);248}249250unsigned getNumOperands() const { return Operands.size(); }251inline VPValue *getOperand(unsigned N) const {252assert(N < Operands.size() && "Operand index out of bounds");253return Operands[N];254}255256void setOperand(unsigned I, VPValue *New) {257Operands[I]->removeUser(*this);258Operands[I] = New;259New->addUser(*this);260}261262typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;263typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;264typedef iterator_range<operand_iterator> operand_range;265typedef iterator_range<const_operand_iterator> const_operand_range;266267operand_iterator op_begin() { return Operands.begin(); }268const_operand_iterator op_begin() const { return Operands.begin(); }269operand_iterator op_end() { return Operands.end(); }270const_operand_iterator op_end() const { return Operands.end(); }271operand_range operands() { return operand_range(op_begin(), op_end()); }272const_operand_range operands() const {273return const_operand_range(op_begin(), op_end());274}275276/// Returns true if the VPUser uses scalars of operand \p Op. Conservatively277/// returns if only first (scalar) lane is used, as default.278virtual bool usesScalars(const VPValue *Op) const {279assert(is_contained(operands(), Op) &&280"Op must be an operand of the recipe");281return onlyFirstLaneUsed(Op);282}283284/// Returns true if the VPUser only uses the first lane of operand \p Op.285/// Conservatively returns false.286virtual bool onlyFirstLaneUsed(const VPValue *Op) const {287assert(is_contained(operands(), Op) &&288"Op must be an operand of the recipe");289return false;290}291292/// Returns true if the VPUser only uses the first part of operand \p Op.293/// Conservatively returns false.294virtual bool onlyFirstPartUsed(const VPValue *Op) const {295assert(is_contained(operands(), Op) &&296"Op must be an operand of the recipe");297return false;298}299};300301/// This class augments a recipe with a set of VPValues defined by the recipe.302/// It allows recipes to define zero, one or multiple VPValues. A VPDef owns303/// the VPValues it defines and is responsible for deleting its defined values.304/// Single-value VPDefs that also inherit from VPValue must make sure to inherit305/// from VPDef before VPValue.306class VPDef {307friend class VPValue;308309/// Subclass identifier (for isa/dyn_cast).310const unsigned char SubclassID;311312/// The VPValues defined by this VPDef.313TinyPtrVector<VPValue *> DefinedValues;314315/// Add \p V as a defined value by this VPDef.316void addDefinedValue(VPValue *V) {317assert(V->Def == this &&318"can only add VPValue already linked with this VPDef");319DefinedValues.push_back(V);320}321322/// Remove \p V from the values defined by this VPDef. \p V must be a defined323/// value of this VPDef.324void removeDefinedValue(VPValue *V) {325assert(V->Def == this && "can only remove VPValue linked with this VPDef");326assert(is_contained(DefinedValues, V) &&327"VPValue to remove must be in DefinedValues");328llvm::erase(DefinedValues, V);329V->Def = nullptr;330}331332public:333/// An enumeration for keeping track of the concrete subclass of VPRecipeBase334/// that is actually instantiated. Values of this enumeration are kept in the335/// SubclassID field of the VPRecipeBase objects. They are used for concrete336/// type identification.337using VPRecipeTy = enum {338VPBranchOnMaskSC,339VPDerivedIVSC,340VPExpandSCEVSC,341VPInstructionSC,342VPInterleaveSC,343VPReductionEVLSC,344VPReductionSC,345VPReplicateSC,346VPScalarCastSC,347VPScalarIVStepsSC,348VPVectorPointerSC,349VPWidenCallSC,350VPWidenCanonicalIVSC,351VPWidenCastSC,352VPWidenGEPSC,353VPWidenLoadEVLSC,354VPWidenLoadSC,355VPWidenStoreEVLSC,356VPWidenStoreSC,357VPWidenSC,358VPWidenSelectSC,359VPBlendSC,360// START: Phi-like recipes. Need to be kept together.361VPWidenPHISC,362VPPredInstPHISC,363// START: SubclassID for recipes that inherit VPHeaderPHIRecipe.364// VPHeaderPHIRecipe need to be kept together.365VPCanonicalIVPHISC,366VPActiveLaneMaskPHISC,367VPEVLBasedIVPHISC,368VPFirstOrderRecurrencePHISC,369VPWidenIntOrFpInductionSC,370VPWidenPointerInductionSC,371VPReductionPHISC,372// END: SubclassID for recipes that inherit VPHeaderPHIRecipe373// END: Phi-like recipes374VPFirstPHISC = VPWidenPHISC,375VPFirstHeaderPHISC = VPCanonicalIVPHISC,376VPLastHeaderPHISC = VPReductionPHISC,377VPLastPHISC = VPReductionPHISC,378};379380VPDef(const unsigned char SC) : SubclassID(SC) {}381382virtual ~VPDef() {383for (VPValue *D : make_early_inc_range(DefinedValues)) {384assert(D->Def == this &&385"all defined VPValues should point to the containing VPDef");386assert(D->getNumUsers() == 0 &&387"all defined VPValues should have no more users");388D->Def = nullptr;389delete D;390}391}392393/// Returns the only VPValue defined by the VPDef. Can only be called for394/// VPDefs with a single defined value.395VPValue *getVPSingleValue() {396assert(DefinedValues.size() == 1 && "must have exactly one defined value");397assert(DefinedValues[0] && "defined value must be non-null");398return DefinedValues[0];399}400const VPValue *getVPSingleValue() const {401assert(DefinedValues.size() == 1 && "must have exactly one defined value");402assert(DefinedValues[0] && "defined value must be non-null");403return DefinedValues[0];404}405406/// Returns the VPValue with index \p I defined by the VPDef.407VPValue *getVPValue(unsigned I) {408assert(DefinedValues[I] && "defined value must be non-null");409return DefinedValues[I];410}411const VPValue *getVPValue(unsigned I) const {412assert(DefinedValues[I] && "defined value must be non-null");413return DefinedValues[I];414}415416/// Returns an ArrayRef of the values defined by the VPDef.417ArrayRef<VPValue *> definedValues() { return DefinedValues; }418/// Returns an ArrayRef of the values defined by the VPDef.419ArrayRef<VPValue *> definedValues() const { return DefinedValues; }420421/// Returns the number of values defined by the VPDef.422unsigned getNumDefinedValues() const { return DefinedValues.size(); }423424/// \return an ID for the concrete type of this object.425/// This is used to implement the classof checks. This should not be used426/// for any other purpose, as the values may change as LLVM evolves.427unsigned getVPDefID() const { return SubclassID; }428429#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)430/// Dump the VPDef to stderr (for debugging).431void dump() const;432433/// Each concrete VPDef prints itself.434virtual void print(raw_ostream &O, const Twine &Indent,435VPSlotTracker &SlotTracker) const = 0;436#endif437};438439class VPlan;440class VPBasicBlock;441442/// This class can be used to assign names to VPValues. For VPValues without443/// underlying value, assign consecutive numbers and use those as names (wrapped444/// in vp<>). Otherwise, use the name from the underlying value (wrapped in445/// ir<>), appending a .V version number if there are multiple uses of the same446/// name. Allows querying names for VPValues for printing, similar to the447/// ModuleSlotTracker for IR values.448class VPSlotTracker {449/// Keep track of versioned names assigned to VPValues with underlying IR450/// values.451DenseMap<const VPValue *, std::string> VPValue2Name;452/// Keep track of the next number to use to version the base name.453StringMap<unsigned> BaseName2Version;454455/// Number to assign to the next VPValue without underlying value.456unsigned NextSlot = 0;457458void assignName(const VPValue *V);459void assignNames(const VPlan &Plan);460void assignNames(const VPBasicBlock *VPBB);461462public:463VPSlotTracker(const VPlan *Plan = nullptr) {464if (Plan)465assignNames(*Plan);466}467468/// Returns the name assigned to \p V, if there is one, otherwise try to469/// construct one from the underlying value, if there's one; else return470/// <badref>.471std::string getOrCreateName(const VPValue *V) const;472};473474} // namespace llvm475476#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H477478479