blob: a0d5bd9ef30560663eca6a59080a911a83cf11d8 [file] [log] [blame]
Akira Hatanaka30a84782013-03-14 18:27:31 +00001//===-- Mips16ISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips16 ----===//
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// Subclass of MipsDAGToDAGISel specialized for mips16.
11//
12//===----------------------------------------------------------------------===//
13
Akira Hatanaka30a84782013-03-14 18:27:31 +000014#include "Mips16ISelDAGToDAG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "Mips.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000017#include "MipsMachineFunction.h"
18#include "MipsRegisterInfo.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000026#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/Type.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetMachine.h"
34using namespace llvm;
35
Chandler Carruth84e68b22014-04-22 02:41:26 +000036#define DEBUG_TYPE "mips-isel"
37
Reed Kotler1595f362013-04-09 19:46:01 +000038bool Mips16DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher96e72c62015-01-29 23:27:36 +000039 Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget());
Eric Christopher22405e42014-07-10 17:26:51 +000040 if (!Subtarget->inMips16Mode())
Reed Kotler1595f362013-04-09 19:46:01 +000041 return false;
42 return MipsDAGToDAGISel::runOnMachineFunction(MF);
43}
Akira Hatanaka30a84782013-03-14 18:27:31 +000044/// Select multiply instructions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000045std::pair<SDNode *, SDNode *>
46Mips16DAGToDAGISel::selectMULT(SDNode *N, unsigned Opc, const SDLoc &DL, EVT Ty,
Akira Hatanaka30a84782013-03-14 18:27:31 +000047 bool HasLo, bool HasHi) {
Craig Topper062a2ba2014-04-25 05:30:21 +000048 SDNode *Lo = nullptr, *Hi = nullptr;
Akira Hatanaka040d2252013-03-14 18:33:23 +000049 SDNode *Mul = CurDAG->getMachineNode(Opc, DL, MVT::Glue, N->getOperand(0),
Akira Hatanaka30a84782013-03-14 18:27:31 +000050 N->getOperand(1));
51 SDValue InFlag = SDValue(Mul, 0);
52
53 if (HasLo) {
54 unsigned Opcode = Mips::Mflo16;
Akira Hatanaka040d2252013-03-14 18:33:23 +000055 Lo = CurDAG->getMachineNode(Opcode, DL, Ty, MVT::Glue, InFlag);
Akira Hatanaka30a84782013-03-14 18:27:31 +000056 InFlag = SDValue(Lo, 1);
57 }
58 if (HasHi) {
59 unsigned Opcode = Mips::Mfhi16;
Akira Hatanaka040d2252013-03-14 18:33:23 +000060 Hi = CurDAG->getMachineNode(Opcode, DL, Ty, InFlag);
Akira Hatanaka30a84782013-03-14 18:27:31 +000061 }
62 return std::make_pair(Lo, Hi);
63}
64
Akira Hatanaka040d2252013-03-14 18:33:23 +000065void Mips16DAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
Akira Hatanaka30a84782013-03-14 18:27:31 +000066 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
67
68 if (!MipsFI->globalBaseRegSet())
69 return;
70
71 MachineBasicBlock &MBB = MF.front();
Tim Northover775aaeb2015-11-05 21:54:58 +000072 MachineBasicBlock::iterator I = MBB.begin();
73 MachineRegisterInfo &RegInfo = MF.getRegInfo();
74 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
75 DebugLoc DL;
76 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
77 const TargetRegisterClass *RC = &Mips::CPU16RegsRegClass;
78
Akira Hatanaka30a84782013-03-14 18:27:31 +000079 V0 = RegInfo.createVirtualRegister(RC);
80 V1 = RegInfo.createVirtualRegister(RC);
81 V2 = RegInfo.createVirtualRegister(RC);
82
Simon Dardisf1148202016-08-24 13:00:47 +000083
84 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
85 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
86 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
Daniel Sanders8015c702016-06-15 09:44:22 +000087 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
Reed Kotlerd6aadc72013-09-18 22:46:09 +000088
Akira Hatanaka30a84782013-03-14 18:27:31 +000089 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
90 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
Daniel Sanders8015c702016-06-15 09:44:22 +000091 .addReg(V1)
92 .addReg(V2);
Akira Hatanaka30a84782013-03-14 18:27:31 +000093}
94
Akira Hatanaka040d2252013-03-14 18:33:23 +000095void Mips16DAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
96 initGlobalBaseReg(MF);
Akira Hatanaka30a84782013-03-14 18:27:31 +000097}
98
Daniel Sandersde7816b2016-06-16 10:20:59 +000099bool Mips16DAGToDAGISel::selectAddr(bool SPAllowed, SDValue Addr, SDValue &Base,
100 SDValue &Offset) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000101 SDLoc DL(Addr);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000102 EVT ValTy = Addr.getValueType();
103
Akira Hatanaka30a84782013-03-14 18:27:31 +0000104 // if Address is FI, get the TargetFrameIndex.
Daniel Sandersde7816b2016-06-16 10:20:59 +0000105 if (SPAllowed) {
106 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
107 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
108 Offset = CurDAG->getTargetConstant(0, DL, ValTy);
109 return true;
110 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000111 }
112 // on PIC code Load GA
113 if (Addr.getOpcode() == MipsISD::Wrapper) {
Daniel Sanders8015c702016-06-15 09:44:22 +0000114 Base = Addr.getOperand(0);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000115 Offset = Addr.getOperand(1);
116 return true;
117 }
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000118 if (!TM.isPositionIndependent()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000119 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Daniel Sanders8015c702016-06-15 09:44:22 +0000120 Addr.getOpcode() == ISD::TargetGlobalAddress))
Akira Hatanaka30a84782013-03-14 18:27:31 +0000121 return false;
122 }
123 // Addresses of the form FI+const or FI|const
124 if (CurDAG->isBaseWithConstantOffset(Addr)) {
125 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
126 if (isInt<16>(CN->getSExtValue())) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000127 // If the first operand is a FI, get the TargetFI Node
Daniel Sandersde7816b2016-06-16 10:20:59 +0000128 if (SPAllowed) {
129 if (FrameIndexSDNode *FIN =
130 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
131 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
132 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), DL, ValTy);
133 return true;
134 }
135 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000136
Daniel Sandersde7816b2016-06-16 10:20:59 +0000137 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000138 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), DL, ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000139 return true;
140 }
141 }
142 // Operand is a result from an ADD.
143 if (Addr.getOpcode() == ISD::ADD) {
144 // When loading from constant pools, load the lower address part in
145 // the instruction itself. Example, instead of:
146 // lui $2, %hi($CPI1_0)
147 // addiu $2, $2, %lo($CPI1_0)
148 // lwc1 $f0, 0($2)
149 // Generate:
150 // lui $2, %hi($CPI1_0)
151 // lwc1 $f0, %lo($CPI1_0)($2)
152 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
153 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
154 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
155 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
156 isa<JumpTableSDNode>(Opnd0)) {
157 Base = Addr.getOperand(0);
158 Offset = Opnd0;
159 return true;
160 }
161 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000162 }
Daniel Sanders8015c702016-06-15 09:44:22 +0000163 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000164 Offset = CurDAG->getTargetConstant(0, DL, ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000165 return true;
166}
167
Daniel Sandersde7816b2016-06-16 10:20:59 +0000168bool Mips16DAGToDAGISel::selectAddr16(SDValue Addr, SDValue &Base,
169 SDValue &Offset) {
170 return selectAddr(false, Addr, Base, Offset);
171}
172
173bool Mips16DAGToDAGISel::selectAddr16SP(SDValue Addr, SDValue &Base,
174 SDValue &Offset) {
175 return selectAddr(true, Addr, Base, Offset);
176}
177
Akira Hatanaka30a84782013-03-14 18:27:31 +0000178/// Select instructions not customized! Used for
179/// expanded, promoted and normal instructions
Justin Bognereeae7512016-05-13 23:55:59 +0000180bool Mips16DAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000181 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000182 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000183
184 ///
185 // Instruction Selection not handled by the auto-generated
186 // tablegen selection should be handled here.
187 ///
188 EVT NodeTy = Node->getValueType(0);
189 unsigned MultOpc;
190
Daniel Sanders8015c702016-06-15 09:44:22 +0000191 switch (Opcode) {
192 default:
193 break;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000194
Akira Hatanaka30a84782013-03-14 18:27:31 +0000195 /// Mul with two results
196 case ISD::SMUL_LOHI:
197 case ISD::UMUL_LOHI: {
198 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 : Mips::MultRxRy16);
Daniel Sanders8015c702016-06-15 09:44:22 +0000199 std::pair<SDNode *, SDNode *> LoHi =
200 selectMULT(Node, MultOpc, DL, NodeTy, true, true);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000201 if (!SDValue(Node, 0).use_empty())
202 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
203
204 if (!SDValue(Node, 1).use_empty())
205 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
206
Justin Bognere58d6222016-05-13 06:30:15 +0000207 CurDAG->RemoveDeadNode(Node);
Justin Bognereeae7512016-05-13 23:55:59 +0000208 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000209 }
210
211 case ISD::MULHS:
212 case ISD::MULHU: {
213 MultOpc = (Opcode == ISD::MULHU ? Mips::MultuRxRy16 : Mips::MultRxRy16);
Justin Bognereeae7512016-05-13 23:55:59 +0000214 auto LoHi = selectMULT(Node, MultOpc, DL, NodeTy, false, true);
215 ReplaceNode(Node, LoHi.second);
216 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000217 }
218 }
219
Justin Bognereeae7512016-05-13 23:55:59 +0000220 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000221}
222
Daniel Sanders46fe6552016-07-14 13:25:22 +0000223FunctionPass *llvm::createMips16ISelDag(MipsTargetMachine &TM,
224 CodeGenOpt::Level OptLevel) {
225 return new Mips16DAGToDAGISel(TM, OptLevel);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000226}