blob: 968e5364842f30d878d44e7883ea9e6aebcc1df0 [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"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000020#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "MipsRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanaka44b6c712012-02-28 02:55:02 +000027#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/GlobalValue.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/Intrinsics.h"
31#include "llvm/IR/Type.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Support/CFG.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000037using namespace llvm;
38
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000039//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000041//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000043//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000044// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
45// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000046//===----------------------------------------------------------------------===//
Akira Hatanaka7065b7b2012-03-08 01:51:59 +000047
Akira Hatanaka648f00c2012-02-24 22:34:47 +000048bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
49 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +000050
Akira Hatanakaf2835122013-03-14 18:33:23 +000051 processFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +000052
53 return Ret;
54}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000056/// getGlobalBaseReg - Output the instructions required to put the
57/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +000058SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000059 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +000060 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000061}
62
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063/// ComplexPattern used on MipsInstrInfo
64/// Used on Mips Load/Store instructions
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000065bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
66 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000067 llvm_unreachable("Unimplemented function.");
Akira Hatanakadc2f7922013-02-15 21:20:45 +000068 return false;
69}
70
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000071bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
72 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000073 llvm_unreachable("Unimplemented function.");
74 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075}
76
Akira Hatanakaabbf9df2013-02-16 00:14:37 +000077bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
78 SDValue &Offset) const {
Akira Hatanaka554d9312013-03-14 18:27:31 +000079 llvm_unreachable("Unimplemented function.");
80 return false;
Akira Hatanakadc2f7922013-02-15 21:20:45 +000081}
82
Akira Hatanakaf2835122013-03-14 18:33:23 +000083bool MipsDAGToDAGISel::selectAddr16(SDNode *Parent, SDValue N, SDValue &Base,
Akira Hatanaka554d9312013-03-14 18:27:31 +000084 SDValue &Offset, SDValue &Alias) {
85 llvm_unreachable("Unimplemented function.");
86 return false;
Reed Kotlerf99998a2012-10-28 06:02:37 +000087}
Akira Hatanaka2fd04752011-12-20 23:10:57 +000088
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089/// Select instructions not customized! Used for
90/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +000091SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092 unsigned Opcode = Node->getOpcode();
93
94 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +000095 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
97 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +000098 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +000099 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000100 return NULL;
101 }
102
Akira Hatanaka554d9312013-03-14 18:27:31 +0000103 // See if subclasses can handle this node.
Akira Hatanakaf2835122013-03-14 18:33:23 +0000104 std::pair<bool, SDNode*> Ret = selectNode(Node);
Akira Hatanaka554d9312013-03-14 18:27:31 +0000105
106 if (Ret.first)
107 return Ret.second;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000108
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000110 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000112 // Get target GOT address.
113 case ISD::GLOBAL_OFFSET_TABLE:
114 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000115
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000116#ifndef NDEBUG
117 case ISD::LOAD:
118 case ISD::STORE:
119 assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
120 cast<MemSDNode>(Node)->getAlignment() &&
121 "Unexpected unaligned loads/stores.");
122 break;
123#endif
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124 }
125
126 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000127 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000128
Chris Lattner7c306da2010-03-02 06:34:30 +0000129 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000130 if (ResNode == NULL || ResNode == Node)
131 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000132 else
133 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000134 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000135 return ResNode;
136}
137
Akira Hatanaka21afc632011-06-21 00:40:49 +0000138bool MipsDAGToDAGISel::
139SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
140 std::vector<SDValue> &OutOps) {
141 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
142 OutOps.push_back(Op);
143 return false;
144}
145
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000146/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000147/// MIPS-specific DAG, ready for instruction scheduling.
148FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
Akira Hatanaka554d9312013-03-14 18:27:31 +0000149 if (TM.getSubtargetImpl()->inMips16Mode())
150 return llvm::createMips16ISelDag(TM);
151
152 return llvm::createMipsSEISelDag(TM);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000153}