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