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