2008-11-07 10:59:00 +00:00
|
|
|
//===-- XCoreISelDAGToDAG.cpp - A dag to dag inst selector for XCore ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an instruction selector for the XCore target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "XCore.h"
|
|
|
|
#include "XCoreTargetMachine.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
#include "llvm/CallingConv.h"
|
|
|
|
#include "llvm/Constants.h"
|
2009-07-14 23:09:55 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-11-07 10:59:00 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAGISel.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-08 20:53:28 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-07 10:59:00 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// XCoreDAGToDAGISel - XCore specific code to select XCore machine
|
|
|
|
/// instructions for SelectionDAG operations.
|
|
|
|
///
|
|
|
|
namespace {
|
|
|
|
class XCoreDAGToDAGISel : public SelectionDAGISel {
|
2010-04-17 15:26:15 +00:00
|
|
|
const XCoreTargetLowering &Lowering;
|
2008-11-07 10:59:00 +00:00
|
|
|
const XCoreSubtarget &Subtarget;
|
|
|
|
|
|
|
|
public:
|
|
|
|
XCoreDAGToDAGISel(XCoreTargetMachine &TM)
|
2009-01-15 19:20:50 +00:00
|
|
|
: SelectionDAGISel(TM),
|
2008-11-07 10:59:00 +00:00
|
|
|
Lowering(*TM.getTargetLowering()),
|
|
|
|
Subtarget(*TM.getSubtargetImpl()) { }
|
|
|
|
|
2010-01-05 01:24:18 +00:00
|
|
|
SDNode *Select(SDNode *N);
|
2008-11-07 10:59:00 +00:00
|
|
|
|
|
|
|
/// getI32Imm - Return a target constant with the specified value, of type
|
|
|
|
/// i32.
|
|
|
|
inline SDValue getI32Imm(unsigned Imm) {
|
2009-08-11 20:47:22 +00:00
|
|
|
return CurDAG->getTargetConstant(Imm, MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complex Pattern Selectors.
|
2010-01-05 01:24:18 +00:00
|
|
|
bool SelectADDRspii(SDNode *Op, SDValue Addr, SDValue &Base,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Offset);
|
2010-01-05 01:24:18 +00:00
|
|
|
bool SelectADDRdpii(SDNode *Op, SDValue Addr, SDValue &Base,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Offset);
|
2010-01-05 01:24:18 +00:00
|
|
|
bool SelectADDRcpii(SDNode *Op, SDValue Addr, SDValue &Base,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Offset);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "XCore DAG->DAG Pattern Instruction Selection";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the pieces autogenerated from the target description.
|
|
|
|
#include "XCoreGenDAGISel.inc"
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// createXCoreISelDag - This pass converts a legalized DAG into a
|
|
|
|
/// XCore-specific DAG, ready for instruction scheduling.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createXCoreISelDag(XCoreTargetMachine &TM) {
|
|
|
|
return new XCoreDAGToDAGISel(TM);
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:24:18 +00:00
|
|
|
bool XCoreDAGToDAGISel::SelectADDRspii(SDNode *Op, SDValue Addr,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Base, SDValue &Offset) {
|
|
|
|
FrameIndexSDNode *FIN = 0;
|
2008-11-07 11:21:09 +00:00
|
|
|
if ((FIN = dyn_cast<FrameIndexSDNode>(Addr))) {
|
2009-08-11 20:47:22 +00:00
|
|
|
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
|
|
|
|
Offset = CurDAG->getTargetConstant(0, MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
|
|
|
ConstantSDNode *CN = 0;
|
|
|
|
if ((FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
|
|
|
|
&& (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
|
2009-01-15 11:32:30 +00:00
|
|
|
&& (CN->getSExtValue() % 4 == 0 && CN->getSExtValue() >= 0)) {
|
|
|
|
// Constant positive word offset from frame index
|
2009-08-11 20:47:22 +00:00
|
|
|
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
|
|
|
|
Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:24:18 +00:00
|
|
|
bool XCoreDAGToDAGISel::SelectADDRdpii(SDNode *Op, SDValue Addr,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Base, SDValue &Offset) {
|
|
|
|
if (Addr.getOpcode() == XCoreISD::DPRelativeWrapper) {
|
|
|
|
Base = Addr.getOperand(0);
|
2009-08-11 20:47:22 +00:00
|
|
|
Offset = CurDAG->getTargetConstant(0, MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
|
|
|
ConstantSDNode *CN = 0;
|
|
|
|
if ((Addr.getOperand(0).getOpcode() == XCoreISD::DPRelativeWrapper)
|
|
|
|
&& (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
|
|
|
|
&& (CN->getSExtValue() % 4 == 0)) {
|
|
|
|
// Constant word offset from a object in the data region
|
|
|
|
Base = Addr.getOperand(0).getOperand(0);
|
2009-08-11 20:47:22 +00:00
|
|
|
Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:24:18 +00:00
|
|
|
bool XCoreDAGToDAGISel::SelectADDRcpii(SDNode *Op, SDValue Addr,
|
2008-11-07 10:59:00 +00:00
|
|
|
SDValue &Base, SDValue &Offset) {
|
|
|
|
if (Addr.getOpcode() == XCoreISD::CPRelativeWrapper) {
|
|
|
|
Base = Addr.getOperand(0);
|
2009-08-11 20:47:22 +00:00
|
|
|
Offset = CurDAG->getTargetConstant(0, MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
|
|
|
ConstantSDNode *CN = 0;
|
|
|
|
if ((Addr.getOperand(0).getOpcode() == XCoreISD::CPRelativeWrapper)
|
|
|
|
&& (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
|
|
|
|
&& (CN->getSExtValue() % 4 == 0)) {
|
|
|
|
// Constant word offset from a object in the data region
|
|
|
|
Base = Addr.getOperand(0).getOperand(0);
|
2009-08-11 20:47:22 +00:00
|
|
|
Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
|
2008-11-07 10:59:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:24:18 +00:00
|
|
|
SDNode *XCoreDAGToDAGISel::Select(SDNode *N) {
|
2009-02-06 02:08:06 +00:00
|
|
|
DebugLoc dl = N->getDebugLoc();
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT NVT = N->getValueType(0);
|
2009-08-11 20:47:22 +00:00
|
|
|
if (NVT == MVT::i32) {
|
2008-11-07 10:59:00 +00:00
|
|
|
switch (N->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case ISD::Constant: {
|
|
|
|
if (Predicate_immMskBitp(N)) {
|
2010-03-01 19:38:53 +00:00
|
|
|
// Transformation function: get the size of a mask
|
|
|
|
int64_t MaskVal = cast<ConstantSDNode>(N)->getZExtValue();
|
|
|
|
assert(isMask_32(MaskVal));
|
|
|
|
// Look for the first non-zero bit
|
|
|
|
SDValue MskSize = getI32Imm(32 - CountLeadingZeros_32(MaskVal));
|
2009-09-25 18:54:59 +00:00
|
|
|
return CurDAG->getMachineNode(XCore::MKMSK_rus, dl,
|
|
|
|
MVT::i32, MskSize);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
else if (! Predicate_immU16(N)) {
|
|
|
|
unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
|
|
|
|
SDValue CPIdx =
|
2009-08-13 21:58:54 +00:00
|
|
|
CurDAG->getTargetConstantPool(ConstantInt::get(
|
|
|
|
Type::getInt32Ty(*CurDAG->getContext()), Val),
|
2008-11-07 10:59:00 +00:00
|
|
|
TLI.getPointerTy());
|
2009-09-25 18:54:59 +00:00
|
|
|
return CurDAG->getMachineNode(XCore::LDWCP_lru6, dl, MVT::i32,
|
|
|
|
MVT::Other, CPIdx,
|
|
|
|
CurDAG->getEntryNode());
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XCoreISD::LADD: {
|
2010-01-05 01:24:18 +00:00
|
|
|
SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
|
|
|
|
N->getOperand(2) };
|
2009-10-06 16:01:09 +00:00
|
|
|
return CurDAG->getMachineNode(XCore::LADD_l5r, dl, MVT::i32, MVT::i32,
|
|
|
|
Ops, 3);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
|
|
|
case XCoreISD::LSUB: {
|
2010-01-05 01:24:18 +00:00
|
|
|
SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
|
|
|
|
N->getOperand(2) };
|
2009-10-06 16:01:09 +00:00
|
|
|
return CurDAG->getMachineNode(XCore::LSUB_l5r, dl, MVT::i32, MVT::i32,
|
|
|
|
Ops, 3);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|
2010-03-10 11:41:08 +00:00
|
|
|
case XCoreISD::MACCU: {
|
|
|
|
SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
|
|
|
|
N->getOperand(2), N->getOperand(3) };
|
|
|
|
return CurDAG->getMachineNode(XCore::MACCU_l4r, dl, MVT::i32, MVT::i32,
|
|
|
|
Ops, 4);
|
|
|
|
}
|
|
|
|
case XCoreISD::MACCS: {
|
|
|
|
SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
|
|
|
|
N->getOperand(2), N->getOperand(3) };
|
|
|
|
return CurDAG->getMachineNode(XCore::MACCS_l4r, dl, MVT::i32, MVT::i32,
|
|
|
|
Ops, 4);
|
|
|
|
}
|
2010-03-10 13:27:10 +00:00
|
|
|
case XCoreISD::LMUL: {
|
|
|
|
SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
|
|
|
|
N->getOperand(2), N->getOperand(3) };
|
|
|
|
return CurDAG->getMachineNode(XCore::LMUL_l6r, dl, MVT::i32, MVT::i32,
|
|
|
|
Ops, 4);
|
|
|
|
}
|
2008-11-07 10:59:00 +00:00
|
|
|
// Other cases are autogenerated.
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 01:24:18 +00:00
|
|
|
return SelectCode(N);
|
2008-11-07 10:59:00 +00:00
|
|
|
}
|