26 #define DEBUG_TYPE "hexagon-isel"
31 cl::desc(
"Rebalance address calculation trees to improve "
32 "instruction selection"));
39 cl::desc(
"Rebalance address tree only if this allows optimizations"));
44 cl::init(
false),
cl::desc(
"Rebalance address tree only if it is imbalanced"));
74 bool ComplexPatternFuncMutatesDAG()
const override {
77 void PreprocessISelDAG()
override;
78 void EmitFunctionEntryCode()
override;
90 return "Hexagon DAG->DAG Pattern Instruction Selection";
101 void SelectFrameIndex(
SDNode *
N);
104 bool SelectInlineAsmMemoryOperand(
const SDValue &
Op,
105 unsigned ConstraintID,
106 std::vector<SDValue> &OutOps)
override;
113 void SelectZeroExtend(
SDNode *
N);
114 void SelectIntrinsicWChain(
SDNode *
N);
115 void SelectIntrinsicWOChain(
SDNode *
N);
116 void SelectConstant(
SDNode *
N);
117 void SelectConstantFP(
SDNode *
N);
121 #include "HexagonGenDAGISel.inc"
124 bool keepsLowBits(
const SDValue &Val,
unsigned NumBits,
SDValue &Src);
125 bool isOrEquivalentToAdd(
const SDNode *
N)
const;
126 bool isAlignedMemNode(
const MemSDNode *
N)
const;
128 bool isPositiveHalfWord(
const SDNode *
N)
const;
131 void ppSimplifyOrSelect0(std::vector<SDNode*> &&Nodes);
132 void ppAddrReorderAddShl(std::vector<SDNode*> &&Nodes);
133 void ppAddrRewriteAndSrl(std::vector<SDNode*> &&Nodes);
134 void ppHoistZextI1(std::vector<SDNode*> &&Nodes);
143 unsigned getUsesInFunction(
const Value *V);
145 void rebalanceAddressTrees();
156 return new HexagonDAGToDAGISel(TM, OptLevel);
165 case Intrinsic::hexagon_C2_cmpeq:
166 case Intrinsic::hexagon_C2_cmpgt:
167 case Intrinsic::hexagon_C2_cmpgtu:
168 case Intrinsic::hexagon_C2_cmpgtup:
169 case Intrinsic::hexagon_C2_cmpgtp:
170 case Intrinsic::hexagon_C2_cmpeqp:
171 case Intrinsic::hexagon_C2_bitsset:
172 case Intrinsic::hexagon_C2_bitsclr:
173 case Intrinsic::hexagon_C2_cmpeqi:
174 case Intrinsic::hexagon_C2_cmpgti:
175 case Intrinsic::hexagon_C2_cmpgtui:
176 case Intrinsic::hexagon_C2_cmpgei:
177 case Intrinsic::hexagon_C2_cmpgeui:
178 case Intrinsic::hexagon_C2_cmplt:
179 case Intrinsic::hexagon_C2_cmpltu:
180 case Intrinsic::hexagon_C2_bitsclri:
181 case Intrinsic::hexagon_C2_and:
182 case Intrinsic::hexagon_C2_or:
183 case Intrinsic::hexagon_C2_xor:
184 case Intrinsic::hexagon_C2_andn:
185 case Intrinsic::hexagon_C2_not:
186 case Intrinsic::hexagon_C2_orn:
187 case Intrinsic::hexagon_C2_pxfer_map:
188 case Intrinsic::hexagon_C2_any8:
189 case Intrinsic::hexagon_C2_all8:
190 case Intrinsic::hexagon_A2_vcmpbeq:
191 case Intrinsic::hexagon_A2_vcmpbgtu:
192 case Intrinsic::hexagon_A2_vcmpheq:
193 case Intrinsic::hexagon_A2_vcmphgt:
194 case Intrinsic::hexagon_A2_vcmphgtu:
195 case Intrinsic::hexagon_A2_vcmpweq:
196 case Intrinsic::hexagon_A2_vcmpwgt:
197 case Intrinsic::hexagon_A2_vcmpwgtu:
198 case Intrinsic::hexagon_C2_tfrrp:
199 case Intrinsic::hexagon_S2_tstbit_i:
200 case Intrinsic::hexagon_S2_tstbit_r:
209 int32_t Inc = cast<ConstantSDNode>(Offset.
getNode())->getSExtValue();
217 bool IsValidInc = HII->isValidAutoIncImm(LoadedVT, Inc);
223 Opcode = IsValidInc ? Hexagon::L2_loadrub_pi : Hexagon::L2_loadrub_io;
225 Opcode = IsValidInc ? Hexagon::L2_loadrb_pi : Hexagon::L2_loadrb_io;
229 Opcode = IsValidInc ? Hexagon::L2_loadruh_pi : Hexagon::L2_loadruh_io;
231 Opcode = IsValidInc ? Hexagon::L2_loadrh_pi : Hexagon::L2_loadrh_io;
234 Opcode = IsValidInc ? Hexagon::L2_loadri_pi : Hexagon::L2_loadri_io;
237 Opcode = IsValidInc ? Hexagon::L2_loadrd_pi : Hexagon::L2_loadrd_io;
244 if (isAlignedMemNode(LD)) {
246 Opcode = IsValidInc ? Hexagon::V6_vL32b_nt_pi : Hexagon::V6_vL32b_nt_ai;
248 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi : Hexagon::V6_vL32b_ai;
250 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi : Hexagon::V6_vL32Ub_ai;
258 if (isAlignedMemNode(LD)) {
260 Opcode = IsValidInc ? Hexagon::V6_vL32b_nt_pi_128B
261 : Hexagon::V6_vL32b_nt_ai_128B;
263 Opcode = IsValidInc ? Hexagon::V6_vL32b_pi_128B
264 : Hexagon::V6_vL32b_ai_128B;
266 Opcode = IsValidInc ? Hexagon::V6_vL32Ub_pi_128B
267 : Hexagon::V6_vL32Ub_ai_128B;
282 return CurDAG->getMachineNode(Hexagon::A4_combineir, dl,
MVT::i64,
286 return CurDAG->getMachineNode(Hexagon::A2_sxtw, dl,
MVT::i64,
328 ReplaceUses(From, To, 3);
329 CurDAG->RemoveDeadNode(LD);
338 unsigned IntNo = cast<ConstantSDNode>(IntN->
getOperand(1))->getZExtValue();
340 static std::map<unsigned,unsigned> LoadPciMap = {
341 { Intrinsic::hexagon_circ_ldb, Hexagon::L2_loadrb_pci },
342 { Intrinsic::hexagon_circ_ldub, Hexagon::L2_loadrub_pci },
343 { Intrinsic::hexagon_circ_ldh, Hexagon::L2_loadrh_pci },
344 { Intrinsic::hexagon_circ_lduh, Hexagon::L2_loadruh_pci },
345 { Intrinsic::hexagon_circ_ldw, Hexagon::L2_loadri_pci },
346 { Intrinsic::hexagon_circ_ldd, Hexagon::L2_loadrd_pci },
348 auto FLC = LoadPciMap.find(IntNo);
349 if (FLC != LoadPciMap.end()) {
350 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl,
MVT::i32,
355 auto Inc = cast<ConstantSDNode>(IntN->
getOperand(5));
357 MachineSDNode *Res = CurDAG->getMachineNode(FLC->second, dl, RTys,
362 static std::map<unsigned,unsigned> LoadPbrMap = {
363 { Intrinsic::hexagon_brev_ldb, Hexagon::L2_loadrb_pbr },
364 { Intrinsic::hexagon_brev_ldub, Hexagon::L2_loadrub_pbr },
365 { Intrinsic::hexagon_brev_ldh, Hexagon::L2_loadrh_pbr },
366 { Intrinsic::hexagon_brev_lduh, Hexagon::L2_loadruh_pbr },
367 { Intrinsic::hexagon_brev_ldw, Hexagon::L2_loadri_pbr },
368 { Intrinsic::hexagon_brev_ldd, Hexagon::L2_loadrd_pbr },
370 auto FLB = LoadPbrMap.find(IntNo);
371 if (FLB != LoadPbrMap.end()) {
372 SDNode *Mod = CurDAG->getMachineNode(Hexagon::A2_tfrrcr, dl,
MVT::i32,
377 MachineSDNode *Res = CurDAG->getMachineNode(FLB->second, dl, RTys,
393 unsigned Size = 1U << (SizeBits-1);
401 TS = CurDAG->getStore(
SDValue(LoadN, 2), dl,
SDValue(LoadN, 0), Loc, PI,
404 TS = CurDAG->getTruncStore(
SDValue(LoadN, 2), dl,
SDValue(LoadN, 0), Loc,
405 PI, MVT::getIntegerVT(Size * 8), Size);
411 StoreN = Handle.getValue().getNode();
420 bool HexagonDAGToDAGISel::tryLoadOfLoadIntrinsic(
LoadSDNode *N) {
451 switch (cast<ConstantSDNode>(C->
getOperand(1))->getZExtValue()) {
452 case Intrinsic::hexagon_brev_ldub:
453 case Intrinsic::hexagon_brev_lduh:
454 case Intrinsic::hexagon_circ_ldub:
455 case Intrinsic::hexagon_circ_lduh:
458 case Intrinsic::hexagon_brev_ldw:
459 case Intrinsic::hexagon_brev_ldd:
460 case Intrinsic::hexagon_circ_ldw:
461 case Intrinsic::hexagon_circ_ldd:
477 SDNode *S = StoreInstrForLoadIntrinsic(L, C);
484 CurDAG->RemoveDeadNode(C);
491 void HexagonDAGToDAGISel::SelectLoad(
SDNode *N) {
498 SelectIndexedLoad(LD, dl);
503 if (tryLoadOfLoadIntrinsic(LD))
515 int32_t Inc = cast<ConstantSDNode>(Offset.
getNode())->getSExtValue();
519 bool IsValidInc = HII->isValidAutoIncImm(StoredVT, Inc);
525 Opcode = IsValidInc ? Hexagon::S2_storerb_pi : Hexagon::S2_storerb_io;
528 Opcode = IsValidInc ? Hexagon::S2_storerh_pi : Hexagon::S2_storerh_io;
531 Opcode = IsValidInc ? Hexagon::S2_storeri_pi : Hexagon::S2_storeri_io;
534 Opcode = IsValidInc ? Hexagon::S2_storerd_pi : Hexagon::S2_storerd_io;
541 if (isAlignedMemNode(ST)) {
543 Opcode = IsValidInc ? Hexagon::V6_vS32b_nt_pi : Hexagon::V6_vS32b_nt_ai;
545 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi : Hexagon::V6_vS32b_ai;
547 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi : Hexagon::V6_vS32Ub_ai;
555 if (isAlignedMemNode(ST)) {
557 Opcode = IsValidInc ? Hexagon::V6_vS32b_nt_pi_128B
558 : Hexagon::V6_vS32b_nt_ai_128B;
560 Opcode = IsValidInc ? Hexagon::V6_vS32b_pi_128B
561 : Hexagon::V6_vS32b_ai_128B;
563 Opcode = IsValidInc ? Hexagon::V6_vS32Ub_pi_128B
564 : Hexagon::V6_vS32Ub_ai_128B;
573 Value = CurDAG->getTargetExtractSubreg(Hexagon::isub_lo,
574 dl, MVT::i32, Value);
577 SDValue IncV = CurDAG->getTargetConstant(Inc, dl, MVT::i32);
587 SDValue Ops[] = { Base, IncV, Value, Chain };
594 SDValue Zero = CurDAG->getTargetConstant(0, dl, MVT::i32);
599 MachineSDNode *A = CurDAG->getMachineNode(Hexagon::A2_addi, dl, MVT::i32,
604 ReplaceUses(From, To, 2);
605 CurDAG->RemoveDeadNode(ST);
608 void HexagonDAGToDAGISel::SelectStore(
SDNode *N) {
615 SelectIndexedStore(ST, dl);
622 void HexagonDAGToDAGISel::SelectSHL(
SDNode *N) {
627 auto Default = [
this,
N] () ->
void { SelectCode(N); };
633 int32_t ShlConst = cast<ConstantSDNode>(Shl_1)->getSExtValue();
640 int32_t ValConst = C->getSExtValue() << ShlConst;
641 if (isInt<9>(ValConst)) {
642 SDValue Val = CurDAG->getTargetConstant(ValConst, dl, MVT::i32);
643 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
644 MVT::i32, Mul_0, Val);
645 ReplaceNode(N, Result);
661 int32_t ValConst = 1 << (ShlConst + C2->getSExtValue());
662 if (isInt<9>(-ValConst)) {
663 SDValue Val = CurDAG->getTargetConstant(-ValConst, dl, MVT::i32);
664 SDNode *Result = CurDAG->getMachineNode(Hexagon::M2_mpysmi, dl,
665 MVT::i32, Shl2_0, Val);
666 ReplaceNode(N, Result);
687 void HexagonDAGToDAGISel::SelectZeroExtend(
SDNode *N) {
696 SDNode *
Mask = CurDAG->getMachineNode(Hexagon::C2_mask, dl, MVT::i64, Op0);
700 uint64_t MV = 0,
Bit = 1;
701 for (
unsigned i = 0; i <
NE; ++i) {
705 SDValue Ones = CurDAG->getTargetConstant(MV, dl, MVT::i64);
706 SDNode *OnesReg = CurDAG->getMachineNode(Hexagon::CONST64, dl,
709 SDNode *And = CurDAG->getMachineNode(Hexagon::A2_andp, dl, MVT::i64,
711 SDValue SubR = CurDAG->getTargetConstant(Hexagon::isub_lo, dl, MVT::i32);
712 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::EXTRACT_SUBREG, dl, ExVT,
717 CurDAG->getMachineNode(Hexagon::A2_andp, dl, ExVT,
724 unsigned ID = cast<ConstantSDNode>(Int->
getOperand(0))->getZExtValue();
729 SDValue TargetConst0 = CurDAG->getTargetConstant(0, dl, MVT::i32);
730 SDNode *Result_1 = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
732 SDNode *Result_2 = CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl,
733 MVT::i32, TargetConst0);
734 SDNode *Result_3 = CurDAG->getMachineNode(Hexagon::A2_combinew, dl,
738 ReplaceNode(N, Result_3);
743 SDNode* RsPd = CurDAG->getMachineNode(Hexagon::C2_tfrpr, dl,
745 ReplaceNode(N, RsPd);
758 void HexagonDAGToDAGISel::SelectIntrinsicWChain(
SDNode *N) {
760 StoreInstrForLoadIntrinsic(L, N);
761 CurDAG->RemoveDeadNode(N);
767 void HexagonDAGToDAGISel::SelectIntrinsicWOChain(
SDNode *N) {
768 unsigned IID = cast<ConstantSDNode>(N->
getOperand(0))->getZExtValue();
771 case Intrinsic::hexagon_S2_vsplatrb:
774 case Intrinsic::hexagon_S2_vsplatrh:
784 if (keepsLowBits(V, Bits, U)) {
797 void HexagonDAGToDAGISel::SelectConstantFP(
SDNode *N) {
802 SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i32);
803 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::A2_tfrsi, dl, MVT::f32, V));
807 SDValue V = CurDAG->getTargetConstant(A.getZExtValue(), dl, MVT::i64);
808 ReplaceNode(N, CurDAG->getMachineNode(Hexagon::CONST64, dl, MVT::f64, V));
818 void HexagonDAGToDAGISel::SelectConstant(
SDNode *N) {
820 assert(!(cast<ConstantSDNode>(N)->getZExtValue() >> 1));
821 unsigned Opc = (cast<ConstantSDNode>(
N)->getSExtValue() != 0)
824 ReplaceNode(N, CurDAG->getMachineNode(Opc,
SDLoc(N), MVT::i1));
832 void HexagonDAGToDAGISel::SelectFrameIndex(
SDNode *N) {
835 int FX = cast<FrameIndexSDNode>(
N)->getIndex();
838 SDValue FI = CurDAG->getTargetFrameIndex(FX, MVT::i32);
840 SDValue Zero = CurDAG->getTargetConstant(0,
DL, MVT::i32);
849 R = CurDAG->getMachineNode(Hexagon::PS_fi,
DL, MVT::i32, FI, Zero);
854 SDValue Ops[] = { CurDAG->getCopyFromReg(CH,
DL, AR, MVT::i32), FI, Zero };
855 R = CurDAG->getMachineNode(Hexagon::PS_fia,
DL, MVT::i32, Ops);
862 void HexagonDAGToDAGISel::SelectBitcast(
SDNode *N) {
874 CurDAG->RemoveDeadNode(N);
898 bool HexagonDAGToDAGISel::
899 SelectInlineAsmMemoryOperand(
const SDValue &
Op,
unsigned ConstraintID,
900 std::vector<SDValue> &OutOps) {
903 switch (ConstraintID) {
906 case InlineAsm::Constraint_i:
907 case InlineAsm::Constraint_o:
908 case InlineAsm::Constraint_v:
909 case InlineAsm::Constraint_m:
910 if (SelectAddrFI(Inp, Res))
911 OutOps.push_back(Res);
913 OutOps.push_back(Inp);
917 OutOps.push_back(CurDAG->getTargetConstant(0,
SDLoc(Op), MVT::i32));
954 SDValue LDBasePtr = cast<MemSDNode>(SYNode)->getBasePtr();
955 SDValue STBasePtr = cast<MemSDNode>(UUse)->getBasePtr();
956 if (LDBasePtr == STBasePtr)
965 void HexagonDAGToDAGISel::ppSimplifyOrSelect0(std::vector<SDNode*> &&Nodes) {
968 for (
auto I : Nodes) {
972 auto IsZero = [] (
const SDValue &V) ->
bool {
974 return SC->isNullValue();
977 auto IsSelect0 = [IsZero] (
const SDValue &
Op) ->
bool {
984 EVT VT = I->getValueType(0);
985 bool SelN0 = IsSelect0(N0);
998 }
else if (IsZero(SX)) {
1013 void HexagonDAGToDAGISel::ppAddrReorderAddShl(std::vector<SDNode*> &&Nodes) {
1016 for (
auto I : Nodes) {
1048 if (EV % (1 << CV) != 0)
1050 unsigned DV = EV / (1 << CV);
1077 void HexagonDAGToDAGISel::ppAddrRewriteAndSrl(std::vector<SDNode*> &&Nodes) {
1080 for (
SDNode *N : Nodes) {
1118 if (TZ + M1 + LZ != 32)
1142 void HexagonDAGToDAGISel::ppHoistZextI1(std::vector<SDNode*> &&Nodes) {
1145 for (
SDNode *N : Nodes) {
1164 unsigned I1N = I.getOperandNo();
1175 if (isa<MachineSDNode>(U)) {
1184 If0 = DAG.
getNode(UseOpc, dl, UVT, Ops);
1186 If1 = DAG.
getNode(UseOpc, dl, UVT, Ops);
1194 void HexagonDAGToDAGISel::PreprocessISelDAG() {
1197 auto getNodes = [
this] () -> std::vector<SDNode*> {
1198 std::vector<SDNode*>
T;
1199 T.reserve(CurDAG->allnodes_size());
1200 for (
SDNode &N : CurDAG->allnodes())
1207 ppSimplifyOrSelect0(getNodes());
1215 ppAddrReorderAddShl(getNodes());
1230 ppAddrRewriteAndSrl(getNodes());
1234 ppHoistZextI1(getNodes());
1237 dbgs() <<
"Preprocessed (Hexagon) selection DAG:";
1242 rebalanceAddressTrees();
1245 dbgs() <<
"Address tree balanced selection DAG:";
1251 void HexagonDAGToDAGISel::EmitFunctionEntryCode() {
1253 auto &HFI = *HST.getFrameLowering();
1259 unsigned AR = FuncInfo->CreateReg(MVT::i32);
1270 auto &HFI = *HST->getFrameLowering();
1272 int FX = cast<FrameIndexSDNode>(
N)->getIndex();
1275 R = CurDAG->getTargetFrameIndex(FX, MVT::i32);
1279 inline bool HexagonDAGToDAGISel::SelectAddrGA(
SDValue &N,
SDValue &R) {
1280 return SelectGlobalAddress(N, R,
false);
1283 inline bool HexagonDAGToDAGISel::SelectAddrGP(
SDValue &N,
SDValue &R) {
1284 return SelectGlobalAddress(N, R,
true);
1287 bool HexagonDAGToDAGISel::SelectGlobalAddress(
SDValue &N,
SDValue &R,
1302 uint64_t NewOff = GA->getOffset() + (uint64_t)
Const->getSExtValue();
1303 R = CurDAG->getTargetGlobalAddress(GA->getGlobal(),
SDLoc(Const),
1328 bool HexagonDAGToDAGISel::DetectUseSxtw(
SDValue &N,
SDValue &R) {
1385 CurDAG->getTargetConstant(Hexagon::DoubleRegsRegClassID, dl, MVT::i32),
1386 R, CurDAG->getTargetConstant(Hexagon::isub_hi, dl, MVT::i32),
1387 R, CurDAG->getTargetConstant(Hexagon::isub_lo, dl, MVT::i32)
1389 SDNode *T = CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl,
1395 bool HexagonDAGToDAGISel::keepsLowBits(
const SDValue &Val,
unsigned NumBits,
1423 uint64_t Mask = (1 << NumBits) - 1;
1425 if (C->getZExtValue() ==
Mask) {
1431 if (C->getZExtValue() ==
Mask) {
1441 uint64_t Mask = (1 << NumBits) - 1;
1443 if ((C->getZExtValue() &
Mask) == 0) {
1449 if ((C->getZExtValue() &
Mask) == 0) {
1462 bool HexagonDAGToDAGISel::isOrEquivalentToAdd(
const SDNode *N)
const {
1468 if (
auto *FN = dyn_cast<FrameIndexSDNode>(N->
getOperand(0))) {
1472 int32_t Off = C->getSExtValue();
1475 return (Off >= 0) && (((A-1) & Off) ==
unsigned(Off));
1480 bool HexagonDAGToDAGISel::isAlignedMemNode(
const MemSDNode *N)
const {
1484 bool HexagonDAGToDAGISel::isSmallStackStore(
const StoreSDNode *N)
const {
1485 unsigned StackSize = MF->getFrameInfo().estimateStackSize(*MF);
1488 return StackSize <= 56;
1490 return StackSize <= 120;
1492 return StackSize <= 248;
1499 bool HexagonDAGToDAGISel::isPositiveHalfWord(
const SDNode *N)
const {
1500 if (
const ConstantSDNode *CN = dyn_cast<const ConstantSDNode>(N)) {
1529 int HexagonDAGToDAGISel::getWeight(
SDNode *N) {
1532 assert(RootWeights.count(N) &&
"Cannot get weight of unseen root!");
1533 assert(RootWeights[N] != -1 &&
"Cannot get weight of unvisited root!");
1534 assert(RootWeights[N] != -2 &&
"Cannot get weight of RAWU'd root!");
1535 return RootWeights[
N];
1538 int HexagonDAGToDAGISel::getHeight(
SDNode *N) {
1541 assert(RootWeights.count(N) && RootWeights[
N] >= 0 &&
1542 "Cannot query height of unvisited/RAUW'd node!");
1543 return RootHeights[
N];
1547 struct WeightedLeaf {
1552 WeightedLeaf() : Value(
SDValue()) { }
1554 WeightedLeaf(
SDValue Value,
int Weight,
int InsertionOrder) :
1555 Value(Value), Weight(Weight), InsertionOrder(InsertionOrder) {
1556 assert(Weight >= 0 &&
"Weight must be >= 0");
1559 static bool Compare(
const WeightedLeaf &A,
const WeightedLeaf &
B) {
1560 assert(A.Value.getNode() && B.Value.getNode());
1561 return A.Weight == B.Weight ?
1562 (A.InsertionOrder > B.InsertionOrder) :
1563 (A.Weight > B.Weight);
1570 class LeafPrioQueue {
1573 WeightedLeaf ConstElt;
1578 return (!HaveConst && Q.empty());
1582 return Q.size() + HaveConst;
1589 const WeightedLeaf &top() {
1595 WeightedLeaf pop() {
1601 return Q.pop_back_val();
1604 void push(WeightedLeaf L,
bool SeparateConst=
true) {
1605 if (!HaveConst && SeparateConst && isa<ConstantSDNode>(L.Value)) {
1607 cast<ConstantSDNode>(L.Value)->getSExtValue() == 1)
1610 cast<ConstantSDNode>(L.Value)->getSExtValue() == 0)
1623 void pushToBottom(WeightedLeaf L) {
1630 WeightedLeaf findSHL(uint64_t MaxAmount);
1632 WeightedLeaf findMULbyConst();
1634 LeafPrioQueue(
unsigned Opcode) :
1635 HaveConst(
false), Opcode(Opcode) { }
1639 WeightedLeaf LeafPrioQueue::findSHL(uint64_t MaxAmount) {
1641 WeightedLeaf Result;
1643 for (
int Pos = 0,
End = Q.size(); Pos !=
End; ++Pos) {
1644 const WeightedLeaf &L = Q[Pos];
1650 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1651 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1658 if (Result.Value.getNode()) {
1659 Q.erase(&Q[ResultPos]);
1666 WeightedLeaf LeafPrioQueue::findMULbyConst() {
1668 WeightedLeaf Result;
1670 for (
int Pos = 0,
End = Q.size(); Pos !=
End; ++Pos) {
1671 const WeightedLeaf &L = Q[Pos];
1677 if (!Result.Value.getNode() || Result.Weight > L.Weight ||
1678 (Result.Weight == L.Weight && Result.InsertionOrder > L.InsertionOrder))
1685 if (Result.Value.getNode()) {
1686 Q.erase(&Q[ResultPos]);
1693 SDValue HexagonDAGToDAGISel::getMultiplierForSHL(
SDNode *N) {
1695 return CurDAG->getConstant(MulFactor,
SDLoc(N),
1702 unsigned MaxFactor = 0;
1703 for (
int i = 0; i < 2; ++i) {
1726 for (
int i = 0; i < 2; ++i)
1727 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1730 return (NewConst == 1);
1739 SDValue HexagonDAGToDAGISel::factorOutPowerOf2(
SDValue V,
unsigned Power) {
1742 for (
int i=0; i < 2; ++i) {
1743 if (isa<ConstantSDNode>(Ops[i].getNode()) &&
1748 Ops[i] = CurDAG->getConstant(NewConst,
1755 if (ShiftAmount == Power)
1757 Ops[1] = CurDAG->getConstant(ShiftAmount - Power,
1769 unsigned HexagonDAGToDAGISel::getUsesInFunction(
const Value *V) {
1770 if (GAUsesInFunction.count(V))
1771 return GAUsesInFunction[V];
1773 unsigned Result = 0;
1774 const Function *CurF = CurDAG->getMachineFunction().getFunction();
1776 if (isa<Instruction>(U) &&
1781 GAUsesInFunction[V] = Result;
1791 SDValue HexagonDAGToDAGISel::balanceSubTree(
SDNode *N,
bool TopLevel) {
1792 assert(RootWeights.count(N) &&
"Cannot balance non-root node.");
1793 assert(RootWeights[N] != -2 &&
"This node was RAUW'd!");
1797 if (RootWeights[N] != -1)
1811 Weight = getWeight(balanceSubTree(Op0N).getNode());
1814 Weight = getWeight(Op0N);
1818 Weight += getWeight(balanceSubTree(Op1N).getNode());
1821 Weight += getWeight(Op1N);
1823 RootWeights[
N] = Weight;
1827 DEBUG(
dbgs() <<
"--> No need to balance root (Weight=" << Weight
1828 <<
" Height=" << RootHeights[N] <<
"): ");
1834 DEBUG(
dbgs() <<
"** Balancing root node: ");
1839 LeafPrioQueue Leaves(NOpcode);
1847 bool CanFactorize =
false;
1848 WeightedLeaf Mul1, Mul2;
1849 unsigned MaxPowerOf2 = 0;
1854 bool HaveTopLevelShift =
false;
1860 HaveTopLevelShift =
true;
1864 int InsertionOrder = 0;
1866 bool Imbalanced =
false;
1867 int CurrentWeight = 0;
1868 while (!Worklist.
empty()) {
1871 if (Child.
getNode() != N && RootWeights.count(Child.
getNode())) {
1874 int Weight = RootWeights[Child.
getNode()];
1876 Child = balanceSubTree(Child.
getNode());
1878 Weight = getWeight(Child.
getNode());
1879 }
else if (Weight == -2) {
1883 DEBUG(
dbgs() <<
"--> Subtree was RAUWd. Restarting...\n");
1884 return balanceSubTree(N, TopLevel);
1887 NodeHeights[Child] = 1;
1888 CurrentWeight += Weight;
1891 if (TopLevel && !CanFactorize && !HaveTopLevelShift &&
1897 if (!Mul1.Value.getNode()) {
1898 Mul1 = WeightedLeaf(Child, Weight, InsertionOrder++);
1899 MaxPowerOf2 = PowerOf2;
1901 Mul2 = WeightedLeaf(Child, Weight, InsertionOrder++);
1902 MaxPowerOf2 =
std::min(MaxPowerOf2, PowerOf2);
1905 if (MaxPowerOf2 > 3)
1908 CanFactorize =
true;
1911 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1914 int Weight = getWeight(Child.
getNode());
1916 NodeHeights[Child] = getHeight(Child.
getNode());
1917 CurrentWeight += Weight;
1920 GA = WeightedLeaf(Child, Weight, InsertionOrder++);
1922 Leaves.push(WeightedLeaf(Child, Weight, InsertionOrder++));
1926 unsigned ChildOpcode = Child.
getOpcode();
1927 assert(ChildOpcode == NOpcode ||
1933 Op1 = getMultiplierForSHL(Child.
getNode());
1937 if (!NodeHeights.count(Op1) || !NodeHeights.count(Child->
getOperand(0))) {
1938 assert(!NodeHeights.count(Child) &&
"Parent visited before children?");
1948 NodeHeights[Child] =
std::max(NodeHeights[Op1],
1955 <<
" weight=" << CurrentWeight <<
" imbalanced="
1956 << Imbalanced <<
"\n");
1962 DEBUG(
dbgs() <<
"--> Found common factor for two MUL children!\n");
1963 int Weight = Mul1.Weight + Mul2.Weight;
1964 int Height =
std::max(NodeHeights[Mul1.Value], NodeHeights[Mul2.Value]) + 1;
1965 SDValue Mul1Factored = factorOutPowerOf2(Mul1.Value, MaxPowerOf2);
1966 SDValue Mul2Factored = factorOutPowerOf2(Mul2.Value, MaxPowerOf2);
1968 Mul1Factored, Mul2Factored);
1970 Mul1.Value.getValueType());
1973 NodeHeights[New] = Height;
1974 Leaves.push(WeightedLeaf(New, Weight, Mul1.InsertionOrder));
1975 }
else if (Mul1.Value.getNode()) {
1979 if (Mul2.Value.getNode())
1981 CanFactorize =
false;
1987 bool CombinedGA =
false;
1988 if (NOpcode ==
ISD::ADD && GA.Value.getNode() && Leaves.hasConst() &&
1989 GA.Value.hasOneUse() && N->
use_size() < 3) {
1991 cast<GlobalAddressSDNode>(GA.Value.getOperand(0));
1992 ConstantSDNode *Offset = cast<ConstantSDNode>(Leaves.top().Value);
1994 if (getUsesInFunction(GANode->
getGlobal()) == 1 && Offset->hasOneUse() &&
1995 getTargetLowering()->isOffsetFoldingLegal(GANode)) {
1996 DEBUG(
dbgs() <<
"--> Combining GA and offset (" << Offset->getSExtValue()
2001 CurDAG->getTargetGlobalAddress(GANode->
getGlobal(),
SDLoc(GA.Value),
2003 GANode->
getOffset() + (uint64_t)Offset->getSExtValue());
2004 GA.Value = CurDAG->getNode(GA.Value.getOpcode(),
SDLoc(GA.Value),
2005 GA.Value.getValueType(), NewTGA);
2006 GA.Weight += Leaves.top().Weight;
2008 NodeHeights[GA.Value] = getHeight(GA.Value.getNode());
2017 RootWeights[
N] = CurrentWeight;
2018 RootHeights[
N] = NodeHeights[
SDValue(N, 0)];
2024 if (NOpcode ==
ISD::ADD && GA.Value.getNode()) {
2025 WeightedLeaf
SHL = Leaves.findSHL(31);
2026 if (SHL.Value.getNode()) {
2027 int Height =
std::max(NodeHeights[GA.Value], NodeHeights[SHL.Value]) + 1;
2029 GA.Value.getValueType(),
2030 GA.Value, SHL.Value);
2031 GA.Weight = SHL.Weight;
2032 NodeHeights[GA.Value] = Height;
2036 if (GA.Value.getNode())
2041 if (TopLevel && !CanFactorize && Leaves.hasConst()) {
2042 DEBUG(
dbgs() <<
"--> Pushing constant to tip of tree.");
2043 Leaves.pushToBottom(Leaves.pop());
2046 const DataLayout &DL = CurDAG->getDataLayout();
2050 while (Leaves.size() > 1) {
2051 WeightedLeaf L0 = Leaves.pop();
2055 WeightedLeaf L1 = Leaves.findMULbyConst();
2056 if (!L1.Value.getNode())
2059 assert(L0.Weight <= L1.Weight &&
"Priority queue is broken!");
2062 int V0Weight = L0.Weight;
2064 int V1Weight = L1.Weight;
2067 if ((RootWeights.count(V0.
getNode()) && RootWeights[V0.
getNode()] == -2) ||
2068 (RootWeights.count(V1.
getNode()) && RootWeights[V1.
getNode()] == -2)) {
2069 DEBUG(
dbgs() <<
"--> Subtree was RAUWd. Restarting...\n");
2070 return balanceSubTree(N, TopLevel);
2084 assert(NodeHeights.count(V0) && NodeHeights.count(V1) &&
2085 "Children must have been visited before re-combining them!");
2086 int Height =
std::max(NodeHeights[V0], NodeHeights[V1]) + 1;
2089 if (V1C && NOpcode ==
ISD::MUL && V1C->getAPIntValue().isPowerOf2())
2090 NewNode = CurDAG->getNode(
2092 CurDAG->getConstant(
2093 V1C->getAPIntValue().logBase2(),
SDLoc(N),
2096 NewNode = CurDAG->getNode(NOpcode,
SDLoc(N), VT, V0, V1);
2098 NodeHeights[NewNode] = Height;
2100 int Weight = V0Weight + V1Weight;
2101 Leaves.push(WeightedLeaf(NewNode, Weight, L0.InsertionOrder));
2103 DEBUG(
dbgs() <<
"--> Built new node (Weight=" << Weight <<
",Height="
2104 << Height <<
"):\n");
2108 assert(Leaves.size() == 1);
2109 SDValue NewRoot = Leaves.top().Value;
2111 assert(NodeHeights.count(NewRoot));
2112 int Height = NodeHeights[NewRoot];
2115 if (NewRoot.getOpcode() ==
ISD::MUL) {
2118 EVT VT = NewRoot.getValueType();
2120 NewRoot = CurDAG->getNode(
2122 CurDAG->getConstant(
2128 if (N != NewRoot.getNode()) {
2130 DEBUG(NewRoot.dump());
2133 CurDAG->ReplaceAllUsesWith(N, NewRoot.getNode());
2135 RootWeights[
N] = -2;
2137 DEBUG(
dbgs() <<
"--> Root unchanged.\n");
2140 RootWeights[NewRoot.getNode()] = Leaves.top().Weight;
2141 RootHeights[NewRoot.getNode()] = Height;
2146 void HexagonDAGToDAGISel::rebalanceAddressTrees() {
2147 for (
auto I = CurDAG->allnodes_begin(),
E = CurDAG->allnodes_end(); I !=
E;) {
2152 SDValue BasePtr = cast<MemSDNode>(
N)->getBasePtr();
2157 if (RootWeights.count(BasePtr.
getNode()))
2160 DEBUG(
dbgs() <<
"** Rebalancing address calculation in node: ");
2169 while (!Worklist.empty()) {
2170 SDNode *N = Worklist.pop_back_val();
2184 if (RootWeights.count(N))
2187 RootWeights[
N] = -1;
2191 RootWeights[BasePtr.
getNode()] = -1;
2195 N = CurDAG->UpdateNodeOperands(N, N->
getOperand(0),
2205 CurDAG->RemoveDeadNodes();
2206 GAUsesInFunction.clear();
2207 RootHeights.clear();
2208 RootWeights.clear();
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
static bool isMemOPCandidate(SDNode *I, SDNode *U)
void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd)
Assign this MachineSDNodes's memory reference descriptor list.
bool hasOneUse() const
Return true if there is exactly one use of this node.
static bool willShiftRightEliminate(SDValue V, unsigned Amount)
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
unsigned getStackAlignBaseVReg() const
const GlobalValue * getGlobal() const
static bool doesIntrinsicReturnPredicate(unsigned ID)
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static unsigned getPowerOf2Factor(SDValue Val)
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Hexagon target-specific information for each MachineFunction.
unsigned getNumOperands() const
Return the number of values used by this operation.
constexpr bool isInt< 16 >(int64_t x)
const SDValue & getOperand(unsigned Num) const
void setNodeId(int Id)
Set unique node id.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
const SDValue & getBasePtr() const
std::size_t 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...
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
bool isVector() const
Return true if this is a vector value type.
A description of a memory reference used in the backend.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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"))
int64_t getOffset() const
Shift and rotation operations.
std::size_t countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
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.
static bool isTargetConstant(const SDValue &V)
APInt bitcastToAPInt() const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
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...
bool isInteger() const
Return true if this is an integer or a vector integer type.
LLVM_NODISCARD bool empty() const
bool getBoolValue() const
Convert APInt to a boolean value.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const HexagonRegisterInfo & getRegisterInfo() const
HexagonInstrInfo specifics.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
Simple integer binary arithmetic operators.
const SDValue & getBasePtr() const
bool needsAligna(const MachineFunction &MF) const
const APInt & getAPIntValue() const
EVT getMemoryVT() const
Return the type of the in-memory value.
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
This class is used to represent ISD::STORE nodes.
SDNode * getNode() const
get the SDNode which holds the desired result
Function Alias Analysis false
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getScalarSizeInBits() const
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
initializer< Ty > init(const Ty &Val)
std::size_t 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...
size_t use_size() const
Return the number of uses of this node.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
const SDValue & getOperand(unsigned i) const
bool isNonTemporal() const
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
static ManagedStatic< OptionRegistry > OR
unsigned getBitWidth() const
Return the number of bits in the APInt.
static const unsigned End
unsigned getOpcode() const
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...
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
const SDValue & getValue() const
const APFloat & getValueAPF() const
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc, or post-dec.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
This class contains a discriminated union of information about pointers in memory operands...
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
const SDValue & getOffset() const
static bool isOpcodeHandled(const SDNode *N)
void dump() const
Dump this node, for debugging.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
unsigned logBase2() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
An SDNode that represents everything that will be needed to construct a MachineInstr.
const SDValue & getChain() const
LLVM_NODISCARD T pop_back_val()
CHAIN = SC CHAIN, Imm128 - System call.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
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...
Represents one node in the SelectionDAG.
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
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.
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
Class for arbitrary precision integers.
Select(COND, TRUEVAL, FALSEVAL).
int64_t getSExtValue() const
static use_iterator use_end()
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"))
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
static cl::opt< bool > EnableAddressRebalancing("isel-rebalance-addr", cl::Hidden, cl::init(true), cl::desc("Rebalance address calculation trees to improve ""instruction selection"))
uint64_t getConstantOperandVal(unsigned i) const
Bitwise operators - logical and, logical or, logical xor.
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
virtual MVT getScalarShiftAmountTy(const DataLayout &, EVT) const
EVT is not used in-tree, but is used by out-of-tree target.
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
void ReplaceAllUsesWith(SDValue From, SDValue Op)
Modify anything using 'From' to use 'To' instead.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
const SDValue & getOffset() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
FunctionPass * createHexagonISelDag(HexagonTargetMachine &TM, CodeGenOpt::Level OptLevel)
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
EVT getValueType() const
Return the ValueType of the referenced return value.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
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())
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
const HexagonInstrInfo * getInstrInfo() const override
bool isTruncatingStore() const
Return true if the op does a truncation before store.
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)
StringRef - Represent a constant reference to a string, i.e.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
unsigned getAlignment() const
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
uint64_t getZExtValue() const
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
This class is used to represent ISD::LOAD nodes.