blob: 0ad8752e88fcdcca0e0df92daf6f4b0fe25c7908 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MSP430 target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430.h"
15#include "MSP430ISelLowering.h"
16#include "MSP430TargetMachine.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/Debug.h"
31#include <queue>
32#include <set>
33using namespace llvm;
34
35/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
36/// instructions for SelectionDAG operations.
37///
38namespace {
39 class MSP430DAGToDAGISel : public SelectionDAGISel {
40 MSP430TargetLowering &Lowering;
41 const MSP430Subtarget &Subtarget;
42
43 public:
44 MSP430DAGToDAGISel(MSP430TargetMachine &TM)
45 : SelectionDAGISel(TM),
46 Lowering(*TM.getTargetLowering()),
47 Subtarget(*TM.getSubtargetImpl()) { }
48
49 virtual void InstructionSelect();
50
51 virtual const char *getPassName() const {
52 return "MSP430 DAG->DAG Pattern Instruction Selection";
53 }
54
55 // Include the pieces autogenerated from the target description.
56 #include "MSP430GenDAGISel.inc"
57
58 private:
59 SDNode *Select(SDValue Op);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +000060
61 #ifndef NDEBUG
62 unsigned Indent;
63 #endif
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000064 };
65} // end anonymous namespace
66
67/// createMSP430ISelDag - This pass converts a legalized DAG into a
68/// MSP430-specific DAG, ready for instruction scheduling.
69///
70FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM) {
71 return new MSP430DAGToDAGISel(TM);
72}
73
74/// InstructionSelect - This callback is invoked by
75/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Anton Korobeynikov9e123392009-05-03 12:58:40 +000076void MSP430DAGToDAGISel::InstructionSelect() {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000077 DEBUG(BB->dump());
78
79 // Select target instructions for the DAG.
80 SelectRoot(*CurDAG);
81
82 CurDAG->RemoveDeadNodes();
83}
84
85SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +000086 SDNode *Node = Op.getNode();
87
88 // Dump information about the Node being selected
89 #ifndef NDEBUG
90 DOUT << std::string(Indent, ' ') << "Selecting: ";
91 DEBUG(Node->dump(CurDAG));
92 DOUT << "\n";
93 Indent += 2;
94 #endif
95
96 // If we have a custom node, we already have selected!
97 if (Node->isMachineOpcode()) {
98 #ifndef NDEBUG
99 DOUT << std::string(Indent-2, ' ') << "== ";
100 DEBUG(Node->dump(CurDAG));
101 DOUT << "\n";
102 Indent -= 2;
103 #endif
104 return NULL;
105 }
106
107 // Instruction Selection not handled by the auto-generated tablegen selection
108 // should be handled here.
109 // Something like this:
110 // unsigned Opcode = Node->getOpcode();
111 // switch (Opcode) {
112 // default: break;
113 // case ISD::Foo:
114 // return SelectFoo(Node)
115 // }
116
117 // Select the default instruction
118 SDNode *ResNode = SelectCode(Op);
119
120 #ifndef NDEBUG
121 DOUT << std::string(Indent-2, ' ') << "=> ";
122 if (ResNode == NULL || ResNode == Op.getNode())
123 DEBUG(Op.getNode()->dump(CurDAG));
124 else
125 DEBUG(ResNode->dump(CurDAG));
126 DOUT << "\n";
127 Indent -= 2;
128 #endif
129
130 return ResNode;
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000131}