#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/iterator_range.h"
namespace llvm {
class raw_ostream;
class Value;
class VPDef;
class VPSlotTracker;
class VPUser;
class VPRecipeBase;
class VPWidenMemoryInstructionRecipe;
class VPValue {
friend class VPBuilder;
friend class VPDef;
friend class VPInstruction;
friend struct VPlanTransforms;
friend class VPBasicBlock;
friend class VPInterleavedAccessInfo;
friend class VPSlotTracker;
friend class VPRecipeBase;
friend class VPWidenMemoryInstructionRecipe;
const unsigned char SubclassID;
SmallVector<VPUser *, 1> Users;
protected:
Value *UnderlyingVal;
VPDef *Def;
VPValue(const unsigned char SC, Value *UV = nullptr, VPDef *Def = nullptr);
void setUnderlyingValue(Value *Val) {
assert(!UnderlyingVal && "Underlying Value is already set.");
UnderlyingVal = Val;
}
public:
Value *getUnderlyingValue() { return UnderlyingVal; }
const Value *getUnderlyingValue() const { return UnderlyingVal; }
enum {
VPValueSC,
VPVInstructionSC,
VPVMemoryInstructionSC,
VPVReductionSC,
VPVReplicateSC,
VPVWidenSC,
VPVWidenCallSC,
VPVWidenCanonicalIVSC,
VPVWidenGEPSC,
VPVWidenSelectSC,
VPVBlendSC,
VPVCanonicalIVPHISC,
VPVActiveLaneMaskPHISC,
VPVFirstOrderRecurrencePHISC,
VPVWidenPHISC,
VPVWidenIntOrFpInductionSC,
VPVWidenPointerInductionSC,
VPVPredInstPHI,
VPVReductionPHISC,
};
VPValue(Value *UV = nullptr, VPDef *Def = nullptr)
: VPValue(VPValueSC, UV, Def) {}
VPValue(const VPValue &) = delete;
VPValue &operator=(const VPValue &) = delete;
virtual ~VPValue();
unsigned getVPValueID() const { return SubclassID; }
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const;
void print(raw_ostream &OS, VPSlotTracker &Tracker) const;
void dump() const;
#endif
unsigned getNumUsers() const { return Users.size(); }
void addUser(VPUser &User) { Users.push_back(&User); }
void removeUser(VPUser &User) {
bool Found = false;
erase_if(Users, [&User, &Found](VPUser *Other) {
if (Found)
return false;
if (Other == &User) {
Found = true;
return true;
}
return false;
});
}
typedef SmallVectorImpl<VPUser *>::iterator user_iterator;
typedef SmallVectorImpl<VPUser *>::const_iterator const_user_iterator;
typedef iterator_range<user_iterator> user_range;
typedef iterator_range<const_user_iterator> const_user_range;
user_iterator user_begin() { return Users.begin(); }
const_user_iterator user_begin() const { return Users.begin(); }
user_iterator user_end() { return Users.end(); }
const_user_iterator user_end() const { return Users.end(); }
user_range users() { return user_range(user_begin(), user_end()); }
const_user_range users() const {
return const_user_range(user_begin(), user_end());
}
bool hasMoreThanOneUniqueUser() {
if (getNumUsers() == 0)
return false;
auto Current = std::next(user_begin());
while (Current != user_end() && *user_begin() == *Current)
Current++;
return Current != user_end();
}
void replaceAllUsesWith(VPValue *New);
VPDef *getDef() { return Def; }
const VPDef *getDef() const { return Def; }
Value *getLiveInIRValue() {
assert(!getDef() &&
"VPValue is not a live-in; it is defined by a VPDef inside a VPlan");
return getUnderlyingValue();
}
const Value *getLiveInIRValue() const {
assert(!getDef() &&
"VPValue is not a live-in; it is defined by a VPDef inside a VPlan");
return getUnderlyingValue();
}
};
typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
class VPUser {
public:
enum class VPUserID {
Recipe,
LiveOut,
};
private:
SmallVector<VPValue *, 2> Operands;
VPUserID ID;
protected:
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const;
#endif
VPUser(ArrayRef<VPValue *> Operands, VPUserID ID) : ID(ID) {
for (VPValue *Operand : Operands)
addOperand(Operand);
}
VPUser(std::initializer_list<VPValue *> Operands, VPUserID ID)
: VPUser(ArrayRef<VPValue *>(Operands), ID) {}
template <typename IterT>
VPUser(iterator_range<IterT> Operands, VPUserID ID) : ID(ID) {
for (VPValue *Operand : Operands)
addOperand(Operand);
}
public:
VPUser() = delete;
VPUser(const VPUser &) = delete;
VPUser &operator=(const VPUser &) = delete;
virtual ~VPUser() {
for (VPValue *Op : operands())
Op->removeUser(*this);
}
VPUserID getVPUserID() const { return ID; }
void addOperand(VPValue *Operand) {
Operands.push_back(Operand);
Operand->addUser(*this);
}
unsigned getNumOperands() const { return Operands.size(); }
inline VPValue *getOperand(unsigned N) const {
assert(N < Operands.size() && "Operand index out of bounds");
return Operands[N];
}
void setOperand(unsigned I, VPValue *New) {
Operands[I]->removeUser(*this);
Operands[I] = New;
New->addUser(*this);
}
void removeLastOperand() {
VPValue *Op = Operands.pop_back_val();
Op->removeUser(*this);
}
typedef SmallVectorImpl<VPValue *>::iterator operand_iterator;
typedef SmallVectorImpl<VPValue *>::const_iterator const_operand_iterator;
typedef iterator_range<operand_iterator> operand_range;
typedef iterator_range<const_operand_iterator> const_operand_range;
operand_iterator op_begin() { return Operands.begin(); }
const_operand_iterator op_begin() const { return Operands.begin(); }
operand_iterator op_end() { return Operands.end(); }
const_operand_iterator op_end() const { return Operands.end(); }
operand_range operands() { return operand_range(op_begin(), op_end()); }
const_operand_range operands() const {
return const_operand_range(op_begin(), op_end());
}
static inline bool classof(const VPDef *Recipe);
virtual bool usesScalars(const VPValue *Op) const {
assert(is_contained(operands(), Op) &&
"Op must be an operand of the recipe");
return onlyFirstLaneUsed(Op);
}
virtual bool onlyFirstLaneUsed(const VPValue *Op) const {
assert(is_contained(operands(), Op) &&
"Op must be an operand of the recipe");
return false;
}
};
class VPDef {
friend class VPValue;
const unsigned char SubclassID;
TinyPtrVector<VPValue *> DefinedValues;
void addDefinedValue(VPValue *V) {
assert(V->getDef() == this &&
"can only add VPValue already linked with this VPDef");
DefinedValues.push_back(V);
}
void removeDefinedValue(VPValue *V) {
assert(V->getDef() == this &&
"can only remove VPValue linked with this VPDef");
assert(is_contained(DefinedValues, V) &&
"VPValue to remove must be in DefinedValues");
erase_value(DefinedValues, V);
V->Def = nullptr;
}
public:
using VPRecipeTy = enum {
VPBranchOnMaskSC,
VPExpandSCEVSC,
VPInstructionSC,
VPInterleaveSC,
VPReductionSC,
VPReplicateSC,
VPScalarIVStepsSC,
VPWidenCallSC,
VPWidenCanonicalIVSC,
VPWidenGEPSC,
VPWidenMemoryInstructionSC,
VPWidenSC,
VPWidenSelectSC,
VPBlendSC,
VPCanonicalIVPHISC,
VPActiveLaneMaskPHISC,
VPFirstOrderRecurrencePHISC,
VPWidenPHISC,
VPWidenIntOrFpInductionSC,
VPWidenPointerInductionSC,
VPPredInstPHISC,
VPReductionPHISC,
VPFirstPHISC = VPBlendSC,
VPLastPHISC = VPReductionPHISC,
};
VPDef(const unsigned char SC) : SubclassID(SC) {}
virtual ~VPDef() {
for (VPValue *D : make_early_inc_range(DefinedValues)) {
assert(D->Def == this &&
"all defined VPValues should point to the containing VPDef");
assert(D->getNumUsers() == 0 &&
"all defined VPValues should have no more users");
D->Def = nullptr;
delete D;
}
}
VPValue *getVPSingleValue() {
assert(DefinedValues.size() == 1 && "must have exactly one defined value");
assert(DefinedValues[0] && "defined value must be non-null");
return DefinedValues[0];
}
const VPValue *getVPSingleValue() const {
assert(DefinedValues.size() == 1 && "must have exactly one defined value");
assert(DefinedValues[0] && "defined value must be non-null");
return DefinedValues[0];
}
VPValue *getVPValue(unsigned I) {
assert(DefinedValues[I] && "defined value must be non-null");
return DefinedValues[I];
}
const VPValue *getVPValue(unsigned I) const {
assert(DefinedValues[I] && "defined value must be non-null");
return DefinedValues[I];
}
ArrayRef<VPValue *> definedValues() { return DefinedValues; }
ArrayRef<VPValue *> definedValues() const { return DefinedValues; }
unsigned getNumDefinedValues() const { return DefinedValues.size(); }
unsigned getVPDefID() const { return SubclassID; }
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void dump() const;
virtual void print(raw_ostream &O, const Twine &Indent,
VPSlotTracker &SlotTracker) const = 0;
#endif
};
class VPlan;
class VPBasicBlock;
class VPSlotTracker {
DenseMap<const VPValue *, unsigned> Slots;
unsigned NextSlot = 0;
void assignSlot(const VPValue *V);
void assignSlots(const VPlan &Plan);
public:
VPSlotTracker(const VPlan *Plan = nullptr) {
if (Plan)
assignSlots(*Plan);
}
unsigned getSlot(const VPValue *V) const {
auto I = Slots.find(V);
if (I == Slots.end())
return -1;
return I->second;
}
};
}
#endif