2017-09-29 00:27:31 +02:00
|
|
|
//===- HexagonVectorPrint.cpp - Generate vector printing instructions -----===//
|
2016-08-01 21:36:39 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-01 21:36:39 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass adds the capability to generate pseudo vector/predicate register
|
|
|
|
// printing instructions. These pseudo instructions should be used with the
|
|
|
|
// simulator, NEVER on hardware.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "HexagonInstrInfo.h"
|
|
|
|
#include "HexagonSubtarget.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2016-08-01 21:36:39 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2017-11-17 02:07:10 +01:00
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
|
|
|
#include "llvm/IR/InlineAsm.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-08-01 21:36:39 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-09-29 00:27:31 +02:00
|
|
|
#define DEBUG_TYPE "hexagon-vector-print"
|
|
|
|
|
2016-08-25 15:35:48 +02:00
|
|
|
static cl::opt<bool> TraceHexVectorStoresOnly("trace-hex-vector-stores-only",
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(false),
|
|
|
|
cl::desc("Enables tracing of vector stores"));
|
|
|
|
|
2016-08-01 21:36:39 +02:00
|
|
|
namespace llvm {
|
2016-12-17 02:09:05 +01:00
|
|
|
|
2017-09-29 00:27:31 +02:00
|
|
|
FunctionPass *createHexagonVectorPrint();
|
|
|
|
void initializeHexagonVectorPrintPass(PassRegistry&);
|
2016-08-01 21:36:39 +02:00
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
} // end namespace llvm
|
2016-08-01 21:36:39 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class HexagonVectorPrint : public MachineFunctionPass {
|
2017-09-29 00:27:31 +02:00
|
|
|
const HexagonSubtarget *QST = nullptr;
|
|
|
|
const HexagonInstrInfo *QII = nullptr;
|
|
|
|
const HexagonRegisterInfo *QRI = nullptr;
|
2016-08-01 21:36:39 +02:00
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2017-09-29 00:27:31 +02:00
|
|
|
HexagonVectorPrint() : MachineFunctionPass(ID) {
|
2016-12-17 02:09:05 +01:00
|
|
|
initializeHexagonVectorPrintPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getPassName() const override { return "Hexagon VectorPrint pass"; }
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
2016-08-01 21:36:39 +02:00
|
|
|
};
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2017-09-29 00:27:31 +02:00
|
|
|
char HexagonVectorPrint::ID = 0;
|
|
|
|
|
2016-08-01 21:36:39 +02:00
|
|
|
static bool isVecReg(unsigned Reg) {
|
|
|
|
return (Reg >= Hexagon::V0 && Reg <= Hexagon::V31)
|
|
|
|
|| (Reg >= Hexagon::W0 && Reg <= Hexagon::W15)
|
|
|
|
|| (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3);
|
|
|
|
}
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
static std::string getStringReg(unsigned R) {
|
2016-08-01 21:36:39 +02:00
|
|
|
if (R >= Hexagon::V0 && R <= Hexagon::V31) {
|
|
|
|
static const char* S[] = { "20", "21", "22", "23", "24", "25", "26", "27",
|
|
|
|
"28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
|
|
|
|
"30", "31", "32", "33", "34", "35", "36", "37",
|
|
|
|
"38", "39", "3a", "3b", "3c", "3d", "3e", "3f"};
|
|
|
|
return S[R-Hexagon::V0];
|
|
|
|
}
|
|
|
|
if (R >= Hexagon::Q0 && R <= Hexagon::Q3) {
|
|
|
|
static const char* S[] = { "00", "01", "02", "03"};
|
|
|
|
return S[R-Hexagon::Q0];
|
|
|
|
|
|
|
|
}
|
|
|
|
llvm_unreachable("valid vreg");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addAsmInstr(MachineBasicBlock *MBB, unsigned Reg,
|
|
|
|
MachineBasicBlock::instr_iterator I,
|
|
|
|
const DebugLoc &DL, const HexagonInstrInfo *QII,
|
|
|
|
MachineFunction &Fn) {
|
|
|
|
std::string VDescStr = ".long 0x1dffe0" + getStringReg(Reg);
|
2016-11-02 17:43:50 +01:00
|
|
|
const char *cstr = Fn.createExternalSymbolName(VDescStr);
|
2016-08-01 21:36:39 +02:00
|
|
|
unsigned ExtraInfo = InlineAsm::Extra_HasSideEffects;
|
|
|
|
BuildMI(*MBB, I, DL, QII->get(TargetOpcode::INLINEASM))
|
|
|
|
.addExternalSymbol(cstr)
|
|
|
|
.addImm(ExtraInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool getInstrVecReg(const MachineInstr &MI, unsigned &Reg) {
|
|
|
|
if (MI.getNumOperands() < 1) return false;
|
|
|
|
// Vec load or compute.
|
|
|
|
if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef()) {
|
|
|
|
Reg = MI.getOperand(0).getReg();
|
|
|
|
if (isVecReg(Reg))
|
2016-08-25 15:35:48 +02:00
|
|
|
return !TraceHexVectorStoresOnly;
|
2016-08-01 21:36:39 +02:00
|
|
|
}
|
|
|
|
// Vec store.
|
|
|
|
if (MI.mayStore() && MI.getNumOperands() >= 3 && MI.getOperand(2).isReg()) {
|
|
|
|
Reg = MI.getOperand(2).getReg();
|
|
|
|
if (isVecReg(Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Vec store post increment.
|
|
|
|
if (MI.mayStore() && MI.getNumOperands() >= 4 && MI.getOperand(3).isReg()) {
|
|
|
|
Reg = MI.getOperand(3).getReg();
|
|
|
|
if (isVecReg(Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonVectorPrint::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
bool Changed = false;
|
|
|
|
QST = &Fn.getSubtarget<HexagonSubtarget>();
|
|
|
|
QRI = QST->getRegisterInfo();
|
|
|
|
QII = QST->getInstrInfo();
|
|
|
|
std::vector<MachineInstr *> VecPrintList;
|
|
|
|
for (auto &MBB : Fn)
|
|
|
|
for (auto &MI : MBB) {
|
|
|
|
if (MI.isBundle()) {
|
|
|
|
MachineBasicBlock::instr_iterator MII = MI.getIterator();
|
|
|
|
for (++MII; MII != MBB.instr_end() && MII->isInsideBundle(); ++MII) {
|
2016-08-25 15:35:48 +02:00
|
|
|
if (MII->getNumOperands() < 1)
|
|
|
|
continue;
|
2016-08-01 21:36:39 +02:00
|
|
|
unsigned Reg = 0;
|
|
|
|
if (getInstrVecReg(*MII, Reg)) {
|
|
|
|
VecPrintList.push_back((&*MII));
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Found vector reg inside bundle \n";
|
|
|
|
MII->dump());
|
2016-08-01 21:36:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned Reg = 0;
|
|
|
|
if (getInstrVecReg(MI, Reg)) {
|
|
|
|
VecPrintList.push_back(&MI);
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Found vector reg \n"; MI.dump());
|
2016-08-01 21:36:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
Changed = !VecPrintList.empty();
|
2016-08-25 15:35:48 +02:00
|
|
|
if (!Changed)
|
|
|
|
return Changed;
|
2016-08-01 21:36:39 +02:00
|
|
|
|
|
|
|
for (auto *I : VecPrintList) {
|
|
|
|
DebugLoc DL = I->getDebugLoc();
|
|
|
|
MachineBasicBlock *MBB = I->getParent();
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Evaluating V MI\n"; I->dump());
|
2016-08-01 21:36:39 +02:00
|
|
|
unsigned Reg = 0;
|
2016-08-02 13:59:16 +02:00
|
|
|
if (!getInstrVecReg(*I, Reg))
|
2016-08-03 17:51:10 +02:00
|
|
|
llvm_unreachable("Need a vector reg");
|
2016-08-01 21:36:39 +02:00
|
|
|
MachineBasicBlock::instr_iterator MII = I->getIterator();
|
|
|
|
if (I->isInsideBundle()) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "add to end of bundle\n"; I->dump());
|
2016-08-25 15:35:48 +02:00
|
|
|
while (MBB->instr_end() != MII && MII->isInsideBundle())
|
|
|
|
MII++;
|
2016-08-01 21:36:39 +02:00
|
|
|
} else {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "add after instruction\n"; I->dump());
|
2016-08-01 21:36:39 +02:00
|
|
|
MII++;
|
|
|
|
}
|
2016-08-25 15:35:48 +02:00
|
|
|
if (MBB->instr_end() == MII)
|
|
|
|
continue;
|
|
|
|
|
2016-08-01 21:36:39 +02:00
|
|
|
if (Reg >= Hexagon::V0 && Reg <= Hexagon::V31) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "adding dump for V" << Reg - Hexagon::V0 << '\n');
|
2016-08-01 21:36:39 +02:00
|
|
|
addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
|
|
|
|
} else if (Reg >= Hexagon::W0 && Reg <= Hexagon::W15) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "adding dump for W" << Reg - Hexagon::W0 << '\n');
|
2016-08-01 21:36:39 +02:00
|
|
|
addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2 + 1,
|
|
|
|
MII, DL, QII, Fn);
|
|
|
|
addAsmInstr(MBB, Hexagon::V0 + (Reg - Hexagon::W0) * 2,
|
|
|
|
MII, DL, QII, Fn);
|
|
|
|
} else if (Reg >= Hexagon::Q0 && Reg <= Hexagon::Q3) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "adding dump for Q" << Reg - Hexagon::Q0 << '\n');
|
2016-08-01 21:36:39 +02:00
|
|
|
addAsmInstr(MBB, Reg, MII, DL, QII, Fn);
|
|
|
|
} else
|
|
|
|
llvm_unreachable("Bad Vector reg");
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public Constructor Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
INITIALIZE_PASS(HexagonVectorPrint, "hexagon-vector-print",
|
|
|
|
"Hexagon VectorPrint pass", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createHexagonVectorPrint() {
|
|
|
|
return new HexagonVectorPrint();
|
|
|
|
}
|