blob: a842b672736c3aec2d33509ed44ad69ac641a5e3 [file] [log] [blame]
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001//===---- 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 Cahoon55bdeb72015-04-27 14:16:43 +00009// 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 Parzyszek9a278f12013-02-11 21:37:55 +000011// This pass will identify and convert such LOOPn instructions to a proper
12// form.
13//===----------------------------------------------------------------------===//
14
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "Hexagon.h"
16#include "HexagonTargetMachine.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/ADT/DenseMap.h"
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000018#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/Passes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000023#include "llvm/PassSupport.h"
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000024
25using namespace llvm;
26
Brendon Cahoon55bdeb72015-04-27 14:16:43 +000027static 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 Parzyszek9a278f12013-02-11 21:37:55 +000031namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000032 FunctionPass *createHexagonFixupHwLoops();
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000033 void initializeHexagonFixupHwLoopsPass(PassRegistry&);
34}
35
36namespace {
37 struct HexagonFixupHwLoops : public MachineFunctionPass {
38 public:
39 static char ID;
40
41 HexagonFixupHwLoops() : MachineFunctionPass(ID) {
42 initializeHexagonFixupHwLoopsPass(*PassRegistry::getPassRegistry());
43 }
44
Craig Topper906c2cd2014-04-29 07:58:16 +000045 bool runOnMachineFunction(MachineFunction &MF) override;
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000046
Derek Schuff1dbf7a52016-04-04 17:09:25 +000047 MachineFunctionProperties getRequiredProperties() const override {
48 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000049 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000050 }
51
Mehdi Amini117296c2016-10-01 02:56:57 +000052 StringRef getPassName() const override {
Craig Topper906c2cd2014-04-29 07:58:16 +000053 return "Hexagon Hardware Loop Fixup";
54 }
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000055
Craig Topper906c2cd2014-04-29 07:58:16 +000056 void getAnalysisUsage(AnalysisUsage &AU) const override {
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000057 AU.setPreservesCFG();
58 MachineFunctionPass::getAnalysisUsage(AU);
59 }
60
61 private:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000062 /// \brief Check the offset between each loop instruction and
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
Brendon Cahoon55bdeb72015-04-27 14:16:43 +000067 /// \brief Replace loop instruction with the constant extended
68 /// version if the loop label is too far from the loop instruction.
69 void useExtLoopInstr(MachineFunction &MF,
70 MachineBasicBlock::iterator &MII);
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000071 };
72
73 char HexagonFixupHwLoops::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000074}
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000075
76INITIALIZE_PASS(HexagonFixupHwLoops, "hwloopsfixup",
77 "Hexagon Hardware Loops Fixup", false, false)
78
79FunctionPass *llvm::createHexagonFixupHwLoops() {
80 return new HexagonFixupHwLoops();
81}
82
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000083/// \brief Returns true if the instruction is a hardware loop instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000084static 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 Parzyszek9a278f12013-02-11 21:37:55 +000089}
90
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000091bool HexagonFixupHwLoops::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +000092 if (skipFunction(MF.getFunction()))
Andrew Kaylor5b444a22016-04-26 19:46:28 +000093 return false;
Brendon Cahoon55bdeb72015-04-27 14:16:43 +000094 return fixupLoopInstrs(MF);
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000095}
96
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +000097/// \brief For Hexagon, if the loop label is to far from the
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.
105bool 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 Cahoon55bdeb72015-04-27 14:16:43 +0000110 DenseMap<const MachineBasicBlock *, unsigned> BlockToInstOffset;
111
112 const HexagonInstrInfo *HII =
113 static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000114
115 // First pass - compute the offset of each basic block.
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000116 for (const MachineBasicBlock &MBB : MF) {
117 if (MBB.getAlignment()) {
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.
121 int ByteAlign = (1u << MBB.getAlignment()) - 1;
122 InstOffset = (InstOffset + ByteAlign) & ~(ByteAlign);
123 }
124
125 BlockToInstOffset[&MBB] = InstOffset;
126 for (const MachineInstr &MI : MBB)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000127 InstOffset += HII->getSize(MI);
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000128 }
129
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000130 // Second pass - check each loop instruction to see if it needs to be
131 // converted.
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000132 bool Changed = false;
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000133 for (MachineBasicBlock &MBB : MF) {
134 InstOffset = BlockToInstOffset[&MBB];
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000135
136 // Loop over all the instructions.
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000137 MachineBasicBlock::iterator MII = MBB.begin();
138 MachineBasicBlock::iterator MIE = MBB.end();
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000139 while (MII != MIE) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000140 InstOffset += HII->getSize(*MII);
Krzysztof Parzyszek709e4f92017-08-10 15:00:30 +0000141 if (MII->isMetaInstruction()) {
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000142 ++MII;
143 continue;
144 }
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000145 if (isHardwareLoop(*MII)) {
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000146 assert(MII->getOperand(0).isMBB() &&
147 "Expect a basic block as loop operand");
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000148 int diff = InstOffset - BlockToInstOffset[MII->getOperand(0).getMBB()];
149 if ((unsigned)abs(diff) > MaxLoopRange) {
150 useExtLoopInstr(MF, MII);
151 MII = MBB.erase(MII);
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000152 Changed = true;
153 } else {
154 ++MII;
155 }
156 } else {
157 ++MII;
158 }
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000159 }
160 }
161
162 return Changed;
163}
164
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000165/// \brief Replace loop instructions with the constant extended version.
166void HexagonFixupHwLoops::useExtLoopInstr(MachineFunction &MF,
167 MachineBasicBlock::iterator &MII) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000168 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000169 MachineBasicBlock *MBB = MII->getParent();
170 DebugLoc DL = MII->getDebugLoc();
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000171 MachineInstrBuilder MIB;
172 unsigned newOp;
173 switch (MII->getOpcode()) {
174 case Hexagon::J2_loop0r:
175 newOp = Hexagon::J2_loop0rext;
176 break;
177 case Hexagon::J2_loop0i:
178 newOp = Hexagon::J2_loop0iext;
179 break;
180 case Hexagon::J2_loop1r:
181 newOp = Hexagon::J2_loop1rext;
182 break;
183 case Hexagon::J2_loop1i:
184 newOp = Hexagon::J2_loop1iext;
185 break;
186 default:
187 llvm_unreachable("Invalid Hardware Loop Instruction.");
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000188 }
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000189 MIB = BuildMI(*MBB, MII, DL, TII->get(newOp));
190
191 for (unsigned i = 0; i < MII->getNumOperands(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000192 MIB.add(MII->getOperand(i));
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +0000193}