2016-02-12 01:44:59 +08:00
|
|
|
//===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.cpp - MIBuilder--*- 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 MachineIRBuidler class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2016-02-12 05:16:56 +08:00
|
|
|
#include "llvm/Target/TargetOpcodes.h"
|
2016-02-12 01:44:59 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-03-12 01:27:51 +08:00
|
|
|
void MachineIRBuilder::setMF(MachineFunction &MF) {
|
2016-02-12 01:44:59 +08:00
|
|
|
this->MF = &MF;
|
|
|
|
this->MBB = nullptr;
|
|
|
|
this->TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
this->DL = DebugLoc();
|
|
|
|
this->MI = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:27:47 +08:00
|
|
|
void MachineIRBuilder::setMBB(MachineBasicBlock &MBB, bool Beginning) {
|
2016-02-12 01:44:59 +08:00
|
|
|
this->MBB = &MBB;
|
|
|
|
Before = Beginning;
|
|
|
|
assert(&getMF() == MBB.getParent() &&
|
|
|
|
"Basic block is in a different function");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineIRBuilder::setInstr(MachineInstr &MI, bool Before) {
|
|
|
|
assert(MI.getParent() && "Instruction is not part of a basic block");
|
2016-03-12 01:27:47 +08:00
|
|
|
setMBB(*MI.getParent());
|
2016-02-12 01:44:59 +08:00
|
|
|
this->MI = &MI;
|
|
|
|
this->Before = Before;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MachineIRBuilder::getInsertPt() {
|
|
|
|
if (MI) {
|
|
|
|
if (Before)
|
|
|
|
return MI;
|
|
|
|
if (!MI->getNextNode())
|
|
|
|
return getMBB().end();
|
|
|
|
return MI->getNextNode();
|
|
|
|
}
|
|
|
|
return Before ? getMBB().begin() : getMBB().end();
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:27:58 +08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Build instruction variants.
|
|
|
|
//------------------------------------------------------------------------------
|
2016-07-27 00:45:26 +08:00
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildInstr(unsigned Opcode,
|
|
|
|
ArrayRef<LLT> Tys) {
|
|
|
|
MachineInstrBuilder MIB = BuildMI(getMF(), DL, getTII().get(Opcode));
|
2016-07-27 00:45:26 +08:00
|
|
|
if (Tys.size() > 0) {
|
2016-02-12 05:16:56 +08:00
|
|
|
assert(isPreISelGenericOpcode(Opcode) &&
|
|
|
|
"Only generic instruction can have a type");
|
2016-07-27 00:45:26 +08:00
|
|
|
for (unsigned i = 0; i < Tys.size(); ++i)
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB->setType(Tys[i], i);
|
2016-02-12 05:16:56 +08:00
|
|
|
} else
|
|
|
|
assert(!isPreISelGenericOpcode(Opcode) &&
|
|
|
|
"Generic instruction must have a type");
|
2016-07-30 01:43:52 +08:00
|
|
|
getMBB().insert(getInsertPt(), MIB);
|
|
|
|
return MIB;
|
2016-02-12 02:53:28 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFrameIndex(LLT Ty, unsigned Res,
|
|
|
|
int Idx) {
|
|
|
|
return buildInstr(TargetOpcode::G_FRAME_INDEX, Ty)
|
|
|
|
.addDef(Res)
|
|
|
|
.addFrameIndex(Idx);
|
2016-07-23 00:59:52 +08:00
|
|
|
}
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildAdd(LLT Ty, unsigned Res,
|
|
|
|
unsigned Op0, unsigned Op1) {
|
|
|
|
return buildInstr(TargetOpcode::G_ADD, Ty)
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildBr(MachineBasicBlock &Dest) {
|
|
|
|
return buildInstr(TargetOpcode::G_BR, LLT::unsized()).addMBB(&Dest);
|
2016-07-27 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildCopy(unsigned Res, unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::COPY).addDef(Res).addUse(Op);
|
2016-07-27 00:45:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 04:54:13 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildConstant(LLT Ty, unsigned Res,
|
|
|
|
int64_t Val) {
|
|
|
|
return buildInstr(TargetOpcode::G_CONSTANT, Ty).addDef(Res).addImm(Val);
|
|
|
|
}
|
|
|
|
|
2016-08-20 04:09:15 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFConstant(LLT Ty, unsigned Res,
|
|
|
|
const ConstantFP &Val) {
|
|
|
|
return buildInstr(TargetOpcode::G_FCONSTANT, Ty).addDef(Res).addFPImm(&Val);
|
|
|
|
}
|
|
|
|
|
2016-07-30 01:58:00 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildBrCond(LLT Ty, unsigned Tst,
|
|
|
|
MachineBasicBlock &Dest) {
|
|
|
|
return buildInstr(TargetOpcode::G_BRCOND, Ty).addUse(Tst).addMBB(&Dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildLoad(LLT VTy, LLT PTy, unsigned Res,
|
2016-07-30 01:43:52 +08:00
|
|
|
unsigned Addr,
|
|
|
|
MachineMemOperand &MMO) {
|
|
|
|
return buildInstr(TargetOpcode::G_LOAD, {VTy, PTy})
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Addr)
|
|
|
|
.addMemOperand(&MMO);
|
2016-07-27 04:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildStore(LLT VTy, LLT PTy,
|
|
|
|
unsigned Val, unsigned Addr,
|
|
|
|
MachineMemOperand &MMO) {
|
|
|
|
return buildInstr(TargetOpcode::G_STORE, {VTy, PTy})
|
|
|
|
.addUse(Val)
|
|
|
|
.addUse(Addr)
|
|
|
|
.addMemOperand(&MMO);
|
2016-07-27 04:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:17:06 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildUAdde(LLT Ty, unsigned Res,
|
|
|
|
unsigned CarryOut,
|
|
|
|
unsigned Op0, unsigned Op1,
|
|
|
|
unsigned CarryIn) {
|
|
|
|
return buildInstr(TargetOpcode::G_UADDE, Ty)
|
2016-08-05 04:54:13 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addDef(CarryOut)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1)
|
|
|
|
.addUse(CarryIn);
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:11 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildAnyExtend(LLT Ty, unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::G_ANYEXTEND, Ty).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:01:26 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSExt(ArrayRef<LLT> Tys, unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::G_SEXT, Tys).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildZExt(ArrayRef<LLT> Tys, unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::G_ZEXT, Tys).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
2016-08-20 02:32:14 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildExtract(ArrayRef<LLT> ResTys,
|
|
|
|
ArrayRef<unsigned> Results,
|
|
|
|
ArrayRef<uint64_t> Indices,
|
|
|
|
LLT SrcTy, unsigned Src) {
|
|
|
|
assert(ResTys.size() == Results.size() && Results.size() == Indices.size() &&
|
|
|
|
"inconsistent number of regs");
|
|
|
|
assert(!Results.empty() && "invalid trivial extract");
|
|
|
|
|
|
|
|
auto MIB = BuildMI(getMF(), DL, getTII().get(TargetOpcode::G_EXTRACT));
|
|
|
|
for (unsigned i = 0; i < ResTys.size(); ++i)
|
|
|
|
MIB->setType(LLT::scalar(ResTys[i].getSizeInBits()), i);
|
|
|
|
MIB->setType(LLT::scalar(SrcTy.getSizeInBits()), ResTys.size());
|
2016-07-23 04:03:43 +08:00
|
|
|
|
|
|
|
for (auto Res : Results)
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addDef(Res);
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addUse(Src);
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-08-20 02:32:14 +08:00
|
|
|
for (auto Idx : Indices)
|
2016-07-23 04:03:43 +08:00
|
|
|
MIB.addImm(Idx);
|
2016-08-20 02:32:14 +08:00
|
|
|
|
|
|
|
getMBB().insert(getInsertPt(), MIB);
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
return MIB;
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:17:06 +08:00
|
|
|
MachineInstrBuilder
|
2016-08-20 02:32:14 +08:00
|
|
|
MachineIRBuilder::buildSequence(LLT ResTy, unsigned Res,
|
|
|
|
ArrayRef<LLT> OpTys,
|
2016-08-20 01:17:06 +08:00
|
|
|
ArrayRef<unsigned> Ops,
|
2016-08-20 02:32:14 +08:00
|
|
|
ArrayRef<unsigned> Indices) {
|
|
|
|
assert(OpTys.size() == Ops.size() && Ops.size() == Indices.size() &&
|
|
|
|
"incompatible args");
|
|
|
|
assert(!Ops.empty() && "invalid trivial sequence");
|
2016-08-20 01:17:06 +08:00
|
|
|
|
2016-08-20 02:32:14 +08:00
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
buildInstr(TargetOpcode::G_SEQUENCE, LLT::scalar(ResTy.getSizeInBits()));
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addDef(Res);
|
2016-08-20 01:17:06 +08:00
|
|
|
for (unsigned i = 0; i < Ops.size(); ++i) {
|
|
|
|
MIB.addUse(Ops[i]);
|
2016-08-20 02:32:14 +08:00
|
|
|
MIB.addImm(Indices[i]);
|
|
|
|
MIB->setType(LLT::scalar(OpTys[i].getSizeInBits()), MIB->getNumTypes());
|
2016-08-20 01:17:06 +08:00
|
|
|
}
|
2016-07-30 01:43:52 +08:00
|
|
|
return MIB;
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
2016-07-30 06:32:36 +08:00
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildIntrinsic(ArrayRef<LLT> Tys,
|
|
|
|
Intrinsic::ID ID,
|
|
|
|
unsigned Res,
|
|
|
|
bool HasSideEffects) {
|
|
|
|
auto MIB =
|
|
|
|
buildInstr(HasSideEffects ? TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
|
|
|
|
: TargetOpcode::G_INTRINSIC,
|
|
|
|
Tys);
|
|
|
|
if (Res)
|
|
|
|
MIB.addDef(Res);
|
|
|
|
MIB.addIntrinsicID(ID);
|
|
|
|
return MIB;
|
|
|
|
}
|
2016-08-05 02:35:11 +08:00
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildTrunc(LLT Ty, unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::G_TRUNC, Ty).addDef(Res).addUse(Op);
|
|
|
|
}
|
2016-08-18 04:25:25 +08:00
|
|
|
|
2016-08-20 06:40:08 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFPTrunc(LLT Ty, unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::G_FPTRUNC, Ty).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:25:25 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildICmp(ArrayRef<LLT> Tys,
|
|
|
|
CmpInst::Predicate Pred,
|
|
|
|
unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
|
|
|
return buildInstr(TargetOpcode::G_ICMP, Tys)
|
|
|
|
.addDef(Res)
|
|
|
|
.addPredicate(Pred)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
2016-08-20 04:09:07 +08:00
|
|
|
|
2016-08-20 04:48:16 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFCmp(ArrayRef<LLT> Tys,
|
|
|
|
CmpInst::Predicate Pred,
|
|
|
|
unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
|
|
|
return buildInstr(TargetOpcode::G_FCMP, Tys)
|
|
|
|
.addDef(Res)
|
|
|
|
.addPredicate(Pred)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2016-08-20 04:09:07 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSelect(LLT Ty, unsigned Res,
|
|
|
|
unsigned Tst,
|
|
|
|
unsigned Op0, unsigned Op1) {
|
|
|
|
return buildInstr(TargetOpcode::G_SELECT, {Ty, LLT::scalar(1)})
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Tst)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|