blob: 68034afe98d535e999b4142fd7d943460627a8b5 [file] [log] [blame]
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +00001//=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
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// This class implements a deterministic finite automaton (DFA) based
10// packetizing mechanism for VLIW architectures. It provides APIs to
11// determine whether there exists a legal mapping of instructions to
12// functional unit assignments in a packet. The DFA is auto-generated from
13// the target's Schedule.td file.
14//
15// A DFA consists of 3 major elements: states, inputs, and transitions. For
16// the packetizing mechanism, the input is the set of instruction classes for
17// a target. The state models all possible combinations of functional unit
18// consumption for a given set of instructions in a packet. A transition
19// models the addition of an instruction to a packet. In the DFA constructed
20// by this class, if an instruction can be added to a packet, then a valid
21// transition exists from the corresponding state. Invalid transitions
22// indicate that the instruction cannot be added to the current packet.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/CodeGen/DFAPacketizer.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000028#include "llvm/CodeGen/MachineInstr.h"
Andrew Trickebafa0c2012-02-15 18:55:14 +000029#include "llvm/CodeGen/MachineInstrBundle.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/CodeGen/ScheduleDAGInstrs.h"
David Blaikie48319232017-11-08 01:01:31 +000032#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000034#include "llvm/MC/MCInstrDesc.h"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000035#include "llvm/MC/MCInstrItineraries.h"
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +000036#include "llvm/Support/CommandLine.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/raw_ostream.h"
Eugene Zelenko643c0a42017-06-07 23:53:32 +000039#include <algorithm>
40#include <cassert>
41#include <iterator>
42#include <memory>
43#include <vector>
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +000044
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000045using namespace llvm;
46
Eugene Zelenko643c0a42017-06-07 23:53:32 +000047#define DEBUG_TYPE "packets"
48
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +000049static cl::opt<unsigned> InstrLimit("dfa-instr-limit", cl::Hidden,
50 cl::init(0), cl::desc("If present, stops packetizing after N instructions"));
Eugene Zelenko643c0a42017-06-07 23:53:32 +000051
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +000052static unsigned InstrCount = 0;
53
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +000054// --------------------------------------------------------------------
55// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
56
Eugene Zelenko643c0a42017-06-07 23:53:32 +000057static DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
58 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +000059}
Eugene Zelenko643c0a42017-06-07 23:53:32 +000060
61/// Return the DFAInput for an instruction class input vector.
62/// This function is used in both DFAPacketizer.cpp and in
63/// DFAPacketizerEmitter.cpp.
64static DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
65 DFAInput InsnInput = 0;
66 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
67 "Exceeded maximum number of DFA terms");
68 for (auto U : InsnClass)
69 InsnInput = addDFAFuncUnits(InsnInput, U);
70 return InsnInput;
71}
72
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +000073// --------------------------------------------------------------------
74
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +000075DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
76 const DFAStateInput (*SIT)[2],
Sebastian Pop464f3a32011-12-06 17:34:16 +000077 const unsigned *SET):
Eugene Zelenko643c0a42017-06-07 23:53:32 +000078 InstrItins(I), DFAStateInputTable(SIT), DFAStateEntryTable(SET) {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +000079 // Make sure DFA types are large enough for the number of terms & resources.
Benjamin Kramer0ad61072016-05-27 11:36:04 +000080 static_assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <=
81 (8 * sizeof(DFAInput)),
82 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
83 static_assert(
84 (DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput)),
85 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +000086}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000087
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +000088// Read the DFA transition table and update CachedTable.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000089//
90// Format of the transition tables:
91// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
92// transitions
93// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
94// for the ith state
95//
96void DFAPacketizer::ReadTable(unsigned int state) {
97 unsigned ThisState = DFAStateEntryTable[state];
98 unsigned NextStateInTable = DFAStateEntryTable[state+1];
99 // Early exit in case CachedTable has already contains this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000100 // state's transitions.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000101 if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0])))
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000102 return;
103
104 for (unsigned i = ThisState; i < NextStateInTable; i++)
105 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
106 DFAStateInputTable[i][1];
107}
108
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000109// Return the DFAInput for an instruction class.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000110DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
111 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
112 DFAInput InsnInput = 0;
113 unsigned i = 0;
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000114 (void)i;
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000115 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000116 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000117 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000118 assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000119 }
120 return InsnInput;
121}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000122
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000123// Return the DFAInput for an instruction class input vector.
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +0000124DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
125 return getDFAInsnInput(InsnClass);
126}
127
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000128// Check if the resources occupied by a MCInstrDesc are available in the
129// current state.
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000130bool DFAPacketizer::canReserveResources(const MCInstrDesc *MID) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000131 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000132 DFAInput InsnInput = getInsnInput(InsnClass);
133 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000134 ReadTable(CurrentState);
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000135 return CachedTable.count(StateTrans) != 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000136}
137
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000138// Reserve the resources occupied by a MCInstrDesc and change the current
139// state to reflect that change.
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000140void DFAPacketizer::reserveResources(const MCInstrDesc *MID) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000141 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000142 DFAInput InsnInput = getInsnInput(InsnClass);
143 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000144 ReadTable(CurrentState);
145 assert(CachedTable.count(StateTrans) != 0);
146 CurrentState = CachedTable[StateTrans];
147}
148
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000149// Check if the resources occupied by a machine instruction are available
150// in the current state.
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000151bool DFAPacketizer::canReserveResources(MachineInstr &MI) {
152 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000153 return canReserveResources(&MID);
154}
155
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000156// Reserve the resources occupied by a machine instruction and change the
157// current state to reflect that change.
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000158void DFAPacketizer::reserveResources(MachineInstr &MI) {
159 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000160 reserveResources(&MID);
161}
Andrew Trickebafa0c2012-02-15 18:55:14 +0000162
Sirish Pande90233702012-05-01 21:28:30 +0000163namespace llvm {
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000164
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000165// This class extends ScheduleDAGInstrs and overrides the schedule method
166// to build the dependence graph.
Andrew Trickebafa0c2012-02-15 18:55:14 +0000167class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszeke0d52332015-12-14 20:35:13 +0000168private:
169 AliasAnalysis *AA;
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000170 /// Ordered list of DAG postprocessing steps.
171 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000172
Andrew Trickebafa0c2012-02-15 18:55:14 +0000173public:
Krzysztof Parzyszeke0d52332015-12-14 20:35:13 +0000174 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
175 AliasAnalysis *AA);
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000176
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000177 // Actual scheduling work.
Craig Topper9f998de2014-03-07 09:26:03 +0000178 void schedule() override;
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000179
180 /// DefaultVLIWScheduler takes ownership of the Mutation object.
181 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
182 Mutations.push_back(std::move(Mutation));
183 }
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000184
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000185protected:
186 void postprocessDAG();
Andrew Trickebafa0c2012-02-15 18:55:14 +0000187};
Andrew Tricke7461862012-02-15 23:34:15 +0000188
Eugene Zelenko643c0a42017-06-07 23:53:32 +0000189} // end namespace llvm
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000190
Alexey Samsonova046b412014-08-20 20:57:26 +0000191DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszeke0d52332015-12-14 20:35:13 +0000192 MachineLoopInfo &MLI,
193 AliasAnalysis *AA)
194 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande90233702012-05-01 21:28:30 +0000195 CanHandleTerminators = true;
Andrew Trickebafa0c2012-02-15 18:55:14 +0000196}
197
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000198/// Apply each ScheduleDAGMutation step in order.
199void DefaultVLIWScheduler::postprocessDAG() {
200 for (auto &M : Mutations)
201 M->apply(this);
202}
203
Andrew Trick953be892012-03-07 23:00:49 +0000204void DefaultVLIWScheduler::schedule() {
Andrew Trickebafa0c2012-02-15 18:55:14 +0000205 // Build the scheduling graph.
Krzysztof Parzyszeke0d52332015-12-14 20:35:13 +0000206 buildSchedGraph(AA);
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000207 postprocessDAG();
Andrew Trickebafa0c2012-02-15 18:55:14 +0000208}
209
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000210VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
211 MachineLoopInfo &mli, AliasAnalysis *aa)
212 : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
Eric Christophere6d97092014-10-09 01:59:35 +0000213 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000214 VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
Andrew Trickebafa0c2012-02-15 18:55:14 +0000215}
216
Andrew Trickebafa0c2012-02-15 18:55:14 +0000217VLIWPacketizerList::~VLIWPacketizerList() {
Gabor Horvath4d5ff6d2017-05-01 16:18:42 +0000218 delete VLIWScheduler;
219 delete ResourceTracker;
Andrew Trickebafa0c2012-02-15 18:55:14 +0000220}
221
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000222// End the current packet, bundle packet instructions and reset DFA state.
Duncan P. N. Exon Smith0ce039d2016-02-27 19:09:00 +0000223void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
224 MachineBasicBlock::iterator MI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000225 LLVM_DEBUG({
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +0000226 if (!CurrentPacketMIs.empty()) {
227 dbgs() << "Finalizing packet:\n";
228 for (MachineInstr *MI : CurrentPacketMIs)
229 dbgs() << " * " << *MI;
230 }
231 });
Andrew Trickebafa0c2012-02-15 18:55:14 +0000232 if (CurrentPacketMIs.size() > 1) {
Duncan P. N. Exon Smith0ce039d2016-02-27 19:09:00 +0000233 MachineInstr &MIFirst = *CurrentPacketMIs.front();
234 finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
Andrew Trickebafa0c2012-02-15 18:55:14 +0000235 }
236 CurrentPacketMIs.clear();
237 ResourceTracker->clearResources();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000238 LLVM_DEBUG(dbgs() << "End packet\n");
Andrew Trickebafa0c2012-02-15 18:55:14 +0000239}
240
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000241// Bundle machine instructions into packets.
Andrew Trickebafa0c2012-02-15 18:55:14 +0000242void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
243 MachineBasicBlock::iterator BeginItr,
244 MachineBasicBlock::iterator EndItr) {
Sirish Pande90233702012-05-01 21:28:30 +0000245 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
246 VLIWScheduler->startBlock(MBB);
Andrew Trickd2763f62013-08-23 17:48:33 +0000247 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
248 std::distance(BeginItr, EndItr));
Sirish Pande90233702012-05-01 21:28:30 +0000249 VLIWScheduler->schedule();
Andrew Trick7afcda02012-03-07 23:01:09 +0000250
Nicola Zaghen0818e782018-05-14 12:53:11 +0000251 LLVM_DEBUG({
Krzysztof Parzyszek8bd1db42016-07-14 19:04:26 +0000252 dbgs() << "Scheduling DAG of the packetize region\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000253 VLIWScheduler->dump();
Krzysztof Parzyszek8bd1db42016-07-14 19:04:26 +0000254 });
255
Sirish Pande90233702012-05-01 21:28:30 +0000256 // Generate MI -> SU map.
257 MIToSUnit.clear();
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000258 for (SUnit &SU : VLIWScheduler->SUnits)
259 MIToSUnit[SU.getInstr()] = &SU;
Andrew Trickebafa0c2012-02-15 18:55:14 +0000260
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +0000261 bool LimitPresent = InstrLimit.getPosition();
262
Andrew Trickebafa0c2012-02-15 18:55:14 +0000263 // The main packetizer loop.
264 for (; BeginItr != EndItr; ++BeginItr) {
Krzysztof Parzyszek61926b42016-08-19 21:12:52 +0000265 if (LimitPresent) {
266 if (InstrCount >= InstrLimit) {
267 EndItr = BeginItr;
268 break;
269 }
270 InstrCount++;
271 }
Duncan P. N. Exon Smith0ce039d2016-02-27 19:09:00 +0000272 MachineInstr &MI = *BeginItr;
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000273 initPacketizerState();
Andrew Trickebafa0c2012-02-15 18:55:14 +0000274
275 // End the current packet if needed.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000276 if (isSoloInstruction(MI)) {
Andrew Trickebafa0c2012-02-15 18:55:14 +0000277 endPacket(MBB, MI);
278 continue;
279 }
280
Sirish Pande90233702012-05-01 21:28:30 +0000281 // Ignore pseudo instructions.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000282 if (ignorePseudoInstruction(MI, MBB))
Sirish Pande90233702012-05-01 21:28:30 +0000283 continue;
284
Duncan P. N. Exon Smith0ce039d2016-02-27 19:09:00 +0000285 SUnit *SUI = MIToSUnit[&MI];
Andrew Trickebafa0c2012-02-15 18:55:14 +0000286 assert(SUI && "Missing SUnit Info!");
287
288 // Ask DFA if machine resource is available for MI.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000289 LLVM_DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
Krzysztof Parzyszek8bd1db42016-07-14 19:04:26 +0000290
Andrew Trickebafa0c2012-02-15 18:55:14 +0000291 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000292 LLVM_DEBUG({
Krzysztof Parzyszek8bd1db42016-07-14 19:04:26 +0000293 if (ResourceAvail)
294 dbgs() << " Resources are available for adding MI to packet\n";
295 else
296 dbgs() << " Resources NOT available\n";
297 });
Krzysztof Parzyszek8ad916f2015-12-16 16:38:16 +0000298 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trickebafa0c2012-02-15 18:55:14 +0000299 // Dependency check for MI with instructions in CurrentPacketMIs.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000300 for (auto MJ : CurrentPacketMIs) {
Sirish Pande90233702012-05-01 21:28:30 +0000301 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trickebafa0c2012-02-15 18:55:14 +0000302 assert(SUJ && "Missing SUnit Info!");
303
Nicola Zaghen0818e782018-05-14 12:53:11 +0000304 LLVM_DEBUG(dbgs() << " Checking against MJ " << *MJ);
Andrew Trickebafa0c2012-02-15 18:55:14 +0000305 // Is it legal to packetize SUI and SUJ together.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000306 if (!isLegalToPacketizeTogether(SUI, SUJ)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000307 LLVM_DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
Andrew Trickebafa0c2012-02-15 18:55:14 +0000308 // Allow packetization if dependency can be pruned.
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000309 if (!isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trickebafa0c2012-02-15 18:55:14 +0000310 // End the packet if dependency cannot be pruned.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000311 LLVM_DEBUG(dbgs()
312 << " Could not prune dependencies for adding MI\n");
Andrew Trickebafa0c2012-02-15 18:55:14 +0000313 endPacket(MBB, MI);
314 break;
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000315 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000316 LLVM_DEBUG(dbgs() << " Pruned dependence for adding MI\n");
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000317 }
318 }
Andrew Trickebafa0c2012-02-15 18:55:14 +0000319 } else {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000320 LLVM_DEBUG(if (ResourceAvail) dbgs()
321 << "Resources are available, but instruction should not be "
322 "added to packet\n "
323 << MI);
Krzysztof Parzyszek8ad916f2015-12-16 16:38:16 +0000324 // End the packet if resource is not available, or if the instruction
325 // shoud not be added to the current packet.
Andrew Trickebafa0c2012-02-15 18:55:14 +0000326 endPacket(MBB, MI);
327 }
328
329 // Add MI to the current packet.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000330 LLVM_DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
Krzysztof Parzyszekd3b460f2016-01-14 21:17:04 +0000331 BeginItr = addToPacket(MI);
332 } // For all instructions in the packetization range.
Andrew Trickebafa0c2012-02-15 18:55:14 +0000333
334 // End any packet left behind.
335 endPacket(MBB, EndItr);
Sirish Pande90233702012-05-01 21:28:30 +0000336 VLIWScheduler->exitRegion();
337 VLIWScheduler->finishBlock();
Andrew Trickebafa0c2012-02-15 18:55:14 +0000338}
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000339
Krzysztof Parzyszek26843fd2017-10-20 22:08:40 +0000340bool VLIWPacketizerList::alias(const MachineMemOperand &Op1,
341 const MachineMemOperand &Op2,
342 bool UseTBAA) const {
343 if (!Op1.getValue() || !Op2.getValue())
344 return true;
345
346 int64_t MinOffset = std::min(Op1.getOffset(), Op2.getOffset());
347 int64_t Overlapa = Op1.getSize() + Op1.getOffset() - MinOffset;
348 int64_t Overlapb = Op2.getSize() + Op2.getOffset() - MinOffset;
349
350 AliasResult AAResult =
351 AA->alias(MemoryLocation(Op1.getValue(), Overlapa,
352 UseTBAA ? Op1.getAAInfo() : AAMDNodes()),
353 MemoryLocation(Op2.getValue(), Overlapb,
354 UseTBAA ? Op2.getAAInfo() : AAMDNodes()));
355
356 return AAResult != NoAlias;
357}
358
359bool VLIWPacketizerList::alias(const MachineInstr &MI1,
360 const MachineInstr &MI2,
361 bool UseTBAA) const {
362 if (MI1.memoperands_empty() || MI2.memoperands_empty())
363 return true;
364
365 for (const MachineMemOperand *Op1 : MI1.memoperands())
366 for (const MachineMemOperand *Op2 : MI2.memoperands())
367 if (alias(*Op1, *Op2, UseTBAA))
368 return true;
369 return false;
370}
371
Krzysztof Parzyszek96532642016-03-08 15:33:51 +0000372// Add a DAG mutation object to the ordered list.
373void VLIWPacketizerList::addMutation(
374 std::unique_ptr<ScheduleDAGMutation> Mutation) {
375 VLIWScheduler->addMutation(std::move(Mutation));
376}