Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1 | //===---- HexagonFixupHwLoops.cpp - Fixup HW loops too far from LOOPn. ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 6 | // |
| 7 | // The loop start address in the LOOPn instruction is encoded as a distance |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 8 | // from the LOOPn instruction itself. If the start address is too far from |
| 9 | // the LOOPn instruction, the instruction needs to use a constant extender. |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 10 | // This pass will identify and convert such LOOPn instructions to a proper |
| 11 | // form. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 14 | #include "Hexagon.h" |
| 15 | #include "HexagonTargetMachine.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetInstrInfo.h" |
Krzysztof Parzyszek | 8038dad | 2018-03-23 20:41:44 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 23 | #include "llvm/PassSupport.h" |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 27 | static cl::opt<unsigned> MaxLoopRange( |
| 28 | "hexagon-loop-range", cl::Hidden, cl::init(200), |
| 29 | cl::desc("Restrict range of loopN instructions (testing only)")); |
| 30 | |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 31 | namespace llvm { |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 32 | FunctionPass *createHexagonFixupHwLoops(); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 33 | void initializeHexagonFixupHwLoopsPass(PassRegistry&); |
| 34 | } |
| 35 | |
| 36 | namespace { |
| 37 | struct HexagonFixupHwLoops : public MachineFunctionPass { |
| 38 | public: |
| 39 | static char ID; |
| 40 | |
| 41 | HexagonFixupHwLoops() : MachineFunctionPass(ID) { |
| 42 | initializeHexagonFixupHwLoopsPass(*PassRegistry::getPassRegistry()); |
| 43 | } |
| 44 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 45 | bool runOnMachineFunction(MachineFunction &MF) override; |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 46 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 47 | MachineFunctionProperties getRequiredProperties() const override { |
| 48 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 49 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 52 | StringRef getPassName() const override { |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 53 | return "Hexagon Hardware Loop Fixup"; |
| 54 | } |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 55 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 56 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 57 | AU.setPreservesCFG(); |
| 58 | MachineFunctionPass::getAnalysisUsage(AU); |
| 59 | } |
| 60 | |
| 61 | private: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 62 | /// Check the offset between each loop instruction and |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 63 | /// the loop basic block to determine if we can use the LOOP instruction |
| 64 | /// or if we need to set the LC/SA registers explicitly. |
| 65 | bool fixupLoopInstrs(MachineFunction &MF); |
| 66 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 67 | /// Replace loop instruction with the constant extended |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 68 | /// version if the loop label is too far from the loop instruction. |
| 69 | void useExtLoopInstr(MachineFunction &MF, |
| 70 | MachineBasicBlock::iterator &MII); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | char HexagonFixupHwLoops::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 74 | } |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 75 | |
| 76 | INITIALIZE_PASS(HexagonFixupHwLoops, "hwloopsfixup", |
| 77 | "Hexagon Hardware Loops Fixup", false, false) |
| 78 | |
| 79 | FunctionPass *llvm::createHexagonFixupHwLoops() { |
| 80 | return new HexagonFixupHwLoops(); |
| 81 | } |
| 82 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 83 | /// Returns true if the instruction is a hardware loop instruction. |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 84 | static bool isHardwareLoop(const MachineInstr &MI) { |
| 85 | return MI.getOpcode() == Hexagon::J2_loop0r || |
| 86 | MI.getOpcode() == Hexagon::J2_loop0i || |
| 87 | MI.getOpcode() == Hexagon::J2_loop1r || |
| 88 | MI.getOpcode() == Hexagon::J2_loop1i; |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 91 | bool HexagonFixupHwLoops::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 92 | if (skipFunction(MF.getFunction())) |
Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 93 | return false; |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 94 | return fixupLoopInstrs(MF); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 97 | /// For Hexagon, if the loop label is to far from the |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 98 | /// loop instruction then we need to set the LC0 and SA0 registers |
| 99 | /// explicitly instead of using LOOP(start,count). This function |
| 100 | /// checks the distance, and generates register assignments if needed. |
| 101 | /// |
| 102 | /// This function makes two passes over the basic blocks. The first |
| 103 | /// pass computes the offset of the basic block from the start. |
| 104 | /// The second pass checks all the loop instructions. |
| 105 | bool HexagonFixupHwLoops::fixupLoopInstrs(MachineFunction &MF) { |
| 106 | |
| 107 | // Offset of the current instruction from the start. |
| 108 | unsigned InstOffset = 0; |
| 109 | // Map for each basic block to it's first instruction. |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 110 | DenseMap<const MachineBasicBlock *, unsigned> BlockToInstOffset; |
| 111 | |
| 112 | const HexagonInstrInfo *HII = |
| 113 | static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 114 | |
| 115 | // First pass - compute the offset of each basic block. |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 116 | for (const MachineBasicBlock &MBB : MF) { |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame^] | 117 | if (MBB.getAlignment() != llvm::Align::None()) { |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 118 | // Although we don't know the exact layout of the final code, we need |
| 119 | // to account for alignment padding somehow. This heuristic pads each |
| 120 | // aligned basic block according to the alignment value. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame^] | 121 | InstOffset = alignTo(InstOffset, MBB.getAlignment()); |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BlockToInstOffset[&MBB] = InstOffset; |
| 125 | for (const MachineInstr &MI : MBB) |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 126 | InstOffset += HII->getSize(MI); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 129 | // Second pass - check each loop instruction to see if it needs to be |
| 130 | // converted. |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 131 | bool Changed = false; |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 132 | for (MachineBasicBlock &MBB : MF) { |
| 133 | InstOffset = BlockToInstOffset[&MBB]; |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 134 | |
| 135 | // Loop over all the instructions. |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 136 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 137 | MachineBasicBlock::iterator MIE = MBB.end(); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 138 | while (MII != MIE) { |
Krzysztof Parzyszek | 8038dad | 2018-03-23 20:41:44 +0000 | [diff] [blame] | 139 | unsigned InstSize = HII->getSize(*MII); |
Krzysztof Parzyszek | 709e4f9 | 2017-08-10 15:00:30 +0000 | [diff] [blame] | 140 | if (MII->isMetaInstruction()) { |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 141 | ++MII; |
| 142 | continue; |
| 143 | } |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame] | 144 | if (isHardwareLoop(*MII)) { |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 145 | assert(MII->getOperand(0).isMBB() && |
| 146 | "Expect a basic block as loop operand"); |
Krzysztof Parzyszek | 8038dad | 2018-03-23 20:41:44 +0000 | [diff] [blame] | 147 | MachineBasicBlock *TargetBB = MII->getOperand(0).getMBB(); |
| 148 | unsigned Diff = AbsoluteDifference(InstOffset, |
| 149 | BlockToInstOffset[TargetBB]); |
| 150 | if (Diff > MaxLoopRange) { |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 151 | useExtLoopInstr(MF, MII); |
| 152 | MII = MBB.erase(MII); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 153 | Changed = true; |
| 154 | } else { |
| 155 | ++MII; |
| 156 | } |
| 157 | } else { |
| 158 | ++MII; |
| 159 | } |
Krzysztof Parzyszek | 8038dad | 2018-03-23 20:41:44 +0000 | [diff] [blame] | 160 | InstOffset += InstSize; |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | return Changed; |
| 165 | } |
| 166 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 167 | /// Replace loop instructions with the constant extended version. |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 168 | void HexagonFixupHwLoops::useExtLoopInstr(MachineFunction &MF, |
| 169 | MachineBasicBlock::iterator &MII) { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 170 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 171 | MachineBasicBlock *MBB = MII->getParent(); |
| 172 | DebugLoc DL = MII->getDebugLoc(); |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 173 | MachineInstrBuilder MIB; |
| 174 | unsigned newOp; |
| 175 | switch (MII->getOpcode()) { |
| 176 | case Hexagon::J2_loop0r: |
| 177 | newOp = Hexagon::J2_loop0rext; |
| 178 | break; |
| 179 | case Hexagon::J2_loop0i: |
| 180 | newOp = Hexagon::J2_loop0iext; |
| 181 | break; |
| 182 | case Hexagon::J2_loop1r: |
| 183 | newOp = Hexagon::J2_loop1rext; |
| 184 | break; |
| 185 | case Hexagon::J2_loop1i: |
| 186 | newOp = Hexagon::J2_loop1iext; |
| 187 | break; |
| 188 | default: |
| 189 | llvm_unreachable("Invalid Hardware Loop Instruction."); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 190 | } |
Brendon Cahoon | 55bdeb7 | 2015-04-27 14:16:43 +0000 | [diff] [blame] | 191 | MIB = BuildMI(*MBB, MII, DL, TII->get(newOp)); |
| 192 | |
| 193 | for (unsigned i = 0; i < MII->getNumOperands(); ++i) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 194 | MIB.add(MII->getOperand(i)); |
Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 195 | } |