1 //===----- HexagonMCShuffler.cpp - MC bundle shuffling --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the shuffling of insns inside a bundle according to the
11 // packet formation rules of the Hexagon ISA.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "hexagon-shuffle"
18 #include "MCTargetDesc/HexagonMCInstrInfo.h"
19 #include "MCTargetDesc/HexagonMCShuffler.h"
20 #include "MCTargetDesc/HexagonMCTargetDesc.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
28 DisableShuffle("disable-hexagon-shuffle", cl::Hidden, cl::init(false),
29 cl::desc("Disable Hexagon instruction shuffling"));
31 void HexagonMCShuffler::init(MCInst &MCB) {
32 if (HexagonMCInstrInfo::isBundle(MCB)) {
33 MCInst const *Extender = nullptr;
34 // Copy the bundle for the shuffling.
35 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
36 assert(!HexagonMCInstrInfo::getDesc(MCII, *I.getInst()).isPseudo());
37 MCInst *MI = const_cast<MCInst *>(I.getInst());
39 if (!HexagonMCInstrInfo::isImmext(*MI)) {
40 append(MI, Extender, HexagonMCInstrInfo::getUnits(MCII, STI, *MI),
48 BundleFlags = MCB.getOperand(0).getImm();
51 void HexagonMCShuffler::init(MCInst &MCB, MCInst const *AddMI,
52 bool bInsertAtFront) {
53 if (HexagonMCInstrInfo::isBundle(MCB)) {
54 if (bInsertAtFront && AddMI)
55 append(AddMI, nullptr, HexagonMCInstrInfo::getUnits(MCII, STI, *AddMI),
57 MCInst const *Extender = nullptr;
58 // Copy the bundle for the shuffling.
59 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
60 assert(!HexagonMCInstrInfo::getDesc(MCII, *I.getInst()).isPseudo());
61 MCInst *MI = const_cast<MCInst *>(I.getInst());
62 if (!HexagonMCInstrInfo::isImmext(*MI)) {
63 append(MI, Extender, HexagonMCInstrInfo::getUnits(MCII, STI, *MI),
69 if (!bInsertAtFront && AddMI)
70 append(AddMI, nullptr, HexagonMCInstrInfo::getUnits(MCII, STI, *AddMI),
74 BundleFlags = MCB.getOperand(0).getImm();
77 void HexagonMCShuffler::copyTo(MCInst &MCB) {
79 MCB.addOperand(MCOperand::createImm(BundleFlags));
80 // Copy the results into the bundle.
81 for (HexagonShuffler::iterator I = begin(); I != end(); ++I) {
83 MCInst const *MI = I->getDesc();
84 MCInst const *Extender = I->getExtender();
86 MCB.addOperand(MCOperand::createInst(Extender));
87 MCB.addOperand(MCOperand::createInst(MI));
91 bool HexagonMCShuffler::reshuffleTo(MCInst &MCB) {
93 // Copy the results into the bundle.
101 bool llvm::HexagonMCShuffle(MCInstrInfo const &MCII, MCSubtargetInfo const &STI,
103 HexagonMCShuffler MCS(MCII, STI, MCB);
106 // Ignore if user chose so.
109 if (!HexagonMCInstrInfo::bundleSize(MCB)) {
110 // There once was a bundle:
111 // BUNDLE %D2<imp-def>, %R4<imp-def>, %R5<imp-def>, %D7<imp-def>, ...
112 // * %D2<def> = IMPLICIT_DEF; flags:
113 // * %D7<def> = IMPLICIT_DEF; flags:
114 // After the IMPLICIT_DEFs were removed by the asm printer, the bundle
116 DEBUG(dbgs() << "Skipping empty bundle");
118 } else if (!HexagonMCInstrInfo::isBundle(MCB)) {
119 DEBUG(dbgs() << "Skipping stand-alone insn");
123 // Reorder the bundle and copy the result.
124 if (!MCS.reshuffleTo(MCB)) {
125 // Unless there is any error, which should not happen at this point.
126 unsigned shuffleError = MCS.getError();
127 switch (shuffleError) {
129 llvm_unreachable("unknown error");
130 case HexagonShuffler::SHUFFLE_ERROR_INVALID:
131 llvm_unreachable("invalid packet");
132 case HexagonShuffler::SHUFFLE_ERROR_STORES:
133 llvm_unreachable("too many stores");
134 case HexagonShuffler::SHUFFLE_ERROR_LOADS:
135 llvm_unreachable("too many loads");
136 case HexagonShuffler::SHUFFLE_ERROR_BRANCHES:
137 llvm_unreachable("too many branches");
138 case HexagonShuffler::SHUFFLE_ERROR_NOSLOTS:
139 llvm_unreachable("no suitable slot");
140 case HexagonShuffler::SHUFFLE_ERROR_SLOTS:
141 llvm_unreachable("over-subscribed slots");
142 case HexagonShuffler::SHUFFLE_SUCCESS: // Single instruction case.
150 bool llvm::HexagonMCShuffle(MCInstrInfo const &MCII, MCSubtargetInfo const &STI,
151 MCInst &MCB, MCInst const *AddMI, int fixupCount) {
152 if (!HexagonMCInstrInfo::isBundle(MCB) || !AddMI)
155 // if fixups present, make sure we don't insert too many nops that would
156 // later prevent an extender from being inserted.
157 unsigned int bundleSize = HexagonMCInstrInfo::bundleSize(MCB);
158 if (bundleSize >= HEXAGON_PACKET_SIZE)
160 if (fixupCount >= 2) {
163 if (bundleSize == HEXAGON_PACKET_SIZE - 1 && fixupCount)
170 HexagonMCShuffler MCS(MCII, STI, MCB, AddMI);
171 if (!MCS.reshuffleTo(MCB)) {
172 unsigned shuffleError = MCS.getError();
173 switch (shuffleError) {
176 case HexagonShuffler::SHUFFLE_SUCCESS: // single instruction case