27#include "llvm/IR/IntrinsicsHexagon.h"
38#define DEBUG_TYPE "hexagon-subtarget"
40#define GET_SUBTARGETINFO_CTOR
41#define GET_SUBTARGETINFO_TARGET_DESC
42#include "HexagonGenSubtargetInfo.inc"
52 cl::desc(
"Enable the scheduler to generate .cur"));
56 cl::desc(
"Disable Hexagon MI Scheduling"));
60 cl::desc(
"If present, forces/disables the use of long calls"));
64 cl::desc(
"Consider calls to be predicable"));
74 cl::desc(
"Enable checking for cache bank conflicts"));
79 OptLevel(TM.getOptLevel()),
80 CPUString(
std::
string(Hexagon_MC::selectHexagonCPU(CPU))),
81 TargetTriple(TT), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
82 RegInfo(getHwMode()), TLInfo(TM, *this),
83 InstrItins(getInstrItineraryForCPU(CPUString)) {
98 UseHVX128BOps =
false;
101 UseLongCalls =
false;
110 return F ==
"+hvx-qfloat" ||
F ==
"-hvx-qfloat";
115 if (
F.starts_with(
"+hvxv"))
121 if (
F.starts_with(
"+hvx") ||
F ==
"-hvx")
122 return F.take_front(4);
127 bool AddQFloat =
false;
133 }
else if (HvxVer ==
"+hvx") {
142 std::string FeatureString = Features.
getString();
146 UseHVXFloatingPoint = UseHVXIEEEFPOps || UseHVXQFloatOps;
148 if (UseHVXQFloatOps && UseHVXIEEEFPOps && UseHVXFloatingPoint)
150 dbgs() <<
"Behavior is undefined for simultaneous qfloat and ieee hvx codegen...");
166 setFeatureBits(FeatureBits.
reset(Hexagon::FeatureDuplex));
177 if (IncludeBool && Ty == MVT::i1)
189 if (!IncludeBool && ElemTy == MVT::i1)
196 if (IncludeBool && ElemTy == MVT::i1) {
199 for (
MVT T : ElemTypes)
200 if (NumElems *
T.getSizeInBits() == 8 * HwLen)
206 if (VecWidth != 8 * HwLen && VecWidth != 16 * HwLen)
212 if (!VecTy->
isVectorTy() || isa<ScalableVectorType>(VecTy))
225 auto isHvxTy = [
this, IncludeBool](
MVT SimpleTy) {
238 if (SimpleTy.
isValid() && isHvxTy(SimpleTy))
252 if (
D.getKind() ==
SDep::Output &&
D.getReg() == Hexagon::USR_OVF)
254 for (
auto &E : Erase)
267 bool IsLoadMI1 = MI1.
mayLoad();
268 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1))
271 if (SI.getKind() !=
SDep::Order || SI.getLatency() != 0)
274 if (!QII->isHVXVec(MI2))
280 for (
SDep &PI : SI.getSUnit()->Preds) {
281 if (PI.getSUnit() != &SU || PI.getKind() !=
SDep::Order)
284 SI.getSUnit()->setDepthDirty();
298bool HexagonSubtarget::CallMutation::shouldTFRICallBind(
300 const SUnit &Inst2)
const {
312 SUnit* LastSequentialCall =
nullptr;
323 for (
unsigned su = 0, e = DAG->
SUnits.size(); su != e; ++su) {
325 if (DAG->
SUnits[su].getInstr()->isCall())
326 LastSequentialCall = &DAG->
SUnits[su];
328 else if (DAG->
SUnits[su].getInstr()->isCompare() && LastSequentialCall)
332 shouldTFRICallBind(HII, DAG->
SUnits[su], DAG->
SUnits[su+1]))
350 if (
MI->isCopy() &&
MI->getOperand(1).getReg().isPhysical()) {
352 VRegHoldingReg[
MI->getOperand(0).getReg()] =
MI->getOperand(1).getReg();
353 LastVRegUse.
erase(
MI->getOperand(1).getReg());
358 if (MO.isUse() && !
MI->isCopy() &&
359 VRegHoldingReg.
count(MO.getReg())) {
361 LastVRegUse[VRegHoldingReg[MO.getReg()]] = &DAG->
SUnits[su];
362 }
else if (MO.isDef() && MO.getReg().isPhysical()) {
365 if (LastVRegUse.
count(*AI) &&
366 LastVRegUse[*AI] != &DAG->
SUnits[su])
369 LastVRegUse.
erase(*AI);
387 for (
unsigned i = 0, e = DAG->
SUnits.size(); i != e; ++i) {
397 if (BaseOp0 ==
nullptr || !BaseOp0->
isReg() || !Size0.
hasValue() ||
401 for (
unsigned j = i+1, m = std::min(i+32, e); j != m; ++j) {
410 if (BaseOp1 ==
nullptr || !BaseOp1->
isReg() || !Size0.
hasValue() ||
415 if (((Offset0 ^ Offset1) & 0x18) != 0)
439 if (!Src->isInstr() || !Dst->isInstr())
450 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
467 std::optional<unsigned> DLatency;
468 for (
const auto &DDep : Dst->Succs) {
471 for (
unsigned OpNum = 0; OpNum < DDst->
getNumOperands(); OpNum++) {
482 std::optional<unsigned>
Latency =
492 DLatency = std::nullopt;
503 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
509 Latency = updateLatency(*SrcInst, *DstInst, IsArtificial,
Latency);
514 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations)
const {
515 Mutations.push_back(std::make_unique<UsrOverflowMutation>());
516 Mutations.push_back(std::make_unique<HVXMemLatencyMutation>());
517 Mutations.push_back(std::make_unique<BankConflictMutation>());
521 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations)
const {
522 Mutations.push_back(std::make_unique<UsrOverflowMutation>());
523 Mutations.push_back(std::make_unique<HVXMemLatencyMutation>());
527void HexagonSubtarget::anchor() {}
539int HexagonSubtarget::updateLatency(
MachineInstr &SrcInst,
554void HexagonSubtarget::restoreLatency(
SUnit *Src,
SUnit *Dst)
const {
556 for (
auto &
I : Src->Succs) {
557 if (!
I.isAssignedRegDep() ||
I.getSUnit() != Dst)
561 for (
unsigned OpNum = 0; OpNum < SrcI->
getNumOperands(); OpNum++) {
563 bool IsSameOrSubReg =
false;
567 IsSameOrSubReg = (MOReg == DepR);
571 if (MO.
isDef() && IsSameOrSubReg)
575 assert(DefIdx >= 0 &&
"Def Reg not found in Src MI");
578 for (
unsigned OpNum = 0; OpNum < DstI->
getNumOperands(); OpNum++) {
582 &InstrItins, *SrcI, DefIdx, *DstI, OpNum);
588 bool IsArtificial =
I.isArtificial();
596 auto F =
find(Dst->Preds,
T);
598 F->setLatency(
I.getLatency());
603void HexagonSubtarget::changeLatency(
SUnit *Src,
SUnit *Dst,
unsigned Lat)
605 for (
auto &
I : Src->Succs) {
606 if (!
I.isAssignedRegDep() ||
I.getSUnit() != Dst)
613 auto F =
find(Dst->Preds,
T);
622 if (
I.isAssignedRegDep() &&
I.getLatency() == 0 &&
623 !
I.getSUnit()->getInstr()->isPseudo())
632bool HexagonSubtarget::isBestZeroLatency(
SUnit *Src,
SUnit *Dst,
639 if (Dst->isBoundaryNode())
656 SUnit *Best =
nullptr;
657 SUnit *DstBest =
nullptr;
659 if (SrcBest ==
nullptr || Src->NodeNum >= SrcBest->
NodeNum) {
662 if (DstBest ==
nullptr || Dst->NodeNum <= DstBest->
NodeNum)
670 if ((Src == SrcBest && Dst == DstBest ) ||
671 (SrcBest ==
nullptr && Dst == DstBest) ||
672 (Src == SrcBest && Dst ==
nullptr))
677 if (SrcBest !=
nullptr) {
679 changeLatency(SrcBest, Dst, 1);
681 restoreLatency(SrcBest, Dst);
683 if (DstBest !=
nullptr) {
685 changeLatency(Src, DstBest, 1);
687 restoreLatency(Src, DstBest);
692 if (SrcBest && DstBest)
695 changeLatency(SrcBest, DstBest, 0);
700 for (
auto &
I : DstBest->
Preds)
701 if (ExclSrc.
count(
I.getSUnit()) == 0 &&
702 isBestZeroLatency(
I.getSUnit(), DstBest,
TII, ExclSrc, ExclDst))
703 changeLatency(
I.getSUnit(), DstBest, 0);
704 }
else if (SrcBest) {
708 for (
auto &
I : SrcBest->
Succs)
709 if (ExclDst.
count(
I.getSUnit()) == 0 &&
710 isBestZeroLatency(SrcBest,
I.getSUnit(),
TII, ExclSrc, ExclDst))
711 changeLatency(SrcBest,
I.getSUnit(), 0);
737 static Scalar ScalarInts[] = {
738#define GET_SCALAR_INTRINSICS
740#undef GET_SCALAR_INTRINSICS
743 static Hvx HvxInts[] = {
744#define GET_HVX_INTRINSICS
746#undef GET_HVX_INTRINSICS
749 const auto CmpOpcode = [](
auto A,
auto B) {
return A.Opcode <
B.Opcode; };
750 [[maybe_unused]]
static bool SortedScalar =
752 [[maybe_unused]]
static bool SortedHvx =
755 auto [BS, ES] = std::make_pair(std::begin(ScalarInts), std::end(ScalarInts));
756 auto [BH, EH] = std::make_pair(std::begin(HvxInts), std::end(HvxInts));
758 auto FoundScalar = std::lower_bound(BS, ES, Scalar{Opc, 0}, CmpOpcode);
759 if (FoundScalar != ES && FoundScalar->Opcode == Opc)
760 return FoundScalar->IntId;
762 auto FoundHvx = std::lower_bound(BH, EH, Hvx{Opc, 0, 0}, CmpOpcode);
763 if (FoundHvx != EH && FoundHvx->Opcode == Opc) {
766 return FoundHvx->Int64Id;
768 return FoundHvx->Int128Id;
771 std::string
error =
"Invalid opcode (" + std::to_string(Opc) +
")";
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
const HexagonInstrInfo * TII
static cl::opt< bool > DisableHexagonMISched("disable-hexagon-misched", cl::Hidden, cl::desc("Disable Hexagon MI Scheduling"))
static cl::opt< bool > EnableDotCurSched("enable-cur-sched", cl::Hidden, cl::init(true), cl::desc("Enable the scheduler to generate .cur"))
static cl::opt< bool > EnableCheckBankConflict("hexagon-check-bank-conflict", cl::Hidden, cl::init(true), cl::desc("Enable checking for cache bank conflicts"))
static cl::opt< bool > OverrideLongCalls("hexagon-long-calls", cl::Hidden, cl::desc("If present, forces/disables the use of long calls"))
static cl::opt< bool > SchedPredsCloser("sched-preds-closer", cl::Hidden, cl::init(true))
static cl::opt< bool > SchedRetvalOptimization("sched-retval-optimization", cl::Hidden, cl::init(true))
static cl::opt< bool > EnableTCLatencySched("enable-tc-latency-sched", cl::Hidden, cl::init(false))
static cl::opt< bool > EnableBSBSched("enable-bsb-sched", cl::Hidden, cl::init(true))
static SUnit * getZeroLatency(SUnit *N, SmallVector< SDep, 4 > &Deps)
If the SUnit has a zero latency edge, return the other SUnit.
static cl::opt< bool > EnablePredicatedCalls("hexagon-pred-calls", cl::Hidden, cl::desc("Consider calls to be predicable"))
unsigned const TargetRegisterInfo * TRI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallSet class.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool erase(const KeyT &Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Container class for subtarget features.
constexpr FeatureBitset & reset(unsigned I)
unsigned getAddrMode(const MachineInstr &MI) const
bool canExecuteInBundle(const MachineInstr &First, const MachineInstr &Second) const
Can these instructions execute at the same time in a bundle.
std::optional< unsigned > getOperandLatency(const InstrItineraryData *ItinData, const MachineInstr &DefMI, unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const override
getOperandLatency - Compute and return the use operand latency of a given pair of def and use.
bool isToBeScheduledASAP(const MachineInstr &MI1, const MachineInstr &MI2) const
MachineOperand * getBaseAndOffset(const MachineInstr &MI, int64_t &Offset, LocationSize &AccessSize) const
uint64_t getType(const MachineInstr &MI) const
Hexagon::ArchEnum HexagonArchVersion
void adjustSchedDependency(SUnit *Def, int DefOpIdx, SUnit *Use, int UseOpIdx, SDep &Dep, const TargetSchedModel *SchedModel) const override
Perform target specific adjustments to the latency of a schedule dependency.
bool usePredicatedCalls() const
const HexagonInstrInfo * getInstrInfo() const override
const HexagonRegisterInfo * getRegisterInfo() const override
void getSMSMutations(std::vector< std::unique_ptr< ScheduleDAGMutation > > &Mutations) const override
HexagonSubtarget(const Triple &TT, StringRef CPU, StringRef FS, const TargetMachine &TM)
bool isHVXVectorType(EVT VecTy, bool IncludeBool=false) const
void getPostRAMutations(std::vector< std::unique_ptr< ScheduleDAGMutation > > &Mutations) const override
const HexagonTargetLowering * getTargetLowering() const override
bool UseBSBScheduling
True if the target should use Back-Skip-Back scheduling.
unsigned getL1PrefetchDistance() const
ArrayRef< MVT > getHVXElementTypes() const
bool useHVXFloatingPoint() const
bool enableSubRegLiveness() const override
unsigned getVectorLength() const
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
ParseSubtargetFeatures - Parses features string setting specified subtarget options.
bool useHVXV68Ops() const
unsigned getL1CacheLineSize() const
bool isTypeForHVX(Type *VecTy, bool IncludeBool=false) const
Intrinsic::ID getIntrinsicId(unsigned Opc) const
HexagonSubtarget & initializeSubtargetDependencies(StringRef CPU, StringRef FS)
bool enableMachineScheduler() const override
bool useBSBScheduling() const
bool isHVXElementType(MVT Ty, bool IncludeBool=false) const
bool useAA() const override
Enable use of alias analysis during code generation (during MI scheduling, DAGCombine,...
LegalizeTypeAction getPreferredVectorAction(MVT VT) const override
Return the preferred vector type legalization action.
const InstrItinerary * Itineraries
Array of itineraries selected.
TypeSize getValue() const
MCRegAliasIterator enumerates all registers aliasing Reg.
bool isVector() const
Return true if this is a vector value type.
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
bool isValid() const
Return true if this is a valid simple valuetype.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getNumOperands() const
Retuns the total number of operands.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
bool isRegSequence() const
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
@ Output
A register output-dependence (aka WAW).
@ Order
Any other ordering dependency.
void setLatency(unsigned Lat)
Sets the latency for this edge.
@ Barrier
An unknown scheduling barrier.
@ Artificial
Arbitrary strong DAG edge (no real dependence).
unsigned getLatency() const
Returns the latency value for this edge, which roughly means the minimum number of cycles that must e...
bool isArtificial() const
Tests if this is an Order dependence that is marked as "artificial", meaning it isn't necessary for c...
Scheduling unit. This is a node in the scheduling DAG.
bool isInstr() const
Returns true if this SUnit refers to a machine instruction as opposed to an SDNode.
unsigned NodeNum
Entry # of node in the node vector.
void setHeightDirty()
Sets a flag in this node to indicate that its stored Height value will require recomputation the next...
void removePred(const SDep &D)
Removes the specified edge as a pred of the current node if it exists.
SmallVector< SDep, 4 > Succs
All sunit successors.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
A ScheduleDAG for scheduling lists of MachineInstr.
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
const TargetInstrInfo * TII
Target instruction information.
std::vector< SUnit > SUnits
The scheduling units.
MachineFunction & MF
Machine function.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Manages the enabling and disabling of subtarget specific features.
const std::vector< std::string > & getFeatures() const
Returns the vector of individual subtarget features.
std::string getString() const
Returns features as a string.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Primary interface to the complete machine description for the target machine.
Provide an instruction scheduling machine model to CodeGen passes.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Triple - Helper class for working with autoconf configuration names.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void addArchSubtarget(MCSubtargetInfo const *STI, StringRef FS)
FeatureBitset completeHVXFeatures(const FeatureBitset &FB)
std::optional< Hexagon::ArchEnum > getCpu(StringRef CPU)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
cl::opt< bool > HexagonDisableDuplex
Implement std::hash so that hash_code can be used in STL containers.
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
bool isVector() const
Return true if this is a vector value type.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
void apply(ScheduleDAGInstrs *DAG) override
void apply(ScheduleDAGInstrs *DAG) override
void apply(ScheduleDAGInstrs *DAG) override
void apply(ScheduleDAGInstrs *DAG) override