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