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