2009-10-10 01:32:21 +00:00
|
|
|
//==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG class ---==//
|
2008-09-03 16:01:59 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-10-10 01:32:21 +00:00
|
|
|
// This implements the Emit routines for the SelectionDAG class, which creates
|
|
|
|
// MachineInstrs based on the decisions of the SelectionDAG instruction
|
|
|
|
// selection.
|
2008-09-03 16:01:59 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-10 01:32:21 +00:00
|
|
|
#define DEBUG_TYPE "instr-emitter"
|
|
|
|
#include "InstrEmitter.h"
|
2010-03-14 19:56:39 +00:00
|
|
|
#include "SDNodeDbgValue.h"
|
2008-09-03 16:01:59 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-09-03 16:01:59 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-10-10 01:32:21 +00:00
|
|
|
/// CountResults - The results of target nodes have register or immediate
|
2010-12-23 17:24:32 +00:00
|
|
|
/// operands first, then an optional chain, and optional glue operands (which do
|
2009-10-10 01:32:21 +00:00
|
|
|
/// not go into the resulting MachineInstr).
|
|
|
|
unsigned InstrEmitter::CountResults(SDNode *Node) {
|
|
|
|
unsigned N = Node->getNumValues();
|
2010-12-21 02:38:05 +00:00
|
|
|
while (N && Node->getValueType(N - 1) == MVT::Glue)
|
2009-10-10 01:32:21 +00:00
|
|
|
--N;
|
|
|
|
if (N && Node->getValueType(N - 1) == MVT::Other)
|
|
|
|
--N; // Skip over chain result.
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CountOperands - The inputs to target nodes have any actual inputs first,
|
2010-12-23 17:24:32 +00:00
|
|
|
/// followed by an optional chain operand, then an optional glue operand.
|
2009-10-10 01:32:21 +00:00
|
|
|
/// Compute the number of actual operands that will go into the resulting
|
|
|
|
/// MachineInstr.
|
|
|
|
unsigned InstrEmitter::CountOperands(SDNode *Node) {
|
|
|
|
unsigned N = Node->getNumOperands();
|
2010-12-21 02:38:05 +00:00
|
|
|
while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
|
2009-10-10 01:32:21 +00:00
|
|
|
--N;
|
|
|
|
if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
|
|
|
|
--N; // Ignore chain if it exists.
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
|
|
|
|
/// implicit physical register output.
|
2009-10-10 01:32:21 +00:00
|
|
|
void InstrEmitter::
|
2009-06-26 05:39:02 +00:00
|
|
|
EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned,
|
|
|
|
unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2008-09-03 16:01:59 +00:00
|
|
|
unsigned VRBase = 0;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
// Just use the input register directly!
|
|
|
|
SDValue Op(Node, ResNo);
|
|
|
|
if (IsClone)
|
|
|
|
VRBaseMap.erase(Op);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2008-09-03 16:01:59 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the node is only used by a CopyToReg and the dest reg is a vreg, use
|
|
|
|
// the CopyToReg'd destination register instead of creating a new vreg.
|
|
|
|
bool MatchReg = true;
|
2008-09-16 23:12:11 +00:00
|
|
|
const TargetRegisterClass *UseRC = NULL;
|
2011-06-16 22:50:38 +00:00
|
|
|
EVT VT = Node->getValueType(ResNo);
|
|
|
|
|
|
|
|
// Stick to the preferred register classes for legal types.
|
|
|
|
if (TLI->isTypeLegal(VT))
|
|
|
|
UseRC = TLI->getRegClassFor(VT);
|
|
|
|
|
2009-01-16 20:57:18 +00:00
|
|
|
if (!IsClone && !IsCloned)
|
|
|
|
for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
SDNode *User = *UI;
|
|
|
|
bool Match = true;
|
|
|
|
if (User->getOpcode() == ISD::CopyToReg &&
|
|
|
|
User->getOperand(2).getNode() == Node &&
|
|
|
|
User->getOperand(2).getResNo() == ResNo) {
|
|
|
|
unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
|
|
|
|
VRBase = DestReg;
|
|
|
|
Match = false;
|
|
|
|
} else if (DestReg != SrcReg)
|
|
|
|
Match = false;
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
|
|
|
|
SDValue Op = User->getOperand(i);
|
|
|
|
if (Op.getNode() != Node || Op.getResNo() != ResNo)
|
|
|
|
continue;
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT VT = Node->getValueType(Op.getResNo());
|
2010-12-21 02:38:05 +00:00
|
|
|
if (VT == MVT::Other || VT == MVT::Glue)
|
2009-01-16 20:57:18 +00:00
|
|
|
continue;
|
|
|
|
Match = false;
|
|
|
|
if (User->isMachineOpcode()) {
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII->get(User->getMachineOpcode());
|
2009-07-29 21:36:49 +00:00
|
|
|
const TargetRegisterClass *RC = 0;
|
|
|
|
if (i+II.getNumDefs() < II.getNumOperands())
|
2011-06-27 21:26:13 +00:00
|
|
|
RC = TII->getRegClass(II, i+II.getNumDefs(), TRI);
|
2009-01-16 20:57:18 +00:00
|
|
|
if (!UseRC)
|
|
|
|
UseRC = RC;
|
2009-04-13 15:38:05 +00:00
|
|
|
else if (RC) {
|
2009-08-16 17:40:59 +00:00
|
|
|
const TargetRegisterClass *ComRC = getCommonSubClass(UseRC, RC);
|
|
|
|
// If multiple uses expect disjoint register classes, we emit
|
|
|
|
// copies in AddRegisterOperand.
|
|
|
|
if (ComRC)
|
|
|
|
UseRC = ComRC;
|
2009-04-13 15:38:05 +00:00
|
|
|
}
|
2009-01-16 20:57:18 +00:00
|
|
|
}
|
2008-09-16 23:12:11 +00:00
|
|
|
}
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
2009-01-16 20:57:18 +00:00
|
|
|
MatchReg &= Match;
|
|
|
|
if (VRBase)
|
|
|
|
break;
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
|
2010-06-29 14:02:34 +00:00
|
|
|
SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT);
|
2011-06-16 22:50:38 +00:00
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
// Figure out the register class to create for the destreg.
|
|
|
|
if (VRBase) {
|
2009-10-10 01:32:21 +00:00
|
|
|
DstRC = MRI->getRegClass(VRBase);
|
2008-09-16 23:12:11 +00:00
|
|
|
} else if (UseRC) {
|
|
|
|
assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
|
|
|
|
DstRC = UseRC;
|
2008-09-03 16:01:59 +00:00
|
|
|
} else {
|
2008-09-16 23:12:11 +00:00
|
|
|
DstRC = TLI->getRegClassFor(VT);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If all uses are reading from the src physical register and copying the
|
|
|
|
// register is either impossible or very expensive, then don't create a copy.
|
|
|
|
if (MatchReg && SrcRC->getCopyCost() < 0) {
|
|
|
|
VRBase = SrcReg;
|
|
|
|
} else {
|
|
|
|
// Create the reg, emit the copy.
|
2009-10-10 01:32:21 +00:00
|
|
|
VRBase = MRI->createVirtualRegister(DstRC);
|
2010-07-10 19:08:25 +00:00
|
|
|
BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
|
|
|
|
VRBase).addReg(SrcReg);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDValue Op(Node, ResNo);
|
|
|
|
if (IsClone)
|
|
|
|
VRBaseMap.erase(Op);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2008-09-03 16:01:59 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getDstOfCopyToRegUse - If the only use of the specified result number of
|
|
|
|
/// node is a CopyToReg, return its destination register. Return 0 otherwise.
|
2009-10-10 01:32:21 +00:00
|
|
|
unsigned InstrEmitter::getDstOfOnlyCopyToRegUse(SDNode *Node,
|
|
|
|
unsigned ResNo) const {
|
2008-09-03 16:01:59 +00:00
|
|
|
if (!Node->hasOneUse())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SDNode *User = *Node->use_begin();
|
|
|
|
if (User->getOpcode() == ISD::CopyToReg &&
|
|
|
|
User->getOperand(2).getNode() == Node &&
|
|
|
|
User->getOperand(2).getResNo() == ResNo) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-10 01:32:21 +00:00
|
|
|
void InstrEmitter::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II,
|
2009-01-16 20:57:18 +00:00
|
|
|
bool IsClone, bool IsCloned,
|
2009-01-09 22:44:02 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2010-02-09 19:54:29 +00:00
|
|
|
assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF &&
|
2008-09-03 16:01:59 +00:00
|
|
|
"IMPLICIT_DEF should have been handled as a special case elsewhere!");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < II.getNumDefs(); ++i) {
|
|
|
|
// If the specific node value is only used by a CopyToReg and the dest reg
|
2009-04-13 15:38:05 +00:00
|
|
|
// is a vreg in the same register class, use the CopyToReg'd destination
|
|
|
|
// register instead of creating a new vreg.
|
2008-09-03 16:01:59 +00:00
|
|
|
unsigned VRBase = 0;
|
2011-06-27 21:26:13 +00:00
|
|
|
const TargetRegisterClass *RC = TII->getRegClass(II, i, TRI);
|
2009-07-11 01:06:50 +00:00
|
|
|
if (II.OpInfo[i].isOptionalDef()) {
|
|
|
|
// Optional def must be a physical register.
|
|
|
|
unsigned NumResults = CountResults(Node);
|
|
|
|
VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(VRBase));
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(VRBase, true));
|
|
|
|
}
|
2009-01-16 20:57:18 +00:00
|
|
|
|
2009-07-11 01:06:50 +00:00
|
|
|
if (!VRBase && !IsClone && !IsCloned)
|
2009-01-16 20:57:18 +00:00
|
|
|
for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
SDNode *User = *UI;
|
|
|
|
if (User->getOpcode() == ISD::CopyToReg &&
|
|
|
|
User->getOperand(2).getNode() == Node &&
|
|
|
|
User->getOperand(2).getResNo() == i) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2009-10-10 01:32:21 +00:00
|
|
|
const TargetRegisterClass *RegRC = MRI->getRegClass(Reg);
|
2009-04-13 15:38:05 +00:00
|
|
|
if (RegRC == RC) {
|
|
|
|
VRBase = Reg;
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(Reg, true));
|
|
|
|
break;
|
|
|
|
}
|
2009-01-16 20:57:18 +00:00
|
|
|
}
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the result registers for this node and add the result regs to
|
|
|
|
// the machine instruction.
|
|
|
|
if (VRBase == 0) {
|
|
|
|
assert(RC && "Isn't a register operand!");
|
2009-10-10 01:32:21 +00:00
|
|
|
VRBase = MRI->createVirtualRegister(RC);
|
2008-09-03 16:01:59 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateReg(VRBase, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
SDValue Op(Node, i);
|
2009-01-09 22:44:02 +00:00
|
|
|
if (IsClone)
|
|
|
|
VRBaseMap.erase(Op);
|
2008-09-03 16:01:59 +00:00
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2008-09-03 16:01:59 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getVR - Return the virtual register corresponding to the specified result
|
|
|
|
/// of the specified node.
|
2009-10-10 01:32:21 +00:00
|
|
|
unsigned InstrEmitter::getVR(SDValue Op,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2008-09-03 16:01:59 +00:00
|
|
|
if (Op.isMachineOpcode() &&
|
2010-02-09 19:54:29 +00:00
|
|
|
Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
|
2008-09-03 16:01:59 +00:00
|
|
|
// Add an IMPLICIT_DEF instruction before every use.
|
|
|
|
unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
|
2011-06-28 19:10:37 +00:00
|
|
|
// IMPLICIT_DEF can produce any type of result so its MCInstrDesc
|
2008-09-03 16:01:59 +00:00
|
|
|
// does not include operand register class info.
|
|
|
|
if (!VReg) {
|
|
|
|
const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
|
2009-10-10 01:32:21 +00:00
|
|
|
VReg = MRI->createVirtualRegister(RC);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
2010-07-10 13:55:45 +00:00
|
|
|
BuildMI(*MBB, InsertPos, Op.getDebugLoc(),
|
2010-02-09 19:54:29 +00:00
|
|
|
TII->get(TargetOpcode::IMPLICIT_DEF), VReg);
|
2008-09-03 16:01:59 +00:00
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
|
|
|
|
assert(I != VRBaseMap.end() && "Node emitted out of order - late");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2010-08-30 04:36:50 +00:00
|
|
|
|
2009-04-13 15:38:05 +00:00
|
|
|
/// AddRegisterOperand - Add the specified register as an operand to the
|
|
|
|
/// specified machine instr. Insert register copies if the register is
|
|
|
|
/// not in the required register class.
|
|
|
|
void
|
2009-10-10 01:32:21 +00:00
|
|
|
InstrEmitter::AddRegisterOperand(MachineInstr *MI, SDValue Op,
|
|
|
|
unsigned IIOpNum,
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc *II,
|
2010-03-25 01:38:16 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
2010-05-14 22:01:14 +00:00
|
|
|
bool IsDebug, bool IsClone, bool IsCloned) {
|
2009-08-11 20:47:22 +00:00
|
|
|
assert(Op.getValueType() != MVT::Other &&
|
2010-12-21 02:38:05 +00:00
|
|
|
Op.getValueType() != MVT::Glue &&
|
2010-12-23 17:24:32 +00:00
|
|
|
"Chain and glue operands should occur at end of operand list!");
|
2009-04-13 15:38:05 +00:00
|
|
|
// Get/emit the operand.
|
|
|
|
unsigned VReg = getVR(Op, VRBaseMap);
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
|
|
|
|
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
bool isOptDef = IIOpNum < MCID.getNumOperands() &&
|
|
|
|
MCID.OpInfo[IIOpNum].isOptionalDef();
|
2009-04-13 15:38:05 +00:00
|
|
|
|
|
|
|
// If the instruction requires a register in a different class, create
|
|
|
|
// a new virtual register and copy the value into it.
|
|
|
|
if (II) {
|
2009-10-10 01:32:21 +00:00
|
|
|
const TargetRegisterClass *SrcRC = MRI->getRegClass(VReg);
|
2009-07-29 21:36:49 +00:00
|
|
|
const TargetRegisterClass *DstRC = 0;
|
|
|
|
if (IIOpNum < II->getNumOperands())
|
2011-06-27 21:26:13 +00:00
|
|
|
DstRC = TII->getRegClass(*II, IIOpNum, TRI);
|
2011-06-28 19:10:37 +00:00
|
|
|
assert((DstRC || (MCID.isVariadic() && IIOpNum >= MCID.getNumOperands())) &&
|
2009-04-13 15:38:05 +00:00
|
|
|
"Don't have operand info for this instruction!");
|
2011-06-02 05:43:46 +00:00
|
|
|
if (DstRC && !SrcRC->hasSuperClassEq(DstRC)) {
|
2009-10-10 01:32:21 +00:00
|
|
|
unsigned NewVReg = MRI->createVirtualRegister(DstRC);
|
2010-07-10 19:08:25 +00:00
|
|
|
BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
|
2009-04-13 15:38:05 +00:00
|
|
|
VReg = NewVReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:08:21 +00:00
|
|
|
// If this value has only one use, that use is a kill. This is a
|
2010-05-11 21:59:14 +00:00
|
|
|
// conservative approximation. InstrEmitter does trivial coalescing
|
|
|
|
// with CopyFromReg nodes, so don't emit kill flags for them.
|
2010-05-14 22:01:14 +00:00
|
|
|
// Avoid kill flags on Schedule cloned nodes, since there will be
|
|
|
|
// multiple uses.
|
2010-05-11 21:59:14 +00:00
|
|
|
// Tied operands are never killed, so we need to check that. And that
|
|
|
|
// means we need to determine the index of the operand.
|
|
|
|
bool isKill = Op.hasOneUse() &&
|
|
|
|
Op.getNode()->getOpcode() != ISD::CopyFromReg &&
|
2010-05-14 22:01:14 +00:00
|
|
|
!IsDebug &&
|
|
|
|
!(IsClone || IsCloned);
|
2010-05-11 21:59:14 +00:00
|
|
|
if (isKill) {
|
|
|
|
unsigned Idx = MI->getNumOperands();
|
|
|
|
while (Idx > 0 &&
|
|
|
|
MI->getOperand(Idx-1).isReg() && MI->getOperand(Idx-1).isImplicit())
|
|
|
|
--Idx;
|
2011-06-28 19:10:37 +00:00
|
|
|
bool isTied = MI->getDesc().getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
|
2010-05-11 21:59:14 +00:00
|
|
|
if (isTied)
|
|
|
|
isKill = false;
|
|
|
|
}
|
2010-04-30 00:08:21 +00:00
|
|
|
|
2010-03-25 01:38:16 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef,
|
2010-04-30 00:08:21 +00:00
|
|
|
false/*isImp*/, isKill,
|
2010-03-25 01:38:16 +00:00
|
|
|
false/*isDead*/, false/*isUndef*/,
|
|
|
|
false/*isEarlyClobber*/,
|
|
|
|
0/*SubReg*/, IsDebug));
|
2009-04-13 15:38:05 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
/// AddOperand - Add the specified operand to the specified machine instr. II
|
|
|
|
/// specifies the instruction information for the node, and IIOpNum is the
|
|
|
|
/// operand number (in the II) that we are adding. IIOpNum and II are used for
|
|
|
|
/// assertions only.
|
2009-10-10 01:32:21 +00:00
|
|
|
void InstrEmitter::AddOperand(MachineInstr *MI, SDValue Op,
|
|
|
|
unsigned IIOpNum,
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc *II,
|
2010-03-25 01:38:16 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
2010-05-14 22:01:14 +00:00
|
|
|
bool IsDebug, bool IsClone, bool IsCloned) {
|
2008-09-03 16:01:59 +00:00
|
|
|
if (Op.isMachineOpcode()) {
|
2010-05-14 22:01:14 +00:00
|
|
|
AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap,
|
|
|
|
IsDebug, IsClone, IsCloned);
|
2008-09-03 16:01:59 +00:00
|
|
|
} else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
|
2009-09-08 23:05:44 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateImm(C->getSExtValue()));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
|
2008-09-12 18:08:03 +00:00
|
|
|
const ConstantFP *CFP = F->getConstantFPValue();
|
2008-09-03 16:01:59 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateFPImm(CFP));
|
|
|
|
} else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
|
2010-08-30 04:36:50 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
|
2009-06-26 05:52:14 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(), TGA->getOffset(),
|
|
|
|
TGA->getTargetFlags()));
|
2009-04-13 15:38:05 +00:00
|
|
|
} else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
|
|
|
|
MI->addOperand(MachineOperand::CreateMBB(BBNode->getBasicBlock()));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
|
|
|
|
MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
|
|
|
|
} else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
|
2009-06-26 05:52:14 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateJTI(JT->getIndex(),
|
|
|
|
JT->getTargetFlags()));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
|
|
|
|
int Offset = CP->getOffset();
|
|
|
|
unsigned Align = CP->getAlignment();
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Type = CP->getType();
|
2008-09-03 16:01:59 +00:00
|
|
|
// MachineConstantPool wants an explicit alignment.
|
|
|
|
if (Align == 0) {
|
2009-10-10 01:32:21 +00:00
|
|
|
Align = TM->getTargetData()->getPrefTypeAlignment(Type);
|
2008-09-03 16:01:59 +00:00
|
|
|
if (Align == 0) {
|
|
|
|
// Alignment of vector types. FIXME!
|
2009-10-10 01:32:21 +00:00
|
|
|
Align = TM->getTargetData()->getTypeAllocSize(Type);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Idx;
|
2009-10-10 01:32:21 +00:00
|
|
|
MachineConstantPool *MCP = MF->getConstantPool();
|
2008-09-03 16:01:59 +00:00
|
|
|
if (CP->isMachineConstantPoolEntry())
|
2009-10-10 01:32:21 +00:00
|
|
|
Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Align);
|
2008-09-03 16:01:59 +00:00
|
|
|
else
|
2009-10-10 01:32:21 +00:00
|
|
|
Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Align);
|
2009-06-26 05:52:14 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateCPI(Idx, Offset,
|
|
|
|
CP->getTargetFlags()));
|
2008-09-16 21:48:12 +00:00
|
|
|
} else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
|
2009-09-01 22:06:46 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateES(ES->getSymbol(),
|
2009-06-26 05:52:14 +00:00
|
|
|
ES->getTargetFlags()));
|
2009-10-30 01:27:03 +00:00
|
|
|
} else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
|
2009-11-20 23:18:13 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateBA(BA->getBlockAddress(),
|
|
|
|
BA->getTargetFlags()));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else {
|
2009-08-11 20:47:22 +00:00
|
|
|
assert(Op.getValueType() != MVT::Other &&
|
2010-12-21 02:38:05 +00:00
|
|
|
Op.getValueType() != MVT::Glue &&
|
2010-12-23 17:24:32 +00:00
|
|
|
"Chain and glue operands should occur at end of operand list!");
|
2010-05-14 22:01:14 +00:00
|
|
|
AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap,
|
|
|
|
IsDebug, IsClone, IsCloned);
|
2009-04-13 15:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
|
|
|
|
/// "SubIdx"'th sub-register class is the specified register class and whose
|
|
|
|
/// type matches the specified type.
|
|
|
|
static const TargetRegisterClass*
|
|
|
|
getSuperRegisterRegClass(const TargetRegisterClass *TRC,
|
2009-08-10 22:56:29 +00:00
|
|
|
unsigned SubIdx, EVT VT) {
|
2009-04-13 15:38:05 +00:00
|
|
|
// Pick the register class of the superegister for this type
|
|
|
|
for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
|
|
|
|
E = TRC->superregclasses_end(); I != E; ++I)
|
2009-04-28 16:34:09 +00:00
|
|
|
if ((*I)->hasType(VT) && (*I)->getSubRegisterRegClass(SubIdx) == TRC)
|
2009-04-13 15:38:05 +00:00
|
|
|
return *I;
|
|
|
|
assert(false && "Couldn't find the register class");
|
|
|
|
return 0;
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitSubregNode - Generate machine code for subreg nodes.
|
|
|
|
///
|
2009-10-10 01:32:21 +00:00
|
|
|
void InstrEmitter::EmitSubregNode(SDNode *Node,
|
2010-05-14 22:01:14 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
|
|
|
bool IsClone, bool IsCloned) {
|
2008-09-03 16:01:59 +00:00
|
|
|
unsigned VRBase = 0;
|
|
|
|
unsigned Opc = Node->getMachineOpcode();
|
|
|
|
|
|
|
|
// If the node is only used by a CopyToReg and the dest reg is a vreg, use
|
|
|
|
// the CopyToReg'd destination register instead of creating a new vreg.
|
|
|
|
for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
SDNode *User = *UI;
|
|
|
|
if (User->getOpcode() == ISD::CopyToReg &&
|
|
|
|
User->getOperand(2).getNode() == Node) {
|
|
|
|
unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
|
|
|
|
VRBase = DestReg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-09 19:54:29 +00:00
|
|
|
if (Opc == TargetOpcode::EXTRACT_SUBREG) {
|
2010-07-08 16:40:22 +00:00
|
|
|
// EXTRACT_SUBREG is lowered as %dst = COPY %src:sub
|
2008-09-12 16:56:44 +00:00
|
|
|
unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
|
2008-09-03 16:01:59 +00:00
|
|
|
|
|
|
|
// Figure out the register class to create for the destreg.
|
2009-04-13 15:38:05 +00:00
|
|
|
unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
|
2011-01-05 23:06:49 +00:00
|
|
|
MachineInstr *DefMI = MRI->getVRegDef(VReg);
|
|
|
|
unsigned SrcReg, DstReg, DefSubIdx;
|
|
|
|
if (DefMI &&
|
|
|
|
TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
|
|
|
|
SubIdx == DefSubIdx) {
|
|
|
|
// Optimize these:
|
|
|
|
// r1025 = s/zext r1024, 4
|
|
|
|
// r1026 = extract_subreg r1025, 4
|
|
|
|
// to a copy
|
|
|
|
// r1026 = copy r1024
|
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(SrcReg);
|
|
|
|
VRBase = MRI->createVirtualRegister(TRC);
|
|
|
|
BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
|
|
|
|
} else {
|
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(VReg);
|
|
|
|
const TargetRegisterClass *SRC = TRC->getSubRegisterRegClass(SubIdx);
|
|
|
|
assert(SRC && "Invalid subregister index in EXTRACT_SUBREG");
|
2008-09-03 16:01:59 +00:00
|
|
|
|
2011-01-05 23:06:49 +00:00
|
|
|
// Figure out the register class to create for the destreg.
|
|
|
|
// Note that if we're going to directly use an existing register,
|
|
|
|
// it must be precisely the required class, and not a subclass
|
|
|
|
// thereof.
|
|
|
|
if (VRBase == 0 || SRC != MRI->getRegClass(VRBase)) {
|
|
|
|
// Create the reg
|
|
|
|
assert(SRC && "Couldn't find source register class");
|
|
|
|
VRBase = MRI->createVirtualRegister(SRC);
|
|
|
|
}
|
2009-04-14 22:17:14 +00:00
|
|
|
|
2011-01-05 23:06:49 +00:00
|
|
|
// Create the extract_subreg machine instruction.
|
|
|
|
MachineInstr *MI = BuildMI(*MF, Node->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY), VRBase);
|
2010-07-08 16:40:22 +00:00
|
|
|
|
2011-01-05 23:06:49 +00:00
|
|
|
// Add source, and subreg index
|
|
|
|
AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap, /*IsDebug=*/false,
|
|
|
|
IsClone, IsCloned);
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg())&&
|
|
|
|
"Cannot yet extract from physregs");
|
|
|
|
MI->getOperand(1).setSubReg(SubIdx);
|
|
|
|
MBB->insert(InsertPos, MI);
|
|
|
|
}
|
2010-02-09 19:54:29 +00:00
|
|
|
} else if (Opc == TargetOpcode::INSERT_SUBREG ||
|
|
|
|
Opc == TargetOpcode::SUBREG_TO_REG) {
|
2008-09-03 16:01:59 +00:00
|
|
|
SDValue N0 = Node->getOperand(0);
|
|
|
|
SDValue N1 = Node->getOperand(1);
|
|
|
|
SDValue N2 = Node->getOperand(2);
|
2009-04-13 15:38:05 +00:00
|
|
|
unsigned SubReg = getVR(N1, VRBaseMap);
|
2008-09-12 16:56:44 +00:00
|
|
|
unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
|
2009-10-10 01:32:21 +00:00
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
|
2009-04-14 22:17:14 +00:00
|
|
|
const TargetRegisterClass *SRC =
|
2010-05-04 00:22:40 +00:00
|
|
|
getSuperRegisterRegClass(TRC, SubIdx, Node->getValueType(0));
|
2009-04-14 22:17:14 +00:00
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
// Figure out the register class to create for the destreg.
|
2009-04-14 22:17:14 +00:00
|
|
|
// Note that if we're going to directly use an existing register,
|
|
|
|
// it must be precisely the required class, and not a subclass
|
|
|
|
// thereof.
|
2009-10-10 01:32:21 +00:00
|
|
|
if (VRBase == 0 || SRC != MRI->getRegClass(VRBase)) {
|
2009-04-14 22:17:14 +00:00
|
|
|
// Create the reg
|
|
|
|
assert(SRC && "Couldn't find source register class");
|
2009-10-10 01:32:21 +00:00
|
|
|
VRBase = MRI->createVirtualRegister(SRC);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
2009-04-14 22:17:14 +00:00
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
// Create the insert_subreg or subreg_to_reg machine instruction.
|
2009-10-10 01:32:21 +00:00
|
|
|
MachineInstr *MI = BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc));
|
2008-09-03 16:01:59 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateReg(VRBase, true));
|
|
|
|
|
|
|
|
// If creating a subreg_to_reg, then the first input operand
|
|
|
|
// is an implicit value immediate, otherwise it's a register
|
2010-02-09 19:54:29 +00:00
|
|
|
if (Opc == TargetOpcode::SUBREG_TO_REG) {
|
2008-09-03 16:01:59 +00:00
|
|
|
const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
|
2008-09-12 16:56:44 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
|
2008-09-03 16:01:59 +00:00
|
|
|
} else
|
2010-05-14 22:01:14 +00:00
|
|
|
AddOperand(MI, N0, 0, 0, VRBaseMap, /*IsDebug=*/false,
|
|
|
|
IsClone, IsCloned);
|
2008-09-03 16:01:59 +00:00
|
|
|
// Add the subregster being inserted
|
2010-05-14 22:01:14 +00:00
|
|
|
AddOperand(MI, N1, 0, 0, VRBaseMap, /*IsDebug=*/false,
|
|
|
|
IsClone, IsCloned);
|
2008-09-03 16:01:59 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateImm(SubIdx));
|
2009-10-10 01:32:21 +00:00
|
|
|
MBB->insert(InsertPos, MI);
|
2008-09-03 16:01:59 +00:00
|
|
|
} else
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
|
2008-09-03 16:01:59 +00:00
|
|
|
|
|
|
|
SDValue Op(Node, 0);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2008-09-03 16:01:59 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
}
|
|
|
|
|
2009-04-13 21:06:25 +00:00
|
|
|
/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
|
|
|
|
/// COPY_TO_REGCLASS is just a normal copy, except that the destination
|
2009-04-13 15:38:05 +00:00
|
|
|
/// register is constrained to be in a particular register class.
|
|
|
|
///
|
|
|
|
void
|
2009-10-10 01:32:21 +00:00
|
|
|
InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2009-04-13 15:38:05 +00:00
|
|
|
unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
|
|
|
|
|
2010-07-10 19:08:25 +00:00
|
|
|
// Create the new VReg in the destination class and emit a copy.
|
2009-04-13 15:38:05 +00:00
|
|
|
unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
|
|
|
|
const TargetRegisterClass *DstRC = TRI->getRegClass(DstRCIdx);
|
2009-10-10 01:32:21 +00:00
|
|
|
unsigned NewVReg = MRI->createVirtualRegister(DstRC);
|
2010-07-10 19:08:25 +00:00
|
|
|
BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
|
|
|
|
NewVReg).addReg(VReg);
|
2009-04-13 15:38:05 +00:00
|
|
|
|
|
|
|
SDValue Op(Node, 0);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2009-04-13 15:38:05 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
}
|
|
|
|
|
2010-05-04 00:22:40 +00:00
|
|
|
/// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
|
|
|
|
///
|
|
|
|
void InstrEmitter::EmitRegSequence(SDNode *Node,
|
2010-05-14 22:01:14 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
|
|
|
bool IsClone, bool IsCloned) {
|
2011-06-16 18:17:13 +00:00
|
|
|
unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
|
|
|
|
const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
|
2010-05-04 00:22:40 +00:00
|
|
|
unsigned NewVReg = MRI->createVirtualRegister(RC);
|
|
|
|
MachineInstr *MI = BuildMI(*MF, Node->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::REG_SEQUENCE), NewVReg);
|
|
|
|
unsigned NumOps = Node->getNumOperands();
|
2011-06-16 18:17:13 +00:00
|
|
|
assert((NumOps & 1) == 1 &&
|
|
|
|
"REG_SEQUENCE must have an odd number of operands!");
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
|
2011-06-16 18:17:13 +00:00
|
|
|
for (unsigned i = 1; i != NumOps; ++i) {
|
2010-05-04 00:22:40 +00:00
|
|
|
SDValue Op = Node->getOperand(i);
|
2011-06-16 18:17:13 +00:00
|
|
|
if ((i & 1) == 0) {
|
2010-05-04 00:22:40 +00:00
|
|
|
unsigned SubIdx = cast<ConstantSDNode>(Op)->getZExtValue();
|
|
|
|
unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap);
|
2010-05-10 23:08:19 +00:00
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
|
|
|
|
const TargetRegisterClass *SRC =
|
2010-05-18 20:03:28 +00:00
|
|
|
TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
|
2010-12-17 01:21:12 +00:00
|
|
|
if (SRC && SRC != RC) {
|
2010-05-18 20:03:28 +00:00
|
|
|
MRI->setRegClass(NewVReg, SRC);
|
2010-05-18 20:07:47 +00:00
|
|
|
RC = SRC;
|
|
|
|
}
|
2010-05-04 00:22:40 +00:00
|
|
|
}
|
2010-05-14 22:01:14 +00:00
|
|
|
AddOperand(MI, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
|
|
|
|
IsClone, IsCloned);
|
2010-05-04 00:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MBB->insert(InsertPos, MI);
|
|
|
|
SDValue Op(Node, 0);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
|
2010-12-23 00:58:24 +00:00
|
|
|
(void)isNew; // Silence compiler warning.
|
2010-05-04 00:22:40 +00:00
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
}
|
|
|
|
|
2010-03-25 01:38:16 +00:00
|
|
|
/// EmitDbgValue - Generate machine instruction for a dbg_value node.
|
|
|
|
///
|
2010-04-30 19:35:33 +00:00
|
|
|
MachineInstr *
|
|
|
|
InstrEmitter::EmitDbgValue(SDDbgValue *SD,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2010-03-25 01:38:16 +00:00
|
|
|
uint64_t Offset = SD->getOffset();
|
|
|
|
MDNode* MDPtr = SD->getMDPtr();
|
|
|
|
DebugLoc DL = SD->getDebugLoc();
|
|
|
|
|
2010-04-25 21:33:54 +00:00
|
|
|
if (SD->getKind() == SDDbgValue::FRAMEIX) {
|
|
|
|
// Stack address; this needs to be lowered in target-dependent fashion.
|
|
|
|
// EmitTargetCodeForFrameDebugValue is responsible for allocation.
|
|
|
|
unsigned FrameIx = SD->getFrameIx();
|
2010-04-26 07:38:55 +00:00
|
|
|
return TII->emitFrameIndexDebugValue(*MF, FrameIx, Offset, MDPtr, DL);
|
2010-04-25 21:33:54 +00:00
|
|
|
}
|
|
|
|
// Otherwise, we're going to create an instruction here.
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
|
2010-03-25 01:38:16 +00:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
|
|
|
|
if (SD->getKind() == SDDbgValue::SDNODE) {
|
2010-04-06 21:59:56 +00:00
|
|
|
SDNode *Node = SD->getSDNode();
|
|
|
|
SDValue Op = SDValue(Node, SD->getResNo());
|
|
|
|
// It's possible we replaced this SDNode with other(s) and therefore
|
|
|
|
// didn't generate code for it. It's better to catch these cases where
|
|
|
|
// they happen and transfer the debug info, but trying to guarantee that
|
|
|
|
// in all cases would be very fragile; this is a safeguard for any
|
|
|
|
// that were missed.
|
|
|
|
DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
|
|
|
|
if (I==VRBaseMap.end())
|
|
|
|
MIB.addReg(0U); // undef
|
|
|
|
else
|
|
|
|
AddOperand(&*MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap,
|
2010-05-14 22:01:14 +00:00
|
|
|
/*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
|
2010-03-25 01:38:16 +00:00
|
|
|
} else if (SD->getKind() == SDDbgValue::CONST) {
|
2010-04-15 01:51:59 +00:00
|
|
|
const Value *V = SD->getConst();
|
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2011-06-24 20:46:11 +00:00
|
|
|
if (CI->getBitWidth() > 64)
|
|
|
|
MIB.addCImm(CI);
|
2010-05-07 22:19:08 +00:00
|
|
|
else
|
|
|
|
MIB.addImm(CI->getSExtValue());
|
2010-04-15 01:51:59 +00:00
|
|
|
} else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
|
2010-03-25 01:38:16 +00:00
|
|
|
MIB.addFPImm(CF);
|
2010-03-10 22:13:47 +00:00
|
|
|
} else {
|
|
|
|
// Could be an Undef. In any case insert an Undef so we can see what we
|
|
|
|
// dropped.
|
2010-03-25 01:38:16 +00:00
|
|
|
MIB.addReg(0U);
|
2010-03-10 22:13:47 +00:00
|
|
|
}
|
2010-03-06 00:03:23 +00:00
|
|
|
} else {
|
|
|
|
// Insert an Undef so we can see what we dropped.
|
2010-03-25 01:38:16 +00:00
|
|
|
MIB.addReg(0U);
|
2010-03-06 00:03:23 +00:00
|
|
|
}
|
2010-03-25 01:38:16 +00:00
|
|
|
|
|
|
|
MIB.addImm(Offset).addMetadata(MDPtr);
|
|
|
|
return &*MIB;
|
2010-03-06 00:03:23 +00:00
|
|
|
}
|
|
|
|
|
2010-03-25 04:41:16 +00:00
|
|
|
/// EmitMachineNode - Generate machine code for a target-specific node and
|
|
|
|
/// needed dependencies.
|
2008-09-03 16:01:59 +00:00
|
|
|
///
|
2010-03-25 04:41:16 +00:00
|
|
|
void InstrEmitter::
|
|
|
|
EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
|
2010-05-01 00:01:06 +00:00
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2010-03-25 04:41:16 +00:00
|
|
|
unsigned Opc = Node->getMachineOpcode();
|
|
|
|
|
|
|
|
// Handle subreg insert/extract specially
|
|
|
|
if (Opc == TargetOpcode::EXTRACT_SUBREG ||
|
|
|
|
Opc == TargetOpcode::INSERT_SUBREG ||
|
|
|
|
Opc == TargetOpcode::SUBREG_TO_REG) {
|
2010-05-14 22:01:14 +00:00
|
|
|
EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
|
2010-03-25 04:41:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-09-03 16:01:59 +00:00
|
|
|
|
2010-03-25 04:41:16 +00:00
|
|
|
// Handle COPY_TO_REGCLASS specially.
|
|
|
|
if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
|
|
|
|
EmitCopyToRegClassNode(Node, VRBaseMap);
|
|
|
|
return;
|
|
|
|
}
|
2009-04-13 15:38:05 +00:00
|
|
|
|
2010-05-04 00:22:40 +00:00
|
|
|
// Handle REG_SEQUENCE specially.
|
|
|
|
if (Opc == TargetOpcode::REG_SEQUENCE) {
|
2010-05-14 22:01:14 +00:00
|
|
|
EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
|
2010-05-04 00:22:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-25 04:41:16 +00:00
|
|
|
if (Opc == TargetOpcode::IMPLICIT_DEF)
|
|
|
|
// We want a unique VR for each IMPLICIT_DEF use.
|
|
|
|
return;
|
|
|
|
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII->get(Opc);
|
2010-03-25 04:41:16 +00:00
|
|
|
unsigned NumResults = CountResults(Node);
|
|
|
|
unsigned NodeOperands = CountOperands(Node);
|
2010-03-25 05:40:48 +00:00
|
|
|
bool HasPhysRegOuts = NumResults > II.getNumDefs() && II.getImplicitDefs()!=0;
|
2008-09-03 16:01:59 +00:00
|
|
|
#ifndef NDEBUG
|
2010-03-25 04:41:16 +00:00
|
|
|
unsigned NumMIOperands = NodeOperands + NumResults;
|
2010-03-25 05:40:48 +00:00
|
|
|
if (II.isVariadic())
|
|
|
|
assert(NumMIOperands >= II.getNumOperands() &&
|
|
|
|
"Too few operands for a variadic node!");
|
|
|
|
else
|
|
|
|
assert(NumMIOperands >= II.getNumOperands() &&
|
|
|
|
NumMIOperands <= II.getNumOperands()+II.getNumImplicitDefs() &&
|
|
|
|
"#operands for dag node doesn't match .td file!");
|
2008-09-03 16:01:59 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-25 04:41:16 +00:00
|
|
|
// Create the new machine instruction.
|
|
|
|
MachineInstr *MI = BuildMI(*MF, Node->getDebugLoc(), II);
|
2010-06-18 23:28:01 +00:00
|
|
|
|
|
|
|
// The MachineInstr constructor adds implicit-def operands. Scan through
|
|
|
|
// these to determine which are dead.
|
|
|
|
if (MI->getNumOperands() != 0 &&
|
2010-12-21 02:38:05 +00:00
|
|
|
Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
|
2010-06-18 23:28:01 +00:00
|
|
|
// First, collect all used registers.
|
|
|
|
SmallVector<unsigned, 8> UsedRegs;
|
2010-12-23 17:24:32 +00:00
|
|
|
for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser())
|
2010-06-18 23:28:01 +00:00
|
|
|
if (F->getOpcode() == ISD::CopyFromReg)
|
|
|
|
UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
|
|
|
|
else {
|
|
|
|
// Collect declared implicit uses.
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
|
|
|
|
UsedRegs.append(MCID.getImplicitUses(),
|
|
|
|
MCID.getImplicitUses() + MCID.getNumImplicitUses());
|
2010-06-18 23:28:01 +00:00
|
|
|
// In addition to declared implicit uses, we must also check for
|
|
|
|
// direct RegisterSDNode operands.
|
|
|
|
for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i)
|
|
|
|
if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) {
|
|
|
|
unsigned Reg = R->getReg();
|
2011-01-10 02:58:51 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg))
|
2010-06-18 23:28:01 +00:00
|
|
|
UsedRegs.push_back(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Then mark unused registers as dead.
|
|
|
|
MI->setPhysRegsDeadExcept(UsedRegs, *TRI);
|
|
|
|
}
|
2010-03-25 04:41:16 +00:00
|
|
|
|
|
|
|
// Add result register values for things that are defined by this
|
|
|
|
// instruction.
|
|
|
|
if (NumResults)
|
|
|
|
CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
|
|
|
|
|
|
|
|
// Emit all of the actual operands of this instruction, adding them to the
|
|
|
|
// instruction as appropriate.
|
|
|
|
bool HasOptPRefs = II.getNumDefs() > NumResults;
|
|
|
|
assert((!HasOptPRefs || !HasPhysRegOuts) &&
|
|
|
|
"Unable to cope with optional defs and phys regs defs!");
|
|
|
|
unsigned NumSkip = HasOptPRefs ? II.getNumDefs() - NumResults : 0;
|
|
|
|
for (unsigned i = NumSkip; i != NodeOperands; ++i)
|
|
|
|
AddOperand(MI, Node->getOperand(i), i-NumSkip+II.getNumDefs(), &II,
|
2010-05-14 22:01:14 +00:00
|
|
|
VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
|
2010-03-25 04:41:16 +00:00
|
|
|
|
|
|
|
// Transfer all of the memory reference descriptions of this instruction.
|
|
|
|
MI->setMemRefs(cast<MachineSDNode>(Node)->memoperands_begin(),
|
|
|
|
cast<MachineSDNode>(Node)->memoperands_end());
|
|
|
|
|
2010-07-06 20:24:04 +00:00
|
|
|
// Insert the instruction into position in the block. This needs to
|
|
|
|
// happen before any custom inserter hook is called so that the
|
|
|
|
// hook knows where in the block to insert the replacement code.
|
|
|
|
MBB->insert(InsertPos, MI);
|
|
|
|
|
2010-12-08 22:21:42 +00:00
|
|
|
// Additional results must be physical register defs.
|
2010-03-25 04:41:16 +00:00
|
|
|
if (HasPhysRegOuts) {
|
|
|
|
for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
|
|
|
|
unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
|
|
|
|
if (Node->hasAnyUseOfValue(i))
|
|
|
|
EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
|
|
|
|
// If there are no uses, mark the register as dead now, so that
|
|
|
|
// MachineLICM/Sink can see that it's dead. Don't do this if the
|
2010-12-23 17:24:32 +00:00
|
|
|
// node has a Glue value, for the benefit of targets still using
|
|
|
|
// Glue for values in physregs.
|
2010-12-21 02:38:05 +00:00
|
|
|
else if (Node->getValueType(Node->getNumValues()-1) != MVT::Glue)
|
2010-03-25 04:41:16 +00:00
|
|
|
MI->addRegisterDead(Reg, TRI);
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-25 05:40:48 +00:00
|
|
|
|
|
|
|
// If the instruction has implicit defs and the node doesn't, mark the
|
2010-12-23 17:24:32 +00:00
|
|
|
// implicit def as dead. If the node has any glue outputs, we don't do this
|
|
|
|
// because we don't know what implicit defs are being used by glued nodes.
|
2010-12-21 02:38:05 +00:00
|
|
|
if (Node->getValueType(Node->getNumValues()-1) != MVT::Glue)
|
2010-03-25 05:40:48 +00:00
|
|
|
if (const unsigned *IDList = II.getImplicitDefs()) {
|
|
|
|
for (unsigned i = NumResults, e = II.getNumDefs()+II.getNumImplicitDefs();
|
|
|
|
i != e; ++i)
|
|
|
|
MI->addRegisterDead(IDList[i-II.getNumDefs()], TRI);
|
|
|
|
}
|
2011-08-30 19:09:48 +00:00
|
|
|
|
|
|
|
// Run post-isel target hook to adjust this instruction if needed.
|
|
|
|
if (II.hasPostISelHook())
|
|
|
|
TLI->AdjustInstrPostInstrSelection(MI, Node);
|
2010-03-25 04:41:16 +00:00
|
|
|
}
|
2008-09-03 16:01:59 +00:00
|
|
|
|
2010-03-25 04:41:16 +00:00
|
|
|
/// EmitSpecialNode - Generate machine code for a target-independent node and
|
|
|
|
/// needed dependencies.
|
|
|
|
void InstrEmitter::
|
|
|
|
EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap) {
|
2008-09-03 16:01:59 +00:00
|
|
|
switch (Node->getOpcode()) {
|
|
|
|
default:
|
|
|
|
#ifndef NDEBUG
|
2009-10-10 01:32:21 +00:00
|
|
|
Node->dump();
|
2008-09-03 16:01:59 +00:00
|
|
|
#endif
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("This target-independent node should have been selected!");
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
|
|
|
case ISD::EntryToken:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("EntryToken should have been excluded from the schedule!");
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
2009-07-30 08:33:02 +00:00
|
|
|
case ISD::MERGE_VALUES:
|
2008-09-03 16:01:59 +00:00
|
|
|
case ISD::TokenFactor: // fall thru
|
|
|
|
break;
|
|
|
|
case ISD::CopyToReg: {
|
|
|
|
unsigned SrcReg;
|
|
|
|
SDValue SrcVal = Node->getOperand(2);
|
|
|
|
if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
|
|
|
|
SrcReg = R->getReg();
|
|
|
|
else
|
|
|
|
SrcReg = getVR(SrcVal, VRBaseMap);
|
|
|
|
|
|
|
|
unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
|
|
|
|
if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
|
|
|
|
break;
|
2009-04-13 15:38:05 +00:00
|
|
|
|
2010-07-10 19:08:25 +00:00
|
|
|
BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
|
|
|
|
DestReg).addReg(SrcReg);
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ISD::CopyFromReg: {
|
|
|
|
unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
|
2009-01-16 20:57:18 +00:00
|
|
|
EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-03-14 02:33:54 +00:00
|
|
|
case ISD::EH_LABEL: {
|
|
|
|
MCSymbol *S = cast<EHLabelSDNode>(Node)->getLabel();
|
|
|
|
BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::EH_LABEL)).addSym(S);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
case ISD::INLINEASM: {
|
|
|
|
unsigned NumOps = Node->getNumOperands();
|
2010-12-21 02:38:05 +00:00
|
|
|
if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
|
2010-12-23 17:24:32 +00:00
|
|
|
--NumOps; // Ignore the glue operand.
|
2008-09-03 16:01:59 +00:00
|
|
|
|
|
|
|
// Create the inline asm machine instruction.
|
2009-10-10 01:32:21 +00:00
|
|
|
MachineInstr *MI = BuildMI(*MF, Node->getDebugLoc(),
|
2010-02-09 19:54:29 +00:00
|
|
|
TII->get(TargetOpcode::INLINEASM));
|
2008-09-03 16:01:59 +00:00
|
|
|
|
|
|
|
// Add the asm string as an external symbol operand.
|
2010-04-07 05:20:54 +00:00
|
|
|
SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
|
|
|
|
const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
|
2008-09-03 16:01:59 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateES(AsmStr));
|
|
|
|
|
2011-01-07 23:50:32 +00:00
|
|
|
// Add the HasSideEffect and isAlignStack bits.
|
|
|
|
int64_t ExtraInfo =
|
|
|
|
cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
|
2010-07-02 20:16:09 +00:00
|
|
|
getZExtValue();
|
2011-01-07 23:50:32 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateImm(ExtraInfo));
|
2010-07-02 20:16:09 +00:00
|
|
|
|
2008-09-03 16:01:59 +00:00
|
|
|
// Add all of the operand registers to the instruction.
|
2010-04-07 05:20:54 +00:00
|
|
|
for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
|
2008-09-12 16:56:44 +00:00
|
|
|
unsigned Flags =
|
|
|
|
cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
|
2009-03-20 18:03:34 +00:00
|
|
|
unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
|
2008-09-03 16:01:59 +00:00
|
|
|
|
|
|
|
MI->addOperand(MachineOperand::CreateImm(Flags));
|
|
|
|
++i; // Skip the ID value.
|
|
|
|
|
2010-04-07 05:20:54 +00:00
|
|
|
switch (InlineAsm::getKind(Flags)) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Bad flags!");
|
2010-04-07 05:20:54 +00:00
|
|
|
case InlineAsm::Kind_RegDef:
|
2008-09-03 16:01:59 +00:00
|
|
|
for (; NumVals; --NumVals, ++i) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
|
2010-06-09 20:05:00 +00:00
|
|
|
// FIXME: Add dead flags for physical and virtual registers defined.
|
|
|
|
// For now, mark physical register defs as implicit to help fast
|
|
|
|
// regalloc. This makes inline asm look a lot like calls.
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(Reg, true,
|
|
|
|
/*isImp=*/ TargetRegisterInfo::isPhysicalRegister(Reg)));
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-04-07 05:20:54 +00:00
|
|
|
case InlineAsm::Kind_RegDefEarlyClobber:
|
2011-06-27 04:08:33 +00:00
|
|
|
case InlineAsm::Kind_Clobber:
|
2008-09-12 17:49:03 +00:00
|
|
|
for (; NumVals; --NumVals, ++i) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
|
2010-06-09 00:40:31 +00:00
|
|
|
MI->addOperand(MachineOperand::CreateReg(Reg, /*isDef=*/ true,
|
2010-06-09 20:05:00 +00:00
|
|
|
/*isImp=*/ TargetRegisterInfo::isPhysicalRegister(Reg),
|
2010-06-09 00:40:31 +00:00
|
|
|
/*isKill=*/ false,
|
|
|
|
/*isDead=*/ false,
|
|
|
|
/*isUndef=*/false,
|
|
|
|
/*isEarlyClobber=*/ true));
|
2008-09-12 17:49:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-04-07 05:20:54 +00:00
|
|
|
case InlineAsm::Kind_RegUse: // Use of register.
|
|
|
|
case InlineAsm::Kind_Imm: // Immediate.
|
|
|
|
case InlineAsm::Kind_Mem: // Addressing mode.
|
2008-09-03 16:01:59 +00:00
|
|
|
// The addressing mode has been selected, just add all of the
|
|
|
|
// operands to the machine instruction.
|
|
|
|
for (; NumVals; --NumVals, ++i)
|
2010-05-14 22:01:14 +00:00
|
|
|
AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap,
|
|
|
|
/*IsDebug=*/false, IsClone, IsCloned);
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-07 05:38:05 +00:00
|
|
|
|
|
|
|
// Get the mdnode from the asm if it exists and add it to the instruction.
|
|
|
|
SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
|
|
|
|
const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
|
2010-04-26 22:56:56 +00:00
|
|
|
if (MD)
|
|
|
|
MI->addOperand(MachineOperand::CreateMetadata(MD));
|
2010-04-07 05:38:05 +00:00
|
|
|
|
2009-10-10 01:32:21 +00:00
|
|
|
MBB->insert(InsertPos, MI);
|
2008-09-03 16:01:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-10 01:32:21 +00:00
|
|
|
/// InstrEmitter - Construct an InstrEmitter and set it to start inserting
|
|
|
|
/// at the given position in the given block.
|
|
|
|
InstrEmitter::InstrEmitter(MachineBasicBlock *mbb,
|
|
|
|
MachineBasicBlock::iterator insertpos)
|
|
|
|
: MF(mbb->getParent()),
|
|
|
|
MRI(&MF->getRegInfo()),
|
|
|
|
TM(&MF->getTarget()),
|
|
|
|
TII(TM->getInstrInfo()),
|
|
|
|
TRI(TM->getRegisterInfo()),
|
|
|
|
TLI(TM->getTargetLowering()),
|
|
|
|
MBB(mbb), InsertPos(insertpos) {
|
2008-09-03 16:01:59 +00:00
|
|
|
}
|