2002-10-28 22:31:48 +01:00
|
|
|
//===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
|
2005-04-21 22:39:54 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:39:54 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-28 22:31:48 +01:00
|
|
|
//
|
|
|
|
// This file exposes a function named BuildMI, which is useful for dramatically
|
2006-05-04 19:02:51 +02:00
|
|
|
// simplifying how MachineInstr's are created. It allows use of code like this:
|
2002-10-28 22:31:48 +01:00
|
|
|
//
|
2002-10-28 22:43:42 +01:00
|
|
|
// M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
|
2002-10-28 22:31:48 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
|
|
|
|
#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
|
|
|
|
|
2006-11-14 00:36:35 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-10-28 22:31:48 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2008-01-07 08:27:27 +01:00
|
|
|
class TargetInstrDesc;
|
2010-01-13 01:00:24 +01:00
|
|
|
class MDNode;
|
2006-11-28 00:37:22 +01:00
|
|
|
|
2009-05-13 23:33:08 +02:00
|
|
|
namespace RegState {
|
|
|
|
enum {
|
|
|
|
Define = 0x2,
|
|
|
|
Implicit = 0x4,
|
|
|
|
Kill = 0x8,
|
|
|
|
Dead = 0x10,
|
2009-06-30 10:49:04 +02:00
|
|
|
Undef = 0x20,
|
|
|
|
EarlyClobber = 0x40,
|
2010-02-06 03:28:32 +01:00
|
|
|
Debug = 0x80,
|
2009-05-13 23:33:08 +02:00
|
|
|
ImplicitDefine = Implicit | Define,
|
|
|
|
ImplicitKill = Implicit | Kill
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-01-13 01:18:44 +01:00
|
|
|
class MachineInstrBuilder {
|
2002-10-28 22:31:48 +01:00
|
|
|
MachineInstr *MI;
|
2003-01-13 01:18:44 +01:00
|
|
|
public:
|
2009-07-09 08:44:01 +02:00
|
|
|
MachineInstrBuilder() : MI(0) {}
|
2007-03-23 19:44:11 +01:00
|
|
|
explicit MachineInstrBuilder(MachineInstr *mi) : MI(mi) {}
|
2002-10-28 22:31:48 +01:00
|
|
|
|
|
|
|
/// Allow automatic conversion to the machine instruction we are working on.
|
|
|
|
///
|
|
|
|
operator MachineInstr*() const { return MI; }
|
2004-04-01 06:03:10 +02:00
|
|
|
operator MachineBasicBlock::iterator() const { return MI; }
|
2002-10-28 22:31:48 +01:00
|
|
|
|
2002-10-28 22:43:42 +01:00
|
|
|
/// addReg - Add a new virtual register operand...
|
2002-10-28 22:31:48 +01:00
|
|
|
///
|
2006-11-14 00:36:35 +01:00
|
|
|
const
|
2009-05-13 23:33:08 +02:00
|
|
|
MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
|
|
|
|
unsigned SubReg = 0) const {
|
|
|
|
assert((flags & 0x1) == 0 &&
|
|
|
|
"Passing in 'true' to addReg is forbidden! Use enums instead.");
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(RegNo,
|
|
|
|
flags & RegState::Define,
|
|
|
|
flags & RegState::Implicit,
|
|
|
|
flags & RegState::Kill,
|
|
|
|
flags & RegState::Dead,
|
2009-06-30 10:49:04 +02:00
|
|
|
flags & RegState::Undef,
|
|
|
|
flags & RegState::EarlyClobber,
|
2010-02-06 03:28:32 +01:00
|
|
|
SubReg,
|
|
|
|
flags & RegState::Debug));
|
2002-10-28 22:31:48 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2004-02-29 06:06:49 +01:00
|
|
|
/// addImm - Add a new immediate operand.
|
|
|
|
///
|
2006-05-04 20:05:43 +02:00
|
|
|
const MachineInstrBuilder &addImm(int64_t Val) const {
|
2007-12-30 01:35:18 +01:00
|
|
|
MI->addOperand(MachineOperand::CreateImm(Val));
|
2004-02-29 06:06:49 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2002-10-28 22:31:48 +01:00
|
|
|
|
2008-09-12 20:08:03 +02:00
|
|
|
const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
|
2008-08-27 01:19:23 +02:00
|
|
|
MI->addOperand(MachineOperand::CreateFPImm(Val));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-06-25 03:16:22 +02:00
|
|
|
const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
|
|
|
|
unsigned char TargetFlags = 0) const {
|
|
|
|
MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
|
2002-12-15 09:01:02 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2002-12-25 06:01:18 +01:00
|
|
|
|
|
|
|
const MachineInstrBuilder &addFrameIndex(unsigned Idx) const {
|
2007-12-30 01:45:46 +01:00
|
|
|
MI->addOperand(MachineOperand::CreateFI(Idx));
|
2002-12-25 06:01:18 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2003-01-13 01:18:44 +01:00
|
|
|
|
2006-02-25 10:54:52 +01:00
|
|
|
const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
|
2009-06-25 03:16:22 +02:00
|
|
|
int Offset = 0,
|
|
|
|
unsigned char TargetFlags = 0) const {
|
|
|
|
MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
|
2003-01-13 01:18:44 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-06-25 03:16:22 +02:00
|
|
|
const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
|
|
|
|
unsigned char TargetFlags = 0) const {
|
|
|
|
MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
|
2006-04-24 08:42:15 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
|
2009-06-25 03:16:22 +02:00
|
|
|
int64_t Offset = 0,
|
|
|
|
unsigned char TargetFlags = 0) const {
|
|
|
|
MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
|
2003-01-13 01:18:44 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
Teach DAGCombine to fold constant offsets into GlobalAddress nodes,
and add a TargetLowering hook for it to use to determine when this
is legal (i.e. not in PIC mode, etc.)
This allows instruction selection to emit folded constant offsets
in more cases, such as the included testcase, eliminating the need
for explicit arithmetic instructions.
This eliminates the need for the C++ code in X86ISelDAGToDAG.cpp
that attempted to achieve the same effect, but wasn't as effective.
Also, fix handling of offsets in GlobalAddressSDNodes in several
places, including changing GlobalAddressSDNode's offset from
int to int64_t.
The Mips, Alpha, Sparc, and CellSPU targets appear to be
unaware of GlobalAddress offsets currently, so set the hook to
false on those targets.
llvm-svn: 57748
2008-10-18 04:06:02 +02:00
|
|
|
const MachineInstrBuilder &addExternalSymbol(const char *FnName,
|
2009-06-25 03:16:22 +02:00
|
|
|
unsigned char TargetFlags = 0) const {
|
2009-09-02 00:06:46 +02:00
|
|
|
MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
|
2003-01-13 01:18:44 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2008-12-03 19:11:40 +01:00
|
|
|
|
2009-09-25 22:36:54 +02:00
|
|
|
const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
|
2008-12-03 19:11:40 +01:00
|
|
|
MI->addMemOperand(*MI->getParent()->getParent(), MMO);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-02-18 06:45:50 +01:00
|
|
|
|
|
|
|
const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
|
2009-06-25 19:28:07 +02:00
|
|
|
MI->addOperand(MO);
|
2009-02-18 06:45:50 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2010-01-13 01:00:24 +01:00
|
|
|
|
2010-02-26 20:39:56 +01:00
|
|
|
const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
|
2010-01-13 01:00:24 +01:00
|
|
|
MI->addOperand(MachineOperand::CreateMetadata(MD));
|
|
|
|
return *this;
|
|
|
|
}
|
2010-03-13 09:16:25 +01:00
|
|
|
|
|
|
|
const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
|
|
|
|
MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
|
|
|
|
return *this;
|
|
|
|
}
|
2002-10-28 22:31:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// BuildMI - Builder interface. Specify how to create the initial instruction
|
2006-11-28 00:37:22 +01:00
|
|
|
/// itself.
|
2002-10-28 22:31:48 +01:00
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineFunction &MF,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID) {
|
|
|
|
return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL));
|
2002-10-28 22:31:48 +01:00
|
|
|
}
|
|
|
|
|
2004-05-23 07:04:00 +02:00
|
|
|
/// BuildMI - This version of the builder sets up the first operand as a
|
2006-11-28 00:37:22 +01:00
|
|
|
/// destination virtual register.
|
2002-12-13 10:33:06 +01:00
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineFunction &MF,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID,
|
|
|
|
unsigned DestReg) {
|
|
|
|
return MachineInstrBuilder(MF.CreateMachineInstr(TID, DL))
|
2009-05-13 23:33:08 +02:00
|
|
|
.addReg(DestReg, RegState::Define);
|
2002-12-13 10:33:06 +01:00
|
|
|
}
|
|
|
|
|
2004-05-23 07:04:00 +02:00
|
|
|
/// BuildMI - This version of the builder inserts the newly-built
|
|
|
|
/// instruction before the given position in the given MachineBasicBlock, and
|
|
|
|
/// sets up the first operand as a destination virtual register.
|
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID,
|
|
|
|
unsigned DestReg) {
|
|
|
|
MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
|
2004-02-29 05:55:28 +01:00
|
|
|
BB.insert(I, MI);
|
2009-05-13 23:33:08 +02:00
|
|
|
return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
|
2004-02-29 05:55:28 +01:00
|
|
|
}
|
|
|
|
|
2004-05-23 07:04:00 +02:00
|
|
|
/// BuildMI - This version of the builder inserts the newly-built
|
|
|
|
/// instruction before the given position in the given MachineBasicBlock, and
|
|
|
|
/// does NOT take a destination register.
|
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID) {
|
|
|
|
MachineInstr *MI = BB.getParent()->CreateMachineInstr(TID, DL);
|
2004-02-29 05:55:28 +01:00
|
|
|
BB.insert(I, MI);
|
|
|
|
return MachineInstrBuilder(MI);
|
|
|
|
}
|
|
|
|
|
2004-05-23 07:04:00 +02:00
|
|
|
/// BuildMI - This version of the builder inserts the newly-built
|
|
|
|
/// instruction at the end of the given MachineBasicBlock, and does NOT take a
|
|
|
|
/// destination register.
|
2002-10-30 02:48:41 +01:00
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID) {
|
|
|
|
return BuildMI(*BB, BB->end(), DL, TID);
|
|
|
|
}
|
2002-10-30 00:18:23 +01:00
|
|
|
|
2004-05-23 07:04:00 +02:00
|
|
|
/// BuildMI - This version of the builder inserts the newly-built
|
|
|
|
/// instruction at the end of the given MachineBasicBlock, and sets up the first
|
2009-09-20 06:03:25 +02:00
|
|
|
/// operand as a destination virtual register.
|
2002-10-30 02:48:41 +01:00
|
|
|
///
|
2009-02-03 01:55:04 +01:00
|
|
|
inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
|
|
|
|
DebugLoc DL,
|
|
|
|
const TargetInstrDesc &TID,
|
|
|
|
unsigned DestReg) {
|
|
|
|
return BuildMI(*BB, BB->end(), DL, TID, DestReg);
|
|
|
|
}
|
2002-10-30 02:48:41 +01:00
|
|
|
|
2009-05-13 23:33:08 +02:00
|
|
|
inline unsigned getDefRegState(bool B) {
|
|
|
|
return B ? RegState::Define : 0;
|
|
|
|
}
|
|
|
|
inline unsigned getImplRegState(bool B) {
|
|
|
|
return B ? RegState::Implicit : 0;
|
|
|
|
}
|
|
|
|
inline unsigned getKillRegState(bool B) {
|
|
|
|
return B ? RegState::Kill : 0;
|
|
|
|
}
|
|
|
|
inline unsigned getDeadRegState(bool B) {
|
|
|
|
return B ? RegState::Dead : 0;
|
|
|
|
}
|
2009-06-30 10:49:04 +02:00
|
|
|
inline unsigned getUndefRegState(bool B) {
|
|
|
|
return B ? RegState::Undef : 0;
|
|
|
|
}
|
2009-05-13 23:33:08 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-10-28 22:31:48 +01:00
|
|
|
#endif
|