31 #include <llvm/CodeGen/MachineFunction.h>
32 #include "tce_config.h"
33 #include <llvm/IR/Function.h>
34 #include <llvm/Support/CommandLine.h>
35 #include <llvm/Analysis/AliasAnalysis.h>
54 : MachineFunctionPass(ID) {
55 auto* option =
static_cast<llvm::cl::opt<std::string>*
>(
56 llvm::cl::getRegisteredOptions().lookup(
"adf"));
57 const std::string adf = option->getValue();
62 <<
"TCE: unable to load the ADF:" << std::endl
64 <<
"Make sure you give the"
65 <<
" correct adf with the -adf switch to llc";
86 std::string msg =
"BEM does not identify R format ";
95 std::string regs =
"";
98 std::string ss =
static_cast<std::string
>(i);
104 if (foundRegs != 3) {
106 "Invalid amount of register operands, 3 required, found " +
107 std::to_string(foundRegs);
115 const std::string& s)
const {
117 std::string opName =
"";
119 std::string ss =
static_cast<std::string
>(i);
126 std::string msg =
"Machine does not have operation: " + ss;
132 const std::string msg =
133 "Warning: Failed to find operation name from: \"" + s +
"\"";
134 std::cerr << msg << std::endl;
143 const MachineBasicBlock::iterator& it)
const {
144 const unsigned OpIdx = 0;
145 std::vector<int> regIdxs;
146 const std::string asmString =
147 std::string(it->getOperand(OpIdx).getSymbolName());
148 for (
unsigned o = 0; o < it->getNumOperands(); o++) {
149 const MachineOperand& mo = it->getOperand(o);
151 int idx = mo.getReg() - 40;
152 regIdxs.push_back(idx);
153 if (idx < 0 && idx > 31) {
155 "Invalid register index: " + std::to_string(idx) +
156 " in instruction: " + asmString;
161 if (regIdxs.size() != 3) {
162 std::string msg =
"3 reg operands required, found " +
163 std::to_string(regIdxs.size()) +
164 " in instruction: " + asmString;
172 const std::string& opName,
const std::vector<int>& regIdxs)
const {
173 const int fu3Mask = 0b1110000000;
174 const int fu7Mask = 0b11111110000000000;
175 const int opcodeMask = 0b1111111;
177 const int fu3Enc = ((encoding & fu3Mask) >> 7);
178 const int fu7Enc = ((encoding & fu7Mask) >> 10);
179 const int opcodeEnc = encoding & opcodeMask;
180 const int shiftedEnc = (fu7Enc << 25) + (fu3Enc << 12) + opcodeEnc;
182 encoding = shiftedEnc + (regIdxs.at(0) << 7) + (regIdxs.at(1) << 15) +
183 (regIdxs.at(2) << 20);
190 for (MachineFunction::iterator i = MF.begin(); i != MF.end(); i++) {
191 for (MachineBasicBlock::iterator j = i->begin(); j != i->end(); j++) {
192 if (j->isInlineAsm()) {
193 const unsigned OpIdx = 0;
194 std::string asmString =
195 std::string(j->getOperand(OpIdx).getSymbolName());
200 const std::string regs =
findRegs(asmString);
204 std::string* cName =
new std::string(
205 ".long " + std::to_string(encoding) +
"; #" + opName +
207 j->getOperand(OpIdx).ChangeToES(
208 cName->c_str(), j->getOperand(OpIdx).getTargetFlags());