16ac7a348SEugene Zelenko //===- llvm/CodeGen/DFAPacketizer.h - DFA Packetizer for VLIW ---*- C++ -*-===// 208ebdc1eSAnshuman Dasgupta // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 608ebdc1eSAnshuman Dasgupta // 708ebdc1eSAnshuman Dasgupta //===----------------------------------------------------------------------===// 808ebdc1eSAnshuman Dasgupta // This class implements a deterministic finite automaton (DFA) based 908ebdc1eSAnshuman Dasgupta // packetizing mechanism for VLIW architectures. It provides APIs to 1008ebdc1eSAnshuman Dasgupta // determine whether there exists a legal mapping of instructions to 1108ebdc1eSAnshuman Dasgupta // functional unit assignments in a packet. The DFA is auto-generated from 1208ebdc1eSAnshuman Dasgupta // the target's Schedule.td file. 1308ebdc1eSAnshuman Dasgupta // 1408ebdc1eSAnshuman Dasgupta // A DFA consists of 3 major elements: states, inputs, and transitions. For 1508ebdc1eSAnshuman Dasgupta // the packetizing mechanism, the input is the set of instruction classes for 1608ebdc1eSAnshuman Dasgupta // a target. The state models all possible combinations of functional unit 1708ebdc1eSAnshuman Dasgupta // consumption for a given set of instructions in a packet. A transition 1808ebdc1eSAnshuman Dasgupta // models the addition of an instruction to a packet. In the DFA constructed 1908ebdc1eSAnshuman Dasgupta // by this class, if an instruction can be added to a packet, then a valid 2008ebdc1eSAnshuman Dasgupta // transition exists from the corresponding state. Invalid transitions 2108ebdc1eSAnshuman Dasgupta // indicate that the instruction cannot be added to the current packet. 2208ebdc1eSAnshuman Dasgupta // 2308ebdc1eSAnshuman Dasgupta //===----------------------------------------------------------------------===// 2408ebdc1eSAnshuman Dasgupta 2508ebdc1eSAnshuman Dasgupta #ifndef LLVM_CODEGEN_DFAPACKETIZER_H 2608ebdc1eSAnshuman Dasgupta #define LLVM_CODEGEN_DFAPACKETIZER_H 2708ebdc1eSAnshuman Dasgupta 28a79a28b7SChandler Carruth #include "llvm/CodeGen/MachineBasicBlock.h" 2912092a96SJames Molloy #include "llvm/Support/Automaton.h" 306ac7a348SEugene Zelenko #include <cstdint> 3194212168SSirish Pande #include <map> 326ac7a348SEugene Zelenko #include <memory> 336ac7a348SEugene Zelenko #include <utility> 346ac7a348SEugene Zelenko #include <vector> 3508ebdc1eSAnshuman Dasgupta 3608ebdc1eSAnshuman Dasgupta namespace llvm { 3708ebdc1eSAnshuman Dasgupta 386ac7a348SEugene Zelenko class DefaultVLIWScheduler; 39*989f1c72Sserge-sans-paille class ScheduleDAGMutation; 406ac7a348SEugene Zelenko class InstrItineraryData; 416ac7a348SEugene Zelenko class MachineFunction; 4208ebdc1eSAnshuman Dasgupta class MachineInstr; 437a35faeaSAndrew Trick class MachineLoopInfo; 446ac7a348SEugene Zelenko class MCInstrDesc; 457a35faeaSAndrew Trick class SUnit; 466ac7a348SEugene Zelenko class TargetInstrInfo; 4708ebdc1eSAnshuman Dasgupta 4808ebdc1eSAnshuman Dasgupta class DFAPacketizer { 4908ebdc1eSAnshuman Dasgupta private: 5008ebdc1eSAnshuman Dasgupta const InstrItineraryData *InstrItins; 5139525a67Sjmolloy Automaton<uint64_t> A; 5239525a67Sjmolloy /// For every itinerary, an "action" to apply to the automaton. This removes 5339525a67Sjmolloy /// the redundancy in actions between itinerary classes. 5439525a67Sjmolloy ArrayRef<unsigned> ItinActions; 55b6c7fce6SJames Molloy 5608ebdc1eSAnshuman Dasgupta public: DFAPacketizer(const InstrItineraryData * InstrItins,Automaton<uint64_t> a,ArrayRef<unsigned> ItinActions)5739525a67Sjmolloy DFAPacketizer(const InstrItineraryData *InstrItins, Automaton<uint64_t> a, 5839525a67Sjmolloy ArrayRef<unsigned> ItinActions) 5939525a67Sjmolloy : InstrItins(InstrItins), A(std::move(a)), ItinActions(ItinActions) { 6012092a96SJames Molloy // Start off with resource tracking disabled. 6112092a96SJames Molloy A.enableTranscription(false); 6212092a96SJames Molloy } 6308ebdc1eSAnshuman Dasgupta 649aa6137dSSebastian Pop // Reset the current state to make all resources available. clearResources()6508ebdc1eSAnshuman Dasgupta void clearResources() { 6612092a96SJames Molloy A.reset(); 67b6c7fce6SJames Molloy } 68b6c7fce6SJames Molloy 69b6c7fce6SJames Molloy // Set whether this packetizer should track not just whether instructions 70b6c7fce6SJames Molloy // can be packetized, but also which functional units each instruction ends up 71b6c7fce6SJames Molloy // using after packetization. setTrackResources(bool Track)72b6c7fce6SJames Molloy void setTrackResources(bool Track) { 7312092a96SJames Molloy A.enableTranscription(Track); 7408ebdc1eSAnshuman Dasgupta } 7508ebdc1eSAnshuman Dasgupta 76c005e20dSKrzysztof Parzyszek // Check if the resources occupied by a MCInstrDesc are available in 77c005e20dSKrzysztof Parzyszek // the current state. 786ac7a348SEugene Zelenko bool canReserveResources(const MCInstrDesc *MID); 7908ebdc1eSAnshuman Dasgupta 80c005e20dSKrzysztof Parzyszek // Reserve the resources occupied by a MCInstrDesc and change the current 81c005e20dSKrzysztof Parzyszek // state to reflect that change. 826ac7a348SEugene Zelenko void reserveResources(const MCInstrDesc *MID); 8308ebdc1eSAnshuman Dasgupta 84c005e20dSKrzysztof Parzyszek // Check if the resources occupied by a machine instruction are available 85c005e20dSKrzysztof Parzyszek // in the current state. 866ac7a348SEugene Zelenko bool canReserveResources(MachineInstr &MI); 8708ebdc1eSAnshuman Dasgupta 88c005e20dSKrzysztof Parzyszek // Reserve the resources occupied by a machine instruction and change the 89c005e20dSKrzysztof Parzyszek // current state to reflect that change. 906ac7a348SEugene Zelenko void reserveResources(MachineInstr &MI); 9194212168SSirish Pande 92b6c7fce6SJames Molloy // Return the resources used by the InstIdx'th instruction added to this 93b6c7fce6SJames Molloy // packet. The resources are returned as a bitvector of functional units. 94b6c7fce6SJames Molloy // 95b6c7fce6SJames Molloy // Note that a bundle may be packed in multiple valid ways. This function 96b6c7fce6SJames Molloy // returns one arbitary valid packing. 97b6c7fce6SJames Molloy // 98b6c7fce6SJames Molloy // Requires setTrackResources(true) to have been called. 99b6c7fce6SJames Molloy unsigned getUsedResources(unsigned InstIdx); 100b6c7fce6SJames Molloy getInstrItins()10194212168SSirish Pande const InstrItineraryData *getInstrItins() const { return InstrItins; } 10208ebdc1eSAnshuman Dasgupta }; 1037a35faeaSAndrew Trick 104c005e20dSKrzysztof Parzyszek // VLIWPacketizerList implements a simple VLIW packetizer using DFA. The 105c005e20dSKrzysztof Parzyszek // packetizer works on machine basic blocks. For each instruction I in BB, 106c005e20dSKrzysztof Parzyszek // the packetizer consults the DFA to see if machine resources are available 107c005e20dSKrzysztof Parzyszek // to execute I. If so, the packetizer checks if I depends on any instruction 108c005e20dSKrzysztof Parzyszek // in the current packet. If no dependency is found, I is added to current 109c005e20dSKrzysztof Parzyszek // packet and the machine resource is marked as taken. If any dependency is 110c005e20dSKrzysztof Parzyszek // found, a target API call is made to prune the dependence. 1117a35faeaSAndrew Trick class VLIWPacketizerList { 11294212168SSirish Pande protected: 113b9338e7fSKrzysztof Parzyszek MachineFunction &MF; 1147a35faeaSAndrew Trick const TargetInstrInfo *TII; 1151d7b4136SReid Kleckner AAResults *AA; 1167a35faeaSAndrew Trick 11794212168SSirish Pande // The VLIW Scheduler. 11894212168SSirish Pande DefaultVLIWScheduler *VLIWScheduler; 1197a35faeaSAndrew Trick // Vector of instructions assigned to the current packet. 1207a35faeaSAndrew Trick std::vector<MachineInstr*> CurrentPacketMIs; 1217a35faeaSAndrew Trick // DFA resource tracker. 1227a35faeaSAndrew Trick DFAPacketizer *ResourceTracker; 123c005e20dSKrzysztof Parzyszek // Map: MI -> SU. 12494212168SSirish Pande std::map<MachineInstr*, SUnit*> MIToSUnit; 1257a35faeaSAndrew Trick 1267a35faeaSAndrew Trick public: 1271d7b4136SReid Kleckner // The AAResults parameter can be nullptr. 128dac71028SKrzysztof Parzyszek VLIWPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI, 1291d7b4136SReid Kleckner AAResults *AA); 1307a35faeaSAndrew Trick 1317a35faeaSAndrew Trick virtual ~VLIWPacketizerList(); 1327a35faeaSAndrew Trick 133c005e20dSKrzysztof Parzyszek // Implement this API in the backend to bundle instructions. 1347a35faeaSAndrew Trick void PacketizeMIs(MachineBasicBlock *MBB, 1357a35faeaSAndrew Trick MachineBasicBlock::iterator BeginItr, 1367a35faeaSAndrew Trick MachineBasicBlock::iterator EndItr); 1377a35faeaSAndrew Trick 138c005e20dSKrzysztof Parzyszek // Return the ResourceTracker. getResourceTracker()1397a35faeaSAndrew Trick DFAPacketizer *getResourceTracker() {return ResourceTracker;} 1407a35faeaSAndrew Trick 1417a35faeaSAndrew Trick // addToPacket - Add MI to the current packet. addToPacket(MachineInstr & MI)14257022878SDuncan P. N. Exon Smith virtual MachineBasicBlock::iterator addToPacket(MachineInstr &MI) { 14357022878SDuncan P. N. Exon Smith CurrentPacketMIs.push_back(&MI); 14494212168SSirish Pande ResourceTracker->reserveResources(MI); 14557022878SDuncan P. N. Exon Smith return MI; 14694212168SSirish Pande } 1477a35faeaSAndrew Trick 1488d73bdb3SKrzysztof Parzyszek // End the current packet and reset the state of the packetizer. 1498d73bdb3SKrzysztof Parzyszek // Overriding this function allows the target-specific packetizer 1508d73bdb3SKrzysztof Parzyszek // to perform custom finalization. 15157022878SDuncan P. N. Exon Smith virtual void endPacket(MachineBasicBlock *MBB, 15257022878SDuncan P. N. Exon Smith MachineBasicBlock::iterator MI); 15394212168SSirish Pande 154c005e20dSKrzysztof Parzyszek // Perform initialization before packetizing an instruction. This 155c005e20dSKrzysztof Parzyszek // function is supposed to be overrided by the target dependent packetizer. initPacketizerState()156c005e20dSKrzysztof Parzyszek virtual void initPacketizerState() {} 1577a35faeaSAndrew Trick 158c005e20dSKrzysztof Parzyszek // Check if the given instruction I should be ignored by the packetizer. ignorePseudoInstruction(const MachineInstr & I,const MachineBasicBlock * MBB)15957022878SDuncan P. N. Exon Smith virtual bool ignorePseudoInstruction(const MachineInstr &I, 160d44a1fd5SKrzysztof Parzyszek const MachineBasicBlock *MBB) { 16194212168SSirish Pande return false; 16294212168SSirish Pande } 1637a35faeaSAndrew Trick 164c005e20dSKrzysztof Parzyszek // Return true if instruction MI can not be packetized with any other 165c005e20dSKrzysztof Parzyszek // instruction, which means that MI itself is a packet. isSoloInstruction(const MachineInstr & MI)16657022878SDuncan P. N. Exon Smith virtual bool isSoloInstruction(const MachineInstr &MI) { return true; } 1677a35faeaSAndrew Trick 1682005d7dcSKrzysztof Parzyszek // Check if the packetizer should try to add the given instruction to 1692005d7dcSKrzysztof Parzyszek // the current packet. One reasons for which it may not be desirable 1702005d7dcSKrzysztof Parzyszek // to include an instruction in the current packet could be that it 1712005d7dcSKrzysztof Parzyszek // would cause a stall. 1722005d7dcSKrzysztof Parzyszek // If this function returns "false", the current packet will be ended, 1732005d7dcSKrzysztof Parzyszek // and the instruction will be added to the next packet. shouldAddToPacket(const MachineInstr & MI)17457022878SDuncan P. N. Exon Smith virtual bool shouldAddToPacket(const MachineInstr &MI) { return true; } 1752005d7dcSKrzysztof Parzyszek 176c005e20dSKrzysztof Parzyszek // Check if it is legal to packetize SUI and SUJ together. isLegalToPacketizeTogether(SUnit * SUI,SUnit * SUJ)1777a35faeaSAndrew Trick virtual bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) { 1787a35faeaSAndrew Trick return false; 1797a35faeaSAndrew Trick } 1807a35faeaSAndrew Trick 181c005e20dSKrzysztof Parzyszek // Check if it is legal to prune dependece between SUI and SUJ. isLegalToPruneDependencies(SUnit * SUI,SUnit * SUJ)1827a35faeaSAndrew Trick virtual bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) { 1837a35faeaSAndrew Trick return false; 1847a35faeaSAndrew Trick } 1851a1d78b8SKrzysztof Parzyszek 1861a1d78b8SKrzysztof Parzyszek // Add a DAG mutation to be done before the packetization begins. 1871a1d78b8SKrzysztof Parzyszek void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation); 1889d19c8caSKrzysztof Parzyszek 1899d19c8caSKrzysztof Parzyszek bool alias(const MachineInstr &MI1, const MachineInstr &MI2, 1909d19c8caSKrzysztof Parzyszek bool UseTBAA = true) const; 1919d19c8caSKrzysztof Parzyszek 1929d19c8caSKrzysztof Parzyszek private: 1939d19c8caSKrzysztof Parzyszek bool alias(const MachineMemOperand &Op1, const MachineMemOperand &Op2, 1949d19c8caSKrzysztof Parzyszek bool UseTBAA = true) const; 1957a35faeaSAndrew Trick }; 196c005e20dSKrzysztof Parzyszek 1976ac7a348SEugene Zelenko } // end namespace llvm 19808ebdc1eSAnshuman Dasgupta 1996ac7a348SEugene Zelenko #endif // LLVM_CODEGEN_DFAPACKETIZER_H 200