blob: b99be5d7a87c40b4f53dc33de03bdb5e9433b8f6 [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"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000026#include "llvm/CodeGen/MachineFunction.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000027#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick7a35fae2012-02-15 18:55:14 +000028#include "llvm/CodeGen/MachineInstrBundle.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000029#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/CodeGen/ScheduleDAGInstrs.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000031#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000033#include "llvm/MC/MCInstrDesc.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000034#include "llvm/MC/MCInstrItineraries.h"
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000035#include "llvm/Support/CommandLine.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000038#include <algorithm>
39#include <cassert>
40#include <iterator>
41#include <memory>
42#include <vector>
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000043
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000044using namespace llvm;
45
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000046#define DEBUG_TYPE "packets"
47
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000048static cl::opt<unsigned> InstrLimit("dfa-instr-limit", cl::Hidden,
49 cl::init(0), cl::desc("If present, stops packetizing after N instructions"));
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000050
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +000051static unsigned InstrCount = 0;
52
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000053// --------------------------------------------------------------------
54// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
55
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000056static DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
57 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000058}
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000059
60/// Return the DFAInput for an instruction class input vector.
61/// This function is used in both DFAPacketizer.cpp and in
62/// DFAPacketizerEmitter.cpp.
63static DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
64 DFAInput InsnInput = 0;
65 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
66 "Exceeded maximum number of DFA terms");
67 for (auto U : InsnClass)
68 InsnInput = addDFAFuncUnits(InsnInput, U);
69 return InsnInput;
70}
71
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000072// --------------------------------------------------------------------
73
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000074DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
75 const DFAStateInput (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000076 const unsigned *SET):
Eugene Zelenko6ac7a342017-06-07 23:53:32 +000077 InstrItins(I), DFAStateInputTable(SIT), DFAStateEntryTable(SET) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000078 // Make sure DFA types are large enough for the number of terms & resources.
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +000079 static_assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <=
80 (8 * sizeof(DFAInput)),
81 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
82 static_assert(
83 (DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput)),
84 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000085}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000086
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000087// Read the DFA transition table and update CachedTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000088//
89// Format of the transition tables:
90// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
91// transitions
92// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
93// for the ith state
94//
95void DFAPacketizer::ReadTable(unsigned int state) {
96 unsigned ThisState = DFAStateEntryTable[state];
97 unsigned NextStateInTable = DFAStateEntryTable[state+1];
98 // Early exit in case CachedTable has already contains this
Sebastian Pop9aa61372011-12-06 17:34:11 +000099 // state's transitions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000100 if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0])))
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000101 return;
102
103 for (unsigned i = ThisState; i < NextStateInTable; i++)
104 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
105 DFAStateInputTable[i][1];
106}
107
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000108// Return the DFAInput for an instruction class.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000109DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
110 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
111 DFAInput InsnInput = 0;
112 unsigned i = 0;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000113 (void)i;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000114 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000115 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000116 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000117 assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000118 }
119 return InsnInput;
120}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000121
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000122// Return the DFAInput for an instruction class input vector.
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +0000123DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
124 return getDFAInsnInput(InsnClass);
125}
126
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000127// Check if the resources occupied by a MCInstrDesc are available in the
128// current state.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000129bool DFAPacketizer::canReserveResources(const MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000130 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000131 DFAInput InsnInput = getInsnInput(InsnClass);
132 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000133 ReadTable(CurrentState);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000134 return CachedTable.count(StateTrans) != 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000135}
136
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000137// Reserve the resources occupied by a MCInstrDesc and change the current
138// state to reflect that change.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000139void DFAPacketizer::reserveResources(const MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000140 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000141 DFAInput InsnInput = getInsnInput(InsnClass);
142 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000143 ReadTable(CurrentState);
144 assert(CachedTable.count(StateTrans) != 0);
145 CurrentState = CachedTable[StateTrans];
146}
147
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000148// Check if the resources occupied by a machine instruction are available
149// in the current state.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000150bool DFAPacketizer::canReserveResources(MachineInstr &MI) {
151 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000152 return canReserveResources(&MID);
153}
154
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000155// Reserve the resources occupied by a machine instruction and change the
156// current state to reflect that change.
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000157void DFAPacketizer::reserveResources(MachineInstr &MI) {
158 const MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000159 reserveResources(&MID);
160}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000161
Sirish Pande94212162012-05-01 21:28:30 +0000162namespace llvm {
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000163
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000164// This class extends ScheduleDAGInstrs and overrides the schedule method
165// to build the dependence graph.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000166class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000167private:
168 AliasAnalysis *AA;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000169 /// Ordered list of DAG postprocessing steps.
170 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000171
Andrew Trick7a35fae2012-02-15 18:55:14 +0000172public:
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000173 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
174 AliasAnalysis *AA);
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000175
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000176 // Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000177 void schedule() override;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000178
179 /// DefaultVLIWScheduler takes ownership of the Mutation object.
180 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
181 Mutations.push_back(std::move(Mutation));
182 }
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000183
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000184protected:
185 void postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000186};
Andrew Trick20349b82012-02-15 23:34:15 +0000187
Eugene Zelenko6ac7a342017-06-07 23:53:32 +0000188} // end namespace llvm
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000189
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000190DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000191 MachineLoopInfo &MLI,
192 AliasAnalysis *AA)
193 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande94212162012-05-01 21:28:30 +0000194 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000195}
196
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000197/// Apply each ScheduleDAGMutation step in order.
198void DefaultVLIWScheduler::postprocessDAG() {
199 for (auto &M : Mutations)
200 M->apply(this);
201}
202
Andrew Trick52226d42012-03-07 23:00:49 +0000203void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000204 // Build the scheduling graph.
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000205 buildSchedGraph(AA);
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000206 postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000207}
208
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000209VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
210 MachineLoopInfo &mli, AliasAnalysis *aa)
211 : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
Eric Christopher143f02c2014-10-09 01:59:35 +0000212 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000213 VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000214}
215
Andrew Trick7a35fae2012-02-15 18:55:14 +0000216VLIWPacketizerList::~VLIWPacketizerList() {
Gabor Horvath43b72d52017-05-01 16:18:42 +0000217 delete VLIWScheduler;
218 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000219}
220
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000221// End the current packet, bundle packet instructions and reset DFA state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000222void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
223 MachineBasicBlock::iterator MI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000224 LLVM_DEBUG({
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000225 if (!CurrentPacketMIs.empty()) {
226 dbgs() << "Finalizing packet:\n";
227 for (MachineInstr *MI : CurrentPacketMIs)
228 dbgs() << " * " << *MI;
229 }
230 });
Andrew Trick7a35fae2012-02-15 18:55:14 +0000231 if (CurrentPacketMIs.size() > 1) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000232 MachineInstr &MIFirst = *CurrentPacketMIs.front();
233 finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000234 }
235 CurrentPacketMIs.clear();
236 ResourceTracker->clearResources();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000237 LLVM_DEBUG(dbgs() << "End packet\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000238}
239
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000240// Bundle machine instructions into packets.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000241void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
242 MachineBasicBlock::iterator BeginItr,
243 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000244 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
245 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000246 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
247 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000248 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000249
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000250 LLVM_DEBUG({
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000251 dbgs() << "Scheduling DAG of the packetize region\n";
Matthias Braun726e12c2018-09-19 00:23:35 +0000252 VLIWScheduler->dump();
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000253 });
254
Sirish Pande94212162012-05-01 21:28:30 +0000255 // Generate MI -> SU map.
256 MIToSUnit.clear();
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000257 for (SUnit &SU : VLIWScheduler->SUnits)
258 MIToSUnit[SU.getInstr()] = &SU;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000259
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000260 bool LimitPresent = InstrLimit.getPosition();
261
Andrew Trick7a35fae2012-02-15 18:55:14 +0000262 // The main packetizer loop.
263 for (; BeginItr != EndItr; ++BeginItr) {
Krzysztof Parzyszeke4582d42016-08-19 21:12:52 +0000264 if (LimitPresent) {
265 if (InstrCount >= InstrLimit) {
266 EndItr = BeginItr;
267 break;
268 }
269 InstrCount++;
270 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000271 MachineInstr &MI = *BeginItr;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000272 initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000273
274 // End the current packet if needed.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000275 if (isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000276 endPacket(MBB, MI);
277 continue;
278 }
279
Sirish Pande94212162012-05-01 21:28:30 +0000280 // Ignore pseudo instructions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000281 if (ignorePseudoInstruction(MI, MBB))
Sirish Pande94212162012-05-01 21:28:30 +0000282 continue;
283
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000284 SUnit *SUI = MIToSUnit[&MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000285 assert(SUI && "Missing SUnit Info!");
286
287 // Ask DFA if machine resource is available for MI.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000288 LLVM_DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000289
Andrew Trick7a35fae2012-02-15 18:55:14 +0000290 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000291 LLVM_DEBUG({
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000292 if (ResourceAvail)
293 dbgs() << " Resources are available for adding MI to packet\n";
294 else
295 dbgs() << " Resources NOT available\n";
296 });
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000297 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000298 // Dependency check for MI with instructions in CurrentPacketMIs.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000299 for (auto MJ : CurrentPacketMIs) {
Sirish Pande94212162012-05-01 21:28:30 +0000300 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000301 assert(SUJ && "Missing SUnit Info!");
302
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000303 LLVM_DEBUG(dbgs() << " Checking against MJ " << *MJ);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000304 // Is it legal to packetize SUI and SUJ together.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000305 if (!isLegalToPacketizeTogether(SUI, SUJ)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000306 LLVM_DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000307 // Allow packetization if dependency can be pruned.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000308 if (!isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000309 // End the packet if dependency cannot be pruned.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000310 LLVM_DEBUG(dbgs()
311 << " Could not prune dependencies for adding MI\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000312 endPacket(MBB, MI);
313 break;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000314 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000315 LLVM_DEBUG(dbgs() << " Pruned dependence for adding MI\n");
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000316 }
317 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000318 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000319 LLVM_DEBUG(if (ResourceAvail) dbgs()
320 << "Resources are available, but instruction should not be "
321 "added to packet\n "
322 << MI);
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000323 // End the packet if resource is not available, or if the instruction
324 // shoud not be added to the current packet.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000325 endPacket(MBB, MI);
326 }
327
328 // Add MI to the current packet.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000329 LLVM_DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000330 BeginItr = addToPacket(MI);
331 } // For all instructions in the packetization range.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000332
333 // End any packet left behind.
334 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000335 VLIWScheduler->exitRegion();
336 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000337}
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000338
Krzysztof Parzyszek9d19c8c2017-10-20 22:08:40 +0000339bool VLIWPacketizerList::alias(const MachineMemOperand &Op1,
340 const MachineMemOperand &Op2,
341 bool UseTBAA) const {
342 if (!Op1.getValue() || !Op2.getValue())
343 return true;
344
345 int64_t MinOffset = std::min(Op1.getOffset(), Op2.getOffset());
346 int64_t Overlapa = Op1.getSize() + Op1.getOffset() - MinOffset;
347 int64_t Overlapb = Op2.getSize() + Op2.getOffset() - MinOffset;
348
349 AliasResult AAResult =
350 AA->alias(MemoryLocation(Op1.getValue(), Overlapa,
351 UseTBAA ? Op1.getAAInfo() : AAMDNodes()),
352 MemoryLocation(Op2.getValue(), Overlapb,
353 UseTBAA ? Op2.getAAInfo() : AAMDNodes()));
354
355 return AAResult != NoAlias;
356}
357
358bool VLIWPacketizerList::alias(const MachineInstr &MI1,
359 const MachineInstr &MI2,
360 bool UseTBAA) const {
361 if (MI1.memoperands_empty() || MI2.memoperands_empty())
362 return true;
363
364 for (const MachineMemOperand *Op1 : MI1.memoperands())
365 for (const MachineMemOperand *Op2 : MI2.memoperands())
366 if (alias(*Op1, *Op2, UseTBAA))
367 return true;
368 return false;
369}
370
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000371// Add a DAG mutation object to the ordered list.
372void VLIWPacketizerList::addMutation(
373 std::unique_ptr<ScheduleDAGMutation> Mutation) {
374 VLIWScheduler->addMutation(std::move(Mutation));
375}