[llvm] r260547 - [GlobalISel] Add a MachineIRBuilder class.
Quentin Colombet via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 11 09:45:00 PST 2016
Author: qcolombet
Date: Thu Feb 11 11:44:59 2016
New Revision: 260547
URL: http://llvm.org/viewvc/llvm-project?rev=260547&view=rev
Log:
[GlobalISel] Add a MachineIRBuilder class.
Helper class to build machine instrs. This is a higher abstraction
than MachineInstrBuilder.
Added:
llvm/trunk/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
llvm/trunk/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
Modified:
llvm/trunk/lib/CodeGen/GlobalISel/CMakeLists.txt
Added: llvm/trunk/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h?rev=260547&view=auto
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h (added)
+++ llvm/trunk/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h Thu Feb 11 11:44:59 2016
@@ -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
Modified: llvm/trunk/lib/CodeGen/GlobalISel/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalISel/CMakeLists.txt?rev=260547&r1=260546&r2=260547&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/GlobalISel/CMakeLists.txt (original)
+++ llvm/trunk/lib/CodeGen/GlobalISel/CMakeLists.txt Thu Feb 11 11:44:59 2016
@@ -1,5 +1,6 @@
add_llvm_library(LLVMGlobalISel
IRTranslator.cpp
+ MachineIRBuilder.cpp
)
add_dependencies(LLVMGlobalISel intrinsics_gen)
Added: llvm/trunk/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp?rev=260547&view=auto
==============================================================================
--- llvm/trunk/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp (added)
+++ llvm/trunk/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp Thu Feb 11 11:44:59 2016
@@ -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;
+}
More information about the llvm-commits
mailing list