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" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DebugLoc.h" |
| 42 | #include "llvm/MC/MCInstrDesc.h" |
| 43 | #include "llvm/Pass.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ErrorHandling.h" |
| 47 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 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 | |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 499 | /// Return true if we can update the offset in MI so that MI and MJ |
| 500 | /// can be packetized together. |
| 501 | bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) { |
| 502 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 503 | MachineInstr &MI = *SUI->getInstr(); |
| 504 | MachineInstr &MJ = *SUJ->getInstr(); |
| 505 | |
| 506 | unsigned BPI, OPI; |
| 507 | if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI)) |
| 508 | return false; |
| 509 | unsigned BPJ, OPJ; |
| 510 | if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ)) |
| 511 | return false; |
| 512 | unsigned Reg = MI.getOperand(BPI).getReg(); |
| 513 | if (Reg != MJ.getOperand(BPJ).getReg()) |
| 514 | return false; |
| 515 | // Make sure that the dependences do not restrict adding MI to the packet. |
| 516 | // That is, ignore anti dependences, and make sure the only data dependence |
| 517 | // involves the specific register. |
| 518 | for (const auto &PI : SUI->Preds) |
| 519 | if (PI.getKind() != SDep::Anti && |
| 520 | (PI.getKind() != SDep::Data || PI.getReg() != Reg)) |
| 521 | return false; |
| 522 | int Incr; |
| 523 | if (!HII->getIncrementValue(MJ, Incr)) |
| 524 | return false; |
| 525 | |
| 526 | int64_t Offset = MI.getOperand(OPI).getImm(); |
| 527 | MI.getOperand(OPI).setImm(Offset + Incr); |
| 528 | ChangedOffset = Offset; |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | /// Undo the changed offset. This is needed if the instruction cannot be |
| 533 | /// added to the current packet due to a different instruction. |
| 534 | void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) { |
| 535 | unsigned BP, OP; |
| 536 | if (!HII->getBaseAndOffsetPosition(MI, BP, OP)) |
| 537 | llvm_unreachable("Unable to find base and offset operands."); |
| 538 | MI.getOperand(OP).setImm(ChangedOffset); |
| 539 | } |
| 540 | |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 541 | enum PredicateKind { |
| 542 | PK_False, |
| 543 | PK_True, |
| 544 | PK_Unknown |
| 545 | }; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 546 | |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 547 | /// Returns true if an instruction is predicated on p0 and false if it's |
| 548 | /// predicated on !p0. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 549 | static PredicateKind getPredicateSense(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 550 | const HexagonInstrInfo *HII) { |
| 551 | if (!HII->isPredicated(MI)) |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 552 | return PK_Unknown; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 553 | if (HII->isPredicatedTrue(MI)) |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 554 | return PK_True; |
Jyotsna Verma | 300f0b9 | 2013-05-10 20:27:34 +0000 | [diff] [blame] | 555 | return PK_False; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 558 | static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 559 | const HexagonInstrInfo *HII) { |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 560 | assert(HII->isPostIncrement(MI) && "Not a post increment operation."); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 561 | #ifndef NDEBUG |
| 562 | // Post Increment means duplicates. Use dense map to find duplicates in the |
| 563 | // list. Caution: Densemap initializes with the minimum of 64 buckets, |
| 564 | // whereas there are at most 5 operands in the post increment. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 565 | DenseSet<unsigned> DefRegsSet; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 566 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 567 | if (MO.isReg() && MO.isDef()) |
| 568 | DefRegsSet.insert(MO.getReg()); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 569 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 570 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 571 | if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg())) |
| 572 | return MO; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 573 | #else |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 574 | if (MI.mayLoad()) { |
| 575 | const MachineOperand &Op1 = MI.getOperand(1); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 576 | // The 2nd operand is always the post increment operand in load. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 577 | assert(Op1.isReg() && "Post increment operand has be to a register."); |
| 578 | return Op1; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 579 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 580 | if (MI.getDesc().mayStore()) { |
| 581 | const MachineOperand &Op0 = MI.getOperand(0); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 582 | // The 1st operand is always the post increment operand in store. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 583 | assert(Op0.isReg() && "Post increment operand has be to a register."); |
| 584 | return Op0; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 585 | } |
| 586 | #endif |
| 587 | // we should never come here. |
| 588 | llvm_unreachable("mayLoad or mayStore not set for Post Increment operation"); |
| 589 | } |
| 590 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 591 | // Get the value being stored. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 592 | static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 593 | // value being stored is always the last operand. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 594 | return MI.getOperand(MI.getNumOperands()-1); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 597 | static bool isLoadAbsSet(const MachineInstr &MI) { |
| 598 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 599 | switch (Opc) { |
| 600 | case Hexagon::L4_loadrd_ap: |
| 601 | case Hexagon::L4_loadrb_ap: |
| 602 | case Hexagon::L4_loadrh_ap: |
| 603 | case Hexagon::L4_loadrub_ap: |
| 604 | case Hexagon::L4_loadruh_ap: |
| 605 | case Hexagon::L4_loadri_ap: |
| 606 | return true; |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 611 | static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 612 | assert(isLoadAbsSet(MI)); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 613 | return MI.getOperand(1); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 616 | // Can be new value store? |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 617 | // Following restrictions are to be respected in convert a store into |
| 618 | // a new value store. |
| 619 | // 1. If an instruction uses auto-increment, its address register cannot |
| 620 | // be a new-value register. Arch Spec 5.4.2.1 |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 621 | // 2. If an instruction uses absolute-set addressing mode, its address |
| 622 | // 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] | 623 | // 3. If an instruction produces a 64-bit result, its registers cannot be used |
| 624 | // as new-value registers. Arch Spec 5.4.2.2. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 625 | // 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] | 626 | // the instruction that uses the new-value register must also be conditional, |
| 627 | // and both must always have their predicates evaluate identically. |
| 628 | // Arch Spec 5.4.2.3. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 629 | // 5. There is an implied restriction that a packet cannot have another store, |
| 630 | // 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] | 631 | // already a store in a packet, there can not be a new value store. |
| 632 | // Arch Spec: 3.4.4.2 |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 633 | bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI, |
| 634 | const MachineInstr &PacketMI, unsigned DepReg) { |
Jyotsna Verma | 438cec5 | 2013-05-10 20:58:11 +0000 | [diff] [blame] | 635 | // Make sure we are looking at the store, that can be promoted. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 636 | if (!HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 637 | return false; |
| 638 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 639 | // Make sure there is dependency and can be new value'd. |
| 640 | const MachineOperand &Val = getStoreValueOperand(MI); |
| 641 | if (Val.isReg() && Val.getReg() != DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 642 | return false; |
| 643 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 644 | const MCInstrDesc& MCID = PacketMI.getDesc(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 645 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 646 | // First operand is always the result. |
| 647 | const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF); |
| 648 | // Double regs can not feed into new value store: PRM section: 5.4.2.2. |
| 649 | if (PacketRC == &Hexagon::DoubleRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 650 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 651 | |
| 652 | // New-value stores are of class NV (slot 0), dual stores require class ST |
| 653 | // in slot 0 (PRM 5.5). |
| 654 | for (auto I : CurrentPacketMIs) { |
| 655 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
| 656 | if (PacketSU->getInstr()->mayStore()) |
| 657 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // Make sure it's NOT the post increment register that we are going to |
| 661 | // new value. |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 662 | if (HII->isPostIncrement(MI) && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 663 | getPostIncrementOperand(MI, HII).getReg() == DepReg) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 667 | if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 668 | getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) { |
| 669 | // If source is post_inc, or absolute-set addressing, it can not feed |
| 670 | // into new value store |
| 671 | // r3 = memw(r2++#4) |
| 672 | // memw(r30 + #-1404) = r2.new -> can not be new value store |
| 673 | // arch spec section: 5.4.2.1. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 677 | if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg) |
| 678 | return false; |
| 679 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 680 | // 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] | 681 | // also be predicated. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 682 | if (HII->isPredicated(PacketMI)) { |
| 683 | if (!HII->isPredicated(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 684 | return false; |
| 685 | |
| 686 | // Check to make sure that they both will have their predicates |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 687 | // evaluate identically. |
Sirish Pande | 95d0117 | 2012-05-11 20:00:34 +0000 | [diff] [blame] | 688 | unsigned predRegNumSrc = 0; |
| 689 | unsigned predRegNumDst = 0; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 690 | const TargetRegisterClass* predRegClass = nullptr; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 691 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 692 | // Get predicate register used in the source instruction. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 693 | for (auto &MO : PacketMI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 694 | if (!MO.isReg()) |
| 695 | continue; |
| 696 | predRegNumSrc = MO.getReg(); |
| 697 | predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc); |
| 698 | if (predRegClass == &Hexagon::PredRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 699 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 700 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 701 | assert((predRegClass == &Hexagon::PredRegsRegClass) && |
| 702 | "predicate register not found in a predicated PacketMI instruction"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 703 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 704 | // Get predicate register used in new-value store instruction. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 705 | for (auto &MO : MI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 706 | if (!MO.isReg()) |
| 707 | continue; |
| 708 | predRegNumDst = MO.getReg(); |
| 709 | predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst); |
| 710 | if (predRegClass == &Hexagon::PredRegsRegClass) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 711 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 712 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 713 | assert((predRegClass == &Hexagon::PredRegsRegClass) && |
| 714 | "predicate register not found in a predicated MI instruction"); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 715 | |
| 716 | // New-value register producer and user (store) need to satisfy these |
| 717 | // constraints: |
| 718 | // 1) Both instructions should be predicated on the same register. |
| 719 | // 2) If producer of the new-value register is .new predicated then store |
| 720 | // should also be .new predicated and if producer is not .new predicated |
| 721 | // then store should not be .new predicated. |
| 722 | // 3) Both new-value register producer and user should have same predicate |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 723 | // sense, i.e, either both should be negated or both should be non-negated. |
| 724 | if (predRegNumDst != predRegNumSrc || |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 725 | HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) || |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 726 | getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII)) |
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 | } |
| 729 | |
| 730 | // Make sure that other than the new-value register no other store instruction |
| 731 | // register has been modified in the same packet. Predicate registers can be |
| 732 | // modified by they should not be modified between the producer and the store |
| 733 | // instruction as it will make them both conditional on different values. |
| 734 | // We already know this to be true for all the instructions before and |
| 735 | // including PacketMI. Howerver, we need to perform the check for the |
| 736 | // remaining instructions in the packet. |
| 737 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 738 | unsigned StartCheck = 0; |
| 739 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 740 | for (auto I : CurrentPacketMIs) { |
| 741 | SUnit *TempSU = MIToSUnit.find(I)->second; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 742 | MachineInstr &TempMI = *TempSU->getInstr(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 743 | |
| 744 | // Following condition is true for all the instructions until PacketMI is |
| 745 | // reached (StartCheck is set to 0 before the for loop). |
| 746 | // StartCheck flag is 1 for all the instructions after PacketMI. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 747 | if (&TempMI != &PacketMI && !StartCheck) // Start processing only after |
| 748 | continue; // encountering PacketMI. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 749 | |
| 750 | StartCheck = 1; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 751 | if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 752 | continue; |
| 753 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 754 | for (auto &MO : MI.operands()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 755 | if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 756 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 759 | // Make sure that for non-POST_INC stores: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 760 | // 1. The only use of reg is DepReg and no other registers. |
| 761 | // This handles V4 base+index registers. |
| 762 | // The following store can not be dot new. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 763 | // Eg. r0 = add(r0, #3) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 764 | // memw(r1+r0<<#2) = r0 |
Krzysztof Parzyszek | 8fb181c | 2016-08-01 17:55:48 +0000 | [diff] [blame] | 765 | if (!HII->isPostIncrement(MI)) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 766 | for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) { |
| 767 | const MachineOperand &MO = MI.getOperand(opNum); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 768 | if (MO.isReg() && MO.getReg() == DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 769 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 770 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // If data definition is because of implicit definition of the register, |
| 774 | // do not newify the store. Eg. |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 775 | // %r9 = ZXTH %r12, implicit %d6, implicit-def %r12 |
| 776 | // S2_storerh_io %r8, 2, killed %r12; mem:ST2[%scevgep343] |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 777 | for (auto &MO : PacketMI.operands()) { |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 778 | if (MO.isRegMask() && MO.clobbersPhysReg(DepReg)) |
| 779 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 780 | if (!MO.isReg() || !MO.isDef() || !MO.isImplicit()) |
| 781 | continue; |
| 782 | unsigned R = MO.getReg(); |
| 783 | if (R == DepReg || HRI->isSuperRegister(DepReg, R)) |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | // Handle imp-use of super reg case. There is a target independent side |
| 788 | // change that should prevent this situation but I am handling it for |
| 789 | // just-in-case. For example, we cannot newify R2 in the following case: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 790 | // %r3 = A2_tfrsi 0; |
| 791 | // S2_storeri_io killed %r0, 0, killed %r2, implicit killed %d1; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 792 | for (auto &MO : MI.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 793 | if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg) |
| 794 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | // Can be dot new store. |
| 798 | return true; |
| 799 | } |
| 800 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 801 | // 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] | 802 | bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 803 | const SUnit *PacketSU, unsigned DepReg, |
| 804 | MachineBasicBlock::iterator &MII) { |
| 805 | if (!HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 806 | return false; |
| 807 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 808 | // Check to see the store can be new value'ed. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 809 | MachineInstr &PacketMI = *PacketSU->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 810 | if (canPromoteToNewValueStore(MI, PacketMI, DepReg)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 811 | return true; |
| 812 | |
| 813 | // Check to see the compare/jump can be new value'ed. |
| 814 | // This is done as a pass on its own. Don't need to check it here. |
| 815 | return false; |
| 816 | } |
| 817 | |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 818 | static bool isImplicitDependency(const MachineInstr &I, bool CheckDef, |
| 819 | unsigned DepReg) { |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 820 | for (auto &MO : I.operands()) { |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 821 | if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg)) |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 822 | return true; |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 823 | if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit()) |
| 824 | continue; |
| 825 | if (CheckDef == MO.isDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 826 | return true; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 827 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 831 | // Check to see if an instruction can be dot new |
| 832 | // There are three kinds. |
| 833 | // 1. dot new on predicate - V2/V3/V4 |
| 834 | // 2. dot new on stores NV/ST - V4 |
| 835 | // 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] | 836 | bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 837 | const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII, |
| 838 | const TargetRegisterClass* RC) { |
Jyotsna Verma | a46059b | 2013-03-28 19:44:04 +0000 | [diff] [blame] | 839 | // Already a dot new instruction. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 840 | if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 841 | return false; |
| 842 | |
Krzysztof Parzyszek | 2a48059 | 2016-07-26 20:30:30 +0000 | [diff] [blame] | 843 | if (!isNewifiable(MI, RC)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 844 | return false; |
| 845 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 846 | const MachineInstr &PI = *PacketSU->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 847 | |
| 848 | // The "new value" cannot come from inline asm. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 849 | if (PI.isInlineAsm()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 850 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 851 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 852 | // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no |
| 853 | // sense. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 854 | if (PI.isImplicitDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 855 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 856 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 857 | // If dependency is trough an implicitly defined register, we should not |
| 858 | // newify the use. |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 859 | if (isImplicitDependency(PI, true, DepReg) || |
| 860 | isImplicitDependency(MI, false, DepReg)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 861 | return false; |
| 862 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 863 | const MCInstrDesc& MCID = PI.getDesc(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 864 | const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 865 | if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 866 | return false; |
| 867 | |
| 868 | // predicate .new |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 869 | if (RC == &Hexagon::PredRegsRegClass) |
Krzysztof Parzyszek | 3cf1657 | 2017-06-01 18:02:40 +0000 | [diff] [blame] | 870 | return HII->predCanBeUsedAsDotNew(PI, DepReg); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 871 | |
| 872 | if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI)) |
| 873 | return false; |
| 874 | |
| 875 | // Create a dot new machine instruction to see if resources can be |
| 876 | // allocated. If not, bail out now. |
| 877 | int NewOpcode = HII->getDotNewOp(MI); |
| 878 | const MCInstrDesc &D = HII->get(NewOpcode); |
| 879 | MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc()); |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 880 | bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 881 | MF.DeleteMachineInstr(NewMI); |
| 882 | if (!ResourcesAvailable) |
| 883 | return false; |
| 884 | |
| 885 | // New Value Store only. New Value Jump generated as a separate pass. |
| 886 | if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII)) |
| 887 | return false; |
| 888 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 892 | // Go through the packet instructions and search for an anti dependency between |
| 893 | // them and DepReg from MI. Consider this case: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 894 | // Trying to add |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 895 | // a) %r1 = TFRI_cdNotPt %p3, 2 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 896 | // to this packet: |
| 897 | // { |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 898 | // b) %p0 = C2_or killed %p3, killed %p0 |
| 899 | // c) %p3 = C2_tfrrp %r23 |
| 900 | // d) %r1 = C2_cmovenewit %p3, 4 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 901 | // } |
| 902 | // The P3 from a) and d) will be complements after |
| 903 | // a)'s P3 is converted to .new form |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 904 | // Anti-dep between c) and b) is irrelevant for this case |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 905 | bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 906 | unsigned DepReg) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 907 | SUnit *PacketSUDep = MIToSUnit.find(&MI)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 908 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 909 | for (auto I : CurrentPacketMIs) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 910 | // We only care for dependencies to predicated instructions |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 911 | if (!HII->isPredicated(*I)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 912 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 913 | |
| 914 | // Scheduling Unit for current insn in the packet |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 915 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 916 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 917 | // Look at dependencies between current members of the packet and |
| 918 | // predicate defining instruction MI. Make sure that dependency is |
| 919 | // on the exact register we care about. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 920 | if (PacketSU->isSucc(PacketSUDep)) { |
| 921 | for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 922 | auto &Dep = PacketSU->Succs[i]; |
| 923 | if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti && |
| 924 | Dep.getReg() == DepReg) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 925 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | return false; |
| 931 | } |
| 932 | |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 933 | /// Gets the predicate register of a predicated instruction. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 934 | static unsigned getPredicatedRegister(MachineInstr &MI, |
Benjamin Kramer | e79beac | 2013-05-23 15:43:11 +0000 | [diff] [blame] | 935 | const HexagonInstrInfo *QII) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 936 | /// We use the following rule: The first predicate register that is a use is |
| 937 | /// the predicate register of a predicated instruction. |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 938 | assert(QII->isPredicated(MI) && "Must be predicated instruction"); |
| 939 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 940 | for (auto &Op : MI.operands()) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 941 | if (Op.isReg() && Op.getReg() && Op.isUse() && |
| 942 | Hexagon::PredRegsRegClass.contains(Op.getReg())) |
| 943 | return Op.getReg(); |
| 944 | } |
| 945 | |
| 946 | llvm_unreachable("Unknown instruction operand layout"); |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 947 | return 0; |
| 948 | } |
| 949 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 950 | // Given two predicated instructions, this function detects whether |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 951 | // the predicates are complements. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 952 | bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1, |
| 953 | MachineInstr &MI2) { |
Jyotsna Verma | 11bd54a | 2013-05-14 16:36:34 +0000 | [diff] [blame] | 954 | // If we don't know the predicate sense of the instructions bail out early, we |
| 955 | // need it later. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 956 | if (getPredicateSense(MI1, HII) == PK_Unknown || |
| 957 | getPredicateSense(MI2, HII) == PK_Unknown) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 958 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 959 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 960 | // Scheduling unit for candidate. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 961 | SUnit *SU = MIToSUnit[&MI1]; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 962 | |
| 963 | // One corner case deals with the following scenario: |
| 964 | // Trying to add |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 965 | // a) %r24 = A2_tfrt %p0, %r25 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 966 | // to this packet: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 967 | // { |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 968 | // b) %r25 = A2_tfrf %p0, %r24 |
| 969 | // c) %p0 = C2_cmpeqi %r26, 1 |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 970 | // } |
| 971 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 972 | // On general check a) and b) are complements, but presence of c) will |
| 973 | // convert a) to .new form, and then it is not a complement. |
| 974 | // We attempt to detect it by analyzing existing dependencies in the packet. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 975 | |
| 976 | // Analyze relationships between all existing members of the packet. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 977 | // Look for Anti dependecy on the same predicate reg as used in the |
| 978 | // candidate. |
| 979 | for (auto I : CurrentPacketMIs) { |
| 980 | // Scheduling Unit for current insn in the packet. |
| 981 | SUnit *PacketSU = MIToSUnit.find(I)->second; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 982 | |
| 983 | // If this instruction in the packet is succeeded by the candidate... |
| 984 | if (PacketSU->isSucc(SU)) { |
| 985 | for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 986 | auto Dep = PacketSU->Succs[i]; |
| 987 | // The corner case exist when there is true data dependency between |
| 988 | // candidate and one of current packet members, this dep is on |
| 989 | // 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] | 990 | // the packet. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 991 | if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data && |
| 992 | Hexagon::PredRegsRegClass.contains(Dep.getReg())) { |
| 993 | // Here I know that I is predicate setting instruction with true |
| 994 | // data dep to candidate on the register we care about - c) in the |
| 995 | // above example. Now I need to see if there is an anti dependency |
| 996 | // from c) to any other instruction in the same packet on the pred |
| 997 | // reg of interest. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 998 | if (restrictingDepExistInPacket(*I, Dep.getReg())) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 999 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1005 | // If the above case does not apply, check regular complement condition. |
| 1006 | // Check that the predicate register is the same and that the predicate |
| 1007 | // sense is different We also need to differentiate .old vs. .new: !p0 |
| 1008 | // is not complementary to p0.new. |
| 1009 | unsigned PReg1 = getPredicatedRegister(MI1, HII); |
| 1010 | unsigned PReg2 = getPredicatedRegister(MI2, HII); |
| 1011 | return PReg1 == PReg2 && |
| 1012 | Hexagon::PredRegsRegClass.contains(PReg1) && |
| 1013 | Hexagon::PredRegsRegClass.contains(PReg2) && |
| 1014 | getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) && |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1015 | HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1018 | // Initialize packetizer flags. |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1019 | void HexagonPacketizerList::initPacketizerState() { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1020 | Dependence = false; |
| 1021 | PromotedToDotNew = false; |
| 1022 | GlueToNewValueJump = false; |
| 1023 | GlueAllocframeStore = false; |
| 1024 | FoundSequentialDependence = false; |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1025 | ChangedOffset = INT64_MAX; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1028 | // Ignore bundling of pseudo instructions. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1029 | bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI, |
| 1030 | const MachineBasicBlock *) { |
| 1031 | if (MI.isDebugValue()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1032 | return true; |
| 1033 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1034 | if (MI.isCFIInstruction()) |
Krzysztof Parzyszek | 6bbcb31 | 2015-04-22 15:47:35 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1037 | // We must print out inline assembly. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1038 | if (MI.isInlineAsm()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1039 | return false; |
| 1040 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1041 | if (MI.isImplicitDef()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1042 | return false; |
| 1043 | |
| 1044 | // We check if MI has any functional units mapped to it. If it doesn't, |
| 1045 | // we ignore the instruction. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1046 | const MCInstrDesc& TID = MI.getDesc(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1047 | auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass()); |
Hal Finkel | 8db5547 | 2012-06-22 20:27:13 +0000 | [diff] [blame] | 1048 | unsigned FuncUnits = IS->getUnits(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1049 | return !FuncUnits; |
| 1050 | } |
| 1051 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1052 | bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) { |
| 1053 | if (MI.isEHLabel() || MI.isCFIInstruction()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1054 | return true; |
| 1055 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1056 | // Consider inline asm to not be a solo instruction by default. |
| 1057 | // Inline asm will be put in a packet temporarily, but then it will be |
| 1058 | // removed, and placed outside of the packet (before or after, depending |
| 1059 | // on dependencies). This is to reduce the impact of inline asm as a |
| 1060 | // "packet splitting" instruction. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1061 | if (MI.isInlineAsm() && !ScheduleInlineAsm) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | |
| 1064 | // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints: |
| 1065 | // trap, pause, barrier, icinva, isync, and syncht are solo instructions. |
| 1066 | // They must not be grouped with other instructions in a packet. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1067 | if (isSchedBarrier(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1068 | return true; |
| 1069 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1070 | if (HII->isSolo(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1071 | return true; |
| 1072 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1073 | if (MI.getOpcode() == Hexagon::A2_nop) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1074 | return true; |
| 1075 | |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1079 | // Quick check if instructions MI and MJ cannot coexist in the same packet. |
| 1080 | // Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm", |
| 1081 | // but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm". |
| 1082 | // For full test call this function twice: |
| 1083 | // cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI) |
| 1084 | // Doing the test only one way saves the amount of code in this function, |
| 1085 | // 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] | 1086 | static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ, |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1087 | const HexagonInstrInfo &HII) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1088 | const MachineFunction *MF = MI.getParent()->getParent(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1089 | if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() && |
| 1090 | HII.isHVXMemWithAIndirect(MI, MJ)) |
| 1091 | return true; |
| 1092 | |
| 1093 | // An inline asm cannot be together with a branch, because we may not be |
| 1094 | // able to remove the asm out after packetizing (i.e. if the asm must be |
| 1095 | // moved past the bundle). Similarly, two asms cannot be together to avoid |
| 1096 | // complications when determining their relative order outside of a bundle. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1097 | if (MI.isInlineAsm()) |
| 1098 | return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() || |
| 1099 | MJ.isCall() || MJ.isTerminator(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1100 | |
Krzysztof Parzyszek | 639545b | 2016-08-19 16:57:05 +0000 | [diff] [blame] | 1101 | switch (MI.getOpcode()) { |
| 1102 | case (Hexagon::S2_storew_locked): |
| 1103 | case (Hexagon::S4_stored_locked): |
| 1104 | case (Hexagon::L2_loadw_locked): |
| 1105 | case (Hexagon::L4_loadd_locked): |
| 1106 | case (Hexagon::Y4_l2fetch): { |
| 1107 | // These instructions can only be grouped with ALU32 or non-floating-point |
| 1108 | // XTYPE instructions. Since there is no convenient way of identifying fp |
| 1109 | // XTYPE instructions, only allow grouping with ALU32 for now. |
| 1110 | unsigned TJ = HII.getType(MJ); |
Krzysztof Parzyszek | 5ea971c | 2017-02-07 17:47:37 +0000 | [diff] [blame] | 1111 | if (TJ != HexagonII::TypeALU32_2op && |
| 1112 | TJ != HexagonII::TypeALU32_3op && |
| 1113 | TJ != HexagonII::TypeALU32_ADDI) |
Krzysztof Parzyszek | 639545b | 2016-08-19 16:57:05 +0000 | [diff] [blame] | 1114 | return true; |
| 1115 | break; |
| 1116 | } |
| 1117 | default: |
| 1118 | break; |
| 1119 | } |
| 1120 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1121 | // "False" really means that the quick check failed to determine if |
| 1122 | // I and J cannot coexist. |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1126 | // Full, symmetric check. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1127 | bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI, |
| 1128 | const MachineInstr &MJ) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1129 | return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII); |
| 1130 | } |
| 1131 | |
| 1132 | void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) { |
| 1133 | for (auto &B : MF) { |
| 1134 | MachineBasicBlock::iterator BundleIt; |
| 1135 | MachineBasicBlock::instr_iterator NextI; |
| 1136 | for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) { |
| 1137 | NextI = std::next(I); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1138 | MachineInstr &MI = *I; |
| 1139 | if (MI.isBundle()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1140 | BundleIt = I; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1141 | if (!MI.isInsideBundle()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1142 | continue; |
| 1143 | |
| 1144 | // Decide on where to insert the instruction that we are pulling out. |
| 1145 | // Debug instructions always go before the bundle, but the placement of |
| 1146 | // INLINE_ASM depends on potential dependencies. By default, try to |
| 1147 | // put it before the bundle, but if the asm writes to a register that |
| 1148 | // other instructions in the bundle read, then we need to place it |
| 1149 | // after the bundle (to preserve the bundle semantics). |
| 1150 | bool InsertBeforeBundle; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1151 | if (MI.isInlineAsm()) |
| 1152 | InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI); |
| 1153 | else if (MI.isDebugValue()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1154 | InsertBeforeBundle = true; |
| 1155 | else |
| 1156 | continue; |
| 1157 | |
| 1158 | BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle); |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | // Check if a given instruction is of class "system". |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1164 | static bool isSystemInstr(const MachineInstr &MI) { |
| 1165 | unsigned Opc = MI.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1166 | switch (Opc) { |
| 1167 | case Hexagon::Y2_barrier: |
| 1168 | case Hexagon::Y2_dcfetchbo: |
| 1169 | return true; |
| 1170 | } |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1174 | bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I, |
| 1175 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1176 | // The dependence graph may not include edges between dead definitions, |
| 1177 | // so without extra checks, we could end up packetizing two instruction |
| 1178 | // defining the same (dead) register. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1179 | if (I.isCall() || J.isCall()) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1180 | return false; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1181 | if (HII->isPredicated(I) || HII->isPredicated(J)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | |
| 1184 | BitVector DeadDefs(Hexagon::NUM_TARGET_REGS); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1185 | for (auto &MO : I.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1186 | if (!MO.isReg() || !MO.isDef() || !MO.isDead()) |
| 1187 | continue; |
| 1188 | DeadDefs[MO.getReg()] = true; |
| 1189 | } |
| 1190 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1191 | for (auto &MO : J.operands()) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1192 | if (!MO.isReg() || !MO.isDef() || !MO.isDead()) |
| 1193 | continue; |
| 1194 | unsigned R = MO.getReg(); |
| 1195 | if (R != Hexagon::USR_OVF && DeadDefs[R]) |
| 1196 | return true; |
| 1197 | } |
| 1198 | return false; |
| 1199 | } |
| 1200 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1201 | bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I, |
| 1202 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1203 | // A save callee-save register function call can only be in a packet |
| 1204 | // with instructions that don't write to the callee-save registers. |
| 1205 | if ((HII->isSaveCalleeSavedRegsCall(I) && |
| 1206 | doesModifyCalleeSavedReg(J, HRI)) || |
| 1207 | (HII->isSaveCalleeSavedRegsCall(J) && |
| 1208 | doesModifyCalleeSavedReg(I, HRI))) |
| 1209 | return true; |
| 1210 | |
| 1211 | // Two control flow instructions cannot go in the same packet. |
| 1212 | if (isControlFlow(I) && isControlFlow(J)) |
| 1213 | return true; |
| 1214 | |
| 1215 | // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot |
| 1216 | // contain a speculative indirect jump, |
| 1217 | // a new-value compare jump or a dealloc_return. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1218 | auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool { |
| 1219 | if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1220 | return true; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1221 | if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1222 | return true; |
| 1223 | return false; |
| 1224 | }; |
| 1225 | |
| 1226 | if (HII->isLoopN(I) && isBadForLoopN(J)) |
| 1227 | return true; |
| 1228 | if (HII->isLoopN(J) && isBadForLoopN(I)) |
| 1229 | return true; |
| 1230 | |
| 1231 | // dealloc_return cannot appear in the same packet as a conditional or |
| 1232 | // unconditional jump. |
| 1233 | return HII->isDeallocRet(I) && |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1234 | (J.isBranch() || J.isCall() || J.isBarrier()); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1237 | bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I, |
| 1238 | const MachineInstr &J) { |
| 1239 | // Adding I to a packet that has J. |
| 1240 | |
| 1241 | // Regmasks are not reflected in the scheduling dependency graph, so |
| 1242 | // we need to check them manually. This code assumes that regmasks only |
| 1243 | // occur on calls, and the problematic case is when we add an instruction |
| 1244 | // defining a register R to a packet that has a call that clobbers R via |
| 1245 | // a regmask. Those cannot be packetized together, because the call will |
| 1246 | // be executed last. That's also a reson why it is ok to add a call |
| 1247 | // clobbering R to a packet that defines R. |
| 1248 | |
| 1249 | // Look for regmasks in J. |
| 1250 | for (const MachineOperand &OpJ : J.operands()) { |
| 1251 | if (!OpJ.isRegMask()) |
| 1252 | continue; |
| 1253 | assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call"); |
| 1254 | for (const MachineOperand &OpI : I.operands()) { |
| 1255 | if (OpI.isReg()) { |
| 1256 | if (OpJ.clobbersPhysReg(OpI.getReg())) |
| 1257 | return true; |
| 1258 | } else if (OpI.isRegMask()) { |
| 1259 | // Both are regmasks. Assume that they intersect. |
| 1260 | return true; |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1267 | bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I, |
| 1268 | const MachineInstr &J) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1269 | bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1270 | bool StoreI = I.mayStore(), StoreJ = J.mayStore(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1271 | if ((SysI && StoreJ) || (SysJ && StoreI)) |
| 1272 | return true; |
| 1273 | |
| 1274 | if (StoreI && StoreJ) { |
| 1275 | if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I)) |
| 1276 | return true; |
| 1277 | } else { |
| 1278 | // A memop cannot be in the same packet with another memop or a store. |
| 1279 | // Two stores can be together, but here I and J cannot both be stores. |
| 1280 | bool MopStI = HII->isMemOp(I) || StoreI; |
| 1281 | bool MopStJ = HII->isMemOp(J) || StoreJ; |
| 1282 | if (MopStI && MopStJ) |
| 1283 | return true; |
| 1284 | } |
| 1285 | |
| 1286 | return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J)); |
| 1287 | } |
| 1288 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1289 | // SUI is the current instruction that is out side of the current packet. |
| 1290 | // SUJ is the current instruction inside the current packet against which that |
| 1291 | // SUI will be packetized. |
| 1292 | bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1293 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 1294 | MachineInstr &I = *SUI->getInstr(); |
| 1295 | MachineInstr &J = *SUJ->getInstr(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1296 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1297 | // Clear IgnoreDepMIs when Packet starts. |
| 1298 | if (CurrentPacketMIs.size() == 1) |
| 1299 | IgnoreDepMIs.clear(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1300 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1301 | MachineBasicBlock::iterator II = I.getIterator(); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1302 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1303 | // Solo instructions cannot go in the packet. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1304 | assert(!isSoloInstruction(I) && "Unexpected solo instr!"); |
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 | if (cannotCoexist(I, J)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1307 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1308 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1309 | Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J); |
| 1310 | if (Dependence) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1311 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1312 | |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1313 | // Regmasks are not accounted for in the scheduling graph, so we need |
| 1314 | // to explicitly check for dependencies caused by them. They should only |
| 1315 | // appear on calls, so it's not too pessimistic to reject all regmask |
| 1316 | // dependencies. |
| 1317 | Dependence = hasRegMaskDependence(I, J); |
| 1318 | if (Dependence) |
| 1319 | return false; |
| 1320 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1321 | // V4 allows dual stores. It does not allow second store, if the first |
| 1322 | // store is not in SLOT0. New value store, new value jump, dealloc_return |
| 1323 | // and memop always take SLOT0. Arch spec 3.4.4.2. |
| 1324 | Dependence = hasV4SpecificDependence(I, J); |
| 1325 | if (Dependence) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1326 | return false; |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1327 | |
| 1328 | // If an instruction feeds new value jump, glue it. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1329 | MachineBasicBlock::iterator NextMII = I.getIterator(); |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1330 | ++NextMII; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1331 | if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 1332 | MachineInstr &NextMI = *NextMII; |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1333 | |
| 1334 | bool secondRegMatch = false; |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 1335 | const MachineOperand &NOp0 = NextMI.getOperand(0); |
| 1336 | const MachineOperand &NOp1 = NextMI.getOperand(1); |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1337 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1338 | if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg()) |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1339 | secondRegMatch = true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1340 | |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1341 | for (MachineInstr *PI : CurrentPacketMIs) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1342 | // NVJ can not be part of the dual jump - Arch Spec: section 7.8. |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1343 | if (PI->isCall()) { |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1344 | Dependence = true; |
| 1345 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1346 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1347 | // Validate: |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1348 | // 1. Packet does not have a store in it. |
| 1349 | // 2. If the first operand of the nvj is newified, and the second |
| 1350 | // operand is also a reg, it (second reg) is not defined in |
| 1351 | // the same packet. |
| 1352 | // 3. If the second operand of the nvj is newified, (which means |
| 1353 | // first operand is also a reg), first reg is not defined in |
| 1354 | // the same packet. |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1355 | if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() || |
| 1356 | HII->isLoopN(*PI)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1357 | Dependence = true; |
| 1358 | break; |
| 1359 | } |
| 1360 | // Check #2/#3. |
| 1361 | const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1; |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1362 | if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) { |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1363 | Dependence = true; |
| 1364 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1365 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1366 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1367 | |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1368 | GlueToNewValueJump = true; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1369 | if (Dependence) |
Colin LeMahieu | 4fd203d | 2015-02-09 21:56:37 +0000 | [diff] [blame] | 1370 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1373 | // There no dependency between a prolog instruction and its successor. |
| 1374 | if (!SUJ->isSucc(SUI)) |
| 1375 | return true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1376 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1377 | for (unsigned i = 0; i < SUJ->Succs.size(); ++i) { |
| 1378 | if (FoundSequentialDependence) |
| 1379 | break; |
| 1380 | |
| 1381 | if (SUJ->Succs[i].getSUnit() != SUI) |
| 1382 | continue; |
| 1383 | |
| 1384 | SDep::Kind DepType = SUJ->Succs[i].getKind(); |
| 1385 | // For direct calls: |
| 1386 | // Ignore register dependences for call instructions for packetization |
| 1387 | // purposes except for those due to r31 and predicate registers. |
| 1388 | // |
| 1389 | // For indirect calls: |
| 1390 | // Same as direct calls + check for true dependences to the register |
| 1391 | // used in the indirect call. |
| 1392 | // |
| 1393 | // We completely ignore Order dependences for call instructions. |
| 1394 | // |
| 1395 | // For returns: |
| 1396 | // Ignore register dependences for return instructions like jumpr, |
| 1397 | // dealloc return unless we have dependencies on the explicit uses |
| 1398 | // of the registers used by jumpr (like r31) or dealloc return |
| 1399 | // (like r29 or r30). |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1400 | unsigned DepReg = 0; |
| 1401 | const TargetRegisterClass *RC = nullptr; |
| 1402 | if (DepType == SDep::Data) { |
| 1403 | DepReg = SUJ->Succs[i].getReg(); |
| 1404 | RC = HRI->getMinimalPhysRegClass(DepReg); |
| 1405 | } |
| 1406 | |
Krzysztof Parzyszek | 38e2ccc | 2016-08-23 16:01:01 +0000 | [diff] [blame] | 1407 | if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1408 | if (!isRegDependence(DepType)) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1409 | continue; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1410 | if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg())) |
| 1411 | continue; |
| 1412 | } |
| 1413 | |
| 1414 | if (DepType == SDep::Data) { |
| 1415 | if (canPromoteToDotCur(J, SUJ, DepReg, II, RC)) |
| 1416 | if (promoteToDotCur(J, DepType, II, RC)) |
| 1417 | continue; |
| 1418 | } |
| 1419 | |
| 1420 | // Data dpendence ok if we have load.cur. |
| 1421 | if (DepType == SDep::Data && HII->isDotCurInst(J)) { |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1422 | if (HII->isHVXVec(I)) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1423 | continue; |
| 1424 | } |
| 1425 | |
| 1426 | // For instructions that can be promoted to dot-new, try to promote. |
| 1427 | if (DepType == SDep::Data) { |
| 1428 | if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) { |
| 1429 | if (promoteToDotNew(I, DepType, II, RC)) { |
| 1430 | PromotedToDotNew = true; |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1431 | if (cannotCoexist(I, J)) |
| 1432 | FoundSequentialDependence = true; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1433 | continue; |
| 1434 | } |
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 (HII->isNewValueJump(I)) |
| 1437 | continue; |
| 1438 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1439 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1440 | // For predicated instructions, if the predicates are complements then |
| 1441 | // there can be no dependence. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1442 | if (HII->isPredicated(I) && HII->isPredicated(J) && |
| 1443 | arePredicatesComplements(I, J)) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1444 | // Not always safe to do this translation. |
| 1445 | // DAG Builder attempts to reduce dependence edges using transitive |
| 1446 | // nature of dependencies. Here is an example: |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1447 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1448 | // r0 = tfr_pt ... (1) |
| 1449 | // r0 = tfr_pf ... (2) |
| 1450 | // r0 = tfr_pt ... (3) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1451 | // |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1452 | // There will be an output dependence between (1)->(2) and (2)->(3). |
| 1453 | // However, there is no dependence edge between (1)->(3). This results |
| 1454 | // in all 3 instructions going in the same packet. We ignore dependce |
| 1455 | // only once to avoid this situation. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1456 | auto Itr = find(IgnoreDepMIs, &J); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1457 | if (Itr != IgnoreDepMIs.end()) { |
| 1458 | Dependence = true; |
| 1459 | return false; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1460 | } |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1461 | IgnoreDepMIs.push_back(&I); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1462 | continue; |
| 1463 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1464 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1465 | // Ignore Order dependences between unconditional direct branches |
| 1466 | // and non-control-flow instructions. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1467 | if (isDirectJump(I) && !J.isBranch() && !J.isCall() && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1468 | DepType == SDep::Order) |
| 1469 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1470 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1471 | // Ignore all dependences for jumps except for true and output |
| 1472 | // dependences. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1473 | if (I.isConditionalBranch() && DepType != SDep::Data && |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1474 | DepType != SDep::Output) |
| 1475 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1476 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1477 | if (DepType == SDep::Output) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1478 | FoundSequentialDependence = true; |
| 1479 | break; |
| 1480 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1481 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1482 | // For Order dependences: |
| 1483 | // 1. On V4 or later, volatile loads/stores can be packetized together, |
| 1484 | // unless other rules prevent is. |
| 1485 | // 2. Store followed by a load is not allowed. |
| 1486 | // 3. Store followed by a store is only valid on V4 or later. |
| 1487 | // 4. Load followed by any memory operation is allowed. |
| 1488 | if (DepType == SDep::Order) { |
| 1489 | if (!PacketizeVolatiles) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1490 | bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1491 | if (OrdRefs) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1492 | FoundSequentialDependence = true; |
| 1493 | break; |
| 1494 | } |
| 1495 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1496 | // J is first, I is second. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1497 | bool LoadJ = J.mayLoad(), StoreJ = J.mayStore(); |
| 1498 | bool LoadI = I.mayLoad(), StoreI = I.mayStore(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1499 | if (StoreJ) { |
| 1500 | // Two stores are only allowed on V4+. Load following store is never |
| 1501 | // allowed. |
Krzysztof Parzyszek | 9d19c8c | 2017-10-20 22:08:40 +0000 | [diff] [blame] | 1502 | if (LoadI && alias(J, I)) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1503 | FoundSequentialDependence = true; |
| 1504 | break; |
| 1505 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1506 | } else if (!LoadJ || (!LoadI && !StoreI)) { |
| 1507 | // If J is neither load nor store, assume a dependency. |
| 1508 | // 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] | 1509 | FoundSequentialDependence = true; |
| 1510 | break; |
| 1511 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1512 | // Store followed by store: not OK on V2. |
| 1513 | // Store followed by load: not OK on all. |
| 1514 | // Load followed by store: OK on all. |
| 1515 | // Load followed by load: OK on all. |
| 1516 | continue; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1519 | // For V4, special case ALLOCFRAME. Even though there is dependency |
| 1520 | // between ALLOCFRAME and subsequent store, allow it to be packetized |
| 1521 | // in a same packet. This implies that the store is using the caller's |
| 1522 | // SP. Hence, offset needs to be updated accordingly. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1523 | if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) { |
| 1524 | unsigned Opc = I.getOpcode(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1525 | switch (Opc) { |
| 1526 | case Hexagon::S2_storerd_io: |
| 1527 | case Hexagon::S2_storeri_io: |
| 1528 | case Hexagon::S2_storerh_io: |
| 1529 | case Hexagon::S2_storerb_io: |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1530 | if (I.getOperand(0).getReg() == HRI->getStackRegister()) { |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1531 | // Since this store is to be glued with allocframe in the same |
| 1532 | // packet, it will use SP of the previous stack frame, i.e. |
| 1533 | // caller's SP. Therefore, we need to recalculate offset |
| 1534 | // according to this change. |
| 1535 | GlueAllocframeStore = useCallersSP(I); |
| 1536 | if (GlueAllocframeStore) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1537 | continue; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1538 | } |
| 1539 | default: |
| 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1544 | // There are certain anti-dependencies that cannot be ignored. |
| 1545 | // Specifically: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame^] | 1546 | // J2_call ... implicit-def %r0 ; SUJ |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1547 | // R0 = ... ; SUI |
| 1548 | // Those cannot be packetized together, since the call will observe |
| 1549 | // the effect of the assignment to R0. |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1550 | if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) { |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1551 | // Check if I defines any volatile register. We should also check |
| 1552 | // registers that the call may read, but these happen to be a |
| 1553 | // subset of the volatile register set. |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1554 | for (const MachineOperand &Op : I.operands()) { |
| 1555 | if (Op.isReg() && Op.isDef()) { |
| 1556 | unsigned R = Op.getReg(); |
| 1557 | if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI)) |
| 1558 | continue; |
| 1559 | } else if (!Op.isRegMask()) { |
| 1560 | // If I has a regmask assume dependency. |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1561 | continue; |
Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 1562 | } |
Krzysztof Parzyszek | adb7ff0 | 2016-05-06 19:13:38 +0000 | [diff] [blame] | 1563 | FoundSequentialDependence = true; |
| 1564 | break; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | // Skip over remaining anti-dependences. Two instructions that are |
| 1569 | // anti-dependent can share a packet, since in most such cases all |
| 1570 | // operands are read before any modifications take place. |
| 1571 | // The exceptions are branch and call instructions, since they are |
| 1572 | // executed after all other instructions have completed (at least |
| 1573 | // conceptually). |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1574 | if (DepType != SDep::Anti) { |
| 1575 | FoundSequentialDependence = true; |
| 1576 | break; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1580 | if (FoundSequentialDependence) { |
| 1581 | Dependence = true; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1582 | return false; |
| 1583 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1584 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1585 | return true; |
| 1586 | } |
| 1587 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1588 | bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1589 | assert(SUI->getInstr() && SUJ->getInstr()); |
| 1590 | MachineInstr &I = *SUI->getInstr(); |
| 1591 | MachineInstr &J = *SUJ->getInstr(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1592 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1593 | bool Coexist = !cannotCoexist(I, J); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1594 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1595 | if (Coexist && !Dependence) |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1596 | return true; |
| 1597 | |
| 1598 | // Check if the instruction was promoted to a dot-new. If so, demote it |
| 1599 | // back into a dot-old. |
| 1600 | if (PromotedToDotNew) |
| 1601 | demoteToDotOld(I); |
| 1602 | |
| 1603 | cleanUpDotCur(); |
| 1604 | // Check if the instruction (must be a store) was glued with an allocframe |
| 1605 | // instruction. If so, restore its offset to its original value, i.e. use |
| 1606 | // current SP instead of caller's SP. |
| 1607 | if (GlueAllocframeStore) { |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1608 | useCalleesSP(I); |
| 1609 | GlueAllocframeStore = false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1610 | } |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1611 | |
| 1612 | if (ChangedOffset != INT64_MAX) |
| 1613 | undoChangedOffset(I); |
Krzysztof Parzyszek | c4a9a8d | 2017-10-11 21:20:43 +0000 | [diff] [blame] | 1614 | |
| 1615 | if (GlueToNewValueJump) { |
| 1616 | // Putting I and J together would prevent the new-value jump from being |
| 1617 | // packetized with the producer. In that case I and J must be separated. |
| 1618 | GlueToNewValueJump = false; |
| 1619 | return false; |
| 1620 | } |
| 1621 | |
| 1622 | if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) { |
Krzysztof Parzyszek | 8f174dd | 2017-10-11 15:51:44 +0000 | [diff] [blame] | 1623 | FoundSequentialDependence = false; |
| 1624 | Dependence = false; |
| 1625 | return true; |
| 1626 | } |
| 1627 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1628 | return false; |
| 1629 | } |
| 1630 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1631 | MachineBasicBlock::iterator |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1632 | HexagonPacketizerList::addToPacket(MachineInstr &MI) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1633 | MachineBasicBlock::iterator MII = MI.getIterator(); |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1634 | MachineBasicBlock *MBB = MI.getParent(); |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1635 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 1636 | if (CurrentPacketMIs.empty()) |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1637 | PacketStalls = false; |
| 1638 | PacketStalls |= producesStall(MI); |
| 1639 | |
| 1640 | if (MI.isImplicitDef()) |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1641 | return MII; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1642 | assert(ResourceTracker->canReserveResources(MI)); |
| 1643 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1644 | bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1645 | bool Good = true; |
| 1646 | |
| 1647 | if (GlueToNewValueJump) { |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1648 | MachineInstr &NvjMI = *++MII; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1649 | // We need to put both instructions in the same packet: MI and NvjMI. |
| 1650 | // Either of them can require a constant extender. Try to add both to |
| 1651 | // the current packet, and if that fails, end the packet and start a |
| 1652 | // new one. |
| 1653 | ResourceTracker->reserveResources(MI); |
| 1654 | if (ExtMI) |
| 1655 | Good = tryAllocateResourcesForConstExt(true); |
| 1656 | |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1657 | bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1658 | if (Good) { |
| 1659 | if (ResourceTracker->canReserveResources(NvjMI)) |
| 1660 | ResourceTracker->reserveResources(NvjMI); |
| 1661 | else |
| 1662 | Good = false; |
| 1663 | } |
| 1664 | if (Good && ExtNvjMI) |
| 1665 | Good = tryAllocateResourcesForConstExt(true); |
| 1666 | |
| 1667 | if (!Good) { |
| 1668 | endPacket(MBB, MI); |
| 1669 | assert(ResourceTracker->canReserveResources(MI)); |
| 1670 | ResourceTracker->reserveResources(MI); |
| 1671 | if (ExtMI) { |
| 1672 | assert(canReserveResourcesForConstExt()); |
| 1673 | tryAllocateResourcesForConstExt(true); |
| 1674 | } |
| 1675 | assert(ResourceTracker->canReserveResources(NvjMI)); |
| 1676 | ResourceTracker->reserveResources(NvjMI); |
| 1677 | if (ExtNvjMI) { |
| 1678 | assert(canReserveResourcesForConstExt()); |
| 1679 | reserveResourcesForConstExt(); |
| 1680 | } |
| 1681 | } |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1682 | CurrentPacketMIs.push_back(&MI); |
| 1683 | CurrentPacketMIs.push_back(&NvjMI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1684 | return MII; |
| 1685 | } |
| 1686 | |
| 1687 | ResourceTracker->reserveResources(MI); |
| 1688 | if (ExtMI && !tryAllocateResourcesForConstExt(true)) { |
| 1689 | endPacket(MBB, MI); |
| 1690 | if (PromotedToDotNew) |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1691 | demoteToDotOld(MI); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1692 | if (GlueAllocframeStore) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1693 | useCalleesSP(MI); |
Krzysztof Parzyszek | 3b4682f | 2016-07-26 14:24:46 +0000 | [diff] [blame] | 1694 | GlueAllocframeStore = false; |
| 1695 | } |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1696 | ResourceTracker->reserveResources(MI); |
| 1697 | reserveResourcesForConstExt(); |
| 1698 | } |
| 1699 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1700 | CurrentPacketMIs.push_back(&MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1701 | return MII; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1704 | void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB, |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1705 | MachineBasicBlock::iterator MI) { |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1706 | OldPacketMIs = CurrentPacketMIs; |
| 1707 | VLIWPacketizerList::endPacket(MBB, MI); |
| 1708 | } |
| 1709 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 1710 | bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) { |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1711 | return !producesStall(MI); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1714 | // V60 forward scheduling. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1715 | bool HexagonPacketizerList::producesStall(const MachineInstr &I) { |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1716 | // If the packet already stalls, then ignore the stall from a subsequent |
| 1717 | // instruction in the same packet. |
| 1718 | if (PacketStalls) |
| 1719 | return false; |
| 1720 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1721 | // Check whether the previous packet is in a different loop. If this is the |
| 1722 | // case, there is little point in trying to avoid a stall because that would |
| 1723 | // favor the rare case (loop entry) over the common case (loop iteration). |
| 1724 | // |
| 1725 | // TODO: We should really be able to check all the incoming edges if this is |
| 1726 | // the first packet in a basic block, so we can avoid stalls from the loop |
| 1727 | // backedge. |
| 1728 | if (!OldPacketMIs.empty()) { |
| 1729 | auto *OldBB = OldPacketMIs.front()->getParent(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 1730 | auto *ThisBB = I.getParent(); |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1731 | if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB)) |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1735 | SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)]; |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1736 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1737 | // Check if the latency is 0 between this instruction and any instruction |
| 1738 | // in the current packet. If so, we disregard any potential stalls due to |
| 1739 | // the instructions in the previous packet. Most of the instruction pairs |
| 1740 | // that can go together in the same packet have 0 latency between them. |
| 1741 | // Only exceptions are newValueJumps as they're generated much later and |
| 1742 | // the latencies can't be changed at that point. Another is .cur |
| 1743 | // instructions if its consumer has a 0 latency successor (such as .new). |
| 1744 | // In this case, the latency between .cur and the consumer stays non-zero |
| 1745 | // even though we can have both .cur and .new in the same packet. Changing |
| 1746 | // the latency to 0 is not an option as it causes software pipeliner to |
| 1747 | // not pipeline in some cases. |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1748 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1749 | // For Example: |
| 1750 | // { |
| 1751 | // I1: v6.cur = vmem(r0++#1) |
| 1752 | // I2: v7 = valign(v6,v4,r2) |
| 1753 | // I3: vmem(r5++#1) = v7.new |
| 1754 | // } |
| 1755 | // 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] | 1756 | |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1757 | for (auto J : CurrentPacketMIs) { |
| 1758 | SUnit *SUJ = MIToSUnit[J]; |
| 1759 | for (auto &Pred : SUI->Preds) |
| 1760 | if (Pred.getSUnit() == SUJ && |
| 1761 | (Pred.getLatency() == 0 || HII->isNewValueJump(I) || |
| 1762 | HII->isToBeScheduledASAP(*J, I))) |
| 1763 | return false; |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1766 | // Check if the latency is greater than one between this instruction and any |
| 1767 | // instruction in the previous packet. |
Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 1768 | for (auto J : OldPacketMIs) { |
| 1769 | SUnit *SUJ = MIToSUnit[J]; |
| 1770 | for (auto &Pred : SUI->Preds) |
| 1771 | if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1) |
| 1772 | return true; |
| 1773 | } |
| 1774 | |
| 1775 | // Check if the latency is greater than one between this instruction and any |
| 1776 | // instruction in the previous packet. |
Krzysztof Parzyszek | 9aaf923 | 2017-05-02 18:12:19 +0000 | [diff] [blame] | 1777 | for (auto J : OldPacketMIs) { |
| 1778 | SUnit *SUJ = MIToSUnit[J]; |
| 1779 | for (auto &Pred : SUI->Preds) |
| 1780 | if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1) |
| 1781 | return true; |
| 1782 | } |
| 1783 | |
Krzysztof Parzyszek | 56bbf54 | 2015-12-16 19:36:12 +0000 | [diff] [blame] | 1784 | return false; |
| 1785 | } |
| 1786 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 1787 | //===----------------------------------------------------------------------===// |
| 1788 | // Public Constructor Functions |
| 1789 | //===----------------------------------------------------------------------===// |
| 1790 | |
| 1791 | FunctionPass *llvm::createHexagonPacketizer() { |
| 1792 | return new HexagonPacketizer(); |
| 1793 | } |