LLVM  15.0.0git
HexagonMCShuffler.cpp
Go to the documentation of this file.
1 //===----- HexagonMCShuffler.cpp - MC bundle shuffling --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the shuffling of insns inside a bundle according to the
10 // packet formation rules of the Hexagon ISA.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstrDesc.h"
19 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/Support/Debug.h"
23 #include <cassert>
24 
25 #define DEBUG_TYPE "hexagon-shuffle"
26 
27 using namespace llvm;
28 
29 static cl::opt<bool>
30  DisableShuffle("disable-hexagon-shuffle", cl::Hidden, cl::init(false),
31  cl::desc("Disable Hexagon instruction shuffling"));
32 
33 void HexagonMCShuffler::init(MCInst &MCB) {
35  MCInst const *Extender = nullptr;
36  // Copy the bundle for the shuffling.
37  for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
38  MCInst &MI = *const_cast<MCInst *>(I.getInst());
39  LLVM_DEBUG(dbgs() << "Shuffling: " << MCII.getName(MI.getOpcode())
40  << '\n');
42 
45  Extender = nullptr;
46  } else
47  Extender = &MI;
48  }
49  }
50 
51  Loc = MCB.getLoc();
52  BundleFlags = MCB.getOperand(0).getImm();
53 }
54 
55 void HexagonMCShuffler::init(MCInst &MCB, MCInst const &AddMI,
56  bool bInsertAtFront) {
58  if (bInsertAtFront)
59  append(AddMI, nullptr, HexagonMCInstrInfo::getUnits(MCII, STI, AddMI));
60  MCInst const *Extender = nullptr;
61  // Copy the bundle for the shuffling.
62  for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
64  MCInst &MI = *const_cast<MCInst *>(I.getInst());
67  Extender = nullptr;
68  } else
69  Extender = &MI;
70  }
71  if (!bInsertAtFront)
72  append(AddMI, nullptr, HexagonMCInstrInfo::getUnits(MCII, STI, AddMI));
73  }
74 
75  Loc = MCB.getLoc();
76  BundleFlags = MCB.getOperand(0).getImm();
77 }
78 
80  MCB.clear();
82  MCB.setLoc(Loc);
83  // Copy the results into the bundle.
84  for (auto &I : *this) {
85  MCInst const &MI = I.getDesc();
86  MCInst const *Extender = I.getExtender();
87  if (Extender)
88  MCB.addOperand(MCOperand::createInst(Extender));
90  }
91 }
92 
94  if (shuffle()) {
95  // Copy the results into the bundle.
96  copyTo(MCB);
97  return true;
98  }
99  LLVM_DEBUG(MCB.dump());
100  return false;
101 }
102 
103 bool llvm::HexagonMCShuffle(MCContext &Context, bool ReportErrors,
104  MCInstrInfo const &MCII, MCSubtargetInfo const &STI,
105  MCInst &MCB) {
106  HexagonMCShuffler MCS(Context, ReportErrors, MCII, STI, MCB);
107 
108  if (DisableShuffle)
109  // Ignore if user chose so.
110  return false;
111 
112  if (!HexagonMCInstrInfo::bundleSize(MCB)) {
113  // There once was a bundle:
114  // BUNDLE implicit-def %d2, implicit-def %r4, implicit-def %r5,
115  // implicit-def %d7, ...
116  // * %d2 = IMPLICIT_DEF; flags:
117  // * %d7 = IMPLICIT_DEF; flags:
118  // After the IMPLICIT_DEFs were removed by the asm printer, the bundle
119  // became empty.
120  LLVM_DEBUG(dbgs() << "Skipping empty bundle");
121  return false;
122  } else if (!HexagonMCInstrInfo::isBundle(MCB)) {
123  LLVM_DEBUG(dbgs() << "Skipping stand-alone insn");
124  return false;
125  }
126 
127  return MCS.reshuffleTo(MCB);
128 }
129 
131  MCSubtargetInfo const &STI, MCInst &MCB,
132  SmallVector<DuplexCandidate, 8> possibleDuplexes) {
133 
134  if (DisableShuffle || possibleDuplexes.size() == 0)
135  return false;
136 
137  if (!HexagonMCInstrInfo::bundleSize(MCB)) {
138  // There once was a bundle:
139  // BUNDLE implicit-def %d2, implicit-def %r4, implicit-def %r5,
140  // implicit-def %d7, ...
141  // * %d2 = IMPLICIT_DEF; flags:
142  // * %d7 = IMPLICIT_DEF; flags:
143  // After the IMPLICIT_DEFs were removed by the asm printer, the bundle
144  // became empty.
145  LLVM_DEBUG(dbgs() << "Skipping empty bundle");
146  return false;
147  } else if (!HexagonMCInstrInfo::isBundle(MCB)) {
148  LLVM_DEBUG(dbgs() << "Skipping stand-alone insn");
149  return false;
150  }
151 
152  bool doneShuffling = false;
153  while (possibleDuplexes.size() > 0 && (!doneShuffling)) {
154  // case of Duplex Found
155  DuplexCandidate duplexToTry = possibleDuplexes.pop_back_val();
156  MCInst Attempt(MCB);
157  HexagonMCInstrInfo::replaceDuplex(Context, Attempt, duplexToTry);
158  HexagonMCShuffler MCS(Context, false, MCII, STI, Attempt); // copy packet to the shuffler
159  if (MCS.size() == 1) { // case of one duplex
160  // copy the created duplex in the shuffler to the bundle
161  MCS.copyTo(MCB);
162  return false;
163  }
164  // try shuffle with this duplex
165  doneShuffling = MCS.reshuffleTo(MCB);
166 
167  if (doneShuffling)
168  break;
169  }
170 
171  if (!doneShuffling) {
172  HexagonMCShuffler MCS(Context, false, MCII, STI, MCB);
173  doneShuffling = MCS.reshuffleTo(MCB); // shuffle
174  }
175 
176  return doneShuffling;
177 }
178 
180  MCSubtargetInfo const &STI, MCInst &MCB,
181  MCInst const &AddMI, int fixupCount) {
183  return false;
184 
185  // if fixups present, make sure we don't insert too many nops that would
186  // later prevent an extender from being inserted.
187  unsigned int bundleSize = HexagonMCInstrInfo::bundleSize(MCB);
189  return false;
190  bool bhasDuplex = HexagonMCInstrInfo::hasDuplex(MCII, MCB);
191  if (fixupCount >= 2) {
192  if (bhasDuplex) {
193  if (bundleSize >= HEXAGON_PACKET_SIZE - 1) {
194  return false;
195  }
196  } else {
197  return false;
198  }
199  } else {
200  if (bundleSize == HEXAGON_PACKET_SIZE - 1 && fixupCount)
201  return false;
202  }
203 
204  if (DisableShuffle)
205  return false;
206 
207  // mgl: temporary code (shuffler doesn't take into account the fact that
208  // a duplex takes up two slots. for example, 3 nops can be put into a packet
209  // containing a duplex oversubscribing slots by 1).
210  unsigned maxBundleSize = (HexagonMCInstrInfo::hasImmExt(MCB))
212  : HEXAGON_PACKET_SIZE - 1;
213  if (bhasDuplex && bundleSize >= maxBundleSize)
214  return false;
215 
216  HexagonMCShuffler MCS(Context, false, MCII, STI, MCB, AddMI, false);
217  return MCS.reshuffleTo(MCB);
218 }
llvm::HexagonShuffler::MCII
const MCInstrInfo & MCII
Definition: HexagonShuffler.h:164
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::HexagonMCInstrInfo::getDesc
const MCInstrDesc & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:255
llvm::DuplexCandidate
Definition: HexagonMCInstrInfo.h:34
llvm::HexagonShuffler::shuffle
bool shuffle()
Definition: HexagonShuffler.cpp:652
MCInstrDesc.h
llvm::HexagonShuffler::Loc
SMLoc Loc
Definition: HexagonShuffler.h:166
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::MCInstrDesc::isPseudo
bool isPseudo() const
Return true if this is a pseudo instruction that doesn't correspond to a real machine instruction.
Definition: MCInstrDesc.h:266
llvm::HexagonMCInstrInfo::hasDuplex
bool hasDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:484
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
HexagonShuffler.h
llvm::HexagonMCShuffle
bool HexagonMCShuffle(MCContext &Context, bool ReportErrors, MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &MCB)
Definition: HexagonMCShuffler.cpp:103
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
llvm::MCInst::dump
void dump() const
Definition: MCInst.cpp:104
llvm::HexagonMCInstrInfo::isImmext
bool isImmext(MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:642
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:654
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::HexagonMCInstrInfo::replaceDuplex
void replaceDuplex(MCContext &Context, MCInst &MCI, DuplexCandidate Candidate)
Definition: HexagonMCInstrInfo.cpp:993
llvm::HexagonMCShuffler
Definition: HexagonMCShuffler.h:29
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
HexagonMCShuffler.h
llvm::HexagonMCInstrInfo::hasImmExt
bool hasImmExt(MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:500
HexagonMCInstrInfo.h
llvm::HexagonShuffler::size
unsigned size() const
Definition: HexagonShuffler.h:204
llvm::HexagonMCInstrInfo::isBundle
bool isBundle(MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:539
MCInstrInfo.h
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCInst.h
llvm::HexagonMCInstrInfo::bundleInstructions
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:103
llvm::HexagonMCInstrInfo::bundleSize
size_t bundleSize(MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:116
llvm::MCOperand::createInst
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:169
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::cl::opt< bool >
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::HexagonShuffler::BundleFlags
int64_t BundleFlags
Definition: HexagonShuffler.h:163
llvm::MCInstrInfo::getName
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:69
llvm::HexagonShuffler::STI
const MCSubtargetInfo & STI
Definition: HexagonShuffler.h:165
llvm::HexagonShuffler::append
void append(MCInst const &ID, MCInst const *Extender, unsigned S)
Definition: HexagonShuffler.cpp:181
llvm::X86II::isPseudo
bool isPseudo(uint64_t TSFlags)
Definition: X86BaseInfo.h:979
llvm::HexagonMCShuffler::reshuffleTo
bool reshuffleTo(MCInst &MCB)
Definition: HexagonMCShuffler.cpp:93
llvm::MCInst::getLoc
SMLoc getLoc() const
Definition: MCInst.h:204
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
llvm::MCInst::setLoc
void setLoc(SMLoc loc)
Definition: MCInst.h:203
llvm::HexagonMCInstrInfo::getUnits
unsigned getUnits(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst const &MCI)
Return the slots used by the insn.
Definition: HexagonMCInstrInfo.cpp:450
DisableShuffle
static cl::opt< bool > DisableShuffle("disable-hexagon-shuffle", cl::Hidden, cl::init(false), cl::desc("Disable Hexagon instruction shuffling"))
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
llvm::HexagonMCShuffler::copyTo
void copyTo(MCInst &MCB)
Definition: HexagonMCShuffler.cpp:79
llvm::cl::desc
Definition: CommandLine.h:405
raw_ostream.h
HEXAGON_PACKET_SIZE
#define HEXAGON_PACKET_SIZE
Definition: HexagonMCTargetDesc.h:36
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
Debug.h
llvm::MCInst::clear
void clear()
Definition: MCInst.h:215