blob: 65a8dcd75bdca430acd9d000fd90aa0a61bb7548 [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonVectorPrint.cpp - Generate vector printing instructions -----===//
Ron Lieberman8123b962016-08-01 19:36:39 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Ron Lieberman8123b962016-08-01 19:36:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass adds the capability to generate pseudo vector/predicate register
10// printing instructions. These pseudo instructions should be used with the
11// simulator, NEVER on hardware.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko58655bb2016-12-17 01:09:05 +000015#include "HexagonInstrInfo.h"
16#include "HexagonSubtarget.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstr.h"
Ron Lieberman8123b962016-08-01 19:36:39 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000023#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000024#include "llvm/CodeGen/TargetOpcodes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000025#include "llvm/IR/DebugLoc.h"
26#include "llvm/IR/InlineAsm.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include <string>
33#include <vector>
Ron Lieberman8123b962016-08-01 19:36:39 +000034
35using namespace llvm;
36
Eugene Zelenko3b873362017-09-28 22:27:31 +000037#define DEBUG_TYPE "hexagon-vector-print"
38
Ron Liebermanc93d1232016-08-25 13:35:48 +000039static cl::opt<bool> TraceHexVectorStoresOnly("trace-hex-vector-stores-only",
40 cl::Hidden, cl::ZeroOrMore, cl::init(false),
41 cl::desc("Enables tracing of vector stores"));
42
Ron Lieberman8123b962016-08-01 19:36:39 +000043namespace llvm {
Eugene Zelenko58655bb2016-12-17 01:09:05 +000044
Eugene Zelenko3b873362017-09-28 22:27:31 +000045FunctionPass *createHexagonVectorPrint();
46void initializeHexagonVectorPrintPass(PassRegistry&);
Ron Lieberman8123b962016-08-01 19:36:39 +000047
Eugene Zelenko58655bb2016-12-17 01:09:05 +000048} // end namespace llvm
Ron Lieberman8123b962016-08-01 19:36:39 +000049
50namespace {
51
52class HexagonVectorPrint : public MachineFunctionPass {
Eugene Zelenko3b873362017-09-28 22:27:31 +000053 const HexagonSubtarget *QST = nullptr;
54 const HexagonInstrInfo *QII = nullptr;
55 const HexagonRegisterInfo *QRI = nullptr;
Ron Lieberman8123b962016-08-01 19:36:39 +000056
Eugene Zelenko58655bb2016-12-17 01:09:05 +000057public:
58 static char ID;
Ron Lieberman8123b962016-08-01 19:36:39 +000059
Eugene Zelenko3b873362017-09-28 22:27:31 +000060 HexagonVectorPrint() : MachineFunctionPass(ID) {
Eugene Zelenko58655bb2016-12-17 01:09:05 +000061 initializeHexagonVectorPrintPass(*PassRegistry::getPassRegistry());
62 }
63
64 StringRef getPassName() const override { return "Hexagon VectorPrint pass"; }
65
66 bool runOnMachineFunction(MachineFunction &Fn) override;
Ron Lieberman8123b962016-08-01 19:36:39 +000067};
68
Eugene Zelenko58655bb2016-12-17 01:09:05 +000069} // end anonymous namespace
70
Eugene Zelenko3b873362017-09-28 22:27:31 +000071char HexagonVectorPrint::ID = 0;
72
Ron Lieberman8123b962016-08-01 19:36:39 +000073static bool isVecReg(unsigned Reg) {
74 return (Reg >= Hexagon::V0 && Reg <= Hexagon::V31)
75 || (Reg >= Hexagon::W0 && Reg <= Hexagon::W15)
76 || (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3);
77}
78
Eugene Zelenko58655bb2016-12-17 01:09:05 +000079static std::string getStringReg(unsigned R) {
Ron Lieberman8123b962016-08-01 19:36:39 +000080 if (R >= Hexagon::V0 && R <= Hexagon::V31) {
81 static const char* S[] = { "20", "21", "22", "23", "24", "25", "26", "27",
82 "28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
83 "30", "31", "32", "33", "34", "35", "36", "37",
84 "38", "39", "3a", "3b", "3c", "3d", "3e", "3f"};
85 return S[R-Hexagon::V0];
86 }
87 if (R >= Hexagon::Q0 && R <= Hexagon::Q3) {
88 static const char* S[] = { "00", "01", "02", "03"};
89 return S[R-Hexagon::Q0];
90
91 }
92 llvm_unreachable("valid vreg");
93}
94
95static void addAsmInstr(MachineBasicBlock *MBB, unsigned Reg,
96 MachineBasicBlock::instr_iterator I,
97 const DebugLoc &DL, const HexagonInstrInfo *QII,
98 MachineFunction &Fn) {
Ron Lieberman8123b962016-08-01 19:36:39 +000099 std::string VDescStr = ".long 0x1dffe0" + getStringReg(Reg);
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000100 const char *cstr = Fn.createExternalSymbolName(VDescStr);
Ron Lieberman8123b962016-08-01 19:36:39 +0000101 unsigned ExtraInfo = InlineAsm::Extra_HasSideEffects;
102 BuildMI(*MBB, I, DL, QII->get(TargetOpcode::INLINEASM))
103 .addExternalSymbol(cstr)
104 .addImm(ExtraInfo);
105}
106
107static bool getInstrVecReg(const MachineInstr &MI, unsigned &Reg) {
108 if (MI.getNumOperands() < 1) return false;
109 // Vec load or compute.
110 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef()) {
111 Reg = MI.getOperand(0).getReg();
112 if (isVecReg(Reg))
Ron Liebermanc93d1232016-08-25 13:35:48 +0000113 return !TraceHexVectorStoresOnly;
Ron Lieberman8123b962016-08-01 19:36:39 +0000114 }
115 // Vec store.
116 if (MI.mayStore() && MI.getNumOperands() >= 3 && MI.getOperand(2).isReg()) {
117 Reg = MI.getOperand(2).getReg();
118 if (isVecReg(Reg))
119 return true;
120 }
121 // Vec store post increment.
122 if (MI.mayStore() && MI.getNumOperands() >= 4 && MI.getOperand(3).isReg()) {
123 Reg = MI.getOperand(3).getReg();
124 if (isVecReg(Reg))
125 return true;
126 }
127 return false;
128}
129
130bool HexagonVectorPrint::runOnMachineFunction(MachineFunction &Fn) {
131 bool Changed = false;
132 QST = &Fn.getSubtarget<HexagonSubtarget>();
133 QRI = QST->getRegisterInfo();
134 QII = QST->getInstrInfo();
135 std::vector<MachineInstr *> VecPrintList;
136 for (auto &MBB : Fn)
137 for (auto &MI : MBB) {
138 if (MI.isBundle()) {
139 MachineBasicBlock::instr_iterator MII = MI.getIterator();
140 for (++MII; MII != MBB.instr_end() && MII->isInsideBundle(); ++MII) {
Ron Liebermanc93d1232016-08-25 13:35:48 +0000141 if (MII->getNumOperands() < 1)
142 continue;
Ron Lieberman8123b962016-08-01 19:36:39 +0000143 unsigned Reg = 0;
144 if (getInstrVecReg(*MII, Reg)) {
145 VecPrintList.push_back((&*MII));
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000146 LLVM_DEBUG(dbgs() << "Found vector reg inside bundle \n";
147 MII->dump());
Ron Lieberman8123b962016-08-01 19:36:39 +0000148 }
149 }
150 } else {
151 unsigned Reg = 0;
152 if (getInstrVecReg(MI, Reg)) {
153 VecPrintList.push_back(&MI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000154 LLVM_DEBUG(dbgs() << "Found vector reg \n"; MI.dump());
Ron Lieberman8123b962016-08-01 19:36:39 +0000155 }
156 }
157 }
158
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000159 Changed = !VecPrintList.empty();
Ron Liebermanc93d1232016-08-25 13:35:48 +0000160 if (!Changed)
161 return Changed;
Ron Lieberman8123b962016-08-01 19:36:39 +0000162
163 for (auto *I : VecPrintList) {
164 DebugLoc DL = I->getDebugLoc();
165 MachineBasicBlock *MBB = I->getParent();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000166 LLVM_DEBUG(dbgs() << "Evaluating V MI\n"; I->dump());
Ron Lieberman8123b962016-08-01 19:36:39 +0000167 unsigned Reg = 0;
NAKAMURA Takumi3f704492016-08-02 11:59:16 +0000168 if (!getInstrVecReg(*I, Reg))
Benjamin Kramer0e4b7642016-08-03 15:51:10 +0000169 llvm_unreachable("Need a vector reg");
Ron Lieberman8123b962016-08-01 19:36:39 +0000170 MachineBasicBlock::instr_iterator MII = I->getIterator();
171 if (I->isInsideBundle()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000172 LLVM_DEBUG(dbgs() << "add to end of bundle\n"; I->dump());
Ron Liebermanc93d1232016-08-25 13:35:48 +0000173 while (MBB->instr_end() != MII && MII->isInsideBundle())
174 MII++;
Ron Lieberman8123b962016-08-01 19:36:39 +0000175 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000176 LLVM_DEBUG(dbgs() << "add after instruction\n"; I->dump());
Ron Lieberman8123b962016-08-01 19:36:39 +0000177 MII++;
178 }
Ron Liebermanc93d1232016-08-25 13:35:48 +0000179 if (MBB->instr_end() == MII)
180 continue;
181
Ron Lieberman8123b962016-08-01 19:36:39 +0000182 if (Reg >= Hexagon::V0 && Reg <= Hexagon::V31) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000183 LLVM_DEBUG(dbgs() << "adding dump for V" << Reg - Hexagon::V0 << '\n');
Ron Lieberman8123b962016-08-01 19:36:39 +0000184 addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
185 } else if (Reg >= Hexagon::W0 && Reg <= Hexagon::W15) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000186 LLVM_DEBUG(dbgs() << "adding dump for W" << Reg - Hexagon::W0 << '\n');
Ron Lieberman8123b962016-08-01 19:36:39 +0000187 addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2 + 1,
188 MII, DL, QII, Fn);
189 addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2,
190 MII, DL, QII, Fn);
191 } else if (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000192 LLVM_DEBUG(dbgs() << "adding dump for Q" << Reg - Hexagon::Q0 << '\n');
Ron Lieberman8123b962016-08-01 19:36:39 +0000193 addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
194 } else
195 llvm_unreachable("Bad Vector reg");
196 }
197 return Changed;
198}
199
Ron Lieberman8123b962016-08-01 19:36:39 +0000200//===----------------------------------------------------------------------===//
201// Public Constructor Functions
202//===----------------------------------------------------------------------===//
203INITIALIZE_PASS(HexagonVectorPrint, "hexagon-vector-print",
204 "Hexagon VectorPrint pass", false, false)
205
206FunctionPass *llvm::createHexagonVectorPrint() {
207 return new HexagonVectorPrint();
208}