blob: 768d54fc9c24ebe4ea7901033254a22fda427382 [file] [log] [blame]
Akira Hatanaka30a84782013-03-14 18:27:31 +00001//===-- Mips16ISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips16 ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Akira Hatanaka30a84782013-03-14 18:27:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsDAGToDAGISel specialized for mips16.
10//
11//===----------------------------------------------------------------------===//
12
Akira Hatanaka30a84782013-03-14 18:27:31 +000013#include "Mips16ISelDAGToDAG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000014#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "Mips.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000016#include "MipsMachineFunction.h"
17#include "MipsRegisterInfo.h"
18#include "llvm/CodeGen/MachineConstantPool.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000024#include "llvm/IR/CFG.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000025#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Instructions.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/Type.h"
Akira Hatanaka30a84782013-03-14 18:27:31 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Target/TargetMachine.h"
33using namespace llvm;
34
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "mips-isel"
36
Reed Kotler1595f362013-04-09 19:46:01 +000037bool Mips16DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher96e72c62015-01-29 23:27:36 +000038 Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget());
Eric Christopher22405e42014-07-10 17:26:51 +000039 if (!Subtarget->inMips16Mode())
Reed Kotler1595f362013-04-09 19:46:01 +000040 return false;
41 return MipsDAGToDAGISel::runOnMachineFunction(MF);
42}
Akira Hatanaka30a84782013-03-14 18:27:31 +000043/// Select multiply instructions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000044std::pair<SDNode *, SDNode *>
45Mips16DAGToDAGISel::selectMULT(SDNode *N, unsigned Opc, const SDLoc &DL, EVT Ty,
Akira Hatanaka30a84782013-03-14 18:27:31 +000046 bool HasLo, bool HasHi) {
Craig Topper062a2ba2014-04-25 05:30:21 +000047 SDNode *Lo = nullptr, *Hi = nullptr;
Akira Hatanaka040d2252013-03-14 18:33:23 +000048 SDNode *Mul = CurDAG->getMachineNode(Opc, DL, MVT::Glue, N->getOperand(0),
Akira Hatanaka30a84782013-03-14 18:27:31 +000049 N->getOperand(1));
50 SDValue InFlag = SDValue(Mul, 0);
51
52 if (HasLo) {
53 unsigned Opcode = Mips::Mflo16;
Akira Hatanaka040d2252013-03-14 18:33:23 +000054 Lo = CurDAG->getMachineNode(Opcode, DL, Ty, MVT::Glue, InFlag);
Akira Hatanaka30a84782013-03-14 18:27:31 +000055 InFlag = SDValue(Lo, 1);
56 }
57 if (HasHi) {
58 unsigned Opcode = Mips::Mfhi16;
Akira Hatanaka040d2252013-03-14 18:33:23 +000059 Hi = CurDAG->getMachineNode(Opcode, DL, Ty, InFlag);
Akira Hatanaka30a84782013-03-14 18:27:31 +000060 }
61 return std::make_pair(Lo, Hi);
62}
63
Akira Hatanaka040d2252013-03-14 18:33:23 +000064void Mips16DAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
Akira Hatanaka30a84782013-03-14 18:27:31 +000065 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
66
67 if (!MipsFI->globalBaseRegSet())
68 return;
69
70 MachineBasicBlock &MBB = MF.front();
Tim Northover775aaeb2015-11-05 21:54:58 +000071 MachineBasicBlock::iterator I = MBB.begin();
72 MachineRegisterInfo &RegInfo = MF.getRegInfo();
73 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
74 DebugLoc DL;
Daniel Sanders0c476112019-08-15 19:22:08 +000075 Register V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Tim Northover775aaeb2015-11-05 21:54:58 +000076 const TargetRegisterClass *RC = &Mips::CPU16RegsRegClass;
77
Akira Hatanaka30a84782013-03-14 18:27:31 +000078 V0 = RegInfo.createVirtualRegister(RC);
79 V1 = RegInfo.createVirtualRegister(RC);
80 V2 = RegInfo.createVirtualRegister(RC);
81
Simon Dardisf1148202016-08-24 13:00:47 +000082
83 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
84 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
85 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
Daniel Sanders8015c702016-06-15 09:44:22 +000086 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
Reed Kotlerd6aadc72013-09-18 22:46:09 +000087
Akira Hatanaka30a84782013-03-14 18:27:31 +000088 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
89 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
Daniel Sanders8015c702016-06-15 09:44:22 +000090 .addReg(V1)
91 .addReg(V2);
Akira Hatanaka30a84782013-03-14 18:27:31 +000092}
93
Akira Hatanaka040d2252013-03-14 18:33:23 +000094void Mips16DAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
95 initGlobalBaseReg(MF);
Akira Hatanaka30a84782013-03-14 18:27:31 +000096}
97
Daniel Sandersde7816b2016-06-16 10:20:59 +000098bool Mips16DAGToDAGISel::selectAddr(bool SPAllowed, SDValue Addr, SDValue &Base,
99 SDValue &Offset) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000100 SDLoc DL(Addr);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000101 EVT ValTy = Addr.getValueType();
102
Akira Hatanaka30a84782013-03-14 18:27:31 +0000103 // if Address is FI, get the TargetFrameIndex.
Daniel Sandersde7816b2016-06-16 10:20:59 +0000104 if (SPAllowed) {
105 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
106 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
107 Offset = CurDAG->getTargetConstant(0, DL, ValTy);
108 return true;
109 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000110 }
111 // on PIC code Load GA
112 if (Addr.getOpcode() == MipsISD::Wrapper) {
Daniel Sanders8015c702016-06-15 09:44:22 +0000113 Base = Addr.getOperand(0);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000114 Offset = Addr.getOperand(1);
115 return true;
116 }
Rafael Espindolab30e66b2016-06-28 14:33:28 +0000117 if (!TM.isPositionIndependent()) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000118 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Daniel Sanders8015c702016-06-15 09:44:22 +0000119 Addr.getOpcode() == ISD::TargetGlobalAddress))
Akira Hatanaka30a84782013-03-14 18:27:31 +0000120 return false;
121 }
122 // Addresses of the form FI+const or FI|const
123 if (CurDAG->isBaseWithConstantOffset(Addr)) {
124 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
125 if (isInt<16>(CN->getSExtValue())) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000126 // If the first operand is a FI, get the TargetFI Node
Daniel Sandersde7816b2016-06-16 10:20:59 +0000127 if (SPAllowed) {
128 if (FrameIndexSDNode *FIN =
129 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
130 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
131 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), DL, ValTy);
132 return true;
133 }
134 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000135
Daniel Sandersde7816b2016-06-16 10:20:59 +0000136 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000137 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), DL, ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000138 return true;
139 }
140 }
141 // Operand is a result from an ADD.
142 if (Addr.getOpcode() == ISD::ADD) {
143 // When loading from constant pools, load the lower address part in
144 // the instruction itself. Example, instead of:
145 // lui $2, %hi($CPI1_0)
146 // addiu $2, $2, %lo($CPI1_0)
147 // lwc1 $f0, 0($2)
148 // Generate:
149 // lui $2, %hi($CPI1_0)
150 // lwc1 $f0, %lo($CPI1_0)($2)
151 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
152 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
153 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
154 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
155 isa<JumpTableSDNode>(Opnd0)) {
156 Base = Addr.getOperand(0);
157 Offset = Opnd0;
158 return true;
159 }
160 }
Akira Hatanaka30a84782013-03-14 18:27:31 +0000161 }
Daniel Sanders8015c702016-06-15 09:44:22 +0000162 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000163 Offset = CurDAG->getTargetConstant(0, DL, ValTy);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000164 return true;
165}
166
Daniel Sandersde7816b2016-06-16 10:20:59 +0000167bool Mips16DAGToDAGISel::selectAddr16(SDValue Addr, SDValue &Base,
168 SDValue &Offset) {
169 return selectAddr(false, Addr, Base, Offset);
170}
171
172bool Mips16DAGToDAGISel::selectAddr16SP(SDValue Addr, SDValue &Base,
173 SDValue &Offset) {
174 return selectAddr(true, Addr, Base, Offset);
175}
176
Akira Hatanaka30a84782013-03-14 18:27:31 +0000177/// Select instructions not customized! Used for
178/// expanded, promoted and normal instructions
Justin Bognereeae7512016-05-13 23:55:59 +0000179bool Mips16DAGToDAGISel::trySelect(SDNode *Node) {
Akira Hatanaka30a84782013-03-14 18:27:31 +0000180 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000181 SDLoc DL(Node);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000182
183 ///
184 // Instruction Selection not handled by the auto-generated
185 // tablegen selection should be handled here.
186 ///
187 EVT NodeTy = Node->getValueType(0);
188 unsigned MultOpc;
189
Daniel Sanders8015c702016-06-15 09:44:22 +0000190 switch (Opcode) {
191 default:
192 break;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000193
Akira Hatanaka30a84782013-03-14 18:27:31 +0000194 /// Mul with two results
195 case ISD::SMUL_LOHI:
196 case ISD::UMUL_LOHI: {
197 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 : Mips::MultRxRy16);
Daniel Sanders8015c702016-06-15 09:44:22 +0000198 std::pair<SDNode *, SDNode *> LoHi =
199 selectMULT(Node, MultOpc, DL, NodeTy, true, true);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000200 if (!SDValue(Node, 0).use_empty())
201 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
202
203 if (!SDValue(Node, 1).use_empty())
204 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
205
Justin Bognere58d6222016-05-13 06:30:15 +0000206 CurDAG->RemoveDeadNode(Node);
Justin Bognereeae7512016-05-13 23:55:59 +0000207 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000208 }
209
210 case ISD::MULHS:
211 case ISD::MULHU: {
212 MultOpc = (Opcode == ISD::MULHU ? Mips::MultuRxRy16 : Mips::MultRxRy16);
Justin Bognereeae7512016-05-13 23:55:59 +0000213 auto LoHi = selectMULT(Node, MultOpc, DL, NodeTy, false, true);
214 ReplaceNode(Node, LoHi.second);
215 return true;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000216 }
217 }
218
Justin Bognereeae7512016-05-13 23:55:59 +0000219 return false;
Akira Hatanaka30a84782013-03-14 18:27:31 +0000220}
221
Daniel Sanders46fe6552016-07-14 13:25:22 +0000222FunctionPass *llvm::createMips16ISelDag(MipsTargetMachine &TM,
223 CodeGenOpt::Level OptLevel) {
224 return new Mips16DAGToDAGISel(TM, OptLevel);
Akira Hatanaka30a84782013-03-14 18:27:31 +0000225}