Skip to content

Commit

Permalink
[GlobalISel] Add a MachineIRBuilder class.
Browse files Browse the repository at this point in the history
Helper class to build machine instrs. This is a higher abstraction
than MachineInstrBuilder.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260547 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Quentin Colombet committed Feb 11, 2016
1 parent 9a9aa40 commit eb096dc
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
97 changes: 97 additions & 0 deletions include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - 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 declares the MachineIRBuilder class.
/// This is a helper class to build MachineInstr.
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
#define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H

#include "llvm/CodeGen/GlobalISel/Types.h"

#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/IR/DebugLoc.h"

namespace llvm {

// Forward declarations.
class MachineFunction;
class MachineInstr;
class TargetInstrInfo;

/// Helper class to build MachineInstr.
/// It keeps internally the insertion point and debug location for all
/// the new instructions we want to create.
/// This information can be modify via the related setters.
class MachineIRBuilder {
/// MachineFunction under construction.
MachineFunction *MF;
/// Information used to access the description of the opcodes.
const TargetInstrInfo *TII;
/// Debug location to be set to any instruction we create.
DebugLoc DL;

/// Fields describing the insertion point.
/// @{
MachineBasicBlock *MBB;
MachineInstr *MI;
bool Before;
/// @}

MachineBasicBlock &getMBB() {
assert(MBB && "MachineBasicBlock is not set");
return *MBB;
}

const TargetInstrInfo &getTII() {
assert(TII && "TargetInstrInfo is not set");
return *TII;
}

/// Current insertion point for new instructions.
MachineBasicBlock::iterator getInsertPt();

public:
/// Getter for the function we currently build.
MachineFunction &getMF() {
assert(MF && "MachineFunction is not set");
return *MF;
}

/// Setters for the insertion point.
/// @{
/// Set MachineFunction where to build instructions.
void setFunction(MachineFunction &);

/// Set the insertion point to the beginning (\p Beginning = true) or end
/// (\p Beginning = false) of \p MBB.
/// \pre \p MBB must be contained by getMF().
void setBasicBlock(MachineBasicBlock &MBB, bool Beginning = false);

/// Set the insertion point to before (\p Before = true) or after
/// (\p Before = false) \p MI.
/// \pre MI must be in getMF().
void setInstr(MachineInstr &MI, bool Before = false);
/// @}

/// Set the debug location to \p DL for all the next build instructions.
void setDebugLoc(const DebugLoc &DL) { this->DL = DL; }

/// Build and insert \p Res<def> = \p Opcode \p Op0, \p Op1.
///
/// \pre setBasicBlock or setMI must have been called.
///
/// \return The newly created instruction.
MachineInstr *buildInstr(unsigned Opcode, unsigned Res, unsigned Op0,
unsigned Op1);
};

} // End namespace llvm.
#endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
1 change: 1 addition & 0 deletions lib/CodeGen/GlobalISel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_llvm_library(LLVMGlobalISel
IRTranslator.cpp
MachineIRBuilder.cpp
)

add_dependencies(LLVMGlobalISel intrinsics_gen)
61 changes: 61 additions & 0 deletions lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===-- 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"
#include "llvm/Target/TargetSubtargetInfo.h"

using namespace llvm;

void MachineIRBuilder::setFunction(MachineFunction &MF) {
this->MF = &MF;
this->MBB = nullptr;
this->TII = MF.getSubtarget().getInstrInfo();
this->DL = DebugLoc();
this->MI = nullptr;
}

void MachineIRBuilder::setBasicBlock(MachineBasicBlock &MBB, bool Beginning) {
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");
setBasicBlock(*MI.getParent());
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();
}

MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res,
unsigned Op0, unsigned Op1) {
MachineInstr *NewMI =
BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0).addReg(Op1);
getMBB().insert(getInsertPt(), NewMI);
return NewMI;
}

0 comments on commit eb096dc

Please sign in to comment.