2016-01-21 04:58:56 +08:00
|
|
|
//===-- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator --*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the IRTranslator class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
|
|
|
|
|
2016-02-12 03:59:41 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-02-17 03:26:02 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
|
2016-02-11 06:59:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2016-02-12 01:51:31 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
2016-02-11 06:59:27 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-02-12 01:51:31 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2016-02-12 02:53:28 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2016-02-11 06:59:27 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "irtranslator"
|
|
|
|
|
2016-01-21 04:58:56 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char IRTranslator::ID = 0;
|
2016-03-08 09:38:55 +08:00
|
|
|
INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
|
|
|
|
false, false);
|
2016-01-21 04:58:56 +08:00
|
|
|
|
2016-02-12 01:53:23 +08:00
|
|
|
IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
|
2016-03-08 09:38:55 +08:00
|
|
|
initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
|
2016-02-12 01:53:23 +08:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:27:54 +08:00
|
|
|
unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
|
|
|
|
unsigned &ValReg = ValToVReg[&Val];
|
2016-02-12 01:51:31 +08:00
|
|
|
// Check if this is the first time we see Val.
|
2016-02-12 05:48:32 +08:00
|
|
|
if (!ValReg) {
|
2016-02-12 01:51:31 +08:00
|
|
|
// Fill ValRegsSequence with the sequence of registers
|
|
|
|
// we need to concat together to produce the value.
|
2016-03-12 01:27:54 +08:00
|
|
|
assert(Val.getType()->isSized() &&
|
2016-02-12 01:51:31 +08:00
|
|
|
"Don't know how to create an empty vreg");
|
2016-03-12 01:27:54 +08:00
|
|
|
assert(!Val.getType()->isAggregateType() && "Not yet implemented");
|
|
|
|
unsigned Size = Val.getType()->getPrimitiveSizeInBits();
|
2016-02-12 01:51:31 +08:00
|
|
|
unsigned VReg = MRI->createGenericVirtualRegister(Size);
|
2016-02-12 05:48:32 +08:00
|
|
|
ValReg = VReg;
|
2016-02-12 01:52:28 +08:00
|
|
|
assert(!isa<Constant>(Val) && "Not yet implemented");
|
2016-02-12 01:51:31 +08:00
|
|
|
}
|
2016-02-12 05:48:32 +08:00
|
|
|
return ValReg;
|
2016-02-12 01:51:31 +08:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:27:43 +08:00
|
|
|
MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
|
|
|
|
MachineBasicBlock *&MBB = BBToMBB[&BB];
|
2016-02-12 01:51:31 +08:00
|
|
|
if (!MBB) {
|
2016-02-12 01:53:23 +08:00
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
2016-02-12 01:51:31 +08:00
|
|
|
MBB = MF.CreateMachineBasicBlock();
|
|
|
|
MF.push_back(MBB);
|
|
|
|
}
|
|
|
|
return *MBB;
|
|
|
|
}
|
|
|
|
|
2016-06-11 04:50:18 +08:00
|
|
|
bool IRTranslator::translateBinaryOp(unsigned Opcode, const Instruction &Inst) {
|
2016-02-11 06:59:27 +08:00
|
|
|
// Get or create a virtual register for each value.
|
|
|
|
// Unless the value is a Constant => loadimm cst?
|
|
|
|
// or inline constant each time?
|
|
|
|
// Creation of a virtual register needs to have a size.
|
2016-03-12 01:27:54 +08:00
|
|
|
unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
|
|
|
|
unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
|
|
|
|
unsigned Res = getOrCreateVReg(Inst);
|
2016-06-11 04:50:18 +08:00
|
|
|
MIRBuilder.buildInstr(Opcode, Inst.getType(), Res, Op0, Op1);
|
2016-02-12 01:51:31 +08:00
|
|
|
return true;
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 02:53:28 +08:00
|
|
|
bool IRTranslator::translateReturn(const Instruction &Inst) {
|
|
|
|
assert(isa<ReturnInst>(Inst) && "Return expected");
|
|
|
|
const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
|
|
|
|
// The target may mess up with the insertion point, but
|
|
|
|
// this is not important as a return is the last instruction
|
|
|
|
// of the block anyway.
|
2016-04-15 01:23:33 +08:00
|
|
|
return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
|
2016-02-12 02:53:28 +08:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:28:03 +08:00
|
|
|
bool IRTranslator::translateBr(const Instruction &Inst) {
|
|
|
|
assert(isa<BranchInst>(Inst) && "Branch expected");
|
|
|
|
const BranchInst &BrInst = *cast<BranchInst>(&Inst);
|
|
|
|
if (BrInst.isUnconditional()) {
|
|
|
|
const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
|
|
|
|
MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
|
|
|
|
MIRBuilder.buildInstr(TargetOpcode::G_BR, BrTgt.getType(), TgtBB);
|
|
|
|
} else {
|
|
|
|
assert(0 && "Not yet implemented");
|
|
|
|
}
|
|
|
|
// Link successors.
|
|
|
|
MachineBasicBlock &CurBB = MIRBuilder.getMBB();
|
|
|
|
for (const BasicBlock *Succ : BrInst.successors())
|
|
|
|
CurBB.addSuccessor(&getOrCreateBB(*Succ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-11 06:59:27 +08:00
|
|
|
bool IRTranslator::translate(const Instruction &Inst) {
|
2016-02-12 01:53:23 +08:00
|
|
|
MIRBuilder.setDebugLoc(Inst.getDebugLoc());
|
2016-02-11 06:59:27 +08:00
|
|
|
switch(Inst.getOpcode()) {
|
2016-02-12 02:53:28 +08:00
|
|
|
case Instruction::Add:
|
2016-06-11 04:50:18 +08:00
|
|
|
return translateBinaryOp(TargetOpcode::G_ADD, Inst);
|
2016-06-11 04:50:35 +08:00
|
|
|
case Instruction::Or:
|
|
|
|
return translateBinaryOp(TargetOpcode::G_OR, Inst);
|
2016-03-12 01:28:03 +08:00
|
|
|
case Instruction::Br:
|
|
|
|
return translateBr(Inst);
|
2016-02-12 02:53:28 +08:00
|
|
|
case Instruction::Ret:
|
|
|
|
return translateReturn(Inst);
|
|
|
|
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Opcode not supported");
|
2016-02-11 06:59:27 +08:00
|
|
|
}
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IRTranslator::finalize() {
|
2016-02-11 06:59:27 +08:00
|
|
|
// Release the memory used by the different maps we
|
|
|
|
// needed during the translation.
|
2016-02-12 05:48:32 +08:00
|
|
|
ValToVReg.clear();
|
2016-02-11 06:59:27 +08:00
|
|
|
Constants.clear();
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
|
2016-02-11 06:59:27 +08:00
|
|
|
const Function &F = *MF.getFunction();
|
2016-02-12 03:59:41 +08:00
|
|
|
if (F.empty())
|
|
|
|
return false;
|
2016-02-17 03:26:02 +08:00
|
|
|
CLI = MF.getSubtarget().getCallLowering();
|
2016-03-12 01:27:51 +08:00
|
|
|
MIRBuilder.setMF(MF);
|
2016-02-12 01:51:31 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2016-02-12 03:59:41 +08:00
|
|
|
// Setup the arguments.
|
2016-03-12 01:27:43 +08:00
|
|
|
MachineBasicBlock &MBB = getOrCreateBB(F.front());
|
2016-03-12 01:27:47 +08:00
|
|
|
MIRBuilder.setMBB(MBB);
|
2016-02-12 03:59:41 +08:00
|
|
|
SmallVector<unsigned, 8> VRegArgs;
|
|
|
|
for (const Argument &Arg: F.args())
|
2016-03-12 01:27:54 +08:00
|
|
|
VRegArgs.push_back(getOrCreateVReg(Arg));
|
2016-02-17 03:26:02 +08:00
|
|
|
bool Succeeded =
|
2016-04-15 01:23:33 +08:00
|
|
|
CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
|
2016-02-12 03:59:41 +08:00
|
|
|
if (!Succeeded)
|
|
|
|
report_fatal_error("Unable to lower arguments");
|
|
|
|
|
2016-02-11 06:59:27 +08:00
|
|
|
for (const BasicBlock &BB: F) {
|
2016-03-12 01:27:43 +08:00
|
|
|
MachineBasicBlock &MBB = getOrCreateBB(BB);
|
2016-03-12 01:27:47 +08:00
|
|
|
// Set the insertion point of all the following translations to
|
|
|
|
// the end of this basic block.
|
|
|
|
MIRBuilder.setMBB(MBB);
|
2016-02-11 06:59:27 +08:00
|
|
|
for (const Instruction &Inst: BB) {
|
|
|
|
bool Succeeded = translate(Inst);
|
|
|
|
if (!Succeeded) {
|
|
|
|
DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
|
|
|
|
report_fatal_error("Unable to translate instruction");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 06:23:42 +08:00
|
|
|
|
|
|
|
// Now that the MachineFrameInfo has been configured, no further changes to
|
|
|
|
// the reserved registers are possible.
|
|
|
|
MRI->freezeReservedRegs(MF);
|
|
|
|
|
2016-01-21 04:58:56 +08:00
|
|
|
return false;
|
|
|
|
}
|