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-07-23 00:59:52 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.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",
|
2016-07-26 11:29:18 +08:00
|
|
|
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");
|
2016-07-23 00:59:52 +08:00
|
|
|
unsigned Size = DL->getTypeSizeInBits(Val.getType());
|
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-07-27 04:23:26 +08:00
|
|
|
unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
|
|
|
|
unsigned Alignment = 0;
|
|
|
|
Type *ValTy = nullptr;
|
|
|
|
if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
|
|
|
|
Alignment = SI->getAlignment();
|
|
|
|
ValTy = SI->getValueOperand()->getType();
|
|
|
|
} else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
|
|
|
|
Alignment = LI->getAlignment();
|
|
|
|
ValTy = LI->getType();
|
|
|
|
} else
|
|
|
|
llvm_unreachable("unhandled memory instruction");
|
|
|
|
|
|
|
|
return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
|
|
|
|
}
|
|
|
|
|
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-07-21 03:09:30 +08:00
|
|
|
MIRBuilder.buildInstr(Opcode, LLT{*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);
|
2016-07-27 00:45:26 +08:00
|
|
|
MIRBuilder.buildBr(TgtBB);
|
2016-03-12 01:28:03 +08:00
|
|
|
} 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-07-27 04:23:26 +08:00
|
|
|
bool IRTranslator::translateLoad(const LoadInst &LI) {
|
|
|
|
assert(LI.isSimple() && "only simple loads are supported at the moment");
|
|
|
|
|
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
|
|
|
unsigned Res = getOrCreateVReg(LI);
|
|
|
|
unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
|
|
|
|
LLT VTy{*LI.getType()}, PTy{*LI.getPointerOperand()->getType()};
|
|
|
|
|
|
|
|
MIRBuilder.buildLoad(
|
|
|
|
VTy, PTy, Res, Addr,
|
|
|
|
*MF.getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
|
|
|
|
MachineMemOperand::MOLoad,
|
|
|
|
VTy.getSizeInBits() / 8, getMemOpAlignment(LI)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IRTranslator::translateStore(const StoreInst &SI) {
|
|
|
|
assert(SI.isSimple() && "only simple loads are supported at the moment");
|
|
|
|
|
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
|
|
|
unsigned Val = getOrCreateVReg(*SI.getValueOperand());
|
|
|
|
unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
|
|
|
|
LLT VTy{*SI.getValueOperand()->getType()},
|
|
|
|
PTy{*SI.getPointerOperand()->getType()};
|
|
|
|
|
|
|
|
MIRBuilder.buildStore(
|
|
|
|
VTy, PTy, Val, Addr,
|
|
|
|
*MF.getMachineMemOperand(MachinePointerInfo(SI.getPointerOperand()),
|
|
|
|
MachineMemOperand::MOStore,
|
|
|
|
VTy.getSizeInBits() / 8, getMemOpAlignment(SI)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-26 05:01:29 +08:00
|
|
|
bool IRTranslator::translateBitCast(const CastInst &CI) {
|
|
|
|
if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
|
2016-07-27 00:45:30 +08:00
|
|
|
MIRBuilder.buildCopy(getOrCreateVReg(CI),
|
|
|
|
getOrCreateVReg(*CI.getOperand(0)));
|
2016-07-26 05:01:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return translateCast(TargetOpcode::G_BITCAST, CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
|
|
|
|
unsigned Op = getOrCreateVReg(*CI.getOperand(0));
|
|
|
|
unsigned Res = getOrCreateVReg(CI);
|
|
|
|
MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}},
|
|
|
|
Res, Op);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-23 00:59:52 +08:00
|
|
|
bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
|
|
|
|
assert(AI.isStaticAlloca() && "only handle static allocas now");
|
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
|
|
|
unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
|
|
|
|
unsigned Size =
|
|
|
|
ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
|
|
|
|
|
2016-07-28 01:47:54 +08:00
|
|
|
// Always allocate at least one byte.
|
|
|
|
Size = std::max(Size, 1u);
|
|
|
|
|
2016-07-23 00:59:52 +08:00
|
|
|
unsigned Alignment = AI.getAlignment();
|
|
|
|
if (!Alignment)
|
|
|
|
Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
|
|
|
|
|
|
|
|
unsigned Res = getOrCreateVReg(AI);
|
|
|
|
int FI = MF.getFrameInfo()->CreateStackObject(Size, Alignment, false, &AI);
|
|
|
|
MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
|
|
|
|
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-07-22 01:26:41 +08:00
|
|
|
// Arithmetic operations.
|
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-07-22 01:26:50 +08:00
|
|
|
case Instruction::Sub:
|
|
|
|
return translateBinaryOp(TargetOpcode::G_SUB, Inst);
|
2016-07-23 00:59:52 +08:00
|
|
|
|
2016-07-22 01:26:41 +08:00
|
|
|
// Bitwise operations.
|
2016-07-21 23:50:42 +08:00
|
|
|
case Instruction::And:
|
|
|
|
return translateBinaryOp(TargetOpcode::G_AND, Inst);
|
2016-06-11 04:50:35 +08:00
|
|
|
case Instruction::Or:
|
|
|
|
return translateBinaryOp(TargetOpcode::G_OR, Inst);
|
2016-07-23 00:59:52 +08:00
|
|
|
|
2016-07-22 01:26:41 +08:00
|
|
|
// Branch operations.
|
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);
|
|
|
|
|
2016-07-26 05:01:29 +08:00
|
|
|
// Casts
|
|
|
|
case Instruction::BitCast:
|
|
|
|
return translateBitCast(cast<CastInst>(Inst));
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
|
|
|
|
|
2016-07-27 04:23:26 +08:00
|
|
|
// Memory ops.
|
|
|
|
case Instruction::Load:
|
|
|
|
return translateLoad(cast<LoadInst>(Inst));
|
|
|
|
case Instruction::Store:
|
|
|
|
return translateStore(cast<StoreInst>(Inst));
|
|
|
|
|
2016-07-23 00:59:52 +08:00
|
|
|
case Instruction::Alloca:
|
|
|
|
return translateStaticAlloca(cast<AllocaInst>(Inst));
|
|
|
|
|
2016-02-12 02:53:28 +08:00
|
|
|
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-07-23 00:59:52 +08:00
|
|
|
DL = &F.getParent()->getDataLayout();
|
|
|
|
|
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;
|
|
|
|
}
|