2006-02-05 13:50:24 +08:00
|
|
|
//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-02-05 13:50:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an instruction selector for the SPARC target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SparcTargetMachine.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAGISel.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2008-02-03 13:43:57 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-09 04:53:28 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Instruction Selector Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
|
|
|
|
/// instructions for SelectionDAG operations.
|
|
|
|
///
|
|
|
|
namespace {
|
|
|
|
class SparcDAGToDAGISel : public SelectionDAGISel {
|
|
|
|
/// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
|
|
|
|
/// make the right decision when generating code for different targets.
|
2015-01-31 07:46:43 +08:00
|
|
|
const SparcSubtarget *Subtarget;
|
2006-02-05 13:50:24 +08:00
|
|
|
public:
|
2015-01-31 07:46:43 +08:00
|
|
|
explicit SparcDAGToDAGISel(SparcTargetMachine &tm) : SelectionDAGISel(tm) {}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override {
|
|
|
|
Subtarget = &MF.getSubtarget<SparcSubtarget>();
|
|
|
|
return SelectionDAGISel::runOnMachineFunction(MF);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
|
2014-04-29 15:57:13 +08:00
|
|
|
SDNode *Select(SDNode *N) override;
|
2006-02-05 13:50:24 +08:00
|
|
|
|
|
|
|
// Complex Pattern Selectors.
|
2010-09-22 04:31:19 +08:00
|
|
|
bool SelectADDRrr(SDValue N, SDValue &R1, SDValue &R2);
|
|
|
|
bool SelectADDRri(SDValue N, SDValue &Base, SDValue &Offset);
|
2008-10-10 18:14:15 +08:00
|
|
|
|
2008-10-10 18:14:47 +08:00
|
|
|
/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
|
|
|
|
/// inline asm expressions.
|
2014-04-29 15:57:13 +08:00
|
|
|
bool SelectInlineAsmMemoryOperand(const SDValue &Op,
|
2015-03-13 04:09:39 +08:00
|
|
|
char ConstraintCode,
|
2014-04-29 15:57:13 +08:00
|
|
|
std::vector<SDValue> &OutOps) override;
|
2008-10-10 18:14:47 +08:00
|
|
|
|
2014-04-29 15:57:13 +08:00
|
|
|
const char *getPassName() const override {
|
2006-02-05 13:50:24 +08:00
|
|
|
return "SPARC DAG->DAG Pattern Instruction Selection";
|
2008-10-10 18:14:15 +08:00
|
|
|
}
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
// Include the pieces autogenerated from the target description.
|
|
|
|
#include "SparcGenDAGISel.inc"
|
2009-09-16 01:46:24 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
SDNode* getGlobalBaseReg();
|
2006-02-05 13:50:24 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-09-16 01:46:24 +08:00
|
|
|
SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
|
2015-01-31 07:46:43 +08:00
|
|
|
unsigned GlobalBaseReg = Subtarget->getInstrInfo()->getGlobalBaseReg(MF);
|
2014-10-08 15:32:17 +08:00
|
|
|
return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy()).getNode();
|
2009-09-16 01:46:24 +08:00
|
|
|
}
|
|
|
|
|
2010-09-22 04:31:19 +08:00
|
|
|
bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
|
2008-07-28 05:46:04 +08:00
|
|
|
SDValue &Base, SDValue &Offset) {
|
2006-02-05 13:50:24 +08:00
|
|
|
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
|
2014-10-08 15:32:17 +08:00
|
|
|
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
|
2009-08-12 04:47:22 +08:00
|
|
|
Offset = CurDAG->getTargetConstant(0, MVT::i32);
|
2006-02-05 13:50:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-17 05:48:12 +08:00
|
|
|
if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
|
2013-09-22 14:48:52 +08:00
|
|
|
Addr.getOpcode() == ISD::TargetGlobalAddress ||
|
|
|
|
Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
|
2006-02-10 15:35:42 +08:00
|
|
|
return false; // direct calls.
|
2008-10-10 18:14:15 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
|
|
|
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
|
2010-08-18 02:17:12 +08:00
|
|
|
if (isInt<13>(CN->getSExtValue())) {
|
2008-10-10 18:14:15 +08:00
|
|
|
if (FrameIndexSDNode *FIN =
|
2006-02-05 13:50:24 +08:00
|
|
|
dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
|
|
|
|
// Constant offset from frame ref.
|
2014-10-08 15:32:17 +08:00
|
|
|
Base =
|
|
|
|
CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
|
2006-02-05 13:50:24 +08:00
|
|
|
} else {
|
2006-02-05 16:35:50 +08:00
|
|
|
Base = Addr.getOperand(0);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
2009-08-12 04:47:22 +08:00
|
|
|
Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
|
2006-02-05 13:50:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
|
2006-02-05 16:35:50 +08:00
|
|
|
Base = Addr.getOperand(1);
|
2006-02-05 13:50:24 +08:00
|
|
|
Offset = Addr.getOperand(0).getOperand(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
|
2006-02-05 16:35:50 +08:00
|
|
|
Base = Addr.getOperand(0);
|
2006-02-05 13:50:24 +08:00
|
|
|
Offset = Addr.getOperand(1).getOperand(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2006-02-05 16:35:50 +08:00
|
|
|
Base = Addr;
|
2009-08-12 04:47:22 +08:00
|
|
|
Offset = CurDAG->getTargetConstant(0, MVT::i32);
|
2006-02-05 13:50:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-22 04:31:19 +08:00
|
|
|
bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
|
2006-02-10 15:35:42 +08:00
|
|
|
if (Addr.getOpcode() == ISD::FrameIndex) return false;
|
2008-09-17 05:48:12 +08:00
|
|
|
if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
|
2013-09-22 14:48:52 +08:00
|
|
|
Addr.getOpcode() == ISD::TargetGlobalAddress ||
|
|
|
|
Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
|
2006-02-10 15:35:42 +08:00
|
|
|
return false; // direct calls.
|
2008-10-10 18:14:15 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
2010-08-18 02:17:12 +08:00
|
|
|
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
|
|
|
|
if (isInt<13>(CN->getSExtValue()))
|
|
|
|
return false; // Let the reg+imm pattern catch this!
|
2006-02-05 13:50:24 +08:00
|
|
|
if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
|
|
|
|
Addr.getOperand(1).getOpcode() == SPISD::Lo)
|
|
|
|
return false; // Let the reg+imm pattern catch this!
|
2006-02-05 16:35:50 +08:00
|
|
|
R1 = Addr.getOperand(0);
|
|
|
|
R2 = Addr.getOperand(1);
|
2006-02-05 13:50:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-05 16:35:50 +08:00
|
|
|
R1 = Addr;
|
2014-10-08 15:32:17 +08:00
|
|
|
R2 = CurDAG->getRegister(SP::G0, TLI->getPointerTy());
|
2006-02-05 13:50:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-05 09:24:18 +08:00
|
|
|
SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
|
2013-05-25 10:42:55 +08:00
|
|
|
SDLoc dl(N);
|
2013-09-22 16:21:56 +08:00
|
|
|
if (N->isMachineOpcode()) {
|
|
|
|
N->setNodeId(-1);
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr; // Already selected.
|
2013-09-22 16:21:56 +08:00
|
|
|
}
|
2006-02-09 08:37:58 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
switch (N->getOpcode()) {
|
|
|
|
default: break;
|
2009-09-16 01:46:24 +08:00
|
|
|
case SPISD::GLOBAL_BASE_REG:
|
|
|
|
return getGlobalBaseReg();
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
case ISD::SDIV:
|
|
|
|
case ISD::UDIV: {
|
2013-04-16 10:57:02 +08:00
|
|
|
// sdivx / udivx handle 64-bit divides.
|
|
|
|
if (N->getValueType(0) == MVT::i64)
|
|
|
|
break;
|
2006-02-05 13:50:24 +08:00
|
|
|
// FIXME: should use a custom expander to expose the SRA to the dag.
|
2008-07-28 05:46:04 +08:00
|
|
|
SDValue DivLHS = N->getOperand(0);
|
|
|
|
SDValue DivRHS = N->getOperand(1);
|
2008-10-10 18:14:15 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
// Set the Y register to the high-part.
|
2008-07-28 05:46:04 +08:00
|
|
|
SDValue TopPart;
|
2006-02-05 13:50:24 +08:00
|
|
|
if (N->getOpcode() == ISD::SDIV) {
|
2009-09-26 02:54:59 +08:00
|
|
|
TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
|
2009-08-12 04:47:22 +08:00
|
|
|
CurDAG->getTargetConstant(31, MVT::i32)), 0);
|
2006-02-05 13:50:24 +08:00
|
|
|
} else {
|
2009-08-12 04:47:22 +08:00
|
|
|
TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
2010-12-21 10:38:05 +08:00
|
|
|
TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
|
2009-08-12 04:47:22 +08:00
|
|
|
CurDAG->getRegister(SP::G0, MVT::i32)), 0);
|
2006-02-05 13:50:24 +08:00
|
|
|
|
|
|
|
// FIXME: Handle div by immediate.
|
|
|
|
unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
|
2009-08-12 04:47:22 +08:00
|
|
|
return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
|
2006-08-26 16:00:10 +08:00
|
|
|
TopPart);
|
2008-10-10 18:14:15 +08:00
|
|
|
}
|
2006-02-05 13:50:24 +08:00
|
|
|
case ISD::MULHU:
|
|
|
|
case ISD::MULHS: {
|
|
|
|
// FIXME: Handle mul by immediate.
|
2008-07-28 05:46:04 +08:00
|
|
|
SDValue MulLHS = N->getOperand(0);
|
|
|
|
SDValue MulRHS = N->getOperand(1);
|
2006-02-05 13:50:24 +08:00
|
|
|
unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
|
2010-12-21 10:38:05 +08:00
|
|
|
SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
|
2009-09-26 02:54:59 +08:00
|
|
|
MulLHS, MulRHS);
|
2006-02-05 13:50:24 +08:00
|
|
|
// The high part is in the Y register.
|
2009-08-12 04:47:22 +08:00
|
|
|
return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
}
|
2008-10-10 18:14:15 +08:00
|
|
|
|
2010-01-05 09:24:18 +08:00
|
|
|
return SelectCode(N);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 18:14:47 +08:00
|
|
|
/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
|
|
|
|
/// inline asm expressions.
|
|
|
|
bool
|
|
|
|
SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
|
2015-03-13 04:09:39 +08:00
|
|
|
char ConstraintCode,
|
2008-10-10 18:14:47 +08:00
|
|
|
std::vector<SDValue> &OutOps) {
|
|
|
|
SDValue Op0, Op1;
|
2015-03-13 04:09:39 +08:00
|
|
|
switch (ConstraintCode) {
|
2008-10-10 18:14:47 +08:00
|
|
|
default: return true;
|
2015-03-13 04:09:39 +08:00
|
|
|
case 'm': // memory
|
2010-09-22 04:31:19 +08:00
|
|
|
if (!SelectADDRrr(Op, Op0, Op1))
|
|
|
|
SelectADDRri(Op, Op0, Op1);
|
2008-10-10 18:14:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutOps.push_back(Op0);
|
|
|
|
OutOps.push_back(Op1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-10 18:14:15 +08:00
|
|
|
/// createSparcISelDag - This pass converts a legalized DAG into a
|
2006-02-05 13:50:24 +08:00
|
|
|
/// SPARC-specific DAG, ready for instruction scheduling.
|
|
|
|
///
|
2008-10-04 00:55:19 +08:00
|
|
|
FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
|
2006-02-05 13:50:24 +08:00
|
|
|
return new SparcDAGToDAGISel(TM);
|
|
|
|
}
|