9e55c9ee09ac953de483da24ef287524f8bd00ef
[oota-llvm.git] / lib / Target / Mips / MipsISelDAGToDAG.cpp
1 //===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
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 MIPS target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mips-isel"
15 #include "MipsISelDAGToDAG.h"
16 #include "Mips16ISelDAGToDAG.h"
17 #include "MipsSEISelDAGToDAG.h"
18 #include "Mips.h"
19 #include "MCTargetDesc/MipsBaseInfo.h"
20 #include "MipsAnalyzeImmediate.h"
21 #include "MipsMachineFunction.h"
22 #include "MipsRegisterInfo.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAGNodes.h"
29 #include "llvm/IR/GlobalValue.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/Support/CFG.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetMachine.h"
38 using namespace llvm;
39
40 //===----------------------------------------------------------------------===//
41 // Instruction Selector Implementation
42 //===----------------------------------------------------------------------===//
43
44 //===----------------------------------------------------------------------===//
45 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46 // instructions for SelectionDAG operations.
47 //===----------------------------------------------------------------------===//
48
49 bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
50   bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
51
52   ProcessFunctionAfterISel(MF);
53
54   return Ret;
55 }
56
57 /// getGlobalBaseReg - Output the instructions required to put the
58 /// GOT address into a register.
59 SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
60   unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
61   return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
62 }
63
64 /// ComplexPattern used on MipsInstrInfo
65 /// Used on Mips Load/Store instructions
66 bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
67                                         SDValue &Offset) const {
68   llvm_unreachable("Unimplemented function.");
69   return false;
70 }
71
72 bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
73                                          SDValue &Offset) const {
74   llvm_unreachable("Unimplemented function.");
75   return false;
76 }
77
78 bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
79                                      SDValue &Offset) const {
80   llvm_unreachable("Unimplemented function.");
81   return false;
82 }
83
84 bool MipsDAGToDAGISel::SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base,
85                                     SDValue &Offset, SDValue &Alias) {
86   llvm_unreachable("Unimplemented function.");
87   return false;
88 }
89
90 /// Select instructions not customized! Used for
91 /// expanded, promoted and normal instructions
92 SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
93   unsigned Opcode = Node->getOpcode();
94   DebugLoc dl = Node->getDebugLoc();
95   EVT NodeTy = Node->getValueType(0);
96
97   // Dump information about the Node being selected
98   DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
99
100   // If we have a custom node, we already have selected!
101   if (Node->isMachineOpcode()) {
102     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
103     return NULL;
104   }
105
106   // See if subclasses can handle this node.
107   std::pair<bool, SDNode*> Ret = SelectNode(Node);
108
109   if (Ret.first)
110     return Ret.second;
111
112   switch(Opcode) {
113   default: break;
114
115   // Get target GOT address.
116   case ISD::GLOBAL_OFFSET_TABLE:
117     return getGlobalBaseReg();
118
119 #ifndef NDEBUG
120   case ISD::LOAD:
121   case ISD::STORE:
122     assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
123            cast<MemSDNode>(Node)->getAlignment() &&
124            "Unexpected unaligned loads/stores.");
125     break;
126 #endif
127   }
128
129   // Select the default instruction
130   SDNode *ResNode = SelectCode(Node);
131
132   DEBUG(errs() << "=> ");
133   if (ResNode == NULL || ResNode == Node)
134     DEBUG(Node->dump(CurDAG));
135   else
136     DEBUG(ResNode->dump(CurDAG));
137   DEBUG(errs() << "\n");
138   return ResNode;
139 }
140
141 bool MipsDAGToDAGISel::
142 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
143                              std::vector<SDValue> &OutOps) {
144   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
145   OutOps.push_back(Op);
146   return false;
147 }
148
149 /// createMipsISelDag - This pass converts a legalized DAG into a
150 /// MIPS-specific DAG, ready for instruction scheduling.
151 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
152   if (TM.getSubtargetImpl()->inMips16Mode())
153     return llvm::createMips16ISelDag(TM);
154
155   return llvm::createMipsSEISelDag(TM);
156 }