163faed5bSDimitry Andric //=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
263faed5bSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663faed5bSDimitry Andric //
763faed5bSDimitry Andric //===----------------------------------------------------------------------===//
863faed5bSDimitry Andric // This class implements a deterministic finite automaton (DFA) based
963faed5bSDimitry Andric // packetizing mechanism for VLIW architectures. It provides APIs to
1063faed5bSDimitry Andric // determine whether there exists a legal mapping of instructions to
1163faed5bSDimitry Andric // functional unit assignments in a packet. The DFA is auto-generated from
1263faed5bSDimitry Andric // the target's Schedule.td file.
1363faed5bSDimitry Andric //
1463faed5bSDimitry Andric // A DFA consists of 3 major elements: states, inputs, and transitions. For
1563faed5bSDimitry Andric // the packetizing mechanism, the input is the set of instruction classes for
1663faed5bSDimitry Andric // a target. The state models all possible combinations of functional unit
1763faed5bSDimitry Andric // consumption for a given set of instructions in a packet. A transition
1863faed5bSDimitry Andric // models the addition of an instruction to a packet. In the DFA constructed
1963faed5bSDimitry Andric // by this class, if an instruction can be added to a packet, then a valid
2063faed5bSDimitry Andric // transition exists from the corresponding state. Invalid transitions
2163faed5bSDimitry Andric // indicate that the instruction cannot be added to the current packet.
2263faed5bSDimitry Andric //
2363faed5bSDimitry Andric //===----------------------------------------------------------------------===//
2463faed5bSDimitry Andric
2563faed5bSDimitry Andric #include "llvm/CodeGen/DFAPacketizer.h"
261d5ae102SDimitry Andric #include "llvm/ADT/StringExtras.h"
271d5ae102SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
287ab83427SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
2963faed5bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
3063faed5bSDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h"
317ab83427SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h"
32044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
33044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
347ab83427SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
35b915e9e0SDimitry Andric #include "llvm/Support/CommandLine.h"
367ab83427SDimitry Andric #include "llvm/Support/Debug.h"
377ab83427SDimitry Andric #include "llvm/Support/raw_ostream.h"
387ab83427SDimitry Andric #include <algorithm>
397ab83427SDimitry Andric #include <cassert>
407ab83427SDimitry Andric #include <iterator>
417ab83427SDimitry Andric #include <memory>
427ab83427SDimitry Andric #include <vector>
4301095a5dSDimitry Andric
4463faed5bSDimitry Andric using namespace llvm;
4563faed5bSDimitry Andric
467ab83427SDimitry Andric #define DEBUG_TYPE "packets"
477ab83427SDimitry Andric
48b915e9e0SDimitry Andric static cl::opt<unsigned> InstrLimit("dfa-instr-limit", cl::Hidden,
49b915e9e0SDimitry Andric cl::init(0), cl::desc("If present, stops packetizing after N instructions"));
507ab83427SDimitry Andric
51b915e9e0SDimitry Andric static unsigned InstrCount = 0;
52b915e9e0SDimitry Andric
5301095a5dSDimitry Andric // Check if the resources occupied by a MCInstrDesc are available in the
5401095a5dSDimitry Andric // current state.
canReserveResources(const MCInstrDesc * MID)557ab83427SDimitry Andric bool DFAPacketizer::canReserveResources(const MCInstrDesc *MID) {
56706b4fc4SDimitry Andric unsigned Action = ItinActions[MID->getSchedClass()];
57706b4fc4SDimitry Andric if (MID->getSchedClass() == 0 || Action == 0)
58706b4fc4SDimitry Andric return false;
59706b4fc4SDimitry Andric return A.canAdd(Action);
6063faed5bSDimitry Andric }
6163faed5bSDimitry Andric
6201095a5dSDimitry Andric // Reserve the resources occupied by a MCInstrDesc and change the current
6301095a5dSDimitry Andric // state to reflect that change.
reserveResources(const MCInstrDesc * MID)647ab83427SDimitry Andric void DFAPacketizer::reserveResources(const MCInstrDesc *MID) {
65706b4fc4SDimitry Andric unsigned Action = ItinActions[MID->getSchedClass()];
66706b4fc4SDimitry Andric if (MID->getSchedClass() == 0 || Action == 0)
67706b4fc4SDimitry Andric return;
68706b4fc4SDimitry Andric A.add(Action);
6963faed5bSDimitry Andric }
7063faed5bSDimitry Andric
7101095a5dSDimitry Andric // Check if the resources occupied by a machine instruction are available
7201095a5dSDimitry Andric // in the current state.
canReserveResources(MachineInstr & MI)737ab83427SDimitry Andric bool DFAPacketizer::canReserveResources(MachineInstr &MI) {
747ab83427SDimitry Andric const MCInstrDesc &MID = MI.getDesc();
7563faed5bSDimitry Andric return canReserveResources(&MID);
7663faed5bSDimitry Andric }
7763faed5bSDimitry Andric
7801095a5dSDimitry Andric // Reserve the resources occupied by a machine instruction and change the
7901095a5dSDimitry Andric // current state to reflect that change.
reserveResources(MachineInstr & MI)807ab83427SDimitry Andric void DFAPacketizer::reserveResources(MachineInstr &MI) {
817ab83427SDimitry Andric const MCInstrDesc &MID = MI.getDesc();
8263faed5bSDimitry Andric reserveResources(&MID);
8363faed5bSDimitry Andric }
8463faed5bSDimitry Andric
getUsedResources(unsigned InstIdx)851d5ae102SDimitry Andric unsigned DFAPacketizer::getUsedResources(unsigned InstIdx) {
861d5ae102SDimitry Andric ArrayRef<NfaPath> NfaPaths = A.getNfaPaths();
871d5ae102SDimitry Andric assert(!NfaPaths.empty() && "Invalid bundle!");
881d5ae102SDimitry Andric const NfaPath &RS = NfaPaths.front();
891d5ae102SDimitry Andric
901d5ae102SDimitry Andric // RS stores the cumulative resources used up to and including the I'th
911d5ae102SDimitry Andric // instruction. The 0th instruction is the base case.
921d5ae102SDimitry Andric if (InstIdx == 0)
931d5ae102SDimitry Andric return RS[0];
941d5ae102SDimitry Andric // Return the difference between the cumulative resources used by InstIdx and
951d5ae102SDimitry Andric // its predecessor.
961d5ae102SDimitry Andric return RS[InstIdx] ^ RS[InstIdx - 1];
971d5ae102SDimitry Andric }
981d5ae102SDimitry Andric
DefaultVLIWScheduler(MachineFunction & MF,MachineLoopInfo & MLI,AAResults * AA)9967c32a98SDimitry Andric DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
100dd58ef01SDimitry Andric MachineLoopInfo &MLI,
1011d5ae102SDimitry Andric AAResults *AA)
102dd58ef01SDimitry Andric : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
10358b69754SDimitry Andric CanHandleTerminators = true;
10463faed5bSDimitry Andric }
10563faed5bSDimitry Andric
10601095a5dSDimitry Andric /// Apply each ScheduleDAGMutation step in order.
postProcessDAG()1077fa27ce4SDimitry Andric void DefaultVLIWScheduler::postProcessDAG() {
10801095a5dSDimitry Andric for (auto &M : Mutations)
10901095a5dSDimitry Andric M->apply(this);
11001095a5dSDimitry Andric }
11101095a5dSDimitry Andric
schedule()11263faed5bSDimitry Andric void DefaultVLIWScheduler::schedule() {
11363faed5bSDimitry Andric // Build the scheduling graph.
114dd58ef01SDimitry Andric buildSchedGraph(AA);
1157fa27ce4SDimitry Andric postProcessDAG();
11663faed5bSDimitry Andric }
11763faed5bSDimitry Andric
VLIWPacketizerList(MachineFunction & mf,MachineLoopInfo & mli,AAResults * aa)11801095a5dSDimitry Andric VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
1191d5ae102SDimitry Andric MachineLoopInfo &mli, AAResults *aa)
12001095a5dSDimitry Andric : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
12167c32a98SDimitry Andric ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
1221d5ae102SDimitry Andric ResourceTracker->setTrackResources(true);
12301095a5dSDimitry Andric VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
12463faed5bSDimitry Andric }
12563faed5bSDimitry Andric
~VLIWPacketizerList()12663faed5bSDimitry Andric VLIWPacketizerList::~VLIWPacketizerList() {
12758b69754SDimitry Andric delete VLIWScheduler;
12863faed5bSDimitry Andric delete ResourceTracker;
12963faed5bSDimitry Andric }
13063faed5bSDimitry Andric
13101095a5dSDimitry Andric // End the current packet, bundle packet instructions and reset DFA state.
endPacket(MachineBasicBlock * MBB,MachineBasicBlock::iterator MI)13263faed5bSDimitry Andric void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
13301095a5dSDimitry Andric MachineBasicBlock::iterator MI) {
134eb11fae6SDimitry Andric LLVM_DEBUG({
135b915e9e0SDimitry Andric if (!CurrentPacketMIs.empty()) {
136b915e9e0SDimitry Andric dbgs() << "Finalizing packet:\n";
1371d5ae102SDimitry Andric unsigned Idx = 0;
1381d5ae102SDimitry Andric for (MachineInstr *MI : CurrentPacketMIs) {
1391d5ae102SDimitry Andric unsigned R = ResourceTracker->getUsedResources(Idx++);
1401d5ae102SDimitry Andric dbgs() << " * [res:0x" << utohexstr(R) << "] " << *MI;
1411d5ae102SDimitry Andric }
142b915e9e0SDimitry Andric }
143b915e9e0SDimitry Andric });
14463faed5bSDimitry Andric if (CurrentPacketMIs.size() > 1) {
14501095a5dSDimitry Andric MachineInstr &MIFirst = *CurrentPacketMIs.front();
14601095a5dSDimitry Andric finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
14763faed5bSDimitry Andric }
14863faed5bSDimitry Andric CurrentPacketMIs.clear();
14963faed5bSDimitry Andric ResourceTracker->clearResources();
150eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "End packet\n");
15163faed5bSDimitry Andric }
15263faed5bSDimitry Andric
15301095a5dSDimitry Andric // Bundle machine instructions into packets.
PacketizeMIs(MachineBasicBlock * MBB,MachineBasicBlock::iterator BeginItr,MachineBasicBlock::iterator EndItr)15463faed5bSDimitry Andric void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
15563faed5bSDimitry Andric MachineBasicBlock::iterator BeginItr,
15663faed5bSDimitry Andric MachineBasicBlock::iterator EndItr) {
15758b69754SDimitry Andric assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
15858b69754SDimitry Andric VLIWScheduler->startBlock(MBB);
159f8af5cf6SDimitry Andric VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
160f8af5cf6SDimitry Andric std::distance(BeginItr, EndItr));
16158b69754SDimitry Andric VLIWScheduler->schedule();
16263faed5bSDimitry Andric
163eb11fae6SDimitry Andric LLVM_DEBUG({
16401095a5dSDimitry Andric dbgs() << "Scheduling DAG of the packetize region\n";
165d8e91e46SDimitry Andric VLIWScheduler->dump();
16601095a5dSDimitry Andric });
16701095a5dSDimitry Andric
16858b69754SDimitry Andric // Generate MI -> SU map.
16958b69754SDimitry Andric MIToSUnit.clear();
17001095a5dSDimitry Andric for (SUnit &SU : VLIWScheduler->SUnits)
17101095a5dSDimitry Andric MIToSUnit[SU.getInstr()] = &SU;
17263faed5bSDimitry Andric
173b915e9e0SDimitry Andric bool LimitPresent = InstrLimit.getPosition();
174b915e9e0SDimitry Andric
17563faed5bSDimitry Andric // The main packetizer loop.
17663faed5bSDimitry Andric for (; BeginItr != EndItr; ++BeginItr) {
177b915e9e0SDimitry Andric if (LimitPresent) {
178b915e9e0SDimitry Andric if (InstrCount >= InstrLimit) {
179b915e9e0SDimitry Andric EndItr = BeginItr;
180b915e9e0SDimitry Andric break;
181b915e9e0SDimitry Andric }
182b915e9e0SDimitry Andric InstrCount++;
183b915e9e0SDimitry Andric }
18401095a5dSDimitry Andric MachineInstr &MI = *BeginItr;
18501095a5dSDimitry Andric initPacketizerState();
18663faed5bSDimitry Andric
18763faed5bSDimitry Andric // End the current packet if needed.
18801095a5dSDimitry Andric if (isSoloInstruction(MI)) {
18963faed5bSDimitry Andric endPacket(MBB, MI);
19063faed5bSDimitry Andric continue;
19163faed5bSDimitry Andric }
19263faed5bSDimitry Andric
19358b69754SDimitry Andric // Ignore pseudo instructions.
19401095a5dSDimitry Andric if (ignorePseudoInstruction(MI, MBB))
19558b69754SDimitry Andric continue;
19658b69754SDimitry Andric
19701095a5dSDimitry Andric SUnit *SUI = MIToSUnit[&MI];
19863faed5bSDimitry Andric assert(SUI && "Missing SUnit Info!");
19963faed5bSDimitry Andric
20063faed5bSDimitry Andric // Ask DFA if machine resource is available for MI.
201eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
20201095a5dSDimitry Andric
20363faed5bSDimitry Andric bool ResourceAvail = ResourceTracker->canReserveResources(MI);
204eb11fae6SDimitry Andric LLVM_DEBUG({
20501095a5dSDimitry Andric if (ResourceAvail)
20601095a5dSDimitry Andric dbgs() << " Resources are available for adding MI to packet\n";
20701095a5dSDimitry Andric else
20801095a5dSDimitry Andric dbgs() << " Resources NOT available\n";
20901095a5dSDimitry Andric });
210dd58ef01SDimitry Andric if (ResourceAvail && shouldAddToPacket(MI)) {
21163faed5bSDimitry Andric // Dependency check for MI with instructions in CurrentPacketMIs.
2124b4fe385SDimitry Andric for (auto *MJ : CurrentPacketMIs) {
21358b69754SDimitry Andric SUnit *SUJ = MIToSUnit[MJ];
21463faed5bSDimitry Andric assert(SUJ && "Missing SUnit Info!");
21563faed5bSDimitry Andric
216eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " Checking against MJ " << *MJ);
21763faed5bSDimitry Andric // Is it legal to packetize SUI and SUJ together.
21801095a5dSDimitry Andric if (!isLegalToPacketizeTogether(SUI, SUJ)) {
219eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
22063faed5bSDimitry Andric // Allow packetization if dependency can be pruned.
22101095a5dSDimitry Andric if (!isLegalToPruneDependencies(SUI, SUJ)) {
22263faed5bSDimitry Andric // End the packet if dependency cannot be pruned.
223eb11fae6SDimitry Andric LLVM_DEBUG(dbgs()
224eb11fae6SDimitry Andric << " Could not prune dependencies for adding MI\n");
22563faed5bSDimitry Andric endPacket(MBB, MI);
22663faed5bSDimitry Andric break;
22701095a5dSDimitry Andric }
228eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " Pruned dependence for adding MI\n");
22901095a5dSDimitry Andric }
23001095a5dSDimitry Andric }
23163faed5bSDimitry Andric } else {
232eb11fae6SDimitry Andric LLVM_DEBUG(if (ResourceAvail) dbgs()
233eb11fae6SDimitry Andric << "Resources are available, but instruction should not be "
234eb11fae6SDimitry Andric "added to packet\n "
235eb11fae6SDimitry Andric << MI);
236dd58ef01SDimitry Andric // End the packet if resource is not available, or if the instruction
2377fa27ce4SDimitry Andric // should not be added to the current packet.
23863faed5bSDimitry Andric endPacket(MBB, MI);
23963faed5bSDimitry Andric }
24063faed5bSDimitry Andric
24163faed5bSDimitry Andric // Add MI to the current packet.
242eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
24301095a5dSDimitry Andric BeginItr = addToPacket(MI);
24401095a5dSDimitry Andric } // For all instructions in the packetization range.
24563faed5bSDimitry Andric
24663faed5bSDimitry Andric // End any packet left behind.
24763faed5bSDimitry Andric endPacket(MBB, EndItr);
24858b69754SDimitry Andric VLIWScheduler->exitRegion();
24958b69754SDimitry Andric VLIWScheduler->finishBlock();
25063faed5bSDimitry Andric }
25101095a5dSDimitry Andric
alias(const MachineMemOperand & Op1,const MachineMemOperand & Op2,bool UseTBAA) const252044eb2f6SDimitry Andric bool VLIWPacketizerList::alias(const MachineMemOperand &Op1,
253044eb2f6SDimitry Andric const MachineMemOperand &Op2,
254044eb2f6SDimitry Andric bool UseTBAA) const {
255ac9a064cSDimitry Andric if (!Op1.getValue() || !Op2.getValue() || !Op1.getSize().hasValue() ||
256ac9a064cSDimitry Andric !Op2.getSize().hasValue())
257044eb2f6SDimitry Andric return true;
258044eb2f6SDimitry Andric
259044eb2f6SDimitry Andric int64_t MinOffset = std::min(Op1.getOffset(), Op2.getOffset());
260ac9a064cSDimitry Andric int64_t Overlapa = Op1.getSize().getValue() + Op1.getOffset() - MinOffset;
261ac9a064cSDimitry Andric int64_t Overlapb = Op2.getSize().getValue() + Op2.getOffset() - MinOffset;
262044eb2f6SDimitry Andric
263044eb2f6SDimitry Andric AliasResult AAResult =
264044eb2f6SDimitry Andric AA->alias(MemoryLocation(Op1.getValue(), Overlapa,
265044eb2f6SDimitry Andric UseTBAA ? Op1.getAAInfo() : AAMDNodes()),
266044eb2f6SDimitry Andric MemoryLocation(Op2.getValue(), Overlapb,
267044eb2f6SDimitry Andric UseTBAA ? Op2.getAAInfo() : AAMDNodes()));
268044eb2f6SDimitry Andric
269344a3780SDimitry Andric return AAResult != AliasResult::NoAlias;
270044eb2f6SDimitry Andric }
271044eb2f6SDimitry Andric
alias(const MachineInstr & MI1,const MachineInstr & MI2,bool UseTBAA) const272044eb2f6SDimitry Andric bool VLIWPacketizerList::alias(const MachineInstr &MI1,
273044eb2f6SDimitry Andric const MachineInstr &MI2,
274044eb2f6SDimitry Andric bool UseTBAA) const {
275044eb2f6SDimitry Andric if (MI1.memoperands_empty() || MI2.memoperands_empty())
276044eb2f6SDimitry Andric return true;
277044eb2f6SDimitry Andric
278044eb2f6SDimitry Andric for (const MachineMemOperand *Op1 : MI1.memoperands())
279044eb2f6SDimitry Andric for (const MachineMemOperand *Op2 : MI2.memoperands())
280044eb2f6SDimitry Andric if (alias(*Op1, *Op2, UseTBAA))
281044eb2f6SDimitry Andric return true;
282044eb2f6SDimitry Andric return false;
283044eb2f6SDimitry Andric }
284044eb2f6SDimitry Andric
28501095a5dSDimitry Andric // Add a DAG mutation object to the ordered list.
addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation)28601095a5dSDimitry Andric void VLIWPacketizerList::addMutation(
28701095a5dSDimitry Andric std::unique_ptr<ScheduleDAGMutation> Mutation) {
28801095a5dSDimitry Andric VLIWScheduler->addMutation(std::move(Mutation));
28901095a5dSDimitry Andric }
290