blob: 9e55c9ee09ac953de483da24ef287524f8bd00ef [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines an instruction selector for the MIPS target.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
14#define DEBUG_TYPE "mips-isel"
Akira Hatanaka554d9312013-03-14 18:27:31 +000015#include "MipsISelDAGToDAG.h"
16#include "Mips16ISelDAGToDAG.h"
17#include "MipsSEISelDAGToDAG.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "Mips.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "MCTargetDesc/MipsBaseInfo.h"
Akira Hatanaka57fa3822012-01-25 03:01:35 +000020#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000021#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "MipsRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanaka44b6c712012-02-28 02:55:02 +000028#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/GlobalValue.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/Type.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/Support/CFG.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038using namespace llvm;
39
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000040//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000042//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000044//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000045// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000047//===----------------------------------------------------------------------===//
Akira Hatanaka7065b7b2012-03-08 01:51:59 +000048
Akira Hatanaka648f00c2012-02-24 22:34:47 +000049bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
50 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +000051
Akira Hatanaka7065b7b2012-03-08 01:51:59 +000052 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +000053
54 return Ret;
55}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000057/// getGlobalBaseReg - Output the instructions required to put the
58/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +000059SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000060 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +000061 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000062}
63
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064/// ComplexPattern used on MipsInstrInfo
65/// Used on Mips Load/Store instructions
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000066bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
67 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000068 llvm_unreachable("Unimplemented function.");
Akira Hatanakadc2f7922013-02-15 21:20:45 +000069 return false;
70}
71
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000072bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
73 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000074 llvm_unreachable("Unimplemented function.");
75 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000076}
77
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000078bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
79 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000080 llvm_unreachable("Unimplemented function.");
81 return false;
Akira Hatanakadc2f7922013-02-15 21:20:45 +000082}
83
Akira Hatanaka554d9312013-03-14 18:27:31 +000084bool MipsDAGToDAGISel::SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base,
85 SDValue &Offset, SDValue &Alias) {
86 llvm_unreachable("Unimplemented function.");
87 return false;
Reed Kotlerf99998a2012-10-28 06:02:37 +000088}
Akira Hatanaka2fd04752011-12-20 23:10:57 +000089
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090/// Select instructions not customized! Used for
91/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +000092SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +000094 DebugLoc dl = Node->getDebugLoc();
Akira Hatanaka554d9312013-03-14 18:27:31 +000095 EVT NodeTy = Node->getValueType(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
97 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +000098 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099
100 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000101 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000102 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103 return NULL;
104 }
105
Akira Hatanaka554d9312013-03-14 18:27:31 +0000106 // See if subclasses can handle this node.
107 std::pair<bool, SDNode*> Ret = SelectNode(Node);
108
109 if (Ret.first)
110 return Ret.second;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000111
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000112 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000113 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000115 // Get target GOT address.
116 case ISD::GLOBAL_OFFSET_TABLE:
117 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000118
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000119#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
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000127 }
128
129 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000130 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000131
Chris Lattner7c306da2010-03-02 06:34:30 +0000132 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000133 if (ResNode == NULL || ResNode == Node)
134 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000135 else
136 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000137 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000138 return ResNode;
139}
140
Akira Hatanaka21afc632011-06-21 00:40:49 +0000141bool MipsDAGToDAGISel::
142SelectInlineAsmMemoryOperand(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
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000149/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000150/// MIPS-specific DAG, ready for instruction scheduling.
151FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
Akira Hatanaka554d9312013-03-14 18:27:31 +0000152 if (TM.getSubtargetImpl()->inMips16Mode())
153 return llvm::createMips16ISelDag(TM);
154
155 return llvm::createMipsSEISelDag(TM);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000156}