22 #include "llvm/IR/IntrinsicsHexagon.h" 27 #define DEBUG_TYPE "hexagon-isel" 32 cl::desc(
"Rebalance address calculation trees to improve " 33 "instruction selection"));
40 cl::desc(
"Rebalance address tree only if this allows optimizations"));
45 cl::init(
false),
cl::desc(
"Rebalance address tree only if it is imbalanced"));
54 #define GET_DAGISEL_BODY HexagonDAGToDAGISel 55 #include "HexagonGenDAGISel.inc" 71 int32_t Inc = cast<ConstantSDNode>(Offset.
getNode())->getSExtValue();
85 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
87 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
91 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
93 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
99 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
106 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
116 if (isAlignedMemNode(LD)) {
118 Opcode = IsValidInc ? Hexagon::V6_vL32b_nt_pi : Hexagon::V6_vL32b_nt_ai;
120 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
122 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
191 unsigned IntNo = cast<ConstantSDNode>(IntN->
getOperand(1))->getZExtValue();
193 static std::map<unsigned,unsigned> LoadPciMap = {
194 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
195 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
196 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
197 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
198 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
199 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
201 auto FLC = LoadPciMap.find(IntNo);
202 if (FLC != LoadPciMap.end()) {
206 auto Inc = cast<ConstantSDNode>(IntN->
getOperand(5));
209 { IntN->getOperand(2), I, IntN->getOperand(4),
210 IntN->getOperand(0) });
225 unsigned Size = 1U << (SizeBits-1);
283 switch (cast<ConstantSDNode>(C->
getOperand(1))->getZExtValue()) {
284 case Intrinsic::hexagon_circ_ldub:
285 case Intrinsic::hexagon_circ_lduh:
288 case Intrinsic::hexagon_circ_ldw:
289 case Intrinsic::hexagon_circ_ldd:
323 const SDLoc &dl(IntN);
324 unsigned IntNo = cast<ConstantSDNode>(IntN->
getOperand(1))->getZExtValue();
326 static const std::map<unsigned, unsigned> LoadBrevMap = {
327 { Intrinsic::hexagon_L2_loadrb_pbr, Hexagon::L2_loadrb_pbr },
328 { Intrinsic::hexagon_L2_loadrub_pbr, Hexagon::L2_loadrub_pbr },
329 { Intrinsic::hexagon_L2_loadrh_pbr, Hexagon::L2_loadrh_pbr },
330 { Intrinsic::hexagon_L2_loadruh_pbr, Hexagon::L2_loadruh_pbr },
331 { Intrinsic::hexagon_L2_loadri_pbr, Hexagon::L2_loadri_pbr },
332 { Intrinsic::hexagon_L2_loadrd_pbr, Hexagon::L2_loadrd_pbr }
334 auto FLI = LoadBrevMap.find(IntNo);
335 if (FLI != LoadBrevMap.end()) {
343 FLI->second, dl, RTys,
344 {IntN->getOperand(2), IntN->getOperand(3), IntN->getOperand(0)});
365 unsigned IntNo = cast<ConstantSDNode>(IntN->
getOperand(1))->getZExtValue();
368 static std::map<unsigned,unsigned> LoadNPcMap = {
369 { Intrinsic::hexagon_L2_loadrub_pci, Hexagon::PS_loadrub_pci },
370 { Intrinsic::hexagon_L2_loadrb_pci, Hexagon::PS_loadrb_pci },
371 { Intrinsic::hexagon_L2_loadruh_pci, Hexagon::PS_loadruh_pci },
372 { Intrinsic::hexagon_L2_loadrh_pci, Hexagon::PS_loadrh_pci },
373 { Intrinsic::hexagon_L2_loadri_pci, Hexagon::PS_loadri_pci },
374 { Intrinsic::hexagon_L2_loadrd_pci, Hexagon::PS_loadrd_pci },
375 { Intrinsic::hexagon_L2_loadrub_pcr, Hexagon::PS_loadrub_pcr },
376 { Intrinsic::hexagon_L2_loadrb_pcr, Hexagon::PS_loadrb_pcr },
377 { Intrinsic::hexagon_L2_loadruh_pcr, Hexagon::PS_loadruh_pcr },
378 { Intrinsic::hexagon_L2_loadrh_pcr, Hexagon::PS_loadrh_pcr },
379 { Intrinsic::hexagon_L2_loadri_pcr, Hexagon::PS_loadri_pcr },
380 { Intrinsic::hexagon_L2_loadrd_pcr, Hexagon::PS_loadrd_pcr }
382 auto FLI = LoadNPcMap.find (IntNo);
383 if (FLI != LoadNPcMap.end()) {
385 if (IntNo == Intrinsic::hexagon_L2_loadrd_pci ||
386 IntNo == Intrinsic::hexagon_L2_loadrd_pcr)
391 auto Inc = cast<ConstantSDNode>(IntN->
getOperand(3));
409 static std::map<unsigned,unsigned> StoreNPcMap = {
410 { Intrinsic::hexagon_S2_storerb_pci, Hexagon::PS_storerb_pci },
411 { Intrinsic::hexagon_S2_storerh_pci, Hexagon::PS_storerh_pci },
412 { Intrinsic::hexagon_S2_storerf_pci, Hexagon::PS_storerf_pci },
413 { Intrinsic::hexagon_S2_storeri_pci, Hexagon::PS_storeri_pci },
414 { Intrinsic::hexagon_S2_storerd_pci, Hexagon::PS_storerd_pci },
415 { Intrinsic::hexagon_S2_storerb_pcr, Hexagon::PS_storerb_pcr },
416 { Intrinsic::hexagon_S2_storerh_pcr, Hexagon::PS_storerh_pcr },
417 { Intrinsic::hexagon_S2_storerf_pcr, Hexagon::PS_storerf_pcr },
418 { Intrinsic::hexagon_S2_storeri_pcr, Hexagon::PS_storeri_pcr },
419 { Intrinsic::hexagon_S2_storerd_pcr, Hexagon::PS_storerd_pcr }
421 auto FSI = StoreNPcMap.find (IntNo);
422 if (FSI != StoreNPcMap.end()) {
426 auto Inc = cast<ConstantSDNode>(IntN->
getOperand(3));
470 int32_t Inc = cast<ConstantSDNode>(Offset.
getNode())->getSExtValue();
480 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
483 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
489 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
496 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
506 if (isAlignedMemNode(ST)) {
508 Opcode = IsValidInc ? Hexagon::V6_vS32b_nt_pi : Hexagon::V6_vS32b_nt_ai;
510 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
512 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
574 auto Default = [
this,
N] () ->
void { SelectCode(N); };
580 int32_t ShlConst = cast<ConstantSDNode>(Shl_1)->getSExtValue();
587 int32_t ValConst =
C->getSExtValue() << ShlConst;
588 if (isInt<9>(ValConst)) {
608 int32_t ValConst = 1 << (ShlConst + C2->getSExtValue());
609 if (isInt<9>(-ValConst)) {
640 unsigned IntNo = cast<ConstantSDNode>(N->
getOperand(1))->getZExtValue();
641 if (IntNo == Intrinsic::hexagon_V6_vgathermw ||
642 IntNo == Intrinsic::hexagon_V6_vgathermw_128B ||
643 IntNo == Intrinsic::hexagon_V6_vgathermh ||
644 IntNo == Intrinsic::hexagon_V6_vgathermh_128B ||
645 IntNo == Intrinsic::hexagon_V6_vgathermhw ||
646 IntNo == Intrinsic::hexagon_V6_vgathermhw_128B) {
650 if (IntNo == Intrinsic::hexagon_V6_vgathermwq ||
651 IntNo == Intrinsic::hexagon_V6_vgathermwq_128B ||
652 IntNo == Intrinsic::hexagon_V6_vgathermhq ||
653 IntNo == Intrinsic::hexagon_V6_vgathermhq_128B ||
654 IntNo == Intrinsic::hexagon_V6_vgathermhwq ||
655 IntNo == Intrinsic::hexagon_V6_vgathermhwq_128B) {
664 unsigned IID = cast<ConstantSDNode>(N->
getOperand(0))->getZExtValue();
667 case Intrinsic::hexagon_S2_vsplatrb:
670 case Intrinsic::hexagon_S2_vsplatrh:
673 case Intrinsic::hexagon_V6_vaddcarry:
674 case Intrinsic::hexagon_V6_vaddcarry_128B:
675 case Intrinsic::hexagon_V6_vsubcarry:
676 case Intrinsic::hexagon_V6_vsubcarry_128B:
686 if (keepsLowBits(V, Bits, U)) {
701 auto *CN = cast<ConstantFPSDNode>(
N);
702 APInt A = CN->getValueAPF().bitcastToAPInt();
722 assert(!(cast<ConstantSDNode>(N)->getZExtValue() >> 1));
723 unsigned Opc = (cast<ConstantSDNode>(
N)->getSExtValue() != 0)
736 int FX = cast<FrameIndexSDNode>(
N)->getIndex();
764 : Hexagon::A4_subp_c;
774 return SelectHvxVAlign(N);
813 int Mask = -cast<ConstantSDNode>(A.
getNode())->getSExtValue();
913 std::vector<SDValue> &OutOps) {
916 switch (ConstraintID) {
923 OutOps.push_back(Res);
925 OutOps.push_back(Inp);
966 SDValue LDBasePtr = cast<MemSDNode>(SYNode)->getBasePtr();
967 SDValue STBasePtr = cast<MemSDNode>(UUse)->getBasePtr();
968 if (LDBasePtr == STBasePtr)
977 void HexagonDAGToDAGISel::ppSimplifyOrSelect0(std::vector<SDNode*> &&Nodes) {
980 for (
auto I : Nodes) {
984 auto IsZero = [] (
const SDValue &V) ->
bool {
986 return SC->isNullValue();
989 auto IsSelect0 = [IsZero] (
const SDValue &
Op) ->
bool {
992 return IsZero(
Op.getOperand(1)) || IsZero(
Op.getOperand(2));
995 SDValue N0 =
I->getOperand(0), N1 =
I->getOperand(1);
996 EVT VT =
I->getValueType(0);
997 bool SelN0 = IsSelect0(N0);
1010 }
else if (IsZero(SX)) {
1025 void HexagonDAGToDAGISel::ppAddrReorderAddShl(std::vector<SDNode*> &&Nodes) {
1028 for (
auto I : Nodes) {
1060 if (EV % (1 << CV) != 0)
1062 unsigned DV = EV / (1 << CV);
1089 void HexagonDAGToDAGISel::ppAddrRewriteAndSrl(std::vector<SDNode*> &&Nodes) {
1093 unsigned Opc =
N->getOpcode();
1130 if (TZ + M1 + LZ != 32)
1154 void HexagonDAGToDAGISel::ppHoistZextI1(std::vector<SDNode*> &&Nodes) {
1158 unsigned Opc =
N->getOpcode();
1165 for (
auto I =
N->use_begin(),
E =
N->use_end();
I !=
E; ++
I) {
1176 unsigned I1N =
I.getOperandNo();
1180 EVT BVT = Ops[I1N].getValueType();
1187 if (isa<MachineSDNode>(U)) {
1196 If0 = DAG.
getNode(UseOpc, dl, UVT, Ops);
1198 If1 = DAG.
getNode(UseOpc, dl, UVT, Ops);
1209 auto getNodes = [
this] () -> std::vector<SDNode*> {
1210 std::vector<SDNode*>
T;
1219 ppSimplifyOrSelect0(getNodes());
1227 ppAddrReorderAddShl(getNodes());
1242 ppAddrRewriteAndSrl(getNodes());
1246 ppHoistZextI1(getNodes());
1249 dbgs() <<
"Preprocessed (Hexagon) selection DAG:";
1254 rebalanceAddressTrees();
1257 dbgs() <<
"Address tree balanced selection DAG:";
1266 if (!HFI.needsAligna(*
MF))
1278 void HexagonDAGToDAGISel::updateAligna() {
1280 if (!HFI.needsAligna(*
MF))
1282 auto *AlignaI =
const_cast<MachineInstr*
>(HFI.getAlignaInstr(*
MF));
1283 assert(AlignaI !=
nullptr);
1285 if (AlignaI->getOperand(1).getImm() < MaxA)
1286 AlignaI->getOperand(1).setImm(MaxA);
1295 int FX = cast<FrameIndexSDNode>(
N)->getIndex();
1337 auto IsAligned = [LogAlign] (uint64_t V) ->
bool {
1338 return alignTo(V, (uint64_t)1 << LogAlign) == V;
1345 int32_t V = cast<const ConstantSDNode>(
N)->getZExtValue();
1366 if (LogAlign > 2 || !IsAligned(cast<BlockAddressSDNode>(N)->
getOffset()))
1381 auto IsAligned = [LogAlign] (uint64_t V) ->
bool {
1382 return alignTo(V, (uint64_t)1 << LogAlign) == V;
1397 if (!IsAligned(Const->getZExtValue()))
1401 uint64_t NewOff = GA->getOffset() + (uint64_t)Const->getSExtValue();
1480 if (!S || S->getZExtValue() != 32)
1506 bool HexagonDAGToDAGISel::keepsLowBits(
const SDValue &Val,
unsigned NumBits,
1534 uint64_t
Mask = (1 << NumBits) - 1;
1536 if (
C->getZExtValue() ==
Mask) {
1542 if (
C->getZExtValue() ==
Mask) {
1552 uint64_t
Mask = (1 << NumBits) - 1;
1554 if ((
C->getZExtValue() &
Mask) == 0) {
1560 if ((
C->getZExtValue() &
Mask) == 0) {
1573 bool HexagonDAGToDAGISel::isAlignedMemNode(
const MemSDNode *
N)
const {
1577 bool HexagonDAGToDAGISel::isSmallStackStore(
const StoreSDNode *N)
const {
1581 return StackSize <= 56;
1583 return StackSize <= 120;
1585 return StackSize <= 248;
1592 bool HexagonDAGToDAGISel::isPositiveHalfWord(
const SDNode *N)
const {
1593 if (
const ConstantSDNode *CN = dyn_cast<const ConstantSDNode>(N)) {
1594 int64_t V = CN->getSExtValue();
1604 bool HexagonDAGToDAGISel::hasOneUse(
const SDNode *N)
const {
1626 int HexagonDAGToDAGISel::getWeight(
SDNode *N) {
1629 assert(RootWeights.count(N) &&
"Cannot get weight of unseen root!");
1630 assert(RootWeights[N] != -1 &&
"Cannot get weight of unvisited root!");
1631 assert(RootWeights[N] != -2 &&
"Cannot get weight of RAWU'd root!");
1632 return RootWeights[
N];
1635 int HexagonDAGToDAGISel::getHeight(
SDNode *N) {
1638 assert(RootWeights.count(N) && RootWeights[
N] >= 0 &&
1639 "Cannot query height of unvisited/RAUW'd node!");
1640 return RootHeights[
N];
1644 struct WeightedLeaf {
1651 WeightedLeaf(
SDValue Value,
int Weight,
int InsertionOrder) :
1652 Value(Value), Weight(Weight), InsertionOrder(InsertionOrder) {
1653 assert(Weight >= 0 &&
"Weight must be >= 0");
1656 static bool Compare(
const WeightedLeaf &A,
const WeightedLeaf &
B) {
1657 assert(A.Value.getNode() && B.Value.getNode());
1658 return A.Weight == B.Weight ?
1659 (A.InsertionOrder > B.InsertionOrder) :
1660 (A.Weight > B.Weight);
1667 class LeafPrioQueue {
1670 WeightedLeaf ConstElt;
1675 return (!HaveConst && Q.
empty());
1679 return Q.
size() + HaveConst;
1686 const WeightedLeaf &top() {
1692 WeightedLeaf pop() {
1701 void push(WeightedLeaf L,
bool SeparateConst=
true) {
1702 if (!HaveConst && SeparateConst && isa<ConstantSDNode>(L.Value)) {
1704 cast<ConstantSDNode>(L.Value)->getSExtValue() == 1)
1707 cast<ConstantSDNode>(L.Value)->getSExtValue() == 0)
1720 void pushToBottom(WeightedLeaf L) {
1727 WeightedLeaf findSHL(uint64_t MaxAmount);
1729 WeightedLeaf findMULbyConst();
1731 LeafPrioQueue(
unsigned Opcode) :
1732 HaveConst(
false), Opcode(Opcode) { }
1736 WeightedLeaf LeafPrioQueue::findSHL(uint64_t MaxAmount) {
1738 WeightedLeaf Result;
1740 for (
int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1741 const WeightedLeaf &L = Q[Pos];
1747 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1748 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1755 if (Result.Value.getNode()) {
1756 Q.erase(&Q[ResultPos]);
1763 WeightedLeaf LeafPrioQueue::findMULbyConst() {
1765 WeightedLeaf Result;
1767 for (
int Pos = 0, End = Q.size(); Pos != End; ++Pos) {
1768 const WeightedLeaf &L = Q[Pos];
1774 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1775 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1782 if (Result.Value.getNode()) {
1783 Q.erase(&Q[ResultPos]);
1790 SDValue HexagonDAGToDAGISel::getMultiplierForSHL(
SDNode *N) {
1799 unsigned MaxFactor = 0;
1800 for (
int i = 0; i < 2; ++i) {
1823 for (
int i = 0; i < 2; ++i)
1824 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1827 return (NewConst == 1);
1836 SDValue HexagonDAGToDAGISel::factorOutPowerOf2(
SDValue V,
unsigned Power) {
1839 for (
int i=0; i < 2; ++i) {
1840 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1852 if (ShiftAmount == Power)
1866 unsigned HexagonDAGToDAGISel::getUsesInFunction(
const Value *V) {
1867 if (GAUsesInFunction.count(V))
1868 return GAUsesInFunction[V];
1870 unsigned Result = 0;
1873 if (isa<Instruction>(U) &&
1878 GAUsesInFunction[V] = Result;
1888 SDValue HexagonDAGToDAGISel::balanceSubTree(
SDNode *N,
bool TopLevel) {
1889 assert(RootWeights.count(N) &&
"Cannot balance non-root node.");
1890 assert(RootWeights[N] != -2 &&
"This node was RAUW'd!");
1894 if (RootWeights[N] != -1)
1908 Weight = getWeight(balanceSubTree(Op0N).getNode());
1911 Weight = getWeight(Op0N);
1915 Weight += getWeight(balanceSubTree(Op1N).getNode());
1918 Weight += getWeight(Op1N);
1920 RootWeights[
N] = Weight;
1924 LLVM_DEBUG(
dbgs() <<
"--> No need to balance root (Weight=" << Weight
1925 <<
" Height=" << RootHeights[N] <<
"): ");
1936 LeafPrioQueue Leaves(NOpcode);
1944 bool CanFactorize =
false;
1945 WeightedLeaf Mul1, Mul2;
1946 unsigned MaxPowerOf2 = 0;
1951 bool HaveTopLevelShift =
false;
1957 HaveTopLevelShift =
true;
1961 int InsertionOrder = 0;
1963 bool Imbalanced =
false;
1964 int CurrentWeight = 0;
1965 while (!Worklist.
empty()) {
1968 if (Child.
getNode() != N && RootWeights.count(Child.
getNode())) {
1971 int Weight = RootWeights[Child.
getNode()];
1973 Child = balanceSubTree(Child.
getNode());
1975 Weight = getWeight(Child.
getNode());
1976 }
else if (Weight == -2) {
1981 return balanceSubTree(N, TopLevel);
1984 NodeHeights[Child] = 1;
1985 CurrentWeight += Weight;
1988 if (TopLevel && !CanFactorize && !HaveTopLevelShift &&
1994 if (!Mul1.Value.getNode()) {
1995 Mul1 = WeightedLeaf(Child, Weight, InsertionOrder++);
1996 MaxPowerOf2 = PowerOf2;
1998 Mul2 = WeightedLeaf(Child, Weight, InsertionOrder++);
1999 MaxPowerOf2 = std::min(MaxPowerOf2, PowerOf2);
2002 if (MaxPowerOf2 > 3)
2005 CanFactorize =
true;
2008 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
2011 int Weight = getWeight(Child.
getNode());
2013 NodeHeights[Child] = getHeight(Child.
getNode());
2014 CurrentWeight += Weight;
2017 GA = WeightedLeaf(Child, Weight, InsertionOrder++);
2019 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
2023 unsigned ChildOpcode = Child.
getOpcode();
2024 assert(ChildOpcode == NOpcode ||
2030 Op1 = getMultiplierForSHL(Child.
getNode());
2035 assert(!NodeHeights.
count(Child) &&
"Parent visited before children?");
2045 NodeHeights[Child] =
std::max(NodeHeights[Op1],
2052 <<
" weight=" << CurrentWeight
2053 <<
" imbalanced=" << Imbalanced <<
"\n");
2059 LLVM_DEBUG(
dbgs() <<
"--> Found common factor for two MUL children!\n");
2060 int Weight = Mul1.Weight + Mul2.Weight;
2061 int Height =
std::max(NodeHeights[Mul1.Value], NodeHeights[Mul2.Value]) + 1;
2062 SDValue Mul1Factored = factorOutPowerOf2(Mul1.Value, MaxPowerOf2);
2063 SDValue Mul2Factored = factorOutPowerOf2(Mul2.Value, MaxPowerOf2);
2065 Mul1Factored, Mul2Factored);
2067 Mul1.Value.getValueType());
2070 NodeHeights[New] = Height;
2071 Leaves.push(WeightedLeaf(New, Weight, Mul1.InsertionOrder));
2072 }
else if (Mul1.Value.getNode()) {
2076 if (Mul2.Value.getNode())
2078 CanFactorize =
false;
2084 bool CombinedGA =
false;
2085 if (NOpcode ==
ISD::ADD && GA.Value.getNode() && Leaves.hasConst() &&
2086 GA.Value.hasOneUse() && N->
use_size() < 3) {
2088 cast<GlobalAddressSDNode>(GA.Value.getOperand(0));
2091 if (getUsesInFunction(GANode->
getGlobal()) == 1 && Offset->hasOneUse() &&
2094 << Offset->getSExtValue() <<
"): ");
2100 GANode->
getOffset() + (uint64_t)Offset->getSExtValue());
2102 GA.Value.getValueType(), NewTGA);
2103 GA.Weight += Leaves.top().Weight;
2105 NodeHeights[GA.Value] = getHeight(GA.Value.getNode());
2114 RootWeights[
N] = CurrentWeight;
2115 RootHeights[
N] = NodeHeights[
SDValue(N, 0)];
2121 if (NOpcode ==
ISD::ADD && GA.Value.getNode()) {
2122 WeightedLeaf
SHL = Leaves.findSHL(31);
2123 if (SHL.Value.getNode()) {
2124 int Height =
std::max(NodeHeights[GA.Value], NodeHeights[SHL.Value]) + 1;
2126 GA.Value.getValueType(),
2127 GA.Value, SHL.Value);
2128 GA.Weight = SHL.Weight;
2129 NodeHeights[GA.Value] = Height;
2133 if (GA.Value.getNode())
2138 if (TopLevel && !CanFactorize && Leaves.hasConst()) {
2140 Leaves.pushToBottom(Leaves.pop());
2147 while (Leaves.size() > 1) {
2148 WeightedLeaf L0 = Leaves.pop();
2152 WeightedLeaf L1 = Leaves.findMULbyConst();
2153 if (!L1.Value.getNode())
2156 assert(L0.Weight <= L1.Weight &&
"Priority queue is broken!");
2159 int V0Weight = L0.Weight;
2161 int V1Weight = L1.Weight;
2164 if ((RootWeights.count(V0.
getNode()) && RootWeights[V0.
getNode()] == -2) ||
2165 (RootWeights.count(V1.
getNode()) && RootWeights[V1.
getNode()] == -2)) {
2167 return balanceSubTree(N, TopLevel);
2182 "Children must have been visited before re-combining them!");
2183 int Height =
std::max(NodeHeights[V0], NodeHeights[V1]) + 1;
2186 if (V1C && NOpcode ==
ISD::MUL && V1C->getAPIntValue().isPowerOf2())
2190 V1C->getAPIntValue().logBase2(),
SDLoc(N),
2195 NodeHeights[NewNode] = Height;
2197 int Weight = V0Weight + V1Weight;
2198 Leaves.push(WeightedLeaf(NewNode, Weight, L0.InsertionOrder));
2201 <<
",Height=" << Height <<
"):\n");
2205 assert(Leaves.size() == 1);
2206 SDValue NewRoot = Leaves.top().Value;
2209 int Height = NodeHeights[NewRoot];
2212 if (NewRoot.getOpcode() ==
ISD::MUL) {
2215 EVT VT = NewRoot.getValueType();
2225 if (N != NewRoot.getNode()) {
2232 RootWeights[
N] = -2;
2237 RootWeights[NewRoot.getNode()] = Leaves.top().Weight;
2238 RootHeights[NewRoot.getNode()] = Height;
2243 void HexagonDAGToDAGISel::rebalanceAddressTrees() {
2249 SDValue BasePtr = cast<MemSDNode>(
N)->getBasePtr();
2254 if (RootWeights.count(BasePtr.
getNode()))
2257 LLVM_DEBUG(
dbgs() <<
"** Rebalancing address calculation in node: ");
2266 while (!Worklist.empty()) {
2267 SDNode *N = Worklist.pop_back_val();
2281 if (RootWeights.count(N))
2284 RootWeights[
N] = -1;
2288 RootWeights[BasePtr.
getNode()] = -1;
2303 GAUsesInFunction.clear();
2304 RootHeights.clear();
2305 RootWeights.clear();
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
static MVT getIntegerVT(unsigned BitWidth)
A parsed version of the target data layout string in and methods for querying it. ...
void SelectV2Q(SDNode *N)
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOffset() const
const GlobalValue * getGlobal() const
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static cl::opt< bool > CheckSingleUse("hexagon-isel-su", cl::Hidden, cl::init(true), cl::desc("Enable checking of SDNode's single-use status"))
This class represents lattice values for constants.
static bool isMemOPCandidate(SDNode *I, SDNode *U)
void EmitFunctionEntryCode() override
static bool willShiftRightEliminate(SDValue V, unsigned Amount)
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
void ReplaceUses(SDValue F, SDValue T)
ReplaceUses - replace all uses of the old node F with the use of the new node T.
const SDValue & getBasePtr() const
void SelectIntrinsicWOChain(SDNode *N)
void push_back(const T &Elt)
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
bool DetectUseSxtw(SDValue &N, SDValue &R)
const SDValue & getValue() const
SDVTList getVTList() const
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
bool SelectAddrFI(SDValue &N, SDValue &R)
const SDValue & getChain() const
static unsigned getPowerOf2Factor(SDValue Val)
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc, or post-dec.
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
unsigned getAlignment() const
void SelectFrameIndex(SDNode *N)
Hexagon target-specific information for each MachineFunction.
constexpr bool isInt< 16 >(int64_t x)
bool isInteger() const
Return true if this is an integer or a vector integer type.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
const HexagonFrameLowering * getFrameLowering() const override
void SelectP2D(SDNode *N)
void setNodeId(int Id)
Set unique node id.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
void SelectVAlignAddr(SDNode *N)
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
unsigned getStackAlignBaseVReg() const
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
unsigned getBitWidth() const
Return the number of bits in the APInt.
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
bool isTruncatingStore() const
Return true if the op does a truncation before store.
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool hasOneUse() const
Return true if there is exactly one use of this node.
void SelectVAlign(SDNode *N)
A description of a memory reference used in the backend.
static ManagedStatic< DebugCounter > DC
static cl::opt< bool > RebalanceOnlyForOptimizations("rebalance-only-opt", cl::Hidden, cl::init(false), cl::desc("Rebalance address tree only if this allows optimizations"))
Shift and rotation operations.
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s), MachineInstr opcode, and operands.
const TargetLowering * TLI
static bool isTargetConstant(const SDValue &V)
uint64_t getConstantOperandVal(unsigned i) const
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
void SelectTypecast(SDNode *N)
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
void SelectIndexedLoad(LoadSDNode *LD, const SDLoc &dl)
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
const DataLayout & getDataLayout() const
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
This class is used to represent EVT's, which are used to parameterize some operations.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
iterator_range< allnodes_iterator > allnodes()
bool getBoolValue() const
Convert APInt to a boolean value.
MachineFunction & getMachineFunction() const
SDValue getTargetFrameIndex(int FI, EVT VT)
Simple integer binary arithmetic operators.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
bool SelectNewCircIntrinsic(SDNode *IntN)
Generate a machine instruction node for the new circlar buffer intrinsics.
static cl::opt< bool > EnableAddressRebalancing("isel-rebalance-addr", cl::Hidden, cl::init(true), cl::desc("Rebalance address calculation trees to improve " "instruction selection"))
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
bool SelectBrevLdIntrinsic(SDNode *IntN)
SDNode * StoreInstrForLoadIntrinsic(MachineSDNode *LoadN, SDNode *IntN)
This class is used to represent ISD::STORE nodes.
void SelectConstantFP(SDNode *N)
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const SDValue & getBasePtr() const
initializer< Ty > init(const Ty &Val)
int64_t getOffset() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool SelectAnyImm0(SDValue &N, SDValue &R)
bool SelectAnyInt(SDValue &N, SDValue &R)
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const SDValue & getOperand(unsigned Num) const
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, std::vector< SDValue > &OutOps) override
SelectInlineAsmMemoryOperand - Implement addressing mode selection for inline asm expressions...
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
const SDValue & getOffset() const
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void SelectLoad(SDNode *N)
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
bool isValidAutoIncImm(const EVT VT, const int Offset) const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
const APInt & getAPIntValue() const
bool SelectAnyImm2(SDValue &N, SDValue &R)
FunctionPass class - This class is used to implement most global optimizations.
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void SelectQ2V(SDNode *N)
void SelectIndexedStore(StoreSDNode *ST, const SDLoc &dl)
const MachineBasicBlock & front() const
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
This class contains a discriminated union of information about pointers in memory operands...
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
unsigned getNumOperands() const
Return the number of values used by this operation.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
static bool isOpcodeHandled(const SDNode *N)
void dump() const
Dump this node, for debugging.
bool isHVXVectorType(MVT VecTy, bool IncludeBool=false) const
bool SelectAddrGA(SDValue &N, SDValue &R)
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
void SelectSHL(SDNode *N)
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BlockVerifier::State From
Align max(MaybeAlign Lhs, Align Rhs)
ilist< SDNode >::size_type allnodes_size() const
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool SelectGlobalAddress(SDValue &N, SDValue &R, bool UseGP, uint32_t LogAlign)
void SelectIntrinsicWChain(SDNode *N)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
void SelectD2P(SDNode *N)
virtual MVT getScalarShiftAmountTy(const DataLayout &, EVT) const
EVT is not used in-tree, but is used by out-of-tree target.
An SDNode that represents everything that will be needed to construct a MachineInstr.
LLVM_NODISCARD T pop_back_val()
CHAIN = SC CHAIN, Imm128 - System call.
This is an abstract virtual class for memory operations.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void SelectConstant(SDNode *N)
Represents one node in the SelectionDAG.
allnodes_const_iterator allnodes_begin() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool SelectAnyImmediate(SDValue &N, SDValue &R, uint32_t LogAlign)
unsigned logBase2() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
size_t use_size() const
Return the number of uses of this node.
EVT getMemoryVT() const
Return the type of the in-memory value.
Class for arbitrary precision integers.
Select(COND, TRUEVAL, FALSEVAL).
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
iterator_range< user_iterator > users()
ZERO_EXTEND - Used for integer types, zeroing the new bits.
ANY_EXTEND - Used for integer types. The high bits are undefined.
static cl::opt< bool > RebalanceOnlyImbalancedTrees("rebalance-only-imbal", cl::Hidden, cl::init(false), cl::desc("Rebalance address tree only if it is imbalanced"))
void SelectStore(SDNode *N)
void SelectAddSubCarry(SDNode *N)
bool SelectAnyImm3(SDValue &N, SDValue &R)
bool SelectAnyImm1(SDValue &N, SDValue &R)
Representation of each machine instruction.
allnodes_const_iterator allnodes_end() const
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
unsigned getVectorLength() const
uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
const SDValue & getValue() const
Bitwise operators - logical and, logical or, logical xor.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT)
LLVM_NODISCARD bool empty() const
bool SelectAddrGP(SDValue &N, SDValue &R)
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
APFloat abs(APFloat X)
Returns the absolute value of the argument.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool tryLoadOfLoadIntrinsic(LoadSDNode *N)
void SelectV65Gather(SDNode *N)
FunctionPass * createHexagonISelDag(HexagonTargetMachine &TM, CodeGenOpt::Level OptLevel)
unsigned getOpcode() const
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
const TargetLowering * getTargetLowering() const
bool SelectAnyImm(SDValue &N, SDValue &R)
This class is used to form a handle around another node that is persistent and is updated across invo...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
void PreprocessISelDAG() override
PreprocessISelDAG - This hook allows targets to hack on the graph before instruction selection starts...
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static const Function * getParent(const Value *V)
bool isNonTemporal() const
void ReplaceNode(SDNode *F, SDNode *T)
Replace all uses of F with T, then remove F from the DAG.
void SelectHVXDualOutput(SDNode *N)
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
const SDValue & getOperand(unsigned i) const
uint64_t getZExtValue() const
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
MachineSDNode * LoadInstrForLoadIntrinsic(SDNode *IntN)
void Select(SDNode *N) override
Main hook for targets to transform nodes into machine nodes.
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand *> NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
void SelectV65GatherPred(SDNode *N)
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
This class is used to represent ISD::LOAD nodes.