blob: afcf014bca4095770bec5ee86c07262fbafa24e7 [file] [log] [blame]
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001//=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00006//
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
25#include "llvm/CodeGen/DFAPacketizer.h"
James Molloyb6c7fce62019-09-09 13:17:55 +000026#include "llvm/ADT/StringExtras.h"
Reid Kleckner0ad6c192019-10-19 01:07:48 +000027#include "llvm/Analysis/AliasAnalysis.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000028#include "llvm/CodeGen/MachineFunction.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000029#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick7a35fae2012-02-15 18:55:14 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000031#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/ScheduleDAGInstrs.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000034#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000035#include "llvm/MC/MCInstrDesc.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000036#include "llvm/MC/MCInstrItineraries.h"
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000037#include "llvm/Support/CommandLine.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000038#include "llvm/Support/Debug.h"
39#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000040#include <algorithm>
41#include <cassert>
42#include <iterator>
43#include <memory>
44#include <vector>
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000045
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000046using namespace llvm;
47
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000048#define DEBUG_TYPE "packets"
49
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000050static cl::opt<unsigned> InstrLimit("dfa-instr-limit", cl::Hidden,
51 cl::init(0), cl::desc("If present, stops packetizing after N instructions"));
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000052
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000053static unsigned InstrCount = 0;
54
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000055// Check if the resources occupied by a MCInstrDesc are available in the
56// current state.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000057bool DFAPacketizer::canReserveResources(const MCInstrDesc *MID) {
jmolloy39525a62019-11-04 19:25:13 +000058 unsigned Action = ItinActions[MID->getSchedClass()];
59 if (MID->getSchedClass() == 0 || Action == 0)
60 return false;
61 return A.canAdd(Action);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000062}
63
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000064// Reserve the resources occupied by a MCInstrDesc and change the current
65// state to reflect that change.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000066void DFAPacketizer::reserveResources(const MCInstrDesc *MID) {
jmolloy39525a62019-11-04 19:25:13 +000067 unsigned Action = ItinActions[MID->getSchedClass()];
68 if (MID->getSchedClass() == 0 || Action == 0)
69 return;
70 A.add(Action);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000071}
72
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000073// Check if the resources occupied by a machine instruction are available
74// in the current state.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000075bool DFAPacketizer::canReserveResources(MachineInstr &MI) {
76 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000077 return canReserveResources(&MID);
78}
79
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000080// Reserve the resources occupied by a machine instruction and change the
81// current state to reflect that change.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000082void DFAPacketizer::reserveResources(MachineInstr &MI) {
83 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000084 reserveResources(&MID);
85}
Andrew Trick7a35fae2012-02-15 18:55:14 +000086
James Molloyb6c7fce62019-09-09 13:17:55 +000087unsigned DFAPacketizer::getUsedResources(unsigned InstIdx) {
James Molloy12092a92019-10-17 08:34:29 +000088 ArrayRef<NfaPath> NfaPaths = A.getNfaPaths();
89 assert(!NfaPaths.empty() && "Invalid bundle!");
90 const NfaPath &RS = NfaPaths.front();
James Molloyb6c7fce62019-09-09 13:17:55 +000091
92 // RS stores the cumulative resources used up to and including the I'th
93 // instruction. The 0th instruction is the base case.
94 if (InstIdx == 0)
95 return RS[0];
96 // Return the difference between the cumulative resources used by InstIdx and
97 // its predecessor.
98 return RS[InstIdx] ^ RS[InstIdx - 1];
99}
100
Sirish Pande94212162012-05-01 21:28:30 +0000101namespace llvm {
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000102
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000103// This class extends ScheduleDAGInstrs and overrides the schedule method
104// to build the dependence graph.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000105class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000106private:
Reid Kleckner0ad6c192019-10-19 01:07:48 +0000107 AAResults *AA;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000108 /// Ordered list of DAG postprocessing steps.
109 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000110
Andrew Trick7a35fae2012-02-15 18:55:14 +0000111public:
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000112 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
Reid Kleckner0ad6c192019-10-19 01:07:48 +0000113 AAResults *AA);
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000114
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000115 // Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000116 void schedule() override;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000117
118 /// DefaultVLIWScheduler takes ownership of the Mutation object.
119 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
120 Mutations.push_back(std::move(Mutation));
121 }
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000122
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000123protected:
124 void postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000125};
Andrew Trick20349b82012-02-15 23:34:15 +0000126
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000127} // end namespace llvm
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000128
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000129DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000130 MachineLoopInfo &MLI,
Reid Kleckner0ad6c192019-10-19 01:07:48 +0000131 AAResults *AA)
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000132 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande94212162012-05-01 21:28:30 +0000133 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000134}
135
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000136/// Apply each ScheduleDAGMutation step in order.
137void DefaultVLIWScheduler::postprocessDAG() {
138 for (auto &M : Mutations)
139 M->apply(this);
140}
141
Andrew Trick52226d42012-03-07 23:00:49 +0000142void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000143 // Build the scheduling graph.
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000144 buildSchedGraph(AA);
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000145 postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000146}
147
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000148VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
Reid Kleckner0ad6c192019-10-19 01:07:48 +0000149 MachineLoopInfo &mli, AAResults *aa)
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000150 : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
Eric Christopher143f02c2014-10-09 01:59:35 +0000151 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
James Molloyb6c7fce62019-09-09 13:17:55 +0000152 ResourceTracker->setTrackResources(true);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000153 VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000154}
155
Andrew Trick7a35fae2012-02-15 18:55:14 +0000156VLIWPacketizerList::~VLIWPacketizerList() {
Gabor Horvath43b72d52017-05-01 16:18:42 +0000157 delete VLIWScheduler;
158 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000159}
160
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000161// End the current packet, bundle packet instructions and reset DFA state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000162void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
163 MachineBasicBlock::iterator MI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000164 LLVM_DEBUG({
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000165 if (!CurrentPacketMIs.empty()) {
166 dbgs() << "Finalizing packet:\n";
James Molloyb6c7fce62019-09-09 13:17:55 +0000167 unsigned Idx = 0;
168 for (MachineInstr *MI : CurrentPacketMIs) {
169 unsigned R = ResourceTracker->getUsedResources(Idx++);
170 dbgs() << " * [res:0x" << utohexstr(R) << "] " << *MI;
171 }
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000172 }
173 });
Andrew Trick7a35fae2012-02-15 18:55:14 +0000174 if (CurrentPacketMIs.size() > 1) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000175 MachineInstr &MIFirst = *CurrentPacketMIs.front();
176 finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000177 }
178 CurrentPacketMIs.clear();
179 ResourceTracker->clearResources();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000180 LLVM_DEBUG(dbgs() << "End packet\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000181}
182
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000183// Bundle machine instructions into packets.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000184void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
185 MachineBasicBlock::iterator BeginItr,
186 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000187 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
188 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000189 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
190 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000191 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000192
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000193 LLVM_DEBUG({
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000194 dbgs() << "Scheduling DAG of the packetize region\n";
Matthias Braun726e12c2018-09-19 00:23:35 +0000195 VLIWScheduler->dump();
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000196 });
197
Sirish Pande94212162012-05-01 21:28:30 +0000198 // Generate MI -> SU map.
199 MIToSUnit.clear();
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000200 for (SUnit &SU : VLIWScheduler->SUnits)
201 MIToSUnit[SU.getInstr()] = &SU;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000202
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000203 bool LimitPresent = InstrLimit.getPosition();
204
Andrew Trick7a35fae2012-02-15 18:55:14 +0000205 // The main packetizer loop.
206 for (; BeginItr != EndItr; ++BeginItr) {
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000207 if (LimitPresent) {
208 if (InstrCount >= InstrLimit) {
209 EndItr = BeginItr;
210 break;
211 }
212 InstrCount++;
213 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000214 MachineInstr &MI = *BeginItr;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000215 initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000216
217 // End the current packet if needed.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000218 if (isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000219 endPacket(MBB, MI);
220 continue;
221 }
222
Sirish Pande94212162012-05-01 21:28:30 +0000223 // Ignore pseudo instructions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000224 if (ignorePseudoInstruction(MI, MBB))
Sirish Pande94212162012-05-01 21:28:30 +0000225 continue;
226
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000227 SUnit *SUI = MIToSUnit[&MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000228 assert(SUI && "Missing SUnit Info!");
229
230 // Ask DFA if machine resource is available for MI.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000231 LLVM_DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000232
Andrew Trick7a35fae2012-02-15 18:55:14 +0000233 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000234 LLVM_DEBUG({
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000235 if (ResourceAvail)
236 dbgs() << " Resources are available for adding MI to packet\n";
237 else
238 dbgs() << " Resources NOT available\n";
239 });
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000240 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000241 // Dependency check for MI with instructions in CurrentPacketMIs.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000242 for (auto MJ : CurrentPacketMIs) {
Sirish Pande94212162012-05-01 21:28:30 +0000243 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000244 assert(SUJ && "Missing SUnit Info!");
245
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000246 LLVM_DEBUG(dbgs() << " Checking against MJ " << *MJ);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000247 // Is it legal to packetize SUI and SUJ together.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000248 if (!isLegalToPacketizeTogether(SUI, SUJ)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000249 LLVM_DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000250 // Allow packetization if dependency can be pruned.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000251 if (!isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000252 // End the packet if dependency cannot be pruned.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000253 LLVM_DEBUG(dbgs()
254 << " Could not prune dependencies for adding MI\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000255 endPacket(MBB, MI);
256 break;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000257 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000258 LLVM_DEBUG(dbgs() << " Pruned dependence for adding MI\n");
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000259 }
260 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000261 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000262 LLVM_DEBUG(if (ResourceAvail) dbgs()
263 << "Resources are available, but instruction should not be "
264 "added to packet\n "
265 << MI);
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000266 // End the packet if resource is not available, or if the instruction
267 // shoud not be added to the current packet.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000268 endPacket(MBB, MI);
269 }
270
271 // Add MI to the current packet.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000272 LLVM_DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000273 BeginItr = addToPacket(MI);
274 } // For all instructions in the packetization range.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000275
276 // End any packet left behind.
277 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000278 VLIWScheduler->exitRegion();
279 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000280}
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000281
Krzysztof Parzyszek9d19c8c2017-10-20 22:08:40 +0000282bool VLIWPacketizerList::alias(const MachineMemOperand &Op1,
283 const MachineMemOperand &Op2,
284 bool UseTBAA) const {
285 if (!Op1.getValue() || !Op2.getValue())
286 return true;
287
288 int64_t MinOffset = std::min(Op1.getOffset(), Op2.getOffset());
289 int64_t Overlapa = Op1.getSize() + Op1.getOffset() - MinOffset;
290 int64_t Overlapb = Op2.getSize() + Op2.getOffset() - MinOffset;
291
292 AliasResult AAResult =
293 AA->alias(MemoryLocation(Op1.getValue(), Overlapa,
294 UseTBAA ? Op1.getAAInfo() : AAMDNodes()),
295 MemoryLocation(Op2.getValue(), Overlapb,
296 UseTBAA ? Op2.getAAInfo() : AAMDNodes()));
297
298 return AAResult != NoAlias;
299}
300
301bool VLIWPacketizerList::alias(const MachineInstr &MI1,
302 const MachineInstr &MI2,
303 bool UseTBAA) const {
304 if (MI1.memoperands_empty() || MI2.memoperands_empty())
305 return true;
306
307 for (const MachineMemOperand *Op1 : MI1.memoperands())
308 for (const MachineMemOperand *Op2 : MI2.memoperands())
309 if (alias(*Op1, *Op2, UseTBAA))
310 return true;
311 return false;
312}
313
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000314// Add a DAG mutation object to the ordered list.
315void VLIWPacketizerList::addMutation(
316 std::unique_ptr<ScheduleDAGMutation> Mutation) {
317 VLIWScheduler->addMutation(std::move(Mutation));
318}