Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 1 | //===- HexagonPacketizer.cpp - VLIW packetizer ----------------------------===// |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This implements a simple VLIW packetizer using DFA. The packetizer works on |
| 10 | // machine basic blocks. For each instruction I in BB, the packetizer consults |
| 11 | // the DFA to see if machine resources are available to execute I. If so, the |
| 12 | // packetizer checks if I depends on any instruction J in the current packet. |
| 13 | // If no dependency is found, I is added to current packet and machine resource |
| 14 | // is marked as taken. If any dependency is found, a target API call is made to |
| 15 | // prune the dependence. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "HexagonVLIWPacketizer.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 20 | #include "Hexagon.h" |
| 21 | #include "HexagonInstrInfo.h" |
Jyotsna Verma | 8425643 | 2013-03-01 17:37:13 +0000 | [diff] [blame] | 22 | #include "HexagonRegisterInfo.h" |
| 23 | #include "HexagonSubtarget.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/BitVector.h" |
| 25 | #include "llvm/ADT/DenseSet.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
James Molloy | b6c7fce6 | 2019-09-09 13:17:55 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 30 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 33 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineInstr.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineOperand.h" |
| 39 | #include "llvm/CodeGen/ScheduleDAG.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 41 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 42 | #include "llvm/IR/DebugLoc.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 43 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCInstrDesc.h" |
| 45 | #include "llvm/Pass.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ErrorHandling.h" |
| 49 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 50 | #include <cassert> |
| 51 | #include <cstdint> |
| 52 | #include <iterator> |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace llvm; |
| 55 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "packets" |
| 57 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 58 | static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden, |
| 59 | cl::ZeroOrMore, cl::init(false), |
| 60 | cl::desc("Disable Hexagon packetizer pass")); |
| 61 | |
Benjamin Kramer | dc5f805 | 2019-08-23 19:59:23 +0000 | [diff] [blame] | 62 | static cl::opt<bool> Slot1Store("slot1-store-slot0-load", cl::Hidden, |
| 63 | cl::ZeroOrMore, cl::init(true), |
| 64 | cl::desc("Allow slot1 store and slot0 load")); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 65 | |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 66 | static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles", |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 67 | cl::ZeroOrMore, cl::Hidden, cl::init(true), |
| 68 | cl::desc("Allow non-solo packetization of volatile memory references")); |
| 69 | |
| 70 | static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false), |
| 71 | cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC")); |
| 72 | |
| 73 | static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores", |
| 74 | cl::init(false), cl::Hidden, cl::ZeroOrMore, |
| 75 | cl::desc("Disable vector double new-value-stores")); |
| 76 | |
| 77 | extern cl::opt<bool> ScheduleInlineAsm; |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 78 | |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 79 | namespace llvm { |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 80 | |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 81 | FunctionPass *createHexagonPacketizer(bool Minimal); |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 82 | void initializeHexagonPacketizerPass(PassRegistry&); |
| 83 | |
| 84 | } // end namespace llvm |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 85 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 86 | namespace { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 87 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 88 | class HexagonPacketizer : public MachineFunctionPass { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 89 | public: |
| 90 | static char ID; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 91 | |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 92 | HexagonPacketizer(bool Min = false) |
| 93 | : MachineFunctionPass(ID), Minimal(Min) {} |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 94 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 95 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 96 | AU.setPreservesCFG(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 97 | AU.addRequired<AAResultsWrapperPass>(); |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 98 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 99 | AU.addRequired<MachineDominatorTree>(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 100 | AU.addRequired<MachineLoopInfo>(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 101 | AU.addPreserved<MachineDominatorTree>(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 102 | AU.addPreserved<MachineLoopInfo>(); |
| 103 | MachineFunctionPass::getAnalysisUsage(AU); |
| 104 | } |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 105 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 106 | StringRef getPassName() const override { return "Hexagon Packetizer"; } |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 107 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 108 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 109 | MachineFunctionProperties getRequiredProperties() const override { |
| 110 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 111 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 112 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | const HexagonInstrInfo *HII; |
| 116 | const HexagonRegisterInfo *HRI; |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 117 | const bool Minimal; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 118 | }; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 119 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 120 | } // end anonymous namespace |
| 121 | |
| 122 | char HexagonPacketizer::ID = 0; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 123 | |
Krzysztof Parzyszek | df4a05d | 2017-07-10 18:38:52 +0000 | [diff] [blame] | 124 | INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer", |
| 125 | "Hexagon Packetizer", false, false) |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 126 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 127 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 128 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 129 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Krzysztof Parzyszek | df4a05d | 2017-07-10 18:38:52 +0000 | [diff] [blame] | 130 | INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer", |
| 131 | "Hexagon Packetizer", false, false) |
Jyotsna Verma | 1d29750 | 2013-05-02 15:39:30 +0000 | [diff] [blame] | 132 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 133 | HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF, |
Reid Kleckner | 1d7b413 | 2019-10-19 00:22:07 +0000 | [diff] [blame] | 134 | MachineLoopInfo &MLI, AAResults *AA, |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 135 | const MachineBranchProbabilityInfo *MBPI, bool Minimal) |
| 136 | : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI), |
| 137 | Minimal(Minimal) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 138 | HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); |
| 139 | HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo(); |
Krzysztof Parzyszek | 9be6673 | 2016-07-15 17:48:09 +0000 | [diff] [blame] | 140 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 141 | addMutation(std::make_unique<HexagonSubtarget::UsrOverflowMutation>()); |
| 142 | addMutation(std::make_unique<HexagonSubtarget::HVXMemLatencyMutation>()); |
| 143 | addMutation(std::make_unique<HexagonSubtarget::BankConflictMutation>()); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 146 | // Check if FirstI modifies a register that SecondI reads. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 147 | static bool hasWriteToReadDep(const MachineInstr &FirstI, |
| 148 | const MachineInstr &SecondI, |
| 149 | const TargetRegisterInfo *TRI) { |
| 150 | for (auto &MO : FirstI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 151 | if (!MO.isReg() || !MO.isDef()) |
| 152 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 153 | Register R = MO.getReg(); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 154 | if (SecondI.readsRegister(R, TRI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 161 | static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 162 | MachineBasicBlock::iterator BundleIt, bool Before) { |
| 163 | MachineBasicBlock::instr_iterator InsertPt; |
| 164 | if (Before) |
Duncan P. N. Exon Smith | d84f600 | 2016-02-22 21:30:15 +0000 | [diff] [blame] | 165 | InsertPt = BundleIt.getInstrIterator(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 166 | else |
Duncan P. N. Exon Smith | d84f600 | 2016-02-22 21:30:15 +0000 | [diff] [blame] | 167 | InsertPt = std::next(BundleIt).getInstrIterator(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 168 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 169 | MachineBasicBlock &B = *MI.getParent(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 170 | // The instruction should at least be bundled with the preceding instruction |
| 171 | // (there will always be one, i.e. BUNDLE, if nothing else). |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 172 | assert(MI.isBundledWithPred()); |
| 173 | if (MI.isBundledWithSucc()) { |
| 174 | MI.clearFlag(MachineInstr::BundledSucc); |
| 175 | MI.clearFlag(MachineInstr::BundledPred); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 176 | } else { |
| 177 | // If it's not bundled with the successor (i.e. it is the last one |
| 178 | // in the bundle), then we can simply unbundle it from the predecessor, |
| 179 | // which will take care of updating the predecessor's flag. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 180 | MI.unbundleFromPred(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 181 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 182 | B.splice(InsertPt, &B, MI.getIterator()); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 183 | |
| 184 | // Get the size of the bundle without asserting. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 185 | MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 186 | MachineBasicBlock::const_instr_iterator E = B.instr_end(); |
| 187 | unsigned Size = 0; |
| 188 | for (++I; I != E && I->isBundledWithPred(); ++I) |
| 189 | ++Size; |
| 190 | |
| 191 | // If there are still two or more instructions, then there is nothing |
| 192 | // else to be done. |
| 193 | if (Size > 1) |
| 194 | return BundleIt; |
| 195 | |
| 196 | // Otherwise, extract the single instruction out and delete the bundle. |
| 197 | MachineBasicBlock::iterator NextIt = std::next(BundleIt); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 198 | MachineInstr &SingleI = *BundleIt->getNextNode(); |
| 199 | SingleI.unbundleFromPred(); |
| 200 | assert(!SingleI.isBundledWithSucc()); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 201 | BundleIt->eraseFromParent(); |
| 202 | return NextIt; |
| 203 | } |
| 204 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 205 | bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) { |
Krzysztof Parzyszek | 5d41cc1 | 2018-03-12 17:47:46 +0000 | [diff] [blame] | 206 | auto &HST = MF.getSubtarget<HexagonSubtarget>(); |
Krzysztof Parzyszek | 5d41cc1 | 2018-03-12 17:47:46 +0000 | [diff] [blame] | 207 | HII = HST.getInstrInfo(); |
| 208 | HRI = HST.getRegisterInfo(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 209 | auto &MLI = getAnalysis<MachineLoopInfo>(); |
| 210 | auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 211 | auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
| 212 | |
| 213 | if (EnableGenAllInsnClass) |
| 214 | HII->genAllInsnTimingClasses(MF); |
| 215 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 216 | // Instantiate the packetizer. |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 217 | bool MinOnly = Minimal || DisablePacketizer || !HST.usePackets() || |
| 218 | skipFunction(MF.getFunction()); |
| 219 | HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI, MinOnly); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 220 | |
| 221 | // DFA state table should not be empty. |
| 222 | assert(Packetizer.getResourceTracker() && "Empty DFA table!"); |
| 223 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 224 | // Loop over all basic blocks and remove KILL pseudo-instructions |
| 225 | // These instructions confuse the dependence analysis. Consider: |
| 226 | // D0 = ... (Insn 0) |
| 227 | // R0 = KILL R0, D0 (Insn 1) |
| 228 | // R0 = ... (Insn 2) |
| 229 | // Here, Insn 1 will result in the dependence graph not emitting an output |
| 230 | // dependence between Insn 0 and Insn 2. This can lead to incorrect |
| 231 | // packetization |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 232 | for (MachineBasicBlock &MB : MF) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 233 | auto End = MB.end(); |
| 234 | auto MI = MB.begin(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 235 | while (MI != End) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 236 | auto NextI = std::next(MI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 237 | if (MI->isKill()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 238 | MB.erase(MI); |
| 239 | End = MB.end(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 240 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 241 | MI = NextI; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | // Loop over all of the basic blocks. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 246 | for (auto &MB : MF) { |
| 247 | auto Begin = MB.begin(), End = MB.end(); |
| 248 | while (Begin != End) { |
Krzysztof Parzyszek | e3ec97b | 2017-05-24 13:43:42 +0000 | [diff] [blame] | 249 | // Find the first non-boundary starting from the end of the last |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 250 | // scheduling region. |
| 251 | MachineBasicBlock::iterator RB = Begin; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 252 | while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 253 | ++RB; |
Krzysztof Parzyszek | e3ec97b | 2017-05-24 13:43:42 +0000 | [diff] [blame] | 254 | // Find the first boundary starting from the beginning of the new |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 255 | // region. |
| 256 | MachineBasicBlock::iterator RE = RB; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 257 | while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 258 | ++RE; |
| 259 | // Add the scheduling boundary if it's not block end. |
| 260 | if (RE != End) |
| 261 | ++RE; |
| 262 | // If RB == End, then RE == End. |
| 263 | if (RB != End) |
| 264 | Packetizer.PacketizeMIs(&MB, RB, RE); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 265 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 266 | Begin = RE; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 270 | Packetizer.unpacketizeSoloInstrs(MF); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 271 | return true; |
| 272 | } |
| 273 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 274 | // Reserve resources for a constant extender. Trigger an assertion if the |
| 275 | // reservation fails. |
| 276 | void HexagonPacketizerList::reserveResourcesForConstExt() { |
| 277 | if (!tryAllocateResourcesForConstExt(true)) |
| 278 | llvm_unreachable("Resources not available"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 281 | bool HexagonPacketizerList::canReserveResourcesForConstExt() { |
| 282 | return tryAllocateResourcesForConstExt(false); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 285 | // Allocate resources (i.e. 4 bytes) for constant extender. If succeeded, |
| 286 | // return true, otherwise, return false. |
| 287 | bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) { |
| 288 | auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc()); |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 289 | bool Avail = ResourceTracker->canReserveResources(*ExtMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 290 | if (Reserve && Avail) |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 291 | ResourceTracker->reserveResources(*ExtMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 292 | MF.DeleteMachineInstr(ExtMI); |
| 293 | return Avail; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 296 | bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 297 | SDep::Kind DepType, unsigned DepReg) { |
| 298 | // Check for LR dependence. |
| 299 | if (DepReg == HRI->getRARegister()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 300 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 301 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 302 | if (HII->isDeallocRet(MI)) |
| 303 | if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 304 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 305 | |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 306 | // Call-like instructions can be packetized with preceding instructions |
| 307 | // that define registers implicitly used or modified by the call. Explicit |
| 308 | // uses are still prohibited, as in the case of indirect calls: |
| 309 | // r0 = ... |
| 310 | // J2_jumpr r0 |
| 311 | if (DepType == SDep::Data) { |
| 312 | for (const MachineOperand MO : MI.operands()) |
| 313 | if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit()) |
| 314 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return false; |
| 318 | } |
| 319 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 320 | static bool isRegDependence(const SDep::Kind DepType) { |
| 321 | return DepType == SDep::Data || DepType == SDep::Anti || |
| 322 | DepType == SDep::Output; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 325 | static bool isDirectJump(const MachineInstr &MI) { |
| 326 | return MI.getOpcode() == Hexagon::J2_jump; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 329 | static bool isSchedBarrier(const MachineInstr &MI) { |
| 330 | switch (MI.getOpcode()) { |
Colin LeMahieu | b882f2b | 2015-02-05 18:56:28 +0000 | [diff] [blame] | 331 | case Hexagon::Y2_barrier: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 337 | static bool isControlFlow(const MachineInstr &MI) { |
| 338 | return MI.getDesc().isTerminator() || MI.getDesc().isCall(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 341 | /// Returns true if the instruction modifies a callee-saved register. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 342 | static bool doesModifyCalleeSavedReg(const MachineInstr &MI, |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 343 | const TargetRegisterInfo *TRI) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 344 | const MachineFunction &MF = *MI.getParent()->getParent(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 345 | for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR) |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 346 | if (MI.modifiesRegister(*CSR, TRI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 347 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 351 | // Returns true if an instruction can be promoted to .new predicate or |
| 352 | // new-value store. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 353 | bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI, |
Krzysztof Parzyszek | 2a48059 | 2016-07-26 20:30:30 +0000 | [diff] [blame] | 354 | const TargetRegisterClass *NewRC) { |
| 355 | // Vector stores can be predicated, and can be new-value stores, but |
| 356 | // they cannot be predicated on a .new predicate value. |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 357 | if (NewRC == &Hexagon::PredRegsRegClass) { |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 358 | if (HII->isHVXVec(MI) && MI.mayStore()) |
Krzysztof Parzyszek | 2a48059 | 2016-07-26 20:30:30 +0000 | [diff] [blame] | 359 | return false; |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 360 | return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0; |
| 361 | } |
| 362 | // If the class is not PredRegs, it could only apply to new-value stores. |
| 363 | return HII->mayBeNewStore(MI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 366 | // Promote an instructiont to its .cur form. |
| 367 | // At this time, we have already made a call to canPromoteToDotCur and made |
| 368 | // sure that it can *indeed* be promoted. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 369 | bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 370 | SDep::Kind DepType, MachineBasicBlock::iterator &MII, |
| 371 | const TargetRegisterClass* RC) { |
| 372 | assert(DepType == SDep::Data); |
| 373 | int CurOpcode = HII->getDotCurOp(MI); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 374 | MI.setDesc(HII->get(CurOpcode)); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 378 | void HexagonPacketizerList::cleanUpDotCur() { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 379 | MachineInstr *MI = nullptr; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 380 | for (auto BI : CurrentPacketMIs) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 381 | LLVM_DEBUG(dbgs() << "Cleanup packet has "; BI->dump();); |
Krzysztof Parzyszek | 0a8043e | 2017-05-03 15:28:56 +0000 | [diff] [blame] | 382 | if (HII->isDotCurInst(*BI)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 383 | MI = BI; |
| 384 | continue; |
| 385 | } |
| 386 | if (MI) { |
| 387 | for (auto &MO : BI->operands()) |
| 388 | if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg()) |
| 389 | return; |
| 390 | } |
| 391 | } |
| 392 | if (!MI) |
| 393 | return; |
| 394 | // We did not find a use of the CUR, so de-cur it. |
Krzysztof Parzyszek | 0a8043e | 2017-05-03 15:28:56 +0000 | [diff] [blame] | 395 | MI->setDesc(HII->get(HII->getNonDotCurOp(*MI))); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 396 | LLVM_DEBUG(dbgs() << "Demoted CUR "; MI->dump();); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Check to see if an instruction can be dot cur. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 400 | bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 401 | const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII, |
| 402 | const TargetRegisterClass *RC) { |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 403 | if (!HII->isHVXVec(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 404 | return false; |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 405 | if (!HII->isHVXVec(*MII)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 406 | return false; |
| 407 | |
| 408 | // Already a dot new instruction. |
| 409 | if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI)) |
| 410 | return false; |
| 411 | |
| 412 | if (!HII->mayBeCurLoad(MI)) |
| 413 | return false; |
| 414 | |
| 415 | // The "cur value" cannot come from inline asm. |
| 416 | if (PacketSU->getInstr()->isInlineAsm()) |
| 417 | return false; |
| 418 | |
| 419 | // Make sure candidate instruction uses cur. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 420 | LLVM_DEBUG(dbgs() << "Can we DOT Cur Vector MI\n"; MI.dump(); |
| 421 | dbgs() << "in packet\n";); |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 422 | MachineInstr &MJ = *MII; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 423 | LLVM_DEBUG({ |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 424 | dbgs() << "Checking CUR against "; |
| 425 | MJ.dump(); |
| 426 | }); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 427 | Register DestReg = MI.getOperand(0).getReg(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 428 | bool FoundMatch = false; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 429 | for (auto &MO : MJ.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 430 | if (MO.isReg() && MO.getReg() == DestReg) |
| 431 | FoundMatch = true; |
| 432 | if (!FoundMatch) |
| 433 | return false; |
| 434 | |
| 435 | // Check for existing uses of a vector register within the packet which |
| 436 | // would be affected by converting a vector load into .cur formt. |
| 437 | for (auto BI : CurrentPacketMIs) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 438 | LLVM_DEBUG(dbgs() << "packet has "; BI->dump();); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 439 | if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo())) |
| 440 | return false; |
| 441 | } |
| 442 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 443 | LLVM_DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump();); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 444 | // We can convert the opcode into a .cur. |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | // Promote an instruction to its .new form. At this time, we have already |
| 449 | // made a call to canPromoteToDotNew and made sure that it can *indeed* be |
| 450 | // promoted. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 451 | bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 452 | SDep::Kind DepType, MachineBasicBlock::iterator &MII, |
| 453 | const TargetRegisterClass* RC) { |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 454 | assert(DepType == SDep::Data); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 455 | int NewOpcode; |
| 456 | if (RC == &Hexagon::PredRegsRegClass) |
| 457 | NewOpcode = HII->getDotNewPredOp(MI, MBPI); |
| 458 | else |
| 459 | NewOpcode = HII->getDotNewOp(MI); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 460 | MI.setDesc(HII->get(NewOpcode)); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 464 | bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) { |
Krzysztof Parzyszek | 143158b | 2017-03-06 17:03:16 +0000 | [diff] [blame] | 465 | int NewOpcode = HII->getDotOldOp(MI); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 466 | MI.setDesc(HII->get(NewOpcode)); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 470 | bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) { |
| 471 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 472 | switch (Opc) { |
| 473 | case Hexagon::S2_storerd_io: |
| 474 | case Hexagon::S2_storeri_io: |
| 475 | case Hexagon::S2_storerh_io: |
| 476 | case Hexagon::S2_storerb_io: |
| 477 | break; |
| 478 | default: |
| 479 | llvm_unreachable("Unexpected instruction"); |
| 480 | } |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 481 | unsigned FrameSize = MF.getFrameInfo().getStackSize(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 482 | MachineOperand &Off = MI.getOperand(1); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 483 | int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 484 | if (HII->isValidOffset(Opc, NewOff, HRI)) { |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 485 | Off.setImm(NewOff); |
| 486 | return true; |
| 487 | } |
| 488 | return false; |
| 489 | } |
| 490 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 491 | void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) { |
| 492 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 493 | switch (Opc) { |
| 494 | case Hexagon::S2_storerd_io: |
| 495 | case Hexagon::S2_storeri_io: |
| 496 | case Hexagon::S2_storerh_io: |
| 497 | case Hexagon::S2_storerb_io: |
| 498 | break; |
| 499 | default: |
| 500 | llvm_unreachable("Unexpected instruction"); |
| 501 | } |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 502 | unsigned FrameSize = MF.getFrameInfo().getStackSize(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 503 | MachineOperand &Off = MI.getOperand(1); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 504 | Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE); |
| 505 | } |
| 506 | |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 507 | /// Return true if we can update the offset in MI so that MI and MJ |
| 508 | /// can be packetized together. |
| 509 | bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) { |
| 510 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 511 | MachineInstr &MI = *SUI->getInstr(); |
| 512 | MachineInstr &MJ = *SUJ->getInstr(); |
| 513 | |
| 514 | unsigned BPI, OPI; |
| 515 | if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI)) |
| 516 | return false; |
| 517 | unsigned BPJ, OPJ; |
| 518 | if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ)) |
| 519 | return false; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 520 | Register Reg = MI.getOperand(BPI).getReg(); |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 521 | if (Reg != MJ.getOperand(BPJ).getReg()) |
| 522 | return false; |
| 523 | // Make sure that the dependences do not restrict adding MI to the packet. |
| 524 | // That is, ignore anti dependences, and make sure the only data dependence |
| 525 | // involves the specific register. |
| 526 | for (const auto &PI : SUI->Preds) |
| 527 | if (PI.getKind() != SDep::Anti && |
| 528 | (PI.getKind() != SDep::Data || PI.getReg() != Reg)) |
| 529 | return false; |
| 530 | int Incr; |
| 531 | if (!HII->getIncrementValue(MJ, Incr)) |
| 532 | return false; |
| 533 | |
| 534 | int64_t Offset = MI.getOperand(OPI).getImm(); |
Krzysztof Parzyszek | 0f983d6 | 2018-03-30 19:28:37 +0000 | [diff] [blame] | 535 | if (!HII->isValidOffset(MI.getOpcode(), Offset+Incr, HRI)) |
| 536 | return false; |
| 537 | |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 538 | MI.getOperand(OPI).setImm(Offset + Incr); |
| 539 | ChangedOffset = Offset; |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | /// Undo the changed offset. This is needed if the instruction cannot be |
| 544 | /// added to the current packet due to a different instruction. |
| 545 | void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) { |
| 546 | unsigned BP, OP; |
| 547 | if (!HII->getBaseAndOffsetPosition(MI, BP, OP)) |
| 548 | llvm_unreachable("Unable to find base and offset operands."); |
| 549 | MI.getOperand(OP).setImm(ChangedOffset); |
| 550 | } |
| 551 | |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 552 | enum PredicateKind { |
| 553 | PK_False, |
| 554 | PK_True, |
| 555 | PK_Unknown |
| 556 | }; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 557 | |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 558 | /// Returns true if an instruction is predicated on p0 and false if it's |
| 559 | /// predicated on !p0. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 560 | static PredicateKind getPredicateSense(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 561 | const HexagonInstrInfo *HII) { |
| 562 | if (!HII->isPredicated(MI)) |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 563 | return PK_Unknown; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 564 | if (HII->isPredicatedTrue(MI)) |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 565 | return PK_True; |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 566 | return PK_False; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 569 | static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 570 | const HexagonInstrInfo *HII) { |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 571 | assert(HII->isPostIncrement(MI) && "Not a post increment operation."); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 572 | #ifndef NDEBUG |
| 573 | // Post Increment means duplicates. Use dense map to find duplicates in the |
| 574 | // list. Caution: Densemap initializes with the minimum of 64 buckets, |
| 575 | // whereas there are at most 5 operands in the post increment. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 576 | DenseSet<unsigned> DefRegsSet; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 577 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 578 | if (MO.isReg() && MO.isDef()) |
| 579 | DefRegsSet.insert(MO.getReg()); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 580 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 581 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 582 | if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg())) |
| 583 | return MO; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 584 | #else |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 585 | if (MI.mayLoad()) { |
| 586 | const MachineOperand &Op1 = MI.getOperand(1); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 587 | // The 2nd operand is always the post increment operand in load. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 588 | assert(Op1.isReg() && "Post increment operand has be to a register."); |
| 589 | return Op1; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 590 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 591 | if (MI.getDesc().mayStore()) { |
| 592 | const MachineOperand &Op0 = MI.getOperand(0); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 593 | // The 1st operand is always the post increment operand in store. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 594 | assert(Op0.isReg() && "Post increment operand has be to a register."); |
| 595 | return Op0; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 596 | } |
| 597 | #endif |
| 598 | // we should never come here. |
| 599 | llvm_unreachable("mayLoad or mayStore not set for Post Increment operation"); |
| 600 | } |
| 601 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 602 | // Get the value being stored. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 603 | static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 604 | // value being stored is always the last operand. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 605 | return MI.getOperand(MI.getNumOperands()-1); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 608 | static bool isLoadAbsSet(const MachineInstr &MI) { |
| 609 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 610 | switch (Opc) { |
| 611 | case Hexagon::L4_loadrd_ap: |
| 612 | case Hexagon::L4_loadrb_ap: |
| 613 | case Hexagon::L4_loadrh_ap: |
| 614 | case Hexagon::L4_loadrub_ap: |
| 615 | case Hexagon::L4_loadruh_ap: |
| 616 | case Hexagon::L4_loadri_ap: |
| 617 | return true; |
| 618 | } |
| 619 | return false; |
| 620 | } |
| 621 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 622 | static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 623 | assert(isLoadAbsSet(MI)); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 624 | return MI.getOperand(1); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 627 | // Can be new value store? |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 628 | // Following restrictions are to be respected in convert a store into |
| 629 | // a new value store. |
| 630 | // 1. If an instruction uses auto-increment, its address register cannot |
| 631 | // be a new-value register. Arch Spec 5.4.2.1 |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 632 | // 2. If an instruction uses absolute-set addressing mode, its address |
| 633 | // register cannot be a new-value register. Arch Spec 5.4.2.1. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 634 | // 3. If an instruction produces a 64-bit result, its registers cannot be used |
| 635 | // as new-value registers. Arch Spec 5.4.2.2. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 636 | // 4. If the instruction that sets the new-value register is conditional, then |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 637 | // the instruction that uses the new-value register must also be conditional, |
| 638 | // and both must always have their predicates evaluate identically. |
| 639 | // Arch Spec 5.4.2.3. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 640 | // 5. There is an implied restriction that a packet cannot have another store, |
| 641 | // if there is a new value store in the packet. Corollary: if there is |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 642 | // already a store in a packet, there can not be a new value store. |
| 643 | // Arch Spec: 3.4.4.2 |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 644 | bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI, |
| 645 | const MachineInstr &PacketMI, unsigned DepReg) { |
Jyotsna Verma | 438cec5 | 2013-05-10 20:58:11 +0000 | [diff] [blame] | 646 | // Make sure we are looking at the store, that can be promoted. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 647 | if (!HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 648 | return false; |
| 649 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 650 | // Make sure there is dependency and can be new value'd. |
| 651 | const MachineOperand &Val = getStoreValueOperand(MI); |
| 652 | if (Val.isReg() && Val.getReg() != DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 653 | return false; |
| 654 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 655 | const MCInstrDesc& MCID = PacketMI.getDesc(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 656 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 657 | // First operand is always the result. |
| 658 | const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF); |
| 659 | // Double regs can not feed into new value store: PRM section: 5.4.2.2. |
| 660 | if (PacketRC == &Hexagon::DoubleRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 661 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 662 | |
| 663 | // New-value stores are of class NV (slot 0), dual stores require class ST |
| 664 | // in slot 0 (PRM 5.5). |
| 665 | for (auto I : CurrentPacketMIs) { |
| 666 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
| 667 | if (PacketSU->getInstr()->mayStore()) |
| 668 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // Make sure it's NOT the post increment register that we are going to |
| 672 | // new value. |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 673 | if (HII->isPostIncrement(MI) && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 674 | getPostIncrementOperand(MI, HII).getReg() == DepReg) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 675 | return false; |
| 676 | } |
| 677 | |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 678 | if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 679 | getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) { |
| 680 | // If source is post_inc, or absolute-set addressing, it can not feed |
| 681 | // into new value store |
| 682 | // r3 = memw(r2++#4) |
| 683 | // memw(r30 + #-1404) = r2.new -> can not be new value store |
| 684 | // arch spec section: 5.4.2.1. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 688 | if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg) |
| 689 | return false; |
| 690 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 691 | // If the source that feeds the store is predicated, new value store must |
Jyotsna Verma | 438cec5 | 2013-05-10 20:58:11 +0000 | [diff] [blame] | 692 | // also be predicated. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 693 | if (HII->isPredicated(PacketMI)) { |
| 694 | if (!HII->isPredicated(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 695 | return false; |
| 696 | |
| 697 | // Check to make sure that they both will have their predicates |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 698 | // evaluate identically. |
Sirish Pande | 95d0117 | 2012-05-11 20:00:34 +0000 | [diff] [blame] | 699 | unsigned predRegNumSrc = 0; |
| 700 | unsigned predRegNumDst = 0; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 701 | const TargetRegisterClass* predRegClass = nullptr; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 702 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 703 | // Get predicate register used in the source instruction. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 704 | for (auto &MO : PacketMI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 705 | if (!MO.isReg()) |
| 706 | continue; |
| 707 | predRegNumSrc = MO.getReg(); |
| 708 | predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc); |
| 709 | if (predRegClass == &Hexagon::PredRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 710 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 711 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 712 | assert((predRegClass == &Hexagon::PredRegsRegClass) && |
| 713 | "predicate register not found in a predicated PacketMI instruction"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 714 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 715 | // Get predicate register used in new-value store instruction. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 716 | for (auto &MO : MI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 717 | if (!MO.isReg()) |
| 718 | continue; |
| 719 | predRegNumDst = MO.getReg(); |
| 720 | predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst); |
| 721 | if (predRegClass == &Hexagon::PredRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 722 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 723 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 724 | assert((predRegClass == &Hexagon::PredRegsRegClass) && |
| 725 | "predicate register not found in a predicated MI instruction"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 726 | |
| 727 | // New-value register producer and user (store) need to satisfy these |
| 728 | // constraints: |
| 729 | // 1) Both instructions should be predicated on the same register. |
| 730 | // 2) If producer of the new-value register is .new predicated then store |
| 731 | // should also be .new predicated and if producer is not .new predicated |
| 732 | // then store should not be .new predicated. |
| 733 | // 3) Both new-value register producer and user should have same predicate |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 734 | // sense, i.e, either both should be negated or both should be non-negated. |
| 735 | if (predRegNumDst != predRegNumSrc || |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 736 | HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) || |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 737 | getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 738 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | // Make sure that other than the new-value register no other store instruction |
| 742 | // register has been modified in the same packet. Predicate registers can be |
| 743 | // modified by they should not be modified between the producer and the store |
| 744 | // instruction as it will make them both conditional on different values. |
| 745 | // We already know this to be true for all the instructions before and |
| 746 | // including PacketMI. Howerver, we need to perform the check for the |
| 747 | // remaining instructions in the packet. |
| 748 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 749 | unsigned StartCheck = 0; |
| 750 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 751 | for (auto I : CurrentPacketMIs) { |
| 752 | SUnit *TempSU = MIToSUnit.find(I)->second; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 753 | MachineInstr &TempMI = *TempSU->getInstr(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 754 | |
| 755 | // Following condition is true for all the instructions until PacketMI is |
| 756 | // reached (StartCheck is set to 0 before the for loop). |
| 757 | // StartCheck flag is 1 for all the instructions after PacketMI. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 758 | if (&TempMI != &PacketMI && !StartCheck) // Start processing only after |
| 759 | continue; // encountering PacketMI. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 760 | |
| 761 | StartCheck = 1; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 762 | if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 763 | continue; |
| 764 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 765 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 766 | if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 767 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 770 | // Make sure that for non-POST_INC stores: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 771 | // 1. The only use of reg is DepReg and no other registers. |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 772 | // This handles base+index registers. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 773 | // The following store can not be dot new. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 774 | // Eg. r0 = add(r0, #3) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 775 | // memw(r1+r0<<#2) = r0 |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 776 | if (!HII->isPostIncrement(MI)) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 777 | for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) { |
| 778 | const MachineOperand &MO = MI.getOperand(opNum); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 779 | if (MO.isReg() && MO.getReg() == DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 780 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 781 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // If data definition is because of implicit definition of the register, |
| 785 | // do not newify the store. Eg. |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 786 | // %r9 = ZXTH %r12, implicit %d6, implicit-def %r12 |
| 787 | // S2_storerh_io %r8, 2, killed %r12; mem:ST2[%scevgep343] |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 788 | for (auto &MO : PacketMI.operands()) { |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 789 | if (MO.isRegMask() && MO.clobbersPhysReg(DepReg)) |
| 790 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 791 | if (!MO.isReg() || !MO.isDef() || !MO.isImplicit()) |
| 792 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 793 | Register R = MO.getReg(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 794 | if (R == DepReg || HRI->isSuperRegister(DepReg, R)) |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | // Handle imp-use of super reg case. There is a target independent side |
| 799 | // change that should prevent this situation but I am handling it for |
| 800 | // just-in-case. For example, we cannot newify R2 in the following case: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 801 | // %r3 = A2_tfrsi 0; |
| 802 | // S2_storeri_io killed %r0, 0, killed %r2, implicit killed %d1; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 803 | for (auto &MO : MI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 804 | if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg) |
| 805 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Can be dot new store. |
| 809 | return true; |
| 810 | } |
| 811 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 812 | // Can this MI to promoted to either new value store or new value jump. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 813 | bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 814 | const SUnit *PacketSU, unsigned DepReg, |
| 815 | MachineBasicBlock::iterator &MII) { |
| 816 | if (!HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 817 | return false; |
| 818 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 819 | // Check to see the store can be new value'ed. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 820 | MachineInstr &PacketMI = *PacketSU->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 821 | if (canPromoteToNewValueStore(MI, PacketMI, DepReg)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 822 | return true; |
| 823 | |
| 824 | // Check to see the compare/jump can be new value'ed. |
| 825 | // This is done as a pass on its own. Don't need to check it here. |
| 826 | return false; |
| 827 | } |
| 828 | |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 829 | static bool isImplicitDependency(const MachineInstr &I, bool CheckDef, |
| 830 | unsigned DepReg) { |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 831 | for (auto &MO : I.operands()) { |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 832 | if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg)) |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 833 | return true; |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 834 | if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit()) |
| 835 | continue; |
| 836 | if (CheckDef == MO.isDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 837 | return true; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 838 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 842 | // Check to see if an instruction can be dot new. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 843 | bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 844 | const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII, |
| 845 | const TargetRegisterClass* RC) { |
Jyotsna Verma | a46059b | 2013-03-28 19:44:04 +0000 | [diff] [blame] | 846 | // Already a dot new instruction. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 847 | if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 848 | return false; |
| 849 | |
Krzysztof Parzyszek | 2a48059 | 2016-07-26 20:30:30 +0000 | [diff] [blame] | 850 | if (!isNewifiable(MI, RC)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 851 | return false; |
| 852 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 853 | const MachineInstr &PI = *PacketSU->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 854 | |
| 855 | // The "new value" cannot come from inline asm. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 856 | if (PI.isInlineAsm()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 857 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 858 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 859 | // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no |
| 860 | // sense. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 861 | if (PI.isImplicitDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 862 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 863 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 864 | // If dependency is trough an implicitly defined register, we should not |
| 865 | // newify the use. |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 866 | if (isImplicitDependency(PI, true, DepReg) || |
| 867 | isImplicitDependency(MI, false, DepReg)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 868 | return false; |
| 869 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 870 | const MCInstrDesc& MCID = PI.getDesc(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 871 | const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 872 | if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 873 | return false; |
| 874 | |
| 875 | // predicate .new |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 876 | if (RC == &Hexagon::PredRegsRegClass) |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 877 | return HII->predCanBeUsedAsDotNew(PI, DepReg); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 878 | |
| 879 | if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI)) |
| 880 | return false; |
| 881 | |
| 882 | // Create a dot new machine instruction to see if resources can be |
| 883 | // allocated. If not, bail out now. |
| 884 | int NewOpcode = HII->getDotNewOp(MI); |
| 885 | const MCInstrDesc &D = HII->get(NewOpcode); |
| 886 | MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc()); |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 887 | bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 888 | MF.DeleteMachineInstr(NewMI); |
| 889 | if (!ResourcesAvailable) |
| 890 | return false; |
| 891 | |
| 892 | // New Value Store only. New Value Jump generated as a separate pass. |
| 893 | if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII)) |
| 894 | return false; |
| 895 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 896 | return true; |
| 897 | } |
| 898 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 899 | // Go through the packet instructions and search for an anti dependency between |
| 900 | // them and DepReg from MI. Consider this case: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 901 | // Trying to add |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 902 | // a) %r1 = TFRI_cdNotPt %p3, 2 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 903 | // to this packet: |
| 904 | // { |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 905 | // b) %p0 = C2_or killed %p3, killed %p0 |
| 906 | // c) %p3 = C2_tfrrp %r23 |
| 907 | // d) %r1 = C2_cmovenewit %p3, 4 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 908 | // } |
| 909 | // The P3 from a) and d) will be complements after |
| 910 | // a)'s P3 is converted to .new form |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 911 | // Anti-dep between c) and b) is irrelevant for this case |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 912 | bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 913 | unsigned DepReg) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 914 | SUnit *PacketSUDep = MIToSUnit.find(&MI)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 915 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 916 | for (auto I : CurrentPacketMIs) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 917 | // We only care for dependencies to predicated instructions |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 918 | if (!HII->isPredicated(*I)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 919 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 920 | |
| 921 | // Scheduling Unit for current insn in the packet |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 922 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 923 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 924 | // Look at dependencies between current members of the packet and |
| 925 | // predicate defining instruction MI. Make sure that dependency is |
| 926 | // on the exact register we care about. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 927 | if (PacketSU->isSucc(PacketSUDep)) { |
| 928 | for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 929 | auto &Dep = PacketSU->Succs[i]; |
| 930 | if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti && |
| 931 | Dep.getReg() == DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 932 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | return false; |
| 938 | } |
| 939 | |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 940 | /// Gets the predicate register of a predicated instruction. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 941 | static unsigned getPredicatedRegister(MachineInstr &MI, |
Benjamin Kramer | e79beac | 2013-05-23 15:43:11 +0000 | [diff] [blame] | 942 | const HexagonInstrInfo *QII) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 943 | /// We use the following rule: The first predicate register that is a use is |
| 944 | /// the predicate register of a predicated instruction. |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 945 | assert(QII->isPredicated(MI) && "Must be predicated instruction"); |
| 946 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 947 | for (auto &Op : MI.operands()) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 948 | if (Op.isReg() && Op.getReg() && Op.isUse() && |
| 949 | Hexagon::PredRegsRegClass.contains(Op.getReg())) |
| 950 | return Op.getReg(); |
| 951 | } |
| 952 | |
| 953 | llvm_unreachable("Unknown instruction operand layout"); |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 954 | return 0; |
| 955 | } |
| 956 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 957 | // Given two predicated instructions, this function detects whether |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 958 | // the predicates are complements. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 959 | bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1, |
| 960 | MachineInstr &MI2) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 961 | // If we don't know the predicate sense of the instructions bail out early, we |
| 962 | // need it later. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 963 | if (getPredicateSense(MI1, HII) == PK_Unknown || |
| 964 | getPredicateSense(MI2, HII) == PK_Unknown) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 965 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 966 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 967 | // Scheduling unit for candidate. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 968 | SUnit *SU = MIToSUnit[&MI1]; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 969 | |
| 970 | // One corner case deals with the following scenario: |
| 971 | // Trying to add |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 972 | // a) %r24 = A2_tfrt %p0, %r25 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 973 | // to this packet: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 974 | // { |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 975 | // b) %r25 = A2_tfrf %p0, %r24 |
| 976 | // c) %p0 = C2_cmpeqi %r26, 1 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 977 | // } |
| 978 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 979 | // On general check a) and b) are complements, but presence of c) will |
| 980 | // convert a) to .new form, and then it is not a complement. |
| 981 | // We attempt to detect it by analyzing existing dependencies in the packet. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 982 | |
| 983 | // Analyze relationships between all existing members of the packet. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 984 | // Look for Anti dependecy on the same predicate reg as used in the |
| 985 | // candidate. |
| 986 | for (auto I : CurrentPacketMIs) { |
| 987 | // Scheduling Unit for current insn in the packet. |
| 988 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 989 | |
| 990 | // If this instruction in the packet is succeeded by the candidate... |
| 991 | if (PacketSU->isSucc(SU)) { |
| 992 | for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 993 | auto Dep = PacketSU->Succs[i]; |
| 994 | // The corner case exist when there is true data dependency between |
| 995 | // candidate and one of current packet members, this dep is on |
| 996 | // predicate reg, and there already exist anti dep on the same pred in |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 997 | // the packet. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 998 | if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data && |
| 999 | Hexagon::PredRegsRegClass.contains(Dep.getReg())) { |
| 1000 | // Here I know that I is predicate setting instruction with true |
| 1001 | // data dep to candidate on the register we care about - c) in the |
| 1002 | // above example. Now I need to see if there is an anti dependency |
| 1003 | // from c) to any other instruction in the same packet on the pred |
| 1004 | // reg of interest. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1005 | if (restrictingDepExistInPacket(*I, Dep.getReg())) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1006 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1012 | // If the above case does not apply, check regular complement condition. |
| 1013 | // Check that the predicate register is the same and that the predicate |
| 1014 | // sense is different We also need to differentiate .old vs. .new: !p0 |
| 1015 | // is not complementary to p0.new. |
| 1016 | unsigned PReg1 = getPredicatedRegister(MI1, HII); |
| 1017 | unsigned PReg2 = getPredicatedRegister(MI2, HII); |
| 1018 | return PReg1 == PReg2 && |
| 1019 | Hexagon::PredRegsRegClass.contains(PReg1) && |
| 1020 | Hexagon::PredRegsRegClass.contains(PReg2) && |
| 1021 | getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) && |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1022 | HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1025 | // Initialize packetizer flags. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1026 | void HexagonPacketizerList::initPacketizerState() { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1027 | Dependence = false; |
| 1028 | PromotedToDotNew = false; |
| 1029 | GlueToNewValueJump = false; |
| 1030 | GlueAllocframeStore = false; |
| 1031 | FoundSequentialDependence = false; |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1032 | ChangedOffset = INT64_MAX; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1035 | // Ignore bundling of pseudo instructions. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1036 | bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI, |
| 1037 | const MachineBasicBlock *) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1038 | if (MI.isDebugInstr()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1039 | return true; |
| 1040 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1041 | if (MI.isCFIInstruction()) |
Krzysztof Parzyszek | 6bbcb31 | 2015-04-22 15:47:35 +0000 | [diff] [blame] | 1042 | return false; |
| 1043 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1044 | // We must print out inline assembly. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1045 | if (MI.isInlineAsm()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1048 | if (MI.isImplicitDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1049 | return false; |
| 1050 | |
| 1051 | // We check if MI has any functional units mapped to it. If it doesn't, |
| 1052 | // we ignore the instruction. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1053 | const MCInstrDesc& TID = MI.getDesc(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1054 | auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass()); |
Hal Finkel | 8db5547 | 2012-06-22 20:27:13 +0000 | [diff] [blame] | 1055 | unsigned FuncUnits = IS->getUnits(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1056 | return !FuncUnits; |
| 1057 | } |
| 1058 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1059 | bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1060 | // Ensure any bundles created by gather packetize remain seperate. |
| 1061 | if (MI.isBundle()) |
| 1062 | return true; |
| 1063 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1064 | if (MI.isEHLabel() || MI.isCFIInstruction()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1065 | return true; |
| 1066 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1067 | // Consider inline asm to not be a solo instruction by default. |
| 1068 | // Inline asm will be put in a packet temporarily, but then it will be |
| 1069 | // removed, and placed outside of the packet (before or after, depending |
| 1070 | // on dependencies). This is to reduce the impact of inline asm as a |
| 1071 | // "packet splitting" instruction. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1072 | if (MI.isInlineAsm() && !ScheduleInlineAsm) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1073 | return true; |
| 1074 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1075 | if (isSchedBarrier(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1078 | if (HII->isSolo(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1079 | return true; |
| 1080 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1081 | if (MI.getOpcode() == Hexagon::A2_nop) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1082 | return true; |
| 1083 | |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1087 | // Quick check if instructions MI and MJ cannot coexist in the same packet. |
| 1088 | // Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm", |
| 1089 | // but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm". |
| 1090 | // For full test call this function twice: |
| 1091 | // cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI) |
| 1092 | // Doing the test only one way saves the amount of code in this function, |
| 1093 | // since every test would need to be repeated with the MI and MJ reversed. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1094 | static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1095 | const HexagonInstrInfo &HII) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1096 | const MachineFunction *MF = MI.getParent()->getParent(); |
Krzysztof Parzyszek | d8b780d | 2018-06-20 13:56:09 +0000 | [diff] [blame] | 1097 | if (MF->getSubtarget<HexagonSubtarget>().hasV60OpsOnly() && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1098 | HII.isHVXMemWithAIndirect(MI, MJ)) |
| 1099 | return true; |
| 1100 | |
| 1101 | // An inline asm cannot be together with a branch, because we may not be |
| 1102 | // able to remove the asm out after packetizing (i.e. if the asm must be |
| 1103 | // moved past the bundle). Similarly, two asms cannot be together to avoid |
| 1104 | // complications when determining their relative order outside of a bundle. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1105 | if (MI.isInlineAsm()) |
| 1106 | return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() || |
| 1107 | MJ.isCall() || MJ.isTerminator(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1108 | |
Krzysztof Parzyszek | f4ad2cb | 2018-09-04 21:07:27 +0000 | [diff] [blame] | 1109 | // New-value stores cannot coexist with any other stores. |
| 1110 | if (HII.isNewValueStore(MI) && MJ.mayStore()) |
| 1111 | return true; |
| 1112 | |
Krzysztof Parzyszek | 639545b | 2016-08-19 16:57:05 +0000 | [diff] [blame] | 1113 | switch (MI.getOpcode()) { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1114 | case Hexagon::S2_storew_locked: |
| 1115 | case Hexagon::S4_stored_locked: |
| 1116 | case Hexagon::L2_loadw_locked: |
| 1117 | case Hexagon::L4_loadd_locked: |
Krzysztof Parzyszek | 5c2944c | 2018-06-19 17:26:20 +0000 | [diff] [blame] | 1118 | case Hexagon::Y2_dccleana: |
| 1119 | case Hexagon::Y2_dccleaninva: |
| 1120 | case Hexagon::Y2_dcinva: |
| 1121 | case Hexagon::Y2_dczeroa: |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1122 | case Hexagon::Y4_l2fetch: |
| 1123 | case Hexagon::Y5_l2fetch: { |
Krzysztof Parzyszek | 639545b | 2016-08-19 16:57:05 +0000 | [diff] [blame] | 1124 | // These instructions can only be grouped with ALU32 or non-floating-point |
| 1125 | // XTYPE instructions. Since there is no convenient way of identifying fp |
| 1126 | // XTYPE instructions, only allow grouping with ALU32 for now. |
| 1127 | unsigned TJ = HII.getType(MJ); |
Krzysztof Parzyszek | 5ea971c | 2017-02-07 17:47:37 +0000 | [diff] [blame] | 1128 | if (TJ != HexagonII::TypeALU32_2op && |
| 1129 | TJ != HexagonII::TypeALU32_3op && |
| 1130 | TJ != HexagonII::TypeALU32_ADDI) |
Krzysztof Parzyszek | 639545b | 2016-08-19 16:57:05 +0000 | [diff] [blame] | 1131 | return true; |
| 1132 | break; |
| 1133 | } |
| 1134 | default: |
| 1135 | break; |
| 1136 | } |
| 1137 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1138 | // "False" really means that the quick check failed to determine if |
| 1139 | // I and J cannot coexist. |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1143 | // Full, symmetric check. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1144 | bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI, |
| 1145 | const MachineInstr &MJ) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1146 | return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII); |
| 1147 | } |
| 1148 | |
| 1149 | void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) { |
| 1150 | for (auto &B : MF) { |
| 1151 | MachineBasicBlock::iterator BundleIt; |
| 1152 | MachineBasicBlock::instr_iterator NextI; |
| 1153 | for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) { |
| 1154 | NextI = std::next(I); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1155 | MachineInstr &MI = *I; |
| 1156 | if (MI.isBundle()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1157 | BundleIt = I; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1158 | if (!MI.isInsideBundle()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1159 | continue; |
| 1160 | |
| 1161 | // Decide on where to insert the instruction that we are pulling out. |
| 1162 | // Debug instructions always go before the bundle, but the placement of |
| 1163 | // INLINE_ASM depends on potential dependencies. By default, try to |
| 1164 | // put it before the bundle, but if the asm writes to a register that |
| 1165 | // other instructions in the bundle read, then we need to place it |
| 1166 | // after the bundle (to preserve the bundle semantics). |
| 1167 | bool InsertBeforeBundle; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1168 | if (MI.isInlineAsm()) |
| 1169 | InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI); |
| 1170 | else if (MI.isDebugValue()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1171 | InsertBeforeBundle = true; |
| 1172 | else |
| 1173 | continue; |
| 1174 | |
| 1175 | BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // Check if a given instruction is of class "system". |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1181 | static bool isSystemInstr(const MachineInstr &MI) { |
| 1182 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1183 | switch (Opc) { |
| 1184 | case Hexagon::Y2_barrier: |
| 1185 | case Hexagon::Y2_dcfetchbo: |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1186 | case Hexagon::Y4_l2fetch: |
| 1187 | case Hexagon::Y5_l2fetch: |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1188 | return true; |
| 1189 | } |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1193 | bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I, |
| 1194 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1195 | // The dependence graph may not include edges between dead definitions, |
| 1196 | // so without extra checks, we could end up packetizing two instruction |
| 1197 | // defining the same (dead) register. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1198 | if (I.isCall() || J.isCall()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1199 | return false; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1200 | if (HII->isPredicated(I) || HII->isPredicated(J)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1201 | return false; |
| 1202 | |
| 1203 | BitVector DeadDefs(Hexagon::NUM_TARGET_REGS); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1204 | for (auto &MO : I.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1205 | if (!MO.isReg() || !MO.isDef() || !MO.isDead()) |
| 1206 | continue; |
| 1207 | DeadDefs[MO.getReg()] = true; |
| 1208 | } |
| 1209 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1210 | for (auto &MO : J.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1211 | if (!MO.isReg() || !MO.isDef() || !MO.isDead()) |
| 1212 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1213 | Register R = MO.getReg(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1214 | if (R != Hexagon::USR_OVF && DeadDefs[R]) |
| 1215 | return true; |
| 1216 | } |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1220 | bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I, |
| 1221 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1222 | // A save callee-save register function call can only be in a packet |
| 1223 | // with instructions that don't write to the callee-save registers. |
| 1224 | if ((HII->isSaveCalleeSavedRegsCall(I) && |
| 1225 | doesModifyCalleeSavedReg(J, HRI)) || |
| 1226 | (HII->isSaveCalleeSavedRegsCall(J) && |
| 1227 | doesModifyCalleeSavedReg(I, HRI))) |
| 1228 | return true; |
| 1229 | |
| 1230 | // Two control flow instructions cannot go in the same packet. |
| 1231 | if (isControlFlow(I) && isControlFlow(J)) |
| 1232 | return true; |
| 1233 | |
| 1234 | // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot |
| 1235 | // contain a speculative indirect jump, |
| 1236 | // a new-value compare jump or a dealloc_return. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1237 | auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool { |
| 1238 | if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1239 | return true; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1240 | if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1241 | return true; |
| 1242 | return false; |
| 1243 | }; |
| 1244 | |
| 1245 | if (HII->isLoopN(I) && isBadForLoopN(J)) |
| 1246 | return true; |
| 1247 | if (HII->isLoopN(J) && isBadForLoopN(I)) |
| 1248 | return true; |
| 1249 | |
| 1250 | // dealloc_return cannot appear in the same packet as a conditional or |
| 1251 | // unconditional jump. |
| 1252 | return HII->isDeallocRet(I) && |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1253 | (J.isBranch() || J.isCall() || J.isBarrier()); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1256 | bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I, |
| 1257 | const MachineInstr &J) { |
| 1258 | // Adding I to a packet that has J. |
| 1259 | |
| 1260 | // Regmasks are not reflected in the scheduling dependency graph, so |
| 1261 | // we need to check them manually. This code assumes that regmasks only |
| 1262 | // occur on calls, and the problematic case is when we add an instruction |
| 1263 | // defining a register R to a packet that has a call that clobbers R via |
| 1264 | // a regmask. Those cannot be packetized together, because the call will |
| 1265 | // be executed last. That's also a reson why it is ok to add a call |
| 1266 | // clobbering R to a packet that defines R. |
| 1267 | |
| 1268 | // Look for regmasks in J. |
| 1269 | for (const MachineOperand &OpJ : J.operands()) { |
| 1270 | if (!OpJ.isRegMask()) |
| 1271 | continue; |
| 1272 | assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call"); |
| 1273 | for (const MachineOperand &OpI : I.operands()) { |
| 1274 | if (OpI.isReg()) { |
| 1275 | if (OpJ.clobbersPhysReg(OpI.getReg())) |
| 1276 | return true; |
| 1277 | } else if (OpI.isRegMask()) { |
| 1278 | // Both are regmasks. Assume that they intersect. |
| 1279 | return true; |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 1286 | bool HexagonPacketizerList::hasDualStoreDependence(const MachineInstr &I, |
| 1287 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1288 | bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1289 | bool StoreI = I.mayStore(), StoreJ = J.mayStore(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1290 | if ((SysI && StoreJ) || (SysJ && StoreI)) |
| 1291 | return true; |
| 1292 | |
| 1293 | if (StoreI && StoreJ) { |
| 1294 | if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I)) |
| 1295 | return true; |
| 1296 | } else { |
| 1297 | // A memop cannot be in the same packet with another memop or a store. |
| 1298 | // Two stores can be together, but here I and J cannot both be stores. |
| 1299 | bool MopStI = HII->isMemOp(I) || StoreI; |
| 1300 | bool MopStJ = HII->isMemOp(J) || StoreJ; |
| 1301 | if (MopStI && MopStJ) |
| 1302 | return true; |
| 1303 | } |
| 1304 | |
| 1305 | return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J)); |
| 1306 | } |
| 1307 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1308 | // SUI is the current instruction that is out side of the current packet. |
| 1309 | // SUJ is the current instruction inside the current packet against which that |
| 1310 | // SUI will be packetized. |
| 1311 | bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1312 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 1313 | MachineInstr &I = *SUI->getInstr(); |
| 1314 | MachineInstr &J = *SUJ->getInstr(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1315 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1316 | // Clear IgnoreDepMIs when Packet starts. |
| 1317 | if (CurrentPacketMIs.size() == 1) |
| 1318 | IgnoreDepMIs.clear(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1319 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1320 | MachineBasicBlock::iterator II = I.getIterator(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1321 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1322 | // Solo instructions cannot go in the packet. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1323 | assert(!isSoloInstruction(I) && "Unexpected solo instr!"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1324 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1325 | if (cannotCoexist(I, J)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1326 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1327 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1328 | Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J); |
| 1329 | if (Dependence) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1330 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1331 | |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1332 | // Regmasks are not accounted for in the scheduling graph, so we need |
| 1333 | // to explicitly check for dependencies caused by them. They should only |
| 1334 | // appear on calls, so it's not too pessimistic to reject all regmask |
| 1335 | // dependencies. |
| 1336 | Dependence = hasRegMaskDependence(I, J); |
| 1337 | if (Dependence) |
| 1338 | return false; |
| 1339 | |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 1340 | // Dual-store does not allow second store, if the first store is not |
| 1341 | // in SLOT0. New value store, new value jump, dealloc_return and memop |
| 1342 | // always take SLOT0. Arch spec 3.4.4.2. |
| 1343 | Dependence = hasDualStoreDependence(I, J); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1344 | if (Dependence) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1345 | return false; |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1346 | |
| 1347 | // If an instruction feeds new value jump, glue it. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1348 | MachineBasicBlock::iterator NextMII = I.getIterator(); |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1349 | ++NextMII; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1350 | if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 1351 | MachineInstr &NextMI = *NextMII; |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1352 | |
| 1353 | bool secondRegMatch = false; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 1354 | const MachineOperand &NOp0 = NextMI.getOperand(0); |
| 1355 | const MachineOperand &NOp1 = NextMI.getOperand(1); |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1356 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1357 | if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg()) |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1358 | secondRegMatch = true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1359 | |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1360 | for (MachineInstr *PI : CurrentPacketMIs) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1361 | // NVJ can not be part of the dual jump - Arch Spec: section 7.8. |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1362 | if (PI->isCall()) { |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1363 | Dependence = true; |
| 1364 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1365 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1366 | // Validate: |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1367 | // 1. Packet does not have a store in it. |
| 1368 | // 2. If the first operand of the nvj is newified, and the second |
| 1369 | // operand is also a reg, it (second reg) is not defined in |
| 1370 | // the same packet. |
| 1371 | // 3. If the second operand of the nvj is newified, (which means |
| 1372 | // first operand is also a reg), first reg is not defined in |
| 1373 | // the same packet. |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1374 | if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() || |
| 1375 | HII->isLoopN(*PI)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1376 | Dependence = true; |
| 1377 | break; |
| 1378 | } |
| 1379 | // Check #2/#3. |
| 1380 | const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1; |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1381 | if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) { |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1382 | Dependence = true; |
| 1383 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1384 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1385 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1386 | |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1387 | GlueToNewValueJump = true; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1388 | if (Dependence) |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1389 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1392 | // There no dependency between a prolog instruction and its successor. |
| 1393 | if (!SUJ->isSucc(SUI)) |
| 1394 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1395 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1396 | for (unsigned i = 0; i < SUJ->Succs.size(); ++i) { |
| 1397 | if (FoundSequentialDependence) |
| 1398 | break; |
| 1399 | |
| 1400 | if (SUJ->Succs[i].getSUnit() != SUI) |
| 1401 | continue; |
| 1402 | |
| 1403 | SDep::Kind DepType = SUJ->Succs[i].getKind(); |
| 1404 | // For direct calls: |
| 1405 | // Ignore register dependences for call instructions for packetization |
| 1406 | // purposes except for those due to r31 and predicate registers. |
| 1407 | // |
| 1408 | // For indirect calls: |
| 1409 | // Same as direct calls + check for true dependences to the register |
| 1410 | // used in the indirect call. |
| 1411 | // |
| 1412 | // We completely ignore Order dependences for call instructions. |
| 1413 | // |
| 1414 | // For returns: |
| 1415 | // Ignore register dependences for return instructions like jumpr, |
| 1416 | // dealloc return unless we have dependencies on the explicit uses |
| 1417 | // of the registers used by jumpr (like r31) or dealloc return |
| 1418 | // (like r29 or r30). |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1419 | unsigned DepReg = 0; |
| 1420 | const TargetRegisterClass *RC = nullptr; |
| 1421 | if (DepType == SDep::Data) { |
| 1422 | DepReg = SUJ->Succs[i].getReg(); |
| 1423 | RC = HRI->getMinimalPhysRegClass(DepReg); |
| 1424 | } |
| 1425 | |
Krzysztof Parzyszek | 38e2ccc | 2016-08-23 16:01:01 +0000 | [diff] [blame] | 1426 | if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1427 | if (!isRegDependence(DepType)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1428 | continue; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1429 | if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg())) |
| 1430 | continue; |
| 1431 | } |
| 1432 | |
| 1433 | if (DepType == SDep::Data) { |
| 1434 | if (canPromoteToDotCur(J, SUJ, DepReg, II, RC)) |
| 1435 | if (promoteToDotCur(J, DepType, II, RC)) |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | // Data dpendence ok if we have load.cur. |
| 1440 | if (DepType == SDep::Data && HII->isDotCurInst(J)) { |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1441 | if (HII->isHVXVec(I)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1442 | continue; |
| 1443 | } |
| 1444 | |
| 1445 | // For instructions that can be promoted to dot-new, try to promote. |
| 1446 | if (DepType == SDep::Data) { |
| 1447 | if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) { |
| 1448 | if (promoteToDotNew(I, DepType, II, RC)) { |
| 1449 | PromotedToDotNew = true; |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1450 | if (cannotCoexist(I, J)) |
| 1451 | FoundSequentialDependence = true; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1452 | continue; |
| 1453 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1454 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1455 | if (HII->isNewValueJump(I)) |
| 1456 | continue; |
| 1457 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1458 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1459 | // For predicated instructions, if the predicates are complements then |
| 1460 | // there can be no dependence. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1461 | if (HII->isPredicated(I) && HII->isPredicated(J) && |
| 1462 | arePredicatesComplements(I, J)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1463 | // Not always safe to do this translation. |
| 1464 | // DAG Builder attempts to reduce dependence edges using transitive |
| 1465 | // nature of dependencies. Here is an example: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1466 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1467 | // r0 = tfr_pt ... (1) |
| 1468 | // r0 = tfr_pf ... (2) |
| 1469 | // r0 = tfr_pt ... (3) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1470 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1471 | // There will be an output dependence between (1)->(2) and (2)->(3). |
| 1472 | // However, there is no dependence edge between (1)->(3). This results |
| 1473 | // in all 3 instructions going in the same packet. We ignore dependce |
| 1474 | // only once to avoid this situation. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1475 | auto Itr = find(IgnoreDepMIs, &J); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1476 | if (Itr != IgnoreDepMIs.end()) { |
| 1477 | Dependence = true; |
| 1478 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1479 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1480 | IgnoreDepMIs.push_back(&I); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1481 | continue; |
| 1482 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1483 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1484 | // Ignore Order dependences between unconditional direct branches |
| 1485 | // and non-control-flow instructions. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1486 | if (isDirectJump(I) && !J.isBranch() && !J.isCall() && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1487 | DepType == SDep::Order) |
| 1488 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1489 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1490 | // Ignore all dependences for jumps except for true and output |
| 1491 | // dependences. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1492 | if (I.isConditionalBranch() && DepType != SDep::Data && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1493 | DepType != SDep::Output) |
| 1494 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1495 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1496 | if (DepType == SDep::Output) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1497 | FoundSequentialDependence = true; |
| 1498 | break; |
| 1499 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1500 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1501 | // For Order dependences: |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 1502 | // 1. Volatile loads/stores can be packetized together, unless other |
| 1503 | // rules prevent is. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1504 | // 2. Store followed by a load is not allowed. |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 1505 | // 3. Store followed by a store is valid. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1506 | // 4. Load followed by any memory operation is allowed. |
| 1507 | if (DepType == SDep::Order) { |
| 1508 | if (!PacketizeVolatiles) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1509 | bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1510 | if (OrdRefs) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1511 | FoundSequentialDependence = true; |
| 1512 | break; |
| 1513 | } |
| 1514 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1515 | // J is first, I is second. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1516 | bool LoadJ = J.mayLoad(), StoreJ = J.mayStore(); |
| 1517 | bool LoadI = I.mayLoad(), StoreI = I.mayStore(); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1518 | bool NVStoreJ = HII->isNewValueStore(J); |
| 1519 | bool NVStoreI = HII->isNewValueStore(I); |
| 1520 | bool IsVecJ = HII->isHVXVec(J); |
| 1521 | bool IsVecI = HII->isHVXVec(I); |
| 1522 | |
Krzysztof Parzyszek | d8b780d | 2018-06-20 13:56:09 +0000 | [diff] [blame] | 1523 | if (Slot1Store && MF.getSubtarget<HexagonSubtarget>().hasV65Ops() && |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1524 | ((LoadJ && StoreI && !NVStoreI) || |
| 1525 | (StoreJ && LoadI && !NVStoreJ)) && |
| 1526 | (J.getOpcode() != Hexagon::S2_allocframe && |
| 1527 | I.getOpcode() != Hexagon::S2_allocframe) && |
| 1528 | (J.getOpcode() != Hexagon::L2_deallocframe && |
| 1529 | I.getOpcode() != Hexagon::L2_deallocframe) && |
| 1530 | (!HII->isMemOp(J) && !HII->isMemOp(I)) && (!IsVecJ && !IsVecI)) |
| 1531 | setmemShufDisabled(true); |
| 1532 | else |
| 1533 | if (StoreJ && LoadI && alias(J, I)) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1534 | FoundSequentialDependence = true; |
| 1535 | break; |
| 1536 | } |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1537 | |
| 1538 | if (!StoreJ) |
| 1539 | if (!LoadJ || (!LoadI && !StoreI)) { |
| 1540 | // If J is neither load nor store, assume a dependency. |
| 1541 | // If J is a load, but I is neither, also assume a dependency. |
| 1542 | FoundSequentialDependence = true; |
| 1543 | break; |
| 1544 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1545 | // Store followed by store: not OK on V2. |
| 1546 | // Store followed by load: not OK on all. |
| 1547 | // Load followed by store: OK on all. |
| 1548 | // Load followed by load: OK on all. |
| 1549 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Krzysztof Parzyszek | 6bfc657 | 2018-10-19 17:31:11 +0000 | [diff] [blame] | 1552 | // Special case for ALLOCFRAME: even though there is dependency |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1553 | // between ALLOCFRAME and subsequent store, allow it to be packetized |
| 1554 | // in a same packet. This implies that the store is using the caller's |
| 1555 | // SP. Hence, offset needs to be updated accordingly. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1556 | if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) { |
| 1557 | unsigned Opc = I.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1558 | switch (Opc) { |
| 1559 | case Hexagon::S2_storerd_io: |
| 1560 | case Hexagon::S2_storeri_io: |
| 1561 | case Hexagon::S2_storerh_io: |
| 1562 | case Hexagon::S2_storerb_io: |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1563 | if (I.getOperand(0).getReg() == HRI->getStackRegister()) { |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1564 | // Since this store is to be glued with allocframe in the same |
| 1565 | // packet, it will use SP of the previous stack frame, i.e. |
| 1566 | // caller's SP. Therefore, we need to recalculate offset |
| 1567 | // according to this change. |
| 1568 | GlueAllocframeStore = useCallersSP(I); |
| 1569 | if (GlueAllocframeStore) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1570 | continue; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1571 | } |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 1572 | break; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1573 | default: |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1578 | // There are certain anti-dependencies that cannot be ignored. |
| 1579 | // Specifically: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 1580 | // J2_call ... implicit-def %r0 ; SUJ |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1581 | // R0 = ... ; SUI |
| 1582 | // Those cannot be packetized together, since the call will observe |
| 1583 | // the effect of the assignment to R0. |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1584 | if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) { |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1585 | // Check if I defines any volatile register. We should also check |
| 1586 | // registers that the call may read, but these happen to be a |
| 1587 | // subset of the volatile register set. |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1588 | for (const MachineOperand &Op : I.operands()) { |
| 1589 | if (Op.isReg() && Op.isDef()) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 1590 | Register R = Op.getReg(); |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1591 | if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI)) |
| 1592 | continue; |
| 1593 | } else if (!Op.isRegMask()) { |
| 1594 | // If I has a regmask assume dependency. |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1595 | continue; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1596 | } |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1597 | FoundSequentialDependence = true; |
| 1598 | break; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | // Skip over remaining anti-dependences. Two instructions that are |
| 1603 | // anti-dependent can share a packet, since in most such cases all |
| 1604 | // operands are read before any modifications take place. |
| 1605 | // The exceptions are branch and call instructions, since they are |
| 1606 | // executed after all other instructions have completed (at least |
| 1607 | // conceptually). |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1608 | if (DepType != SDep::Anti) { |
| 1609 | FoundSequentialDependence = true; |
| 1610 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1614 | if (FoundSequentialDependence) { |
| 1615 | Dependence = true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1618 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1619 | return true; |
| 1620 | } |
| 1621 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1622 | bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1623 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 1624 | MachineInstr &I = *SUI->getInstr(); |
| 1625 | MachineInstr &J = *SUJ->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1626 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1627 | bool Coexist = !cannotCoexist(I, J); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1628 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1629 | if (Coexist && !Dependence) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1630 | return true; |
| 1631 | |
| 1632 | // Check if the instruction was promoted to a dot-new. If so, demote it |
| 1633 | // back into a dot-old. |
| 1634 | if (PromotedToDotNew) |
| 1635 | demoteToDotOld(I); |
| 1636 | |
| 1637 | cleanUpDotCur(); |
| 1638 | // Check if the instruction (must be a store) was glued with an allocframe |
| 1639 | // instruction. If so, restore its offset to its original value, i.e. use |
| 1640 | // current SP instead of caller's SP. |
| 1641 | if (GlueAllocframeStore) { |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1642 | useCalleesSP(I); |
| 1643 | GlueAllocframeStore = false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1644 | } |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (ChangedOffset != INT64_MAX) |
| 1647 | undoChangedOffset(I); |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1648 | |
| 1649 | if (GlueToNewValueJump) { |
| 1650 | // Putting I and J together would prevent the new-value jump from being |
| 1651 | // packetized with the producer. In that case I and J must be separated. |
| 1652 | GlueToNewValueJump = false; |
| 1653 | return false; |
| 1654 | } |
| 1655 | |
Krzysztof Parzyszek | f4ad2cb | 2018-09-04 21:07:27 +0000 | [diff] [blame] | 1656 | if (!Coexist) |
| 1657 | return false; |
| 1658 | |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1659 | if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) { |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1660 | FoundSequentialDependence = false; |
| 1661 | Dependence = false; |
| 1662 | return true; |
| 1663 | } |
| 1664 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1668 | |
| 1669 | bool HexagonPacketizerList::foundLSInPacket() { |
| 1670 | bool FoundLoad = false; |
| 1671 | bool FoundStore = false; |
| 1672 | |
| 1673 | for (auto MJ : CurrentPacketMIs) { |
| 1674 | unsigned Opc = MJ->getOpcode(); |
| 1675 | if (Opc == Hexagon::S2_allocframe || Opc == Hexagon::L2_deallocframe) |
| 1676 | continue; |
| 1677 | if (HII->isMemOp(*MJ)) |
| 1678 | continue; |
| 1679 | if (MJ->mayLoad()) |
| 1680 | FoundLoad = true; |
| 1681 | if (MJ->mayStore() && !HII->isNewValueStore(*MJ)) |
| 1682 | FoundStore = true; |
| 1683 | } |
| 1684 | return FoundLoad && FoundStore; |
| 1685 | } |
| 1686 | |
| 1687 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1688 | MachineBasicBlock::iterator |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1689 | HexagonPacketizerList::addToPacket(MachineInstr &MI) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1690 | MachineBasicBlock::iterator MII = MI.getIterator(); |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1691 | MachineBasicBlock *MBB = MI.getParent(); |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1692 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 1693 | if (CurrentPacketMIs.empty()) |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1694 | PacketStalls = false; |
| 1695 | PacketStalls |= producesStall(MI); |
| 1696 | |
Krzysztof Parzyszek | b7e54e8 | 2018-04-06 18:19:22 +0000 | [diff] [blame] | 1697 | if (MI.isImplicitDef()) { |
| 1698 | // Add to the packet to allow subsequent instructions to be checked |
| 1699 | // properly. |
| 1700 | CurrentPacketMIs.push_back(&MI); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1701 | return MII; |
Krzysztof Parzyszek | b7e54e8 | 2018-04-06 18:19:22 +0000 | [diff] [blame] | 1702 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1703 | assert(ResourceTracker->canReserveResources(MI)); |
| 1704 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1705 | bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1706 | bool Good = true; |
| 1707 | |
| 1708 | if (GlueToNewValueJump) { |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1709 | MachineInstr &NvjMI = *++MII; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1710 | // We need to put both instructions in the same packet: MI and NvjMI. |
| 1711 | // Either of them can require a constant extender. Try to add both to |
| 1712 | // the current packet, and if that fails, end the packet and start a |
| 1713 | // new one. |
| 1714 | ResourceTracker->reserveResources(MI); |
| 1715 | if (ExtMI) |
| 1716 | Good = tryAllocateResourcesForConstExt(true); |
| 1717 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1718 | bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1719 | if (Good) { |
| 1720 | if (ResourceTracker->canReserveResources(NvjMI)) |
| 1721 | ResourceTracker->reserveResources(NvjMI); |
| 1722 | else |
| 1723 | Good = false; |
| 1724 | } |
| 1725 | if (Good && ExtNvjMI) |
| 1726 | Good = tryAllocateResourcesForConstExt(true); |
| 1727 | |
| 1728 | if (!Good) { |
| 1729 | endPacket(MBB, MI); |
| 1730 | assert(ResourceTracker->canReserveResources(MI)); |
| 1731 | ResourceTracker->reserveResources(MI); |
| 1732 | if (ExtMI) { |
| 1733 | assert(canReserveResourcesForConstExt()); |
| 1734 | tryAllocateResourcesForConstExt(true); |
| 1735 | } |
| 1736 | assert(ResourceTracker->canReserveResources(NvjMI)); |
| 1737 | ResourceTracker->reserveResources(NvjMI); |
| 1738 | if (ExtNvjMI) { |
| 1739 | assert(canReserveResourcesForConstExt()); |
| 1740 | reserveResourcesForConstExt(); |
| 1741 | } |
| 1742 | } |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1743 | CurrentPacketMIs.push_back(&MI); |
| 1744 | CurrentPacketMIs.push_back(&NvjMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1745 | return MII; |
| 1746 | } |
| 1747 | |
| 1748 | ResourceTracker->reserveResources(MI); |
| 1749 | if (ExtMI && !tryAllocateResourcesForConstExt(true)) { |
| 1750 | endPacket(MBB, MI); |
| 1751 | if (PromotedToDotNew) |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1752 | demoteToDotOld(MI); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1753 | if (GlueAllocframeStore) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1754 | useCalleesSP(MI); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1755 | GlueAllocframeStore = false; |
| 1756 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1757 | ResourceTracker->reserveResources(MI); |
| 1758 | reserveResourcesForConstExt(); |
| 1759 | } |
| 1760 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1761 | CurrentPacketMIs.push_back(&MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1762 | return MII; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1765 | void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB, |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 1766 | MachineBasicBlock::iterator EndMI) { |
| 1767 | // Replace VLIWPacketizerList::endPacket(MBB, EndMI). |
James Molloy | b6c7fce6 | 2019-09-09 13:17:55 +0000 | [diff] [blame] | 1768 | LLVM_DEBUG({ |
| 1769 | if (!CurrentPacketMIs.empty()) { |
| 1770 | dbgs() << "Finalizing packet:\n"; |
| 1771 | unsigned Idx = 0; |
| 1772 | for (MachineInstr *MI : CurrentPacketMIs) { |
| 1773 | unsigned R = ResourceTracker->getUsedResources(Idx++); |
| 1774 | dbgs() << " * [res:0x" << utohexstr(R) << "] " << *MI; |
| 1775 | } |
| 1776 | } |
| 1777 | }); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1778 | |
| 1779 | bool memShufDisabled = getmemShufDisabled(); |
| 1780 | if (memShufDisabled && !foundLSInPacket()) { |
| 1781 | setmemShufDisabled(false); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1782 | LLVM_DEBUG(dbgs() << " Not added to NoShufPacket\n"); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1783 | } |
| 1784 | memShufDisabled = getmemShufDisabled(); |
| 1785 | |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 1786 | OldPacketMIs.clear(); |
| 1787 | for (MachineInstr *MI : CurrentPacketMIs) { |
| 1788 | MachineBasicBlock::instr_iterator NextMI = std::next(MI->getIterator()); |
| 1789 | for (auto &I : make_range(HII->expandVGatherPseudo(*MI), NextMI)) |
| 1790 | OldPacketMIs.push_back(&I); |
| 1791 | } |
| 1792 | CurrentPacketMIs.clear(); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1793 | |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 1794 | if (OldPacketMIs.size() > 1) { |
| 1795 | MachineBasicBlock::instr_iterator FirstMI(OldPacketMIs.front()); |
| 1796 | MachineBasicBlock::instr_iterator LastMI(EndMI.getInstrIterator()); |
| 1797 | finalizeBundle(*MBB, FirstMI, LastMI); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1798 | auto BundleMII = std::prev(FirstMI); |
| 1799 | if (memShufDisabled) |
| 1800 | HII->setBundleNoShuf(BundleMII); |
| 1801 | |
| 1802 | setmemShufDisabled(false); |
| 1803 | } |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 1804 | |
| 1805 | ResourceTracker->clearResources(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1806 | LLVM_DEBUG(dbgs() << "End packet\n"); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1809 | bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) { |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 1810 | if (Minimal) |
| 1811 | return false; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1812 | return !producesStall(MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1815 | // V60 forward scheduling. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1816 | bool HexagonPacketizerList::producesStall(const MachineInstr &I) { |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1817 | // If the packet already stalls, then ignore the stall from a subsequent |
| 1818 | // instruction in the same packet. |
| 1819 | if (PacketStalls) |
| 1820 | return false; |
| 1821 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1822 | // Check whether the previous packet is in a different loop. If this is the |
| 1823 | // case, there is little point in trying to avoid a stall because that would |
| 1824 | // favor the rare case (loop entry) over the common case (loop iteration). |
| 1825 | // |
| 1826 | // TODO: We should really be able to check all the incoming edges if this is |
| 1827 | // the first packet in a basic block, so we can avoid stalls from the loop |
| 1828 | // backedge. |
| 1829 | if (!OldPacketMIs.empty()) { |
| 1830 | auto *OldBB = OldPacketMIs.front()->getParent(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1831 | auto *ThisBB = I.getParent(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1832 | if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB)) |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1836 | SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)]; |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1837 | |
Krzysztof Parzyszek | aca8f32 | 2018-04-06 18:13:11 +0000 | [diff] [blame] | 1838 | // If the latency is 0 and there is a data dependence between this |
| 1839 | // instruction and any instruction in the current packet, we disregard any |
| 1840 | // potential stalls due to the instructions in the previous packet. Most of |
| 1841 | // the instruction pairs that can go together in the same packet have 0 |
| 1842 | // latency between them. The exceptions are |
| 1843 | // 1. NewValueJumps as they're generated much later and the latencies can't |
| 1844 | // be changed at that point. |
| 1845 | // 2. .cur instructions, if its consumer has a 0 latency successor (such as |
| 1846 | // .new). In this case, the latency between .cur and the consumer stays |
| 1847 | // non-zero even though we can have both .cur and .new in the same packet. |
| 1848 | // Changing the latency to 0 is not an option as it causes software pipeliner |
| 1849 | // to not pipeline in some cases. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1850 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1851 | // For Example: |
| 1852 | // { |
| 1853 | // I1: v6.cur = vmem(r0++#1) |
| 1854 | // I2: v7 = valign(v6,v4,r2) |
| 1855 | // I3: vmem(r5++#1) = v7.new |
| 1856 | // } |
| 1857 | // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1858 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1859 | for (auto J : CurrentPacketMIs) { |
| 1860 | SUnit *SUJ = MIToSUnit[J]; |
| 1861 | for (auto &Pred : SUI->Preds) |
Krzysztof Parzyszek | aca8f32 | 2018-04-06 18:13:11 +0000 | [diff] [blame] | 1862 | if (Pred.getSUnit() == SUJ) |
| 1863 | if ((Pred.getLatency() == 0 && Pred.isAssignedRegDep()) || |
| 1864 | HII->isNewValueJump(I) || HII->isToBeScheduledASAP(*J, I)) |
| 1865 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1868 | // Check if the latency is greater than one between this instruction and any |
| 1869 | // instruction in the previous packet. |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1870 | for (auto J : OldPacketMIs) { |
| 1871 | SUnit *SUJ = MIToSUnit[J]; |
| 1872 | for (auto &Pred : SUI->Preds) |
| 1873 | if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1) |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1877 | return false; |
| 1878 | } |
| 1879 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1880 | //===----------------------------------------------------------------------===// |
| 1881 | // Public Constructor Functions |
| 1882 | //===----------------------------------------------------------------------===// |
| 1883 | |
Krzysztof Parzyszek | 39a979c | 2018-08-17 14:24:24 +0000 | [diff] [blame] | 1884 | FunctionPass *llvm::createHexagonPacketizer(bool Minimal) { |
| 1885 | return new HexagonPacketizer(Minimal); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1886 | } |