2009-05-03 12:57:15 +00:00
|
|
|
//===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//
|
|
|
|
//
|
|
|
|
// 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 MSP430 target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MSP430.h"
|
|
|
|
#include "MSP430ISelLowering.h"
|
|
|
|
#include "MSP430TargetMachine.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
#include "llvm/CallingConv.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#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"
|
2009-05-03 12:57:15 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
|
|
|
|
/// instructions for SelectionDAG operations.
|
|
|
|
///
|
|
|
|
namespace {
|
|
|
|
class MSP430DAGToDAGISel : public SelectionDAGISel {
|
|
|
|
MSP430TargetLowering &Lowering;
|
|
|
|
const MSP430Subtarget &Subtarget;
|
|
|
|
|
|
|
|
public:
|
2009-05-03 13:19:42 +00:00
|
|
|
MSP430DAGToDAGISel(MSP430TargetMachine &TM, CodeGenOpt::Level OptLevel)
|
|
|
|
: SelectionDAGISel(TM, OptLevel),
|
2009-05-03 12:57:15 +00:00
|
|
|
Lowering(*TM.getTargetLowering()),
|
|
|
|
Subtarget(*TM.getSubtargetImpl()) { }
|
|
|
|
|
|
|
|
virtual void InstructionSelect();
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "MSP430 DAG->DAG Pattern Instruction Selection";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the pieces autogenerated from the target description.
|
|
|
|
#include "MSP430GenDAGISel.inc"
|
|
|
|
|
|
|
|
private:
|
|
|
|
SDNode *Select(SDValue Op);
|
2009-05-03 13:09:40 +00:00
|
|
|
bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Base, SDValue &Disp);
|
2009-05-03 12:58:58 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned Indent;
|
|
|
|
#endif
|
2009-05-03 12:57:15 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// createMSP430ISelDag - This pass converts a legalized DAG into a
|
|
|
|
/// MSP430-specific DAG, ready for instruction scheduling.
|
|
|
|
///
|
2009-05-03 13:19:42 +00:00
|
|
|
FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
|
|
|
return new MSP430DAGToDAGISel(TM, OptLevel);
|
2009-05-03 12:57:15 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 13:09:10 +00:00
|
|
|
// FIXME: This is pretty dummy routine and needs to be rewritten in the future.
|
2009-05-03 13:06:03 +00:00
|
|
|
bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue Addr,
|
2009-05-03 13:09:40 +00:00
|
|
|
SDValue &Base, SDValue &Disp) {
|
2009-05-03 13:10:11 +00:00
|
|
|
// Try to match frame address first.
|
|
|
|
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
|
2009-08-11 20:47:22 +00:00
|
|
|
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
|
|
|
|
Disp = CurDAG->getTargetConstant(0, MVT::i16);
|
2009-05-03 13:10:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-05-03 13:06:03 +00:00
|
|
|
|
2009-05-03 13:08:51 +00:00
|
|
|
switch (Addr.getOpcode()) {
|
|
|
|
case ISD::ADD:
|
2009-05-03 13:11:35 +00:00
|
|
|
// Operand is a result from ADD with constant operand which fits into i16.
|
|
|
|
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
|
2009-05-03 13:06:03 +00:00
|
|
|
uint64_t CVal = CN->getZExtValue();
|
|
|
|
// Offset should fit into 16 bits.
|
|
|
|
if (((CVal << 48) >> 48) == CVal) {
|
2009-05-03 13:10:11 +00:00
|
|
|
SDValue N0 = Addr.getOperand(0);
|
|
|
|
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N0))
|
2009-08-11 20:47:22 +00:00
|
|
|
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
|
2009-05-03 13:10:11 +00:00
|
|
|
else
|
|
|
|
Base = N0;
|
2009-05-03 13:06:03 +00:00
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
Disp = CurDAG->getTargetConstant(CVal, MVT::i16);
|
2009-05-03 13:06:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-05-03 13:08:51 +00:00
|
|
|
break;
|
|
|
|
case MSP430ISD::Wrapper:
|
|
|
|
SDValue N0 = Addr.getOperand(0);
|
|
|
|
if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
|
2009-05-03 13:09:40 +00:00
|
|
|
Base = CurDAG->getTargetGlobalAddress(G->getGlobal(),
|
2009-08-11 20:47:22 +00:00
|
|
|
MVT::i16, G->getOffset());
|
|
|
|
Disp = CurDAG->getTargetConstant(0, MVT::i16);
|
2009-05-03 13:09:10 +00:00
|
|
|
return true;
|
2009-05-03 13:14:46 +00:00
|
|
|
} else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(N0)) {
|
2009-08-11 20:47:22 +00:00
|
|
|
Base = CurDAG->getTargetExternalSymbol(E->getSymbol(), MVT::i16);
|
|
|
|
Disp = CurDAG->getTargetConstant(0, MVT::i16);
|
2009-05-03 13:08:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
};
|
2009-05-03 13:06:03 +00:00
|
|
|
|
2009-05-03 13:09:40 +00:00
|
|
|
Base = Addr;
|
2009-08-11 20:47:22 +00:00
|
|
|
Disp = CurDAG->getTargetConstant(0, MVT::i16);
|
2009-05-03 13:06:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-03 12:57:15 +00:00
|
|
|
/// InstructionSelect - This callback is invoked by
|
|
|
|
/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
|
2009-05-03 12:58:40 +00:00
|
|
|
void MSP430DAGToDAGISel::InstructionSelect() {
|
2009-05-03 12:57:15 +00:00
|
|
|
DEBUG(BB->dump());
|
|
|
|
|
2009-05-03 13:16:37 +00:00
|
|
|
// Codegen the basic block.
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "===== Instruction selection begins:\n");
|
2009-08-23 08:50:52 +00:00
|
|
|
DEBUG(Indent = 0);
|
2009-05-03 12:57:15 +00:00
|
|
|
SelectRoot(*CurDAG);
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "===== Instruction selection ends:\n");
|
2009-05-03 12:57:15 +00:00
|
|
|
|
|
|
|
CurDAG->RemoveDeadNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
|
2009-05-03 12:58:58 +00:00
|
|
|
SDNode *Node = Op.getNode();
|
2009-05-03 13:10:26 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-05-03 12:58:58 +00:00
|
|
|
|
|
|
|
// Dump information about the Node being selected
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs().indent(Indent) << "Selecting: ");
|
2009-05-03 12:58:58 +00:00
|
|
|
DEBUG(Node->dump(CurDAG));
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "\n");
|
2009-08-23 08:50:52 +00:00
|
|
|
DEBUG(Indent += 2);
|
2009-05-03 12:58:58 +00:00
|
|
|
|
|
|
|
// If we have a custom node, we already have selected!
|
|
|
|
if (Node->isMachineOpcode()) {
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs().indent(Indent-2) << "== ";
|
|
|
|
Node->dump(CurDAG);
|
|
|
|
errs() << "\n");
|
2009-08-23 08:50:52 +00:00
|
|
|
DEBUG(Indent -= 2);
|
2009-05-03 12:58:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-03 13:10:26 +00:00
|
|
|
// Few custom selection stuff.
|
|
|
|
switch (Node->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case ISD::FrameIndex: {
|
2009-08-11 20:47:22 +00:00
|
|
|
assert(Op.getValueType() == MVT::i16);
|
2009-05-03 13:10:26 +00:00
|
|
|
int FI = cast<FrameIndexSDNode>(Node)->getIndex();
|
2009-08-11 20:47:22 +00:00
|
|
|
SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);
|
2009-05-03 13:10:26 +00:00
|
|
|
if (Node->hasOneUse())
|
2009-08-11 20:47:22 +00:00
|
|
|
return CurDAG->SelectNodeTo(Node, MSP430::ADD16ri, MVT::i16,
|
|
|
|
TFI, CurDAG->getTargetConstant(0, MVT::i16));
|
2009-09-25 18:54:59 +00:00
|
|
|
return CurDAG->getMachineNode(MSP430::ADD16ri, dl, MVT::i16,
|
|
|
|
TFI, CurDAG->getTargetConstant(0, MVT::i16));
|
2009-05-03 13:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-03 12:58:58 +00:00
|
|
|
|
|
|
|
// Select the default instruction
|
|
|
|
SDNode *ResNode = SelectCode(Op);
|
|
|
|
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
|
2009-05-03 12:58:58 +00:00
|
|
|
if (ResNode == NULL || ResNode == Op.getNode())
|
|
|
|
DEBUG(Op.getNode()->dump(CurDAG));
|
|
|
|
else
|
|
|
|
DEBUG(ResNode->dump(CurDAG));
|
2009-08-23 06:49:22 +00:00
|
|
|
DEBUG(errs() << "\n");
|
2009-08-23 08:50:52 +00:00
|
|
|
DEBUG(Indent -= 2);
|
2009-05-03 12:58:58 +00:00
|
|
|
|
|
|
|
return ResNode;
|
2009-05-03 12:57:15 +00:00
|
|
|
}
|