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
|
|
|
|
|
|
|
MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, ArrayRef<LLT> Tys) {
|
2016-03-12 01:27:58 +08:00
|
|
|
MachineInstr *NewMI = 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)
|
|
|
|
NewMI->setType(Tys[i], i);
|
2016-02-12 05:16:56 +08:00
|
|
|
} else
|
|
|
|
assert(!isPreISelGenericOpcode(Opcode) &&
|
|
|
|
"Generic instruction must have a type");
|
2016-02-12 02:53:28 +08:00
|
|
|
getMBB().insert(getInsertPt(), NewMI);
|
|
|
|
return NewMI;
|
|
|
|
}
|
|
|
|
|
2016-07-23 00:59:52 +08:00
|
|
|
MachineInstr *MachineIRBuilder::buildFrameIndex(LLT Ty, unsigned Res, int Idx) {
|
|
|
|
MachineInstr *NewMI = buildInstr(TargetOpcode::G_FRAME_INDEX, Ty);
|
|
|
|
auto MIB = MachineInstrBuilder(getMF(), NewMI);
|
|
|
|
MIB.addReg(Res, RegState::Define);
|
2016-07-27 01:42:40 +08:00
|
|
|
MIB.addFrameIndex(Idx);
|
2016-07-23 00:59:52 +08:00
|
|
|
return NewMI;
|
|
|
|
}
|
2016-07-23 04:03:43 +08:00
|
|
|
|
|
|
|
MachineInstr *MachineIRBuilder::buildAdd(LLT Ty, unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
|
|
|
return buildInstr(TargetOpcode::G_ADD, Ty, Res, Op0, Op1);
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:45:26 +08:00
|
|
|
MachineInstr *MachineIRBuilder::buildBr(MachineBasicBlock &Dest) {
|
|
|
|
MachineInstr *NewMI = buildInstr(TargetOpcode::G_BR, LLT::unsized());
|
|
|
|
MachineInstrBuilder(getMF(), NewMI).addMBB(&Dest);
|
|
|
|
return NewMI;
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:45:30 +08:00
|
|
|
MachineInstr *MachineIRBuilder::buildCopy(unsigned Res, unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::COPY, Res, Op);
|
|
|
|
}
|
|
|
|
|
2016-07-23 04:03:43 +08:00
|
|
|
MachineInstr *MachineIRBuilder::buildExtract(LLT Ty, ArrayRef<unsigned> Results,
|
|
|
|
unsigned Src,
|
|
|
|
ArrayRef<unsigned> Indexes) {
|
|
|
|
assert(Results.size() == Indexes.size() && "inconsistent number of regs");
|
|
|
|
|
|
|
|
MachineInstr *NewMI = buildInstr(TargetOpcode::G_EXTRACT, Ty);
|
|
|
|
auto MIB = MachineInstrBuilder(getMF(), NewMI);
|
|
|
|
for (auto Res : Results)
|
|
|
|
MIB.addReg(Res, RegState::Define);
|
|
|
|
|
|
|
|
MIB.addReg(Src);
|
|
|
|
|
|
|
|
for (auto Idx : Indexes)
|
|
|
|
MIB.addImm(Idx);
|
|
|
|
return NewMI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *MachineIRBuilder::buildSequence(LLT Ty, unsigned Res,
|
|
|
|
ArrayRef<unsigned> Ops) {
|
|
|
|
MachineInstr *NewMI = buildInstr(TargetOpcode::G_SEQUENCE, Ty);
|
|
|
|
auto MIB = MachineInstrBuilder(getMF(), NewMI);
|
|
|
|
MIB.addReg(Res, RegState::Define);
|
|
|
|
for (auto Op : Ops)
|
|
|
|
MIB.addReg(Op);
|
|
|
|
return NewMI;
|
|
|
|
}
|