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-11-10 06:39:54 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.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-11-10 06:39:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2016-02-12 01:51:31 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2016-08-27 07:49:05 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2016-02-12 01:51:31 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
2016-02-11 06:59:27 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-09-12 19:20:22 +08:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2016-07-30 06:32:36 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2016-02-12 01:51:31 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2016-07-30 06:32:36 +08:00
|
|
|
#include "llvm/Target/TargetIntrinsicInfo.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-08-27 07:49:05 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
|
|
|
|
INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
|
2016-07-26 11:29:18 +08:00
|
|
|
false, false)
|
2016-01-21 04:58:56 +08:00
|
|
|
|
2016-11-08 09:12:17 +08:00
|
|
|
static void reportTranslationError(const Value &V, const Twine &Message) {
|
|
|
|
std::string ErrStorage;
|
|
|
|
raw_string_ostream Err(ErrStorage);
|
|
|
|
Err << Message << ": " << V << '\n';
|
|
|
|
report_fatal_error(Err.str());
|
|
|
|
}
|
|
|
|
|
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-08-27 07:49:05 +08:00
|
|
|
void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetPassConfig>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-09-15 17:20:34 +08:00
|
|
|
unsigned VReg = MRI->createGenericVirtualRegister(LLT{*Val.getType(), *DL});
|
2016-02-12 05:48:32 +08:00
|
|
|
ValReg = VReg;
|
2016-08-10 05:28:04 +08:00
|
|
|
|
|
|
|
if (auto CV = dyn_cast<Constant>(&Val)) {
|
|
|
|
bool Success = translate(*CV, VReg);
|
2016-08-27 07:49:05 +08:00
|
|
|
if (!Success) {
|
|
|
|
if (!TPC->isGlobalISelAbortEnabled()) {
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->getProperties().set(
|
2016-08-27 07:49:05 +08:00
|
|
|
MachineFunctionProperties::Property::FailedISel);
|
2016-12-06 05:40:33 +08:00
|
|
|
return VReg;
|
2016-08-27 07:49:05 +08:00
|
|
|
}
|
2016-11-08 09:12:17 +08:00
|
|
|
reportTranslationError(Val, "unable to translate constant");
|
2016-08-27 07:49:05 +08:00
|
|
|
}
|
2016-08-10 05:28:04 +08:00
|
|
|
}
|
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-11-01 02:30:59 +08:00
|
|
|
int IRTranslator::getOrCreateFrameIndex(const AllocaInst &AI) {
|
|
|
|
if (FrameIndices.find(&AI) != FrameIndices.end())
|
|
|
|
return FrameIndices[&AI];
|
|
|
|
|
|
|
|
unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
|
|
|
|
unsigned Size =
|
|
|
|
ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
|
|
|
|
|
|
|
|
// Always allocate at least one byte.
|
|
|
|
Size = std::max(Size, 1u);
|
|
|
|
|
|
|
|
unsigned Alignment = AI.getAlignment();
|
|
|
|
if (!Alignment)
|
|
|
|
Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
|
|
|
|
|
|
|
|
int &FI = FrameIndices[&AI];
|
2016-12-08 05:17:47 +08:00
|
|
|
FI = MF->getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
|
2016-11-01 02:30:59 +08:00
|
|
|
return FI;
|
|
|
|
}
|
|
|
|
|
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();
|
2016-08-27 07:49:05 +08:00
|
|
|
} else if (!TPC->isGlobalISelAbortEnabled()) {
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->getProperties().set(
|
2016-08-27 07:49:05 +08:00
|
|
|
MachineFunctionProperties::Property::FailedISel);
|
|
|
|
return 1;
|
2016-07-27 04:23:26 +08:00
|
|
|
} 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-12-08 05:17:47 +08:00
|
|
|
MBB = MF->CreateMachineBasicBlock();
|
|
|
|
MF->push_back(MBB);
|
2016-02-12 01:51:31 +08:00
|
|
|
}
|
|
|
|
return *MBB;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-07-30 02:11:21 +08:00
|
|
|
// FIXME: handle signed/unsigned wrapping flags.
|
|
|
|
|
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-08-11 07:02:41 +08:00
|
|
|
unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
|
|
|
|
unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
|
|
|
|
unsigned Res = getOrCreateVReg(U);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op0).addUse(Op1);
|
2016-02-12 01:51:31 +08:00
|
|
|
return true;
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateCompare(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-20 04:48:16 +08:00
|
|
|
const CmpInst *CI = dyn_cast<CmpInst>(&U);
|
|
|
|
unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
|
|
|
|
unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
|
|
|
|
unsigned Res = getOrCreateVReg(U);
|
|
|
|
CmpInst::Predicate Pred =
|
|
|
|
CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
|
|
|
|
cast<ConstantExpr>(U).getPredicate());
|
|
|
|
|
|
|
|
if (CmpInst::isIntPredicate(Pred))
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
|
2016-08-20 04:48:16 +08:00
|
|
|
else
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildFCmp(Pred, Res, Op0, Op1);
|
2016-08-20 04:48:16 +08:00
|
|
|
|
2016-08-18 04:25:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateRet(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const ReturnInst &RI = cast<ReturnInst>(U);
|
2016-07-30 02:11:21 +08:00
|
|
|
const Value *Ret = RI.getReturnValue();
|
2016-02-12 02:53:28 +08: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-15 01:23:33 +08:00
|
|
|
return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
|
2016-02-12 02:53:28 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateBr(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const BranchInst &BrInst = cast<BranchInst>(U);
|
2016-07-30 01:58:00 +08: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);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildBrCond(Tst, TrueBB);
|
2016-03-12 01:28:03 +08:00
|
|
|
}
|
2016-07-30 01:58:00 +08:00
|
|
|
|
|
|
|
const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
|
|
|
|
MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
|
|
|
|
MIRBuilder.buildBr(TgtBB);
|
|
|
|
|
2016-03-12 01:28:03 +08:00
|
|
|
// Link successors.
|
|
|
|
MachineBasicBlock &CurBB = MIRBuilder.getMBB();
|
|
|
|
for (const BasicBlock *Succ : BrInst.successors())
|
|
|
|
CurBB.addSuccessor(&getOrCreateBB(*Succ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-05 19:28:51 +08:00
|
|
|
bool IRTranslator::translateSwitch(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
|
|
|
// For now, just translate as a chain of conditional branches.
|
|
|
|
// FIXME: could we share most of the logic/code in
|
|
|
|
// SelectionDAGBuilder::visitSwitch between SelectionDAG and GlobalISel?
|
|
|
|
// At first sight, it seems most of the logic in there is independent of
|
|
|
|
// SelectionDAG-specifics and a lot of work went in to optimize switch
|
|
|
|
// lowering in there.
|
|
|
|
|
|
|
|
const SwitchInst &SwInst = cast<SwitchInst>(U);
|
|
|
|
const unsigned SwCondValue = getOrCreateVReg(*SwInst.getCondition());
|
|
|
|
|
|
|
|
LLT LLTi1 = LLT(*Type::getInt1Ty(U.getContext()), *DL);
|
|
|
|
for (auto &CaseIt : SwInst.cases()) {
|
|
|
|
const unsigned CaseValueReg = getOrCreateVReg(*CaseIt.getCaseValue());
|
|
|
|
const unsigned Tst = MRI->createGenericVirtualRegister(LLTi1);
|
|
|
|
MIRBuilder.buildICmp(CmpInst::ICMP_EQ, Tst, CaseValueReg, SwCondValue);
|
|
|
|
MachineBasicBlock &CurBB = MIRBuilder.getMBB();
|
|
|
|
MachineBasicBlock &TrueBB = getOrCreateBB(*CaseIt.getCaseSuccessor());
|
|
|
|
|
|
|
|
MIRBuilder.buildBrCond(Tst, TrueBB);
|
|
|
|
CurBB.addSuccessor(&TrueBB);
|
|
|
|
|
|
|
|
MachineBasicBlock *FalseBB =
|
|
|
|
MF->CreateMachineBasicBlock(SwInst.getParent());
|
|
|
|
MF->push_back(FalseBB);
|
|
|
|
MIRBuilder.buildBr(*FalseBB);
|
|
|
|
CurBB.addSuccessor(FalseBB);
|
|
|
|
|
|
|
|
MIRBuilder.setMBB(*FalseBB);
|
|
|
|
}
|
|
|
|
// handle default case
|
|
|
|
MachineBasicBlock &DefaultBB = getOrCreateBB(*SwInst.getDefaultDest());
|
|
|
|
MIRBuilder.buildBr(DefaultBB);
|
|
|
|
MIRBuilder.getMBB().addSuccessor(&DefaultBB);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateLoad(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const LoadInst &LI = cast<LoadInst>(U);
|
2016-08-27 07:49:05 +08:00
|
|
|
|
2016-10-19 23:55:06 +08:00
|
|
|
if (!TPC->isGlobalISelAbortEnabled() && LI.isAtomic())
|
2016-08-27 07:49:05 +08:00
|
|
|
return false;
|
|
|
|
|
2016-10-19 23:55:06 +08:00
|
|
|
assert(!LI.isAtomic() && "only non-atomic loads are supported at the moment");
|
|
|
|
auto Flags = LI.isVolatile() ? MachineMemOperand::MOVolatile
|
|
|
|
: MachineMemOperand::MONone;
|
|
|
|
Flags |= MachineMemOperand::MOLoad;
|
2016-07-27 04:23:26 +08:00
|
|
|
|
|
|
|
unsigned Res = getOrCreateVReg(LI);
|
|
|
|
unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
|
2016-09-15 17:20:34 +08:00
|
|
|
LLT VTy{*LI.getType(), *DL}, PTy{*LI.getPointerOperand()->getType(), *DL};
|
2016-07-27 04:23:26 +08:00
|
|
|
MIRBuilder.buildLoad(
|
2016-09-09 19:46:34 +08:00
|
|
|
Res, Addr,
|
2016-12-08 05:17:47 +08:00
|
|
|
*MF->getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
|
|
|
|
Flags, DL->getTypeStoreSize(LI.getType()),
|
|
|
|
getMemOpAlignment(LI)));
|
2016-07-27 04:23:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateStore(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const StoreInst &SI = cast<StoreInst>(U);
|
2016-08-27 07:49:05 +08:00
|
|
|
|
2016-10-19 23:55:06 +08:00
|
|
|
if (!TPC->isGlobalISelAbortEnabled() && SI.isAtomic())
|
2016-08-27 07:49:05 +08:00
|
|
|
return false;
|
|
|
|
|
2016-10-19 23:55:06 +08:00
|
|
|
assert(!SI.isAtomic() && "only non-atomic stores supported at the moment");
|
|
|
|
auto Flags = SI.isVolatile() ? MachineMemOperand::MOVolatile
|
|
|
|
: MachineMemOperand::MONone;
|
|
|
|
Flags |= MachineMemOperand::MOStore;
|
2016-07-27 04:23:26 +08:00
|
|
|
|
|
|
|
unsigned Val = getOrCreateVReg(*SI.getValueOperand());
|
|
|
|
unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
|
2016-09-15 17:20:34 +08:00
|
|
|
LLT VTy{*SI.getValueOperand()->getType(), *DL},
|
|
|
|
PTy{*SI.getPointerOperand()->getType(), *DL};
|
2016-07-27 04:23:26 +08:00
|
|
|
|
|
|
|
MIRBuilder.buildStore(
|
2016-12-08 05:17:47 +08:00
|
|
|
Val, Addr,
|
|
|
|
*MF->getMachineMemOperand(
|
|
|
|
MachinePointerInfo(SI.getPointerOperand()), Flags,
|
|
|
|
DL->getTypeStoreSize(SI.getValueOperand()->getType()),
|
|
|
|
getMemOpAlignment(SI)));
|
2016-07-27 04:23:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateExtractValue(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-20 04:09:03 +08:00
|
|
|
const Value *Src = U.getOperand(0);
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(U.getContext());
|
2016-08-20 01:47:05 +08:00
|
|
|
SmallVector<Value *, 1> Indices;
|
|
|
|
|
|
|
|
// getIndexedOffsetInType is designed for GEPs, so the first index is the
|
|
|
|
// usual array element rather than looking into the actual aggregate.
|
|
|
|
Indices.push_back(ConstantInt::get(Int32Ty, 0));
|
2016-08-20 04:09:03 +08:00
|
|
|
|
|
|
|
if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
|
|
|
|
for (auto Idx : EVI->indices())
|
|
|
|
Indices.push_back(ConstantInt::get(Int32Ty, Idx));
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 1; i < U.getNumOperands(); ++i)
|
|
|
|
Indices.push_back(U.getOperand(i));
|
|
|
|
}
|
2016-08-20 01:47:05 +08:00
|
|
|
|
|
|
|
uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
|
|
|
|
|
2016-08-20 04:09:03 +08:00
|
|
|
unsigned Res = getOrCreateVReg(U);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildExtract(Res, Offset, getOrCreateVReg(*Src));
|
2016-08-20 01:47:05 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateInsertValue(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-20 04:09:03 +08:00
|
|
|
const Value *Src = U.getOperand(0);
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(U.getContext());
|
2016-08-20 04:08:55 +08:00
|
|
|
SmallVector<Value *, 1> Indices;
|
|
|
|
|
|
|
|
// getIndexedOffsetInType is designed for GEPs, so the first index is the
|
|
|
|
// usual array element rather than looking into the actual aggregate.
|
|
|
|
Indices.push_back(ConstantInt::get(Int32Ty, 0));
|
2016-08-20 04:09:03 +08:00
|
|
|
|
|
|
|
if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
|
|
|
|
for (auto Idx : IVI->indices())
|
|
|
|
Indices.push_back(ConstantInt::get(Int32Ty, Idx));
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 2; i < U.getNumOperands(); ++i)
|
|
|
|
Indices.push_back(U.getOperand(i));
|
|
|
|
}
|
2016-08-20 04:08:55 +08:00
|
|
|
|
|
|
|
uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
|
|
|
|
|
2016-08-20 04:09:03 +08:00
|
|
|
unsigned Res = getOrCreateVReg(U);
|
|
|
|
const Value &Inserted = *U.getOperand(1);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildInsert(Res, getOrCreateVReg(*Src), getOrCreateVReg(Inserted),
|
|
|
|
Offset);
|
2016-08-20 04:08:55 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateSelect(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildSelect(getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
|
|
|
|
getOrCreateVReg(*U.getOperand(1)),
|
|
|
|
getOrCreateVReg(*U.getOperand(2)));
|
2016-08-20 04:09:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateBitCast(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-09-15 17:20:34 +08:00
|
|
|
if (LLT{*U.getOperand(0)->getType(), *DL} == LLT{*U.getType(), *DL}) {
|
2016-08-11 07:02:41 +08:00
|
|
|
unsigned &Reg = ValToVReg[&U];
|
2016-08-11 00:51:14 +08:00
|
|
|
if (Reg)
|
2016-08-11 07:02:41 +08:00
|
|
|
MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
|
2016-08-11 00:51:14 +08:00
|
|
|
else
|
2016-08-11 07:02:41 +08:00
|
|
|
Reg = getOrCreateVReg(*U.getOperand(0));
|
2016-07-26 05:01:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-12-08 05:29:15 +08:00
|
|
|
return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
|
2016-07-26 05:01:29 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateCast(unsigned Opcode, const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
unsigned Op = getOrCreateVReg(*U.getOperand(0));
|
|
|
|
unsigned Res = getOrCreateVReg(U);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op);
|
2016-07-26 05:01:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateGetElementPtr(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-09-12 19:20:22 +08:00
|
|
|
// FIXME: support vector GEPs.
|
|
|
|
if (U.getType()->isVectorTy())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value &Op0 = *U.getOperand(0);
|
|
|
|
unsigned BaseReg = getOrCreateVReg(Op0);
|
2016-09-15 17:20:34 +08:00
|
|
|
LLT PtrTy{*Op0.getType(), *DL};
|
2016-09-12 19:20:22 +08:00
|
|
|
unsigned PtrSize = DL->getPointerSizeInBits(PtrTy.getAddressSpace());
|
|
|
|
LLT OffsetTy = LLT::scalar(PtrSize);
|
|
|
|
|
|
|
|
int64_t Offset = 0;
|
|
|
|
for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
|
|
|
|
GTI != E; ++GTI) {
|
|
|
|
const Value *Idx = GTI.getOperand();
|
2016-12-02 10:55:30 +08:00
|
|
|
if (StructType *StTy = GTI.getStructTypeOrNull()) {
|
2016-09-12 19:20:22 +08:00
|
|
|
unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
|
|
|
|
Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
|
|
|
|
|
|
|
|
// If this is a scalar constant or a splat vector of constants,
|
|
|
|
// handle it quickly.
|
|
|
|
if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
|
|
|
|
Offset += ElementSize * CI->getSExtValue();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Offset != 0) {
|
|
|
|
unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
|
|
|
|
unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
|
|
|
|
MIRBuilder.buildConstant(OffsetReg, Offset);
|
|
|
|
MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
|
|
|
|
|
|
|
|
BaseReg = NewBaseReg;
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// N = N + Idx * ElementSize;
|
|
|
|
unsigned ElementSizeReg = MRI->createGenericVirtualRegister(OffsetTy);
|
|
|
|
MIRBuilder.buildConstant(ElementSizeReg, ElementSize);
|
|
|
|
|
|
|
|
unsigned IdxReg = getOrCreateVReg(*Idx);
|
|
|
|
if (MRI->getType(IdxReg) != OffsetTy) {
|
|
|
|
unsigned NewIdxReg = MRI->createGenericVirtualRegister(OffsetTy);
|
|
|
|
MIRBuilder.buildSExtOrTrunc(NewIdxReg, IdxReg);
|
|
|
|
IdxReg = NewIdxReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
|
|
|
|
MIRBuilder.buildMul(OffsetReg, ElementSizeReg, IdxReg);
|
|
|
|
|
|
|
|
unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
|
|
|
|
MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
|
|
|
|
BaseReg = NewBaseReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Offset != 0) {
|
|
|
|
unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
|
|
|
|
MIRBuilder.buildConstant(OffsetReg, Offset);
|
|
|
|
MIRBuilder.buildGEP(getOrCreateVReg(U), BaseReg, OffsetReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateMemcpy(const CallInst &CI,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-10-19 04:03:45 +08:00
|
|
|
LLT SizeTy{*CI.getArgOperand(2)->getType(), *DL};
|
|
|
|
if (cast<PointerType>(CI.getArgOperand(0)->getType())->getAddressSpace() !=
|
|
|
|
0 ||
|
|
|
|
cast<PointerType>(CI.getArgOperand(1)->getType())->getAddressSpace() !=
|
|
|
|
0 ||
|
|
|
|
SizeTy.getSizeInBits() != DL->getPointerSizeInBits(0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<CallLowering::ArgInfo, 8> Args;
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
const auto &Arg = CI.getArgOperand(i);
|
|
|
|
Args.emplace_back(getOrCreateVReg(*Arg), Arg->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineOperand Callee = MachineOperand::CreateES("memcpy");
|
|
|
|
|
|
|
|
return CLI->lowerCall(MIRBuilder, Callee,
|
|
|
|
CallLowering::ArgInfo(0, CI.getType()), Args);
|
|
|
|
}
|
2016-09-12 19:20:22 +08:00
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
void IRTranslator::getStackGuard(unsigned DstReg,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-11-01 02:30:59 +08:00
|
|
|
auto MIB = MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD);
|
|
|
|
MIB.addDef(DstReg);
|
|
|
|
|
2016-12-08 05:17:47 +08:00
|
|
|
auto &TLI = *MF->getSubtarget().getTargetLowering();
|
|
|
|
Value *Global = TLI.getSDagStackGuard(*MF->getFunction()->getParent());
|
2016-11-01 02:30:59 +08:00
|
|
|
if (!Global)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachinePointerInfo MPInfo(Global);
|
2016-12-08 05:17:47 +08:00
|
|
|
MachineInstr::mmo_iterator MemRefs = MF->allocateMemRefsArray(1);
|
2016-11-01 02:30:59 +08:00
|
|
|
auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
|
|
|
|
MachineMemOperand::MODereferenceable;
|
|
|
|
*MemRefs =
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->getMachineMemOperand(MPInfo, Flags, DL->getPointerSizeInBits() / 8,
|
|
|
|
DL->getPointerABIAlignment());
|
2016-11-01 02:30:59 +08:00
|
|
|
MIB.setMemRefs(MemRefs, MemRefs + 1);
|
|
|
|
}
|
|
|
|
|
2016-12-09 06:44:00 +08:00
|
|
|
bool IRTranslator::translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
|
|
|
LLT Ty{*CI.getOperand(0)->getType(), *DL};
|
|
|
|
LLT s1 = LLT::scalar(1);
|
|
|
|
unsigned Width = Ty.getSizeInBits();
|
|
|
|
unsigned Res = MRI->createGenericVirtualRegister(Ty);
|
|
|
|
unsigned Overflow = MRI->createGenericVirtualRegister(s1);
|
|
|
|
auto MIB = MIRBuilder.buildInstr(Op)
|
|
|
|
.addDef(Res)
|
|
|
|
.addDef(Overflow)
|
|
|
|
.addUse(getOrCreateVReg(*CI.getOperand(0)))
|
|
|
|
.addUse(getOrCreateVReg(*CI.getOperand(1)));
|
|
|
|
|
|
|
|
if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
|
|
|
|
unsigned Zero = MRI->createGenericVirtualRegister(s1);
|
|
|
|
EntryBuilder.buildConstant(Zero, 0);
|
|
|
|
MIB.addUse(Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
MIRBuilder.buildSequence(getOrCreateVReg(CI), Res, 0, Overflow, Width);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-20 01:17:06 +08:00
|
|
|
switch (ID) {
|
2016-12-09 06:44:00 +08:00
|
|
|
default:
|
|
|
|
break;
|
2016-12-09 06:44:13 +08:00
|
|
|
case Intrinsic::dbg_declare:
|
|
|
|
case Intrinsic::dbg_value:
|
|
|
|
// FIXME: these obviously need to be supported properly.
|
|
|
|
MF->getProperties().set(
|
|
|
|
MachineFunctionProperties::Property::FailedISel);
|
|
|
|
return true;
|
2016-12-09 06:44:00 +08:00
|
|
|
case Intrinsic::uadd_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDE, MIRBuilder);
|
|
|
|
case Intrinsic::sadd_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
|
|
|
|
case Intrinsic::usub_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBE, MIRBuilder);
|
|
|
|
case Intrinsic::ssub_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
|
|
|
|
case Intrinsic::umul_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
|
|
|
|
case Intrinsic::smul_with_overflow:
|
|
|
|
return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
|
2016-10-19 04:03:45 +08:00
|
|
|
case Intrinsic::memcpy:
|
2016-12-08 05:29:15 +08:00
|
|
|
return translateMemcpy(CI, MIRBuilder);
|
2016-11-10 06:39:54 +08:00
|
|
|
case Intrinsic::eh_typeid_for: {
|
|
|
|
GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
|
|
|
|
unsigned Reg = getOrCreateVReg(CI);
|
2016-12-08 05:17:47 +08:00
|
|
|
unsigned TypeID = MF->getTypeIDFor(GV);
|
2016-11-10 06:39:54 +08:00
|
|
|
MIRBuilder.buildConstant(Reg, TypeID);
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-19 04:03:51 +08:00
|
|
|
case Intrinsic::objectsize: {
|
|
|
|
// If we don't know by now, we're never going to know.
|
|
|
|
const ConstantInt *Min = cast<ConstantInt>(CI.getArgOperand(1));
|
|
|
|
|
|
|
|
MIRBuilder.buildConstant(getOrCreateVReg(CI), Min->isZero() ? -1ULL : 0);
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-01 02:30:59 +08:00
|
|
|
case Intrinsic::stackguard:
|
2016-12-08 05:29:15 +08:00
|
|
|
getStackGuard(getOrCreateVReg(CI), MIRBuilder);
|
2016-11-01 02:30:59 +08:00
|
|
|
return true;
|
|
|
|
case Intrinsic::stackprotector: {
|
|
|
|
LLT PtrTy{*CI.getArgOperand(0)->getType(), *DL};
|
|
|
|
unsigned GuardVal = MRI->createGenericVirtualRegister(PtrTy);
|
2016-12-08 05:29:15 +08:00
|
|
|
getStackGuard(GuardVal, MIRBuilder);
|
2016-11-01 02:30:59 +08:00
|
|
|
|
|
|
|
AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
|
|
|
|
MIRBuilder.buildStore(
|
|
|
|
GuardVal, getOrCreateVReg(*Slot),
|
2016-12-08 05:17:47 +08:00
|
|
|
*MF->getMachineMemOperand(
|
|
|
|
MachinePointerInfo::getFixedStack(*MF,
|
|
|
|
getOrCreateFrameIndex(*Slot)),
|
2016-11-01 02:30:59 +08:00
|
|
|
MachineMemOperand::MOStore | MachineMemOperand::MOVolatile,
|
|
|
|
PtrTy.getSizeInBits() / 8, 8));
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-20 01:17:06 +08:00
|
|
|
}
|
2016-12-09 06:44:00 +08:00
|
|
|
return false;
|
2016-08-20 01:17:06 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateCall(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const CallInst &CI = cast<CallInst>(U);
|
2016-12-08 05:17:47 +08:00
|
|
|
auto TII = MF->getTarget().getIntrinsicInfo();
|
2016-08-11 05:44:01 +08:00
|
|
|
const Function *F = CI.getCalledFunction();
|
|
|
|
|
|
|
|
if (!F || !F->isIntrinsic()) {
|
|
|
|
unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
|
|
|
|
SmallVector<unsigned, 8> Args;
|
|
|
|
for (auto &Arg: CI.arg_operands())
|
|
|
|
Args.push_back(getOrCreateVReg(*Arg));
|
|
|
|
|
2016-08-30 03:07:08 +08:00
|
|
|
return CLI->lowerCall(MIRBuilder, CI, Res, Args, [&]() {
|
|
|
|
return getOrCreateVReg(*CI.getCalledValue());
|
|
|
|
});
|
2016-08-11 05:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Intrinsic::ID ID = F->getIntrinsicID();
|
2016-07-30 06:32:36 +08:00
|
|
|
if (TII && ID == Intrinsic::not_intrinsic)
|
2016-08-11 05:44:01 +08:00
|
|
|
ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
|
2016-07-30 06:32:36 +08:00
|
|
|
|
2016-08-11 05:44:01 +08:00
|
|
|
assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
|
2016-07-30 06:32:36 +08:00
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
if (translateKnownIntrinsic(CI, ID, MIRBuilder))
|
2016-08-20 01:17:06 +08:00
|
|
|
return true;
|
|
|
|
|
2016-07-30 06:32:36 +08:00
|
|
|
unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
|
|
|
|
MachineInstrBuilder MIB =
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildIntrinsic(ID, Res, !CI.doesNotAccessMemory());
|
2016-07-30 06:32:36 +08:00
|
|
|
|
|
|
|
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-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateInvoke(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-11-10 06:39:54 +08:00
|
|
|
const InvokeInst &I = cast<InvokeInst>(U);
|
2016-12-08 05:17:47 +08:00
|
|
|
MCContext &Context = MF->getContext();
|
2016-11-10 06:39:54 +08:00
|
|
|
|
|
|
|
const BasicBlock *ReturnBB = I.getSuccessor(0);
|
|
|
|
const BasicBlock *EHPadBB = I.getSuccessor(1);
|
|
|
|
|
|
|
|
const Value *Callee(I.getCalledValue());
|
|
|
|
const Function *Fn = dyn_cast<Function>(Callee);
|
|
|
|
if (isa<InlineAsm>(Callee))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: support invoking patchpoint and statepoint intrinsics.
|
|
|
|
if (Fn && Fn->isIntrinsic())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: support whatever these are.
|
|
|
|
if (I.countOperandBundlesOfType(LLVMContext::OB_deopt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: support Windows exception handling.
|
|
|
|
if (!isa<LandingPadInst>(EHPadBB->front()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2016-12-02 03:32:15 +08:00
|
|
|
// Emit the actual call, bracketed by EH_LABELs so that the MF knows about
|
2016-11-10 06:39:54 +08:00
|
|
|
// the region covered by the try.
|
2016-12-02 03:32:15 +08:00
|
|
|
MCSymbol *BeginSymbol = Context.createTempSymbol();
|
2016-11-10 06:39:54 +08:00
|
|
|
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
|
|
|
|
|
|
|
|
unsigned Res = I.getType()->isVoidTy() ? 0 : getOrCreateVReg(I);
|
|
|
|
SmallVector<CallLowering::ArgInfo, 8> Args;
|
|
|
|
for (auto &Arg: I.arg_operands())
|
|
|
|
Args.emplace_back(getOrCreateVReg(*Arg), Arg->getType());
|
|
|
|
|
|
|
|
if (!CLI->lowerCall(MIRBuilder, MachineOperand::CreateGA(Fn, 0),
|
|
|
|
CallLowering::ArgInfo(Res, I.getType()), Args))
|
|
|
|
return false;
|
|
|
|
|
2016-12-02 03:32:15 +08:00
|
|
|
MCSymbol *EndSymbol = Context.createTempSymbol();
|
2016-11-10 06:39:54 +08:00
|
|
|
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
|
|
|
|
|
|
|
|
// FIXME: track probabilities.
|
|
|
|
MachineBasicBlock &EHPadMBB = getOrCreateBB(*EHPadBB),
|
|
|
|
&ReturnMBB = getOrCreateBB(*ReturnBB);
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
|
2016-11-10 06:39:54 +08:00
|
|
|
MIRBuilder.getMBB().addSuccessor(&ReturnMBB);
|
|
|
|
MIRBuilder.getMBB().addSuccessor(&EHPadMBB);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateLandingPad(const User &U,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-11-10 06:39:54 +08:00
|
|
|
const LandingPadInst &LP = cast<LandingPadInst>(U);
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = MIRBuilder.getMBB();
|
2016-12-02 03:32:15 +08:00
|
|
|
addLandingPadInfo(LP, MBB);
|
2016-11-10 06:39:54 +08:00
|
|
|
|
|
|
|
MBB.setIsEHPad();
|
|
|
|
|
|
|
|
// If there aren't registers to copy the values into (e.g., during SjLj
|
|
|
|
// exceptions), then don't bother.
|
2016-12-08 05:17:47 +08:00
|
|
|
auto &TLI = *MF->getSubtarget().getTargetLowering();
|
|
|
|
const Constant *PersonalityFn = MF->getFunction()->getPersonalityFn();
|
2016-11-10 06:39:54 +08:00
|
|
|
if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
|
|
|
|
TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If landingpad's return type is token type, we don't create DAG nodes
|
|
|
|
// for its exception pointer and selector value. The extraction of exception
|
|
|
|
// pointer or selector value from token type landingpads is not currently
|
|
|
|
// supported.
|
|
|
|
if (LP.getType()->isTokenTy())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Add a label to mark the beginning of the landing pad. Deletion of the
|
|
|
|
// landing pad can thus be detected via the MachineModuleInfo.
|
|
|
|
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
|
2016-12-08 05:17:47 +08:00
|
|
|
.addSym(MF->addLandingPad(&MBB));
|
2016-11-10 06:39:54 +08:00
|
|
|
|
|
|
|
// Mark exception register as live in.
|
|
|
|
SmallVector<unsigned, 2> Regs;
|
|
|
|
SmallVector<uint64_t, 2> Offsets;
|
|
|
|
LLT p0 = LLT::pointer(0, DL->getPointerSizeInBits());
|
|
|
|
if (unsigned Reg = TLI.getExceptionPointerRegister(PersonalityFn)) {
|
|
|
|
unsigned VReg = MRI->createGenericVirtualRegister(p0);
|
|
|
|
MIRBuilder.buildCopy(VReg, Reg);
|
|
|
|
Regs.push_back(VReg);
|
|
|
|
Offsets.push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unsigned Reg = TLI.getExceptionSelectorRegister(PersonalityFn)) {
|
|
|
|
unsigned VReg = MRI->createGenericVirtualRegister(p0);
|
|
|
|
MIRBuilder.buildCopy(VReg, Reg);
|
|
|
|
Regs.push_back(VReg);
|
|
|
|
Offsets.push_back(p0.getSizeInBits());
|
|
|
|
}
|
|
|
|
|
|
|
|
MIRBuilder.buildSequence(getOrCreateVReg(LP), Regs, Offsets);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translateStaticAlloca(const AllocaInst &AI,
|
|
|
|
MachineIRBuilder &MIRBuilder) {
|
2016-08-27 07:49:05 +08:00
|
|
|
if (!TPC->isGlobalISelAbortEnabled() && !AI.isStaticAlloca())
|
|
|
|
return false;
|
|
|
|
|
2016-07-23 00:59:52 +08:00
|
|
|
assert(AI.isStaticAlloca() && "only handle static allocas now");
|
|
|
|
unsigned Res = getOrCreateVReg(AI);
|
2016-11-01 02:30:59 +08:00
|
|
|
int FI = getOrCreateFrameIndex(AI);
|
2016-09-09 19:46:34 +08:00
|
|
|
MIRBuilder.buildFrameIndex(Res, FI);
|
2016-07-23 00:59:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:29:15 +08:00
|
|
|
bool IRTranslator::translatePHI(const User &U, MachineIRBuilder &MIRBuilder) {
|
2016-08-11 07:02:41 +08:00
|
|
|
const PHINode &PI = cast<PHINode>(U);
|
2016-09-09 19:47:31 +08:00
|
|
|
auto MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
|
2016-08-06 01:16:40 +08:00
|
|
|
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;
|
2016-12-08 05:29:15 +08:00
|
|
|
MachineInstrBuilder MIB(*MF, Phi.second);
|
2016-08-06 01:16:40 +08:00
|
|
|
|
|
|
|
// 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-02-11 06:59:27 +08:00
|
|
|
bool IRTranslator::translate(const Instruction &Inst) {
|
2016-12-08 05:29:15 +08:00
|
|
|
CurBuilder.setDebugLoc(Inst.getDebugLoc());
|
2016-02-11 06:59:27 +08:00
|
|
|
switch(Inst.getOpcode()) {
|
2016-08-11 07:02:41 +08:00
|
|
|
#define HANDLE_INST(NUM, OPCODE, CLASS) \
|
2016-12-08 05:29:15 +08:00
|
|
|
case Instruction::OPCODE: return translate##OPCODE(Inst, CurBuilder);
|
2016-08-11 07:02:41 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2016-02-12 02:53:28 +08:00
|
|
|
default:
|
2016-08-27 07:49:05 +08:00
|
|
|
if (!TPC->isGlobalISelAbortEnabled())
|
|
|
|
return false;
|
2016-08-11 07:02:41 +08:00
|
|
|
llvm_unreachable("unknown opcode");
|
2016-02-11 06:59:27 +08:00
|
|
|
}
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 05:28:04 +08:00
|
|
|
bool IRTranslator::translate(const Constant &C, unsigned Reg) {
|
2016-08-10 07:01:30 +08:00
|
|
|
if (auto CI = dyn_cast<ConstantInt>(&C))
|
2016-12-06 05:54:17 +08:00
|
|
|
EntryBuilder.buildConstant(Reg, *CI);
|
2016-08-20 04:09:15 +08:00
|
|
|
else if (auto CF = dyn_cast<ConstantFP>(&C))
|
2016-09-09 19:46:34 +08:00
|
|
|
EntryBuilder.buildFConstant(Reg, *CF);
|
2016-08-10 07:01:30 +08:00
|
|
|
else if (isa<UndefValue>(C))
|
|
|
|
EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
|
2016-08-12 05:40:55 +08:00
|
|
|
else if (isa<ConstantPointerNull>(C))
|
2016-12-06 05:47:07 +08:00
|
|
|
EntryBuilder.buildConstant(Reg, 0);
|
2016-09-12 20:10:41 +08:00
|
|
|
else if (auto GV = dyn_cast<GlobalValue>(&C))
|
|
|
|
EntryBuilder.buildGlobalValue(Reg, GV);
|
2016-08-11 07:02:41 +08:00
|
|
|
else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
|
|
|
|
switch(CE->getOpcode()) {
|
|
|
|
#define HANDLE_INST(NUM, OPCODE, CLASS) \
|
2016-12-08 05:29:15 +08:00
|
|
|
case Instruction::OPCODE: return translate##OPCODE(*CE, EntryBuilder);
|
2016-08-11 07:02:41 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
|
|
|
default:
|
2016-08-27 07:49:05 +08:00
|
|
|
if (!TPC->isGlobalISelAbortEnabled())
|
|
|
|
return false;
|
2016-08-11 07:02:41 +08:00
|
|
|
llvm_unreachable("unknown opcode");
|
|
|
|
}
|
2016-08-27 07:49:05 +08:00
|
|
|
} else if (!TPC->isGlobalISelAbortEnabled())
|
|
|
|
return false;
|
|
|
|
else
|
2016-08-10 07:01:30 +08:00
|
|
|
llvm_unreachable("unhandled constant kind");
|
2016-08-10 05:28:04 +08:00
|
|
|
|
2016-08-10 07:01:30 +08:00
|
|
|
return true;
|
2016-08-10 05:28:04 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 00:21:29 +08:00
|
|
|
void IRTranslator::finalizeFunction() {
|
2016-02-11 06:59:27 +08:00
|
|
|
// Release the memory used by the different maps we
|
|
|
|
// needed during the translation.
|
2016-12-06 07:10:19 +08:00
|
|
|
PendingPHIs.clear();
|
2016-02-12 05:48:32 +08:00
|
|
|
ValToVReg.clear();
|
2016-11-01 02:30:59 +08:00
|
|
|
FrameIndices.clear();
|
2016-02-11 06:59:27 +08:00
|
|
|
Constants.clear();
|
2016-01-21 04:58:56 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:17:47 +08:00
|
|
|
bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
|
|
|
|
MF = &CurMF;
|
|
|
|
const Function &F = *MF->getFunction();
|
2016-02-12 03:59:41 +08:00
|
|
|
if (F.empty())
|
|
|
|
return false;
|
2016-12-08 05:17:47 +08:00
|
|
|
CLI = MF->getSubtarget().getCallLowering();
|
2016-12-08 05:29:15 +08:00
|
|
|
CurBuilder.setMF(*MF);
|
2016-12-08 05:17:47 +08:00
|
|
|
EntryBuilder.setMF(*MF);
|
|
|
|
MRI = &MF->getRegInfo();
|
2016-07-23 00:59:52 +08:00
|
|
|
DL = &F.getParent()->getDataLayout();
|
2016-08-27 07:49:05 +08:00
|
|
|
TPC = &getAnalysis<TargetPassConfig>();
|
2016-07-23 00:59:52 +08:00
|
|
|
|
2016-08-06 01:50:36 +08:00
|
|
|
assert(PendingPHIs.empty() && "stale PHIs");
|
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
// Setup a separate basic-block for the arguments and constants, falling
|
|
|
|
// through to the IR-level Function's entry block.
|
2016-12-08 05:17:47 +08:00
|
|
|
MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
|
|
|
|
MF->push_back(EntryBB);
|
2016-12-08 05:05:38 +08:00
|
|
|
EntryBB->addSuccessor(&getOrCreateBB(F.front()));
|
|
|
|
EntryBuilder.setMBB(*EntryBB);
|
|
|
|
|
|
|
|
// Lower the actual args into this basic block.
|
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-12-08 05:05:38 +08:00
|
|
|
bool Succeeded = CLI->lowerFormalArguments(EntryBuilder, F, VRegArgs);
|
2016-08-27 07:49:05 +08:00
|
|
|
if (!Succeeded) {
|
|
|
|
if (!TPC->isGlobalISelAbortEnabled()) {
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->getProperties().set(
|
2016-08-27 07:49:05 +08:00
|
|
|
MachineFunctionProperties::Property::FailedISel);
|
2016-12-06 07:10:19 +08:00
|
|
|
finalizeFunction();
|
2016-08-27 07:49:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-12 03:59:41 +08:00
|
|
|
report_fatal_error("Unable to lower arguments");
|
2016-08-27 07:49:05 +08:00
|
|
|
}
|
2016-02-12 03:59:41 +08:00
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
// And translate the function!
|
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.
|
2016-12-08 05:29:15 +08:00
|
|
|
CurBuilder.setMBB(MBB);
|
2016-11-10 06:39:54 +08:00
|
|
|
|
2016-02-11 06:59:27 +08:00
|
|
|
for (const Instruction &Inst: BB) {
|
2016-12-06 07:10:19 +08:00
|
|
|
Succeeded &= translate(Inst);
|
2016-02-11 06:59:27 +08:00
|
|
|
if (!Succeeded) {
|
2016-08-27 07:49:05 +08:00
|
|
|
if (TPC->isGlobalISelAbortEnabled())
|
2016-11-08 09:12:17 +08:00
|
|
|
reportTranslationError(Inst, "unable to translate instruction");
|
2016-12-08 05:17:47 +08:00
|
|
|
MF->getProperties().set(
|
|
|
|
MachineFunctionProperties::Property::FailedISel);
|
2016-08-27 07:49:05 +08:00
|
|
|
break;
|
2016-02-11 06:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 06:23:42 +08:00
|
|
|
|
2016-12-06 07:10:19 +08:00
|
|
|
if (Succeeded) {
|
|
|
|
finishPendingPhis();
|
|
|
|
|
|
|
|
// Now that the MachineFrameInfo has been configured, no further changes to
|
|
|
|
// the reserved registers are possible.
|
2016-12-08 05:17:47 +08:00
|
|
|
MRI->freezeReservedRegs(*MF);
|
2016-12-16 07:32:25 +08:00
|
|
|
|
|
|
|
// Merge the argument lowering and constants block with its single
|
|
|
|
// successor, the LLVM-IR entry block. We want the basic block to
|
|
|
|
// be maximal.
|
|
|
|
assert(EntryBB->succ_size() == 1 &&
|
|
|
|
"Custom BB used for lowering should have only one successor");
|
|
|
|
// Get the successor of the current entry block.
|
|
|
|
MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
|
|
|
|
assert(NewEntryBB.pred_size() == 1 &&
|
|
|
|
"LLVM-IR entry block has a predecessor!?");
|
|
|
|
// Move all the instruction from the current entry block to the
|
|
|
|
// new entry block.
|
|
|
|
NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
|
|
|
|
EntryBB->end());
|
|
|
|
|
|
|
|
// Update the live-in information for the new entry block.
|
|
|
|
for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
|
|
|
|
NewEntryBB.addLiveIn(LiveIn);
|
|
|
|
NewEntryBB.sortUniqueLiveIns();
|
|
|
|
|
|
|
|
// Get rid of the now empty basic block.
|
|
|
|
EntryBB->removeSuccessor(&NewEntryBB);
|
|
|
|
MF->remove(EntryBB);
|
|
|
|
|
|
|
|
assert(&MF->front() == &NewEntryBB &&
|
|
|
|
"New entry wasn't next in the list of basic block!");
|
2016-12-06 07:10:19 +08:00
|
|
|
}
|
2016-08-06 01:16:40 +08:00
|
|
|
|
2016-12-06 07:10:19 +08:00
|
|
|
finalizeFunction();
|
2016-07-13 06:23:42 +08:00
|
|
|
|
2016-01-21 04:58:56 +08:00
|
|
|
return false;
|
|
|
|
}
|