From: Quentin Colombet Date: Thu, 11 Feb 2016 17:44:59 +0000 (+0000) Subject: [GlobalISel] Add a MachineIRBuilder class. X-Git-Tag: android-x86-7.1-r4~37856 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=eb096dc0d46996acc1a61e0d89c196cc71bb7f63;p=android-x86%2Fexternal-llvm.git [GlobalISel] Add a MachineIRBuilder class. 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 --- diff --git a/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h b/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h new file mode 100644 index 00000000000..79887297124 --- /dev/null +++ b/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h @@ -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 = \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 diff --git a/lib/CodeGen/GlobalISel/CMakeLists.txt b/lib/CodeGen/GlobalISel/CMakeLists.txt index 07968c8123a..c18934acca1 100644 --- a/lib/CodeGen/GlobalISel/CMakeLists.txt +++ b/lib/CodeGen/GlobalISel/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_library(LLVMGlobalISel IRTranslator.cpp + MachineIRBuilder.cpp ) add_dependencies(LLVMGlobalISel intrinsics_gen) diff --git a/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp b/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp new file mode 100644 index 00000000000..77e73d11318 --- /dev/null +++ b/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp @@ -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; +}