DFAPacketizer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
  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. // This class implements a deterministic finite automaton (DFA) based
  9. // packetizing mechanism for VLIW architectures. It provides APIs to
  10. // determine whether there exists a legal mapping of instructions to
  11. // functional unit assignments in a packet. The DFA is auto-generated from
  12. // the target's Schedule.td file.
  13. //
  14. // A DFA consists of 3 major elements: states, inputs, and transitions. For
  15. // the packetizing mechanism, the input is the set of instruction classes for
  16. // a target. The state models all possible combinations of functional unit
  17. // consumption for a given set of instructions in a packet. A transition
  18. // models the addition of an instruction to a packet. In the DFA constructed
  19. // by this class, if an instruction can be added to a packet, then a valid
  20. // transition exists from the corresponding state. Invalid transitions
  21. // indicate that the instruction cannot be added to the current packet.
  22. //
  23. //===----------------------------------------------------------------------===//
  24. #include "llvm/CodeGen/DFAPacketizer.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/Analysis/AliasAnalysis.h"
  27. #include "llvm/CodeGen/MachineFunction.h"
  28. #include "llvm/CodeGen/MachineInstr.h"
  29. #include "llvm/CodeGen/MachineInstrBundle.h"
  30. #include "llvm/CodeGen/ScheduleDAG.h"
  31. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  32. #include "llvm/CodeGen/TargetInstrInfo.h"
  33. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  34. #include "llvm/MC/MCInstrDesc.h"
  35. #include "llvm/MC/MCInstrItineraries.h"
  36. #include "llvm/Support/CommandLine.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <algorithm>
  40. #include <cassert>
  41. #include <iterator>
  42. #include <memory>
  43. #include <vector>
  44. using namespace llvm;
  45. #define DEBUG_TYPE "packets"
  46. static cl::opt<unsigned> InstrLimit("dfa-instr-limit", cl::Hidden,
  47. cl::init(0), cl::desc("If present, stops packetizing after N instructions"));
  48. static unsigned InstrCount = 0;
  49. // Check if the resources occupied by a MCInstrDesc are available in the
  50. // current state.
  51. bool DFAPacketizer::canReserveResources(const MCInstrDesc *MID) {
  52. unsigned Action = ItinActions[MID->getSchedClass()];
  53. if (MID->getSchedClass() == 0 || Action == 0)
  54. return false;
  55. return A.canAdd(Action);
  56. }
  57. // Reserve the resources occupied by a MCInstrDesc and change the current
  58. // state to reflect that change.
  59. void DFAPacketizer::reserveResources(const MCInstrDesc *MID) {
  60. unsigned Action = ItinActions[MID->getSchedClass()];
  61. if (MID->getSchedClass() == 0 || Action == 0)
  62. return;
  63. A.add(Action);
  64. }
  65. // Check if the resources occupied by a machine instruction are available
  66. // in the current state.
  67. bool DFAPacketizer::canReserveResources(MachineInstr &MI) {
  68. const MCInstrDesc &MID = MI.getDesc();
  69. return canReserveResources(&MID);
  70. }
  71. // Reserve the resources occupied by a machine instruction and change the
  72. // current state to reflect that change.
  73. void DFAPacketizer::reserveResources(MachineInstr &MI) {
  74. const MCInstrDesc &MID = MI.getDesc();
  75. reserveResources(&MID);
  76. }
  77. unsigned DFAPacketizer::getUsedResources(unsigned InstIdx) {
  78. ArrayRef<NfaPath> NfaPaths = A.getNfaPaths();
  79. assert(!NfaPaths.empty() && "Invalid bundle!");
  80. const NfaPath &RS = NfaPaths.front();
  81. // RS stores the cumulative resources used up to and including the I'th
  82. // instruction. The 0th instruction is the base case.
  83. if (InstIdx == 0)
  84. return RS[0];
  85. // Return the difference between the cumulative resources used by InstIdx and
  86. // its predecessor.
  87. return RS[InstIdx] ^ RS[InstIdx - 1];
  88. }
  89. namespace llvm {
  90. // This class extends ScheduleDAGInstrs and overrides the schedule method
  91. // to build the dependence graph.
  92. class DefaultVLIWScheduler : public ScheduleDAGInstrs {
  93. private:
  94. AAResults *AA;
  95. /// Ordered list of DAG postprocessing steps.
  96. std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
  97. public:
  98. DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
  99. AAResults *AA);
  100. // Actual scheduling work.
  101. void schedule() override;
  102. /// DefaultVLIWScheduler takes ownership of the Mutation object.
  103. void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
  104. Mutations.push_back(std::move(Mutation));
  105. }
  106. protected:
  107. void postprocessDAG();
  108. };
  109. } // end namespace llvm
  110. DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
  111. MachineLoopInfo &MLI,
  112. AAResults *AA)
  113. : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
  114. CanHandleTerminators = true;
  115. }
  116. /// Apply each ScheduleDAGMutation step in order.
  117. void DefaultVLIWScheduler::postprocessDAG() {
  118. for (auto &M : Mutations)
  119. M->apply(this);
  120. }
  121. void DefaultVLIWScheduler::schedule() {
  122. // Build the scheduling graph.
  123. buildSchedGraph(AA);
  124. postprocessDAG();
  125. }
  126. VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
  127. MachineLoopInfo &mli, AAResults *aa)
  128. : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
  129. ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
  130. ResourceTracker->setTrackResources(true);
  131. VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
  132. }
  133. VLIWPacketizerList::~VLIWPacketizerList() {
  134. delete VLIWScheduler;
  135. delete ResourceTracker;
  136. }
  137. // End the current packet, bundle packet instructions and reset DFA state.
  138. void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
  139. MachineBasicBlock::iterator MI) {
  140. LLVM_DEBUG({
  141. if (!CurrentPacketMIs.empty()) {
  142. dbgs() << "Finalizing packet:\n";
  143. unsigned Idx = 0;
  144. for (MachineInstr *MI : CurrentPacketMIs) {
  145. unsigned R = ResourceTracker->getUsedResources(Idx++);
  146. dbgs() << " * [res:0x" << utohexstr(R) << "] " << *MI;
  147. }
  148. }
  149. });
  150. if (CurrentPacketMIs.size() > 1) {
  151. MachineInstr &MIFirst = *CurrentPacketMIs.front();
  152. finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
  153. }
  154. CurrentPacketMIs.clear();
  155. ResourceTracker->clearResources();
  156. LLVM_DEBUG(dbgs() << "End packet\n");
  157. }
  158. // Bundle machine instructions into packets.
  159. void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
  160. MachineBasicBlock::iterator BeginItr,
  161. MachineBasicBlock::iterator EndItr) {
  162. assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
  163. VLIWScheduler->startBlock(MBB);
  164. VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
  165. std::distance(BeginItr, EndItr));
  166. VLIWScheduler->schedule();
  167. LLVM_DEBUG({
  168. dbgs() << "Scheduling DAG of the packetize region\n";
  169. VLIWScheduler->dump();
  170. });
  171. // Generate MI -> SU map.
  172. MIToSUnit.clear();
  173. for (SUnit &SU : VLIWScheduler->SUnits)
  174. MIToSUnit[SU.getInstr()] = &SU;
  175. bool LimitPresent = InstrLimit.getPosition();
  176. // The main packetizer loop.
  177. for (; BeginItr != EndItr; ++BeginItr) {
  178. if (LimitPresent) {
  179. if (InstrCount >= InstrLimit) {
  180. EndItr = BeginItr;
  181. break;
  182. }
  183. InstrCount++;
  184. }
  185. MachineInstr &MI = *BeginItr;
  186. initPacketizerState();
  187. // End the current packet if needed.
  188. if (isSoloInstruction(MI)) {
  189. endPacket(MBB, MI);
  190. continue;
  191. }
  192. // Ignore pseudo instructions.
  193. if (ignorePseudoInstruction(MI, MBB))
  194. continue;
  195. SUnit *SUI = MIToSUnit[&MI];
  196. assert(SUI && "Missing SUnit Info!");
  197. // Ask DFA if machine resource is available for MI.
  198. LLVM_DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
  199. bool ResourceAvail = ResourceTracker->canReserveResources(MI);
  200. LLVM_DEBUG({
  201. if (ResourceAvail)
  202. dbgs() << " Resources are available for adding MI to packet\n";
  203. else
  204. dbgs() << " Resources NOT available\n";
  205. });
  206. if (ResourceAvail && shouldAddToPacket(MI)) {
  207. // Dependency check for MI with instructions in CurrentPacketMIs.
  208. for (auto MJ : CurrentPacketMIs) {
  209. SUnit *SUJ = MIToSUnit[MJ];
  210. assert(SUJ && "Missing SUnit Info!");
  211. LLVM_DEBUG(dbgs() << " Checking against MJ " << *MJ);
  212. // Is it legal to packetize SUI and SUJ together.
  213. if (!isLegalToPacketizeTogether(SUI, SUJ)) {
  214. LLVM_DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
  215. // Allow packetization if dependency can be pruned.
  216. if (!isLegalToPruneDependencies(SUI, SUJ)) {
  217. // End the packet if dependency cannot be pruned.
  218. LLVM_DEBUG(dbgs()
  219. << " Could not prune dependencies for adding MI\n");
  220. endPacket(MBB, MI);
  221. break;
  222. }
  223. LLVM_DEBUG(dbgs() << " Pruned dependence for adding MI\n");
  224. }
  225. }
  226. } else {
  227. LLVM_DEBUG(if (ResourceAvail) dbgs()
  228. << "Resources are available, but instruction should not be "
  229. "added to packet\n "
  230. << MI);
  231. // End the packet if resource is not available, or if the instruction
  232. // shoud not be added to the current packet.
  233. endPacket(MBB, MI);
  234. }
  235. // Add MI to the current packet.
  236. LLVM_DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
  237. BeginItr = addToPacket(MI);
  238. } // For all instructions in the packetization range.
  239. // End any packet left behind.
  240. endPacket(MBB, EndItr);
  241. VLIWScheduler->exitRegion();
  242. VLIWScheduler->finishBlock();
  243. }
  244. bool VLIWPacketizerList::alias(const MachineMemOperand &Op1,
  245. const MachineMemOperand &Op2,
  246. bool UseTBAA) const {
  247. if (!Op1.getValue() || !Op2.getValue())
  248. return true;
  249. int64_t MinOffset = std::min(Op1.getOffset(), Op2.getOffset());
  250. int64_t Overlapa = Op1.getSize() + Op1.getOffset() - MinOffset;
  251. int64_t Overlapb = Op2.getSize() + Op2.getOffset() - MinOffset;
  252. AliasResult AAResult =
  253. AA->alias(MemoryLocation(Op1.getValue(), Overlapa,
  254. UseTBAA ? Op1.getAAInfo() : AAMDNodes()),
  255. MemoryLocation(Op2.getValue(), Overlapb,
  256. UseTBAA ? Op2.getAAInfo() : AAMDNodes()));
  257. return AAResult != AliasResult::NoAlias;
  258. }
  259. bool VLIWPacketizerList::alias(const MachineInstr &MI1,
  260. const MachineInstr &MI2,
  261. bool UseTBAA) const {
  262. if (MI1.memoperands_empty() || MI2.memoperands_empty())
  263. return true;
  264. for (const MachineMemOperand *Op1 : MI1.memoperands())
  265. for (const MachineMemOperand *Op2 : MI2.memoperands())
  266. if (alias(*Op1, *Op2, UseTBAA))
  267. return true;
  268. return false;
  269. }
  270. // Add a DAG mutation object to the ordered list.
  271. void VLIWPacketizerList::addMutation(
  272. std::unique_ptr<ScheduleDAGMutation> Mutation) {
  273. VLIWScheduler->addMutation(std::move(Mutation));
  274. }