2016-01-20 20:58:56 +00: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-11 19:59:41 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-02-16 19:26:02 +00:00
|
|
|
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
|
2016-02-10 22:59:27 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2016-07-22 16:59:52 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2016-02-11 17:51:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
2016-02-10 22:59:27 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-07-29 22:32:36 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2016-02-11 17:51:31 +00:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2016-07-29 22:32:36 +00:00
|
|
|
#include "llvm/Target/TargetIntrinsicInfo.h"
|
2016-02-11 18:53:28 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2016-02-10 22:59:27 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "irtranslator"
|
|
|
|
|
2016-01-20 20:58:56 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char IRTranslator::ID = 0;
|
2016-03-08 01:38:55 +00:00
|
|
|
INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
|
2016-07-26 03:29:18 +00:00
|
|
|
false, false)
|
2016-01-20 20:58:56 +00:00
|
|
|
|
2016-02-11 17:53:23 +00:00
|
|
|
IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
|
2016-03-08 01:38:55 +00:00
|
|
|
initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
|
2016-02-11 17:53:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 17:27:54 +00:00
|
|
|
unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
|
|
|
|
unsigned &ValReg = ValToVReg[&Val];
|
2016-02-11 17:51:31 +00:00
|
|
|
// Check if this is the first time we see Val.
|
2016-02-11 21:48:32 +00:00
|
|
|
if (!ValReg) {
|
2016-02-11 17:51:31 +00:00
|
|
|
// Fill ValRegsSequence with the sequence of registers
|
|
|
|
// we need to concat together to produce the value.
|
2016-03-11 17:27:54 +00:00
|
|
|
assert(Val.getType()->isSized() &&
|
2016-02-11 17:51:31 +00:00
|
|
|
"Don't know how to create an empty vreg");
|
2016-03-11 17:27:54 +00:00
|
|
|
assert(!Val.getType()->isAggregateType() && "Not yet implemented");
|
2016-07-22 16:59:52 +00:00
|
|
|
unsigned Size = DL->getTypeSizeInBits(Val.getType());
|
2016-02-11 17:51:31 +00:00
|
|
|
unsigned VReg = MRI->createGenericVirtualRegister(Size);
|
2016-02-11 21:48:32 +00:00
|
|
|
ValReg = VReg;
|
2016-08-09 21:28:04 +00:00
|
|
|
|
|
|
|
if (auto CV = dyn_cast<Constant>(&Val)) {
|
|
|
|
bool Success = translate(*CV, VReg);
|
|
|
|
if (!Success)
|
|
|
|
report_fatal_error("unable to translate constant");
|
|
|
|
}
|
2016-02-11 17:51:31 +00:00
|
|
|
}
|
2016-02-11 21:48:32 +00:00
|
|
|
return ValReg;
|
2016-02-11 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 20:23:26 +00: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-11 17:27:43 +00:00
|
|
|
MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
|
|
|
|
MachineBasicBlock *&MBB = BBToMBB[&BB];
|
2016-02-11 17:51:31 +00:00
|
|
|
if (!MBB) {
|
2016-02-11 17:53:23 +00:00
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
2016-02-11 17:51:31 +00:00
|
|
|
MBB = MF.CreateMachineBasicBlock();
|
|
|
|
MF.push_back(MBB);
|
|
|
|
}
|
|
|
|
return *MBB;
|
|
|
|
}
|
|
|
|
|
2016-07-29 18:11:21 +00:00
|
|
|
bool IRTranslator::translateBinaryOp(unsigned Opcode,
|
|
|
|
const BinaryOperator &Inst) {
|
|
|
|
// FIXME: handle signed/unsigned wrapping flags.
|
|
|
|
|
2016-02-10 22:59:27 +00: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-11 17:27:54 +00:00
|
|
|
unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
|
|
|
|
unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
|
|
|
|
unsigned Res = getOrCreateVReg(Inst);
|
2016-07-29 17:43:52 +00:00
|
|
|
MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()})
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
2016-02-11 17:51:31 +00:00
|
|
|
return true;
|
2016-01-20 20:58:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 18:11:21 +00:00
|
|
|
bool IRTranslator::translateReturn(const ReturnInst &RI) {
|
|
|
|
const Value *Ret = RI.getReturnValue();
|
2016-02-11 18:53:28 +00:00
|
|
|
// 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-14 17:23:33 +00:00
|
|
|
return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
|
2016-02-11 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 18:11:21 +00:00
|
|
|
bool IRTranslator::translateBr(const BranchInst &BrInst) {
|
2016-07-29 17:58:00 +00:00
|
|
|
unsigned Succ = 0;
|
|
|
|
if (!BrInst.isUnconditional()) {
|
|
|
|
// We want a G_BRCOND to the true BB followed by an unconditional branch.
|
|
|
|
unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
|
|
|
|
const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
|
|
|
|
MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
|
|
|
|
MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
|
2016-03-11 17:28:03 +00:00
|
|
|
}
|
2016-07-29 17:58:00 +00:00
|
|
|
|
|
|
|
const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
|
|
|
|
MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
|
|
|
|
MIRBuilder.buildBr(TgtBB);
|
|
|
|
|
2016-03-11 17:28:03 +00:00
|
|
|
// Link successors.
|
|
|
|
MachineBasicBlock &CurBB = MIRBuilder.getMBB();
|
|
|
|
for (const BasicBlock *Succ : BrInst.successors())
|
|
|
|
CurBB.addSuccessor(&getOrCreateBB(*Succ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:23:26 +00: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-25 21:01:29 +00:00
|
|
|
bool IRTranslator::translateBitCast(const CastInst &CI) {
|
|
|
|
if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
|
2016-07-26 16:45:30 +00:00
|
|
|
MIRBuilder.buildCopy(getOrCreateVReg(CI),
|
|
|
|
getOrCreateVReg(*CI.getOperand(0)));
|
2016-07-25 21:01:29 +00: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);
|
2016-07-29 17:43:52 +00:00
|
|
|
MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}})
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op);
|
2016-07-25 21:01:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-29 22:32:36 +00:00
|
|
|
bool IRTranslator::translateCall(const CallInst &CI) {
|
|
|
|
auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
|
|
|
|
const Function &F = *CI.getCalledFunction();
|
|
|
|
Intrinsic::ID ID = F.getIntrinsicID();
|
|
|
|
if (TII && ID == Intrinsic::not_intrinsic)
|
|
|
|
ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(&F));
|
|
|
|
|
|
|
|
assert(ID != Intrinsic::not_intrinsic && "FIXME: support real calls");
|
|
|
|
|
|
|
|
// Need types (starting with return) & args.
|
|
|
|
SmallVector<LLT, 4> Tys;
|
|
|
|
Tys.emplace_back(*CI.getType());
|
|
|
|
for (auto &Arg : CI.arg_operands())
|
|
|
|
Tys.emplace_back(*Arg->getType());
|
|
|
|
|
|
|
|
unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
|
|
|
|
|
|
|
|
for (auto &Arg : CI.arg_operands()) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
|
|
|
|
MIB.addImm(CI->getSExtValue());
|
|
|
|
else
|
|
|
|
MIB.addUse(getOrCreateVReg(*Arg));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:59:52 +00: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-27 17:47:54 +00:00
|
|
|
// Always allocate at least one byte.
|
|
|
|
Size = std::max(Size, 1u);
|
|
|
|
|
2016-07-22 16:59:52 +00:00
|
|
|
unsigned Alignment = AI.getAlignment();
|
|
|
|
if (!Alignment)
|
|
|
|
Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
|
|
|
|
|
|
|
|
unsigned Res = getOrCreateVReg(AI);
|
2016-07-28 20:13:42 +00:00
|
|
|
int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
|
2016-07-22 16:59:52 +00:00
|
|
|
MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-05 17:16:40 +00:00
|
|
|
bool IRTranslator::translatePhi(const PHINode &PI) {
|
|
|
|
MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
|
|
|
|
MIB.addDef(getOrCreateVReg(PI));
|
|
|
|
|
|
|
|
PendingPHIs.emplace_back(&PI, MIB.getInstr());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRTranslator::finishPendingPhis() {
|
|
|
|
for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
|
|
|
|
const PHINode *PI = Phi.first;
|
|
|
|
MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
|
|
|
|
|
|
|
|
// All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
|
|
|
|
// won't create extra control flow here, otherwise we need to find the
|
|
|
|
// dominating predecessor here (or perhaps force the weirder IRTranslators
|
|
|
|
// to provide a simple boundary).
|
|
|
|
for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
|
|
|
|
assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
|
|
|
|
"I appear to have misunderstood Machine PHIs");
|
|
|
|
MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
|
|
|
|
MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 17:50:36 +00:00
|
|
|
|
|
|
|
PendingPHIs.clear();
|
2016-08-05 17:16:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 22:59:27 +00:00
|
|
|
bool IRTranslator::translate(const Instruction &Inst) {
|
2016-02-11 17:53:23 +00:00
|
|
|
MIRBuilder.setDebugLoc(Inst.getDebugLoc());
|
2016-02-10 22:59:27 +00:00
|
|
|
switch(Inst.getOpcode()) {
|
2016-07-21 17:26:41 +00:00
|
|
|
// Arithmetic operations.
|
2016-02-11 18:53:28 +00:00
|
|
|
case Instruction::Add:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBinaryOp(TargetOpcode::G_ADD, cast<BinaryOperator>(Inst));
|
2016-07-21 17:26:50 +00:00
|
|
|
case Instruction::Sub:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBinaryOp(TargetOpcode::G_SUB, cast<BinaryOperator>(Inst));
|
2016-07-22 16:59:52 +00:00
|
|
|
|
2016-07-21 17:26:41 +00:00
|
|
|
// Bitwise operations.
|
2016-07-21 15:50:42 +00:00
|
|
|
case Instruction::And:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBinaryOp(TargetOpcode::G_AND, cast<BinaryOperator>(Inst));
|
2016-08-04 21:39:44 +00:00
|
|
|
case Instruction::Mul:
|
|
|
|
return translateBinaryOp(TargetOpcode::G_MUL, cast<BinaryOperator>(Inst));
|
2016-06-10 20:50:35 +00:00
|
|
|
case Instruction::Or:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBinaryOp(TargetOpcode::G_OR, cast<BinaryOperator>(Inst));
|
2016-07-29 16:56:20 +00:00
|
|
|
case Instruction::Xor:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBinaryOp(TargetOpcode::G_XOR, cast<BinaryOperator>(Inst));
|
2016-07-22 16:59:52 +00:00
|
|
|
|
2016-07-21 17:26:41 +00:00
|
|
|
// Branch operations.
|
2016-03-11 17:28:03 +00:00
|
|
|
case Instruction::Br:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateBr(cast<BranchInst>(Inst));
|
2016-02-11 18:53:28 +00:00
|
|
|
case Instruction::Ret:
|
2016-07-29 18:11:21 +00:00
|
|
|
return translateReturn(cast<ReturnInst>(Inst));
|
2016-02-11 18:53:28 +00:00
|
|
|
|
2016-07-29 22:32:36 +00:00
|
|
|
// Calls
|
|
|
|
case Instruction::Call:
|
|
|
|
return translateCall(cast<CallInst>(Inst));
|
|
|
|
|
2016-08-04 18:35:17 +00:00
|
|
|
// Casts and allied operations
|
2016-07-25 21:01:29 +00:00
|
|
|
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-08-04 18:35:17 +00:00
|
|
|
case Instruction::Trunc:
|
|
|
|
return translateCast(TargetOpcode::G_TRUNC, cast<CastInst>(Inst));
|
2016-07-25 21:01:29 +00:00
|
|
|
|
2016-07-26 20:23:26 +00:00
|
|
|
// Memory ops.
|
|
|
|
case Instruction::Load:
|
|
|
|
return translateLoad(cast<LoadInst>(Inst));
|
|
|
|
case Instruction::Store:
|
|
|
|
return translateStore(cast<StoreInst>(Inst));
|
|
|
|
|
2016-07-22 16:59:52 +00:00
|
|
|
case Instruction::Alloca:
|
|
|
|
return translateStaticAlloca(cast<AllocaInst>(Inst));
|
|
|
|
|
2016-08-05 17:16:40 +00:00
|
|
|
case Instruction::PHI:
|
|
|
|
return translatePhi(cast<PHINode>(Inst));
|
|
|
|
|
2016-07-29 22:41:55 +00:00
|
|
|
case Instruction::Unreachable:
|
|
|
|
return true;
|
|
|
|
|
2016-02-11 18:53:28 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Opcode not supported");
|
2016-02-10 22:59:27 +00:00
|
|
|
}
|
2016-01-20 20:58:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 21:28:04 +00:00
|
|
|
bool IRTranslator::translate(const Constant &C, unsigned Reg) {
|
|
|
|
if (auto CI = dyn_cast<ConstantInt>(&C)) {
|
|
|
|
EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unhandled constant kind");
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:58:56 +00:00
|
|
|
|
|
|
|
void IRTranslator::finalize() {
|
2016-02-10 22:59:27 +00:00
|
|
|
// Release the memory used by the different maps we
|
|
|
|
// needed during the translation.
|
2016-02-11 21:48:32 +00:00
|
|
|
ValToVReg.clear();
|
2016-02-10 22:59:27 +00:00
|
|
|
Constants.clear();
|
2016-01-20 20:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
|
2016-02-10 22:59:27 +00:00
|
|
|
const Function &F = *MF.getFunction();
|
2016-02-11 19:59:41 +00:00
|
|
|
if (F.empty())
|
|
|
|
return false;
|
2016-02-16 19:26:02 +00:00
|
|
|
CLI = MF.getSubtarget().getCallLowering();
|
2016-03-11 17:27:51 +00:00
|
|
|
MIRBuilder.setMF(MF);
|
2016-08-09 21:28:04 +00:00
|
|
|
EntryBuilder.setMF(MF);
|
2016-02-11 17:51:31 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2016-07-22 16:59:52 +00:00
|
|
|
DL = &F.getParent()->getDataLayout();
|
|
|
|
|
2016-08-05 17:50:36 +00:00
|
|
|
assert(PendingPHIs.empty() && "stale PHIs");
|
|
|
|
|
2016-02-11 19:59:41 +00:00
|
|
|
// Setup the arguments.
|
2016-03-11 17:27:43 +00:00
|
|
|
MachineBasicBlock &MBB = getOrCreateBB(F.front());
|
2016-03-11 17:27:47 +00:00
|
|
|
MIRBuilder.setMBB(MBB);
|
2016-02-11 19:59:41 +00:00
|
|
|
SmallVector<unsigned, 8> VRegArgs;
|
|
|
|
for (const Argument &Arg: F.args())
|
2016-03-11 17:27:54 +00:00
|
|
|
VRegArgs.push_back(getOrCreateVReg(Arg));
|
2016-02-16 19:26:02 +00:00
|
|
|
bool Succeeded =
|
2016-04-14 17:23:33 +00:00
|
|
|
CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
|
2016-02-11 19:59:41 +00:00
|
|
|
if (!Succeeded)
|
|
|
|
report_fatal_error("Unable to lower arguments");
|
|
|
|
|
2016-08-09 21:28:04 +00:00
|
|
|
// Now that we've got the ABI handling code, it's safe to set a location for
|
|
|
|
// any Constants we find in the IR.
|
|
|
|
if (MBB.empty())
|
|
|
|
EntryBuilder.setMBB(MBB);
|
|
|
|
else
|
|
|
|
EntryBuilder.setInstr(MBB.back(), /* Before */ false);
|
|
|
|
|
2016-02-10 22:59:27 +00:00
|
|
|
for (const BasicBlock &BB: F) {
|
2016-03-11 17:27:43 +00:00
|
|
|
MachineBasicBlock &MBB = getOrCreateBB(BB);
|
2016-03-11 17:27:47 +00:00
|
|
|
// Set the insertion point of all the following translations to
|
|
|
|
// the end of this basic block.
|
|
|
|
MIRBuilder.setMBB(MBB);
|
2016-02-10 22:59:27 +00: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-12 22:23:42 +00:00
|
|
|
|
2016-08-05 17:16:40 +00:00
|
|
|
finishPendingPhis();
|
|
|
|
|
2016-07-12 22:23:42 +00:00
|
|
|
// Now that the MachineFrameInfo has been configured, no further changes to
|
|
|
|
// the reserved registers are possible.
|
|
|
|
MRI->freezeReservedRegs(MF);
|
|
|
|
|
2016-01-20 20:58:56 +00:00
|
|
|
return false;
|
|
|
|
}
|