LLVM  5.0.0
HexagonMCShuffler.cpp
Go to the documentation of this file.
1 //===----- HexagonMCShuffler.cpp - MC bundle shuffling --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the shuffling of insns inside a bundle according to the
11 // packet formation rules of the Hexagon ISA.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "hexagon-shuffle"
16 
18 #include "Hexagon.h"
22 #include "llvm/Support/Debug.h"
24 
25 using namespace llvm;
26 
27 static cl::opt<bool>
28  DisableShuffle("disable-hexagon-shuffle", cl::Hidden, cl::init(false),
29  cl::desc("Disable Hexagon instruction shuffling"));
30 
31 void HexagonMCShuffler::init(MCInst &MCB) {
33  MCInst const *Extender = nullptr;
34  // Copy the bundle for the shuffling.
35  for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
36  MCInst &MI = *const_cast<MCInst *>(I.getInst());
37  DEBUG(dbgs() << "Shuffling: " << MCII.getName(MI.getOpcode()) << '\n');
38  assert(!HexagonMCInstrInfo::getDesc(MCII, MI).isPseudo());
39 
41  append(MI, Extender, HexagonMCInstrInfo::getUnits(MCII, STI, MI));
42  Extender = nullptr;
43  } else
44  Extender = &MI;
45  }
46  }
47 
48  Loc = MCB.getLoc();
49  BundleFlags = MCB.getOperand(0).getImm();
50 }
51 
52 void HexagonMCShuffler::init(MCInst &MCB, MCInst const &AddMI,
53  bool bInsertAtFront) {
55  if (bInsertAtFront)
56  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());
63  append(MI, Extender, HexagonMCInstrInfo::getUnits(MCII, STI, MI));
64  Extender = nullptr;
65  } else
66  Extender = &MI;
67  }
68  if (!bInsertAtFront)
69  append(AddMI, nullptr, HexagonMCInstrInfo::getUnits(MCII, STI, AddMI));
70  }
71 
72  Loc = MCB.getLoc();
73  BundleFlags = MCB.getOperand(0).getImm();
74 }
75 
77  MCB.clear();
79  MCB.setLoc(Loc);
80  // Copy the results into the bundle.
81  for (HexagonShuffler::iterator I = begin(); I != end(); ++I) {
82 
83  MCInst const &MI = I->getDesc();
84  MCInst const *Extender = I->getExtender();
85  if (Extender)
86  MCB.addOperand(MCOperand::createInst(Extender));
88  }
89 }
90 
92  if (shuffle()) {
93  // Copy the results into the bundle.
94  copyTo(MCB);
95  return true;
96  }
97  DEBUG(MCB.dump());
98  return false;
99 }
100 
102  MCInstrInfo const &MCII, MCSubtargetInfo const &STI,
103  MCInst &MCB) {
104  HexagonMCShuffler MCS(Context, Fatal, MCII, STI, MCB);
105 
106  if (DisableShuffle)
107  // Ignore if user chose so.
108  return false;
109 
110  if (!HexagonMCInstrInfo::bundleSize(MCB)) {
111  // There once was a bundle:
112  // BUNDLE %D2<imp-def>, %R4<imp-def>, %R5<imp-def>, %D7<imp-def>, ...
113  // * %D2<def> = IMPLICIT_DEF; flags:
114  // * %D7<def> = IMPLICIT_DEF; flags:
115  // After the IMPLICIT_DEFs were removed by the asm printer, the bundle
116  // became empty.
117  DEBUG(dbgs() << "Skipping empty bundle");
118  return false;
119  } else if (!HexagonMCInstrInfo::isBundle(MCB)) {
120  DEBUG(dbgs() << "Skipping stand-alone insn");
121  return false;
122  }
123 
124  return MCS.reshuffleTo(MCB);
125 }
126 
127 bool
129  MCSubtargetInfo const &STI, MCInst &MCB,
130  SmallVector<DuplexCandidate, 8> possibleDuplexes) {
131 
132  if (DisableShuffle)
133  return false;
134 
135  if (!HexagonMCInstrInfo::bundleSize(MCB)) {
136  // There once was a bundle:
137  // BUNDLE %D2<imp-def>, %R4<imp-def>, %R5<imp-def>, %D7<imp-def>, ...
138  // * %D2<def> = IMPLICIT_DEF; flags:
139  // * %D7<def> = IMPLICIT_DEF; flags:
140  // After the IMPLICIT_DEFs were removed by the asm printer, the bundle
141  // became empty.
142  DEBUG(dbgs() << "Skipping empty bundle");
143  return false;
144  } else if (!HexagonMCInstrInfo::isBundle(MCB)) {
145  DEBUG(dbgs() << "Skipping stand-alone insn");
146  return false;
147  }
148 
149  bool doneShuffling = false;
150  while (possibleDuplexes.size() > 0 && (!doneShuffling)) {
151  // case of Duplex Found
152  DuplexCandidate duplexToTry = possibleDuplexes.pop_back_val();
153  MCInst Attempt(MCB);
154  HexagonMCInstrInfo::replaceDuplex(Context, Attempt, duplexToTry);
155  HexagonMCShuffler MCS(Context, false, MCII, STI, Attempt); // copy packet to the shuffler
156  if (MCS.size() == 1) { // case of one duplex
157  // copy the created duplex in the shuffler to the bundle
158  MCS.copyTo(MCB);
159  return false;
160  }
161  // try shuffle with this duplex
162  doneShuffling = MCS.reshuffleTo(MCB);
163 
164  if (doneShuffling)
165  break;
166  }
167 
168  if (doneShuffling == false) {
169  HexagonMCShuffler MCS(Context, false, MCII, STI, MCB);
170  doneShuffling = MCS.reshuffleTo(MCB); // shuffle
171  }
172  if (!doneShuffling)
173  return true;
174 
175  return false;
176 }
177 
179  MCSubtargetInfo const &STI, MCInst &MCB,
180  MCInst const &AddMI, int fixupCount) {
182  return false;
183 
184  // if fixups present, make sure we don't insert too many nops that would
185  // later prevent an extender from being inserted.
186  unsigned int bundleSize = HexagonMCInstrInfo::bundleSize(MCB);
187  if (bundleSize >= HEXAGON_PACKET_SIZE)
188  return false;
189  bool bhasDuplex = HexagonMCInstrInfo::hasDuplex(MCII, MCB);
190  if (fixupCount >= 2) {
191  if (bhasDuplex) {
192  if (bundleSize >= HEXAGON_PACKET_SIZE - 1) {
193  return false;
194  }
195  } else {
196  return false;
197  }
198  } else {
199  if (bundleSize == HEXAGON_PACKET_SIZE - 1 && fixupCount)
200  return false;
201  }
202 
203  if (DisableShuffle)
204  return false;
205 
206  // mgl: temporary code (shuffler doesn't take into account the fact that
207  // a duplex takes up two slots. for example, 3 nops can be put into a packet
208  // containing a duplex oversubscribing slots by 1).
209  unsigned maxBundleSize = (HexagonMCInstrInfo::hasImmExt(MCB))
211  : HEXAGON_PACKET_SIZE - 1;
212  if (bhasDuplex && bundleSize >= maxBundleSize)
213  return false;
214 
215  HexagonMCShuffler MCS(Context, false, MCII, STI, MCB, AddMI, false);
216  return MCS.reshuffleTo(MCB);
217 }
void replaceDuplex(MCContext &Context, MCInst &MCI, DuplexCandidate Candidate)
LLVMContext & Context
void clear()
Definition: MCInst.h:182
void append(MCInst const &ID, MCInst const *Extender, unsigned S)
bool isBundle(MCInst const &MCI)
bool isImmext(MCInst const &MCI)
bool reshuffleTo(MCInst &MCB)
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:51
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
Context object for machine code objects.
Definition: MCContext.h:59
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
MCSubtargetInfo const & STI
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
unsigned size() const
bool hasDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
SMLoc getLoc() const
Definition: MCInst.h:171
void setLoc(SMLoc loc)
Definition: MCInst.h:170
HexagonPacket::iterator iterator
MCInstrInfo const & MCII
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool HexagonMCShuffle(MCContext &Context, bool Fatal, MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &)
unsigned getOpcode() const
Definition: MCInst.h:168
int64_t getImm() const
Definition: MCInst.h:76
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:144
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
MCSubtargetInfo - Generic base class for all target subtargets.
MCInstrDesc const & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
size_t bundleSize(MCInst const &MCI)
static cl::opt< bool > DisableShuffle("disable-hexagon-shuffle", cl::Hidden, cl::init(false), cl::desc("Disable Hexagon instruction shuffling"))
bool hasImmExt(MCInst const &MCI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define DEBUG(X)
Definition: Debug.h:118
void dump() const
Definition: MCInst.cpp:70
unsigned getUnits(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst const &MCI)
Return the slots used by the insn.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173