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