LLVM 24.0.0git
HexagonVectorPrint.cpp
Go to the documentation of this file.
1//===- HexagonVectorPrint.cpp - Generate vector printing instructions -----===//
2//
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
6//
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
15#include "Hexagon.h"
16#include "HexagonInstrInfo.h"
17#include "HexagonSubtarget.h"
18#include "llvm/ADT/StringRef.h"
26#include "llvm/IR/DebugLoc.h"
27#include "llvm/IR/InlineAsm.h"
28#include "llvm/Pass.h"
30#include "llvm/Support/Debug.h"
33#include <string>
34#include <vector>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "hexagon-vector-print"
39
40static cl::opt<bool>
41 TraceHexVectorStoresOnly("trace-hex-vector-stores-only", cl::Hidden,
42 cl::desc("Enables tracing of vector stores"));
43
44namespace {
45
46class HexagonVectorPrint : public MachineFunctionPass {
47 const HexagonSubtarget *QST = nullptr;
48 const HexagonInstrInfo *QII = nullptr;
49
50public:
51 static char ID;
52
53 HexagonVectorPrint() : MachineFunctionPass(ID) {}
54
55 StringRef getPassName() const override { return "Hexagon VectorPrint pass"; }
56
57 bool runOnMachineFunction(MachineFunction &Fn) override;
58};
59
60} // end anonymous namespace
61
62char HexagonVectorPrint::ID = 0;
63
64static bool isVecReg(unsigned Reg) {
65 return (Reg >= Hexagon::V0 && Reg <= Hexagon::V31) ||
66 (Reg >= Hexagon::W0 && Reg <= Hexagon::W15) ||
67 (Reg >= Hexagon::WR0 && Reg <= Hexagon::WR15) ||
68 (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3);
69}
70
71static std::string getStringReg(unsigned R) {
72 if (R >= Hexagon::V0 && R <= Hexagon::V31) {
73 static const char* S[] = { "20", "21", "22", "23", "24", "25", "26", "27",
74 "28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
75 "30", "31", "32", "33", "34", "35", "36", "37",
76 "38", "39", "3a", "3b", "3c", "3d", "3e", "3f"};
77 return S[R-Hexagon::V0];
78 }
79 if (R >= Hexagon::Q0 && R <= Hexagon::Q3) {
80 static const char* S[] = { "00", "01", "02", "03"};
81 return S[R-Hexagon::Q0];
82
83 }
84 llvm_unreachable("valid vreg");
85}
86
87static void addAsmInstr(MachineBasicBlock *MBB, unsigned Reg,
89 const DebugLoc &DL, const HexagonInstrInfo *QII,
90 MachineFunction &Fn) {
91 std::string VDescStr = ".long 0x1dffe0" + getStringReg(Reg);
92 const char *cstr = Fn.createExternalSymbolName(VDescStr);
93 unsigned ExtraInfo = InlineAsm::Extra_HasSideEffects;
94 BuildMI(*MBB, I, DL, QII->get(TargetOpcode::INLINEASM))
96 .addImm(ExtraInfo);
97}
98
99static bool getInstrVecReg(const MachineInstr &MI, unsigned &Reg) {
100 if (MI.getNumOperands() < 1) return false;
101 // Vec load or compute.
102 if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef()) {
103 Reg = MI.getOperand(0).getReg();
104 if (isVecReg(Reg))
106 }
107 // Vec store.
108 if (MI.mayStore() && MI.getNumOperands() >= 3 && MI.getOperand(2).isReg()) {
109 Reg = MI.getOperand(2).getReg();
110 if (isVecReg(Reg))
111 return true;
112 }
113 // Vec store post increment.
114 if (MI.mayStore() && MI.getNumOperands() >= 4 && MI.getOperand(3).isReg()) {
115 Reg = MI.getOperand(3).getReg();
116 if (isVecReg(Reg))
117 return true;
118 }
119 return false;
120}
121
122bool HexagonVectorPrint::runOnMachineFunction(MachineFunction &Fn) {
123 bool Changed = false;
124 QST = &Fn.getSubtarget<HexagonSubtarget>();
125 QII = QST->getInstrInfo();
126 std::vector<MachineInstr *> VecPrintList;
127 for (auto &MBB : Fn)
128 for (auto &MI : MBB) {
129 if (MI.isBundle()) {
130 MachineBasicBlock::instr_iterator MII = MI.getIterator();
131 for (++MII; MII != MBB.instr_end() && MII->isInsideBundle(); ++MII) {
132 if (MII->getNumOperands() < 1)
133 continue;
134 unsigned Reg = 0;
135 if (getInstrVecReg(*MII, Reg)) {
136 VecPrintList.push_back((&*MII));
137 LLVM_DEBUG(dbgs() << "Found vector reg inside bundle \n";
138 MII->dump());
139 }
140 }
141 } else {
142 unsigned Reg = 0;
143 if (getInstrVecReg(MI, Reg)) {
144 VecPrintList.push_back(&MI);
145 LLVM_DEBUG(dbgs() << "Found vector reg \n"; MI.dump());
146 }
147 }
148 }
149
150 Changed = !VecPrintList.empty();
151 if (!Changed)
152 return Changed;
153
154 for (auto *I : VecPrintList) {
155 DebugLoc DL = I->getDebugLoc();
156 MachineBasicBlock *MBB = I->getParent();
157 LLVM_DEBUG(dbgs() << "Evaluating V MI\n"; I->dump());
158 unsigned Reg = 0;
159 if (!getInstrVecReg(*I, Reg))
160 llvm_unreachable("Need a vector reg");
161 MachineBasicBlock::instr_iterator MII = I->getIterator();
162 if (I->isInsideBundle()) {
163 LLVM_DEBUG(dbgs() << "add to end of bundle\n"; I->dump());
164 while (MBB->instr_end() != MII && MII->isInsideBundle())
165 MII++;
166 } else {
167 LLVM_DEBUG(dbgs() << "add after instruction\n"; I->dump());
168 MII++;
169 }
170 if (MBB->instr_end() == MII)
171 continue;
172
173 if (Reg >= Hexagon::V0 && Reg <= Hexagon::V31) {
174 LLVM_DEBUG(dbgs() << "adding dump for V" << Reg - Hexagon::V0 << '\n');
175 addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
176 } else if (Reg >= Hexagon::W0 && Reg <= Hexagon::W15) {
177 LLVM_DEBUG(dbgs() << "adding dump for W" << Reg - Hexagon::W0 << '\n');
178 addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2 + 1,
179 MII, DL, QII, Fn);
180 addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2,
181 MII, DL, QII, Fn);
182 } else if (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3) {
183 LLVM_DEBUG(dbgs() << "adding dump for Q" << Reg - Hexagon::Q0 << '\n');
184 addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
185 } else
186 llvm_unreachable("Bad Vector reg");
187 }
188 return Changed;
189}
190
191//===----------------------------------------------------------------------===//
192// Public Constructor Functions
193//===----------------------------------------------------------------------===//
194INITIALIZE_PASS(HexagonVectorPrint, "hexagon-vector-print",
195 "Hexagon VectorPrint pass", false, false)
196
198 return new HexagonVectorPrint();
199}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< bool > TraceHexVectorStoresOnly("trace-hex-vector-stores-only", cl::Hidden, cl::desc("Enables tracing of vector stores"))
static std::string getStringReg(unsigned R)
static bool isVecReg(unsigned Reg)
static void addAsmInstr(MachineBasicBlock *MBB, unsigned Reg, MachineBasicBlock::instr_iterator I, const DebugLoc &DL, const HexagonInstrInfo *QII, MachineFunction &Fn)
static bool getInstrVecReg(const MachineInstr &MI, unsigned &Reg)
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
const HexagonInstrInfo * getInstrInfo() const override
Instructions::iterator instr_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const char * createExternalSymbolName(StringRef Name)
Allocate a string and populate it with the given external symbol name.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createHexagonVectorPrint()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209