1 //===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file defines an instruction selector for the MIPS target.
11 //===----------------------------------------------------------------------===//
13 #include "MipsISelDAGToDAG.h"
14 #include "MCTargetDesc/MipsBaseInfo.h"
16 #include "Mips16ISelDAGToDAG.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSEISelDAGToDAG.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAGNodes.h"
26 #include "llvm/CodeGen/StackProtector.h"
27 #include "llvm/IR/CFG.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetMachine.h"
38 #define DEBUG_TYPE "mips-isel"
40 //===----------------------------------------------------------------------===//
41 // Instruction Selector Implementation
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
45 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46 // instructions for SelectionDAG operations.
47 //===----------------------------------------------------------------------===//
49 void MipsDAGToDAGISel::getAnalysisUsage(AnalysisUsage
&AU
) const {
50 // There are multiple MipsDAGToDAGISel instances added to the pass pipeline.
51 // We need to preserve StackProtector for the next one.
52 AU
.addPreserved
<StackProtector
>();
53 SelectionDAGISel::getAnalysisUsage(AU
);
56 bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction
&MF
) {
57 Subtarget
= &static_cast<const MipsSubtarget
&>(MF
.getSubtarget());
58 bool Ret
= SelectionDAGISel::runOnMachineFunction(MF
);
60 processFunctionAfterISel(MF
);
65 /// getGlobalBaseReg - Output the instructions required to put the
66 /// GOT address into a register.
67 SDNode
*MipsDAGToDAGISel::getGlobalBaseReg() {
68 Register GlobalBaseReg
= MF
->getInfo
<MipsFunctionInfo
>()->getGlobalBaseReg();
69 return CurDAG
->getRegister(GlobalBaseReg
, getTargetLowering()->getPointerTy(
70 CurDAG
->getDataLayout()))
74 /// ComplexPattern used on MipsInstrInfo
75 /// Used on Mips Load/Store instructions
76 bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr
, SDValue
&Base
,
77 SDValue
&Offset
) const {
78 llvm_unreachable("Unimplemented function.");
82 bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr
, SDValue
&Base
,
83 SDValue
&Offset
) const {
84 llvm_unreachable("Unimplemented function.");
88 bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr
, SDValue
&Base
,
89 SDValue
&Offset
) const {
90 llvm_unreachable("Unimplemented function.");
94 bool MipsDAGToDAGISel::selectIntAddr11MM(SDValue Addr
, SDValue
&Base
,
95 SDValue
&Offset
) const {
96 llvm_unreachable("Unimplemented function.");
100 bool MipsDAGToDAGISel::selectIntAddr12MM(SDValue Addr
, SDValue
&Base
,
101 SDValue
&Offset
) const {
102 llvm_unreachable("Unimplemented function.");
106 bool MipsDAGToDAGISel::selectIntAddr16MM(SDValue Addr
, SDValue
&Base
,
107 SDValue
&Offset
) const {
108 llvm_unreachable("Unimplemented function.");
112 bool MipsDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr
, SDValue
&Base
,
113 SDValue
&Offset
) const {
114 llvm_unreachable("Unimplemented function.");
118 bool MipsDAGToDAGISel::selectIntAddrSImm10(SDValue Addr
, SDValue
&Base
,
119 SDValue
&Offset
) const {
120 llvm_unreachable("Unimplemented function.");
124 bool MipsDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr
, SDValue
&Base
,
125 SDValue
&Offset
) const {
126 llvm_unreachable("Unimplemented function.");
130 bool MipsDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr
, SDValue
&Base
,
131 SDValue
&Offset
) const {
132 llvm_unreachable("Unimplemented function.");
136 bool MipsDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr
, SDValue
&Base
,
137 SDValue
&Offset
) const {
138 llvm_unreachable("Unimplemented function.");
142 bool MipsDAGToDAGISel::selectAddr16(SDValue Addr
, SDValue
&Base
,
144 llvm_unreachable("Unimplemented function.");
148 bool MipsDAGToDAGISel::selectAddr16SP(SDValue Addr
, SDValue
&Base
,
150 llvm_unreachable("Unimplemented function.");
154 bool MipsDAGToDAGISel::selectVSplat(SDNode
*N
, APInt
&Imm
,
155 unsigned MinSizeInBits
) const {
156 llvm_unreachable("Unimplemented function.");
160 bool MipsDAGToDAGISel::selectVSplatUimm1(SDValue N
, SDValue
&Imm
) const {
161 llvm_unreachable("Unimplemented function.");
165 bool MipsDAGToDAGISel::selectVSplatUimm2(SDValue N
, SDValue
&Imm
) const {
166 llvm_unreachable("Unimplemented function.");
170 bool MipsDAGToDAGISel::selectVSplatUimm3(SDValue N
, SDValue
&Imm
) const {
171 llvm_unreachable("Unimplemented function.");
175 bool MipsDAGToDAGISel::selectVSplatUimm4(SDValue N
, SDValue
&Imm
) const {
176 llvm_unreachable("Unimplemented function.");
180 bool MipsDAGToDAGISel::selectVSplatUimm5(SDValue N
, SDValue
&Imm
) const {
181 llvm_unreachable("Unimplemented function.");
185 bool MipsDAGToDAGISel::selectVSplatUimm6(SDValue N
, SDValue
&Imm
) const {
186 llvm_unreachable("Unimplemented function.");
190 bool MipsDAGToDAGISel::selectVSplatUimm8(SDValue N
, SDValue
&Imm
) const {
191 llvm_unreachable("Unimplemented function.");
195 bool MipsDAGToDAGISel::selectVSplatSimm5(SDValue N
, SDValue
&Imm
) const {
196 llvm_unreachable("Unimplemented function.");
200 bool MipsDAGToDAGISel::selectVSplatUimmPow2(SDValue N
, SDValue
&Imm
) const {
201 llvm_unreachable("Unimplemented function.");
205 bool MipsDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N
, SDValue
&Imm
) const {
206 llvm_unreachable("Unimplemented function.");
210 bool MipsDAGToDAGISel::selectVSplatMaskL(SDValue N
, SDValue
&Imm
) const {
211 llvm_unreachable("Unimplemented function.");
215 bool MipsDAGToDAGISel::selectVSplatMaskR(SDValue N
, SDValue
&Imm
) const {
216 llvm_unreachable("Unimplemented function.");
220 /// Convert vector addition with vector subtraction if that allows to encode
221 /// constant as an immediate and thus avoid extra 'ldi' instruction.
222 /// add X, <-1, -1...> --> sub X, <1, 1...>
223 bool MipsDAGToDAGISel::selectVecAddAsVecSubIfProfitable(SDNode
*Node
) {
224 assert(Node
->getOpcode() == ISD::ADD
&& "Should only get 'add' here.");
226 EVT VT
= Node
->getValueType(0);
227 assert(VT
.isVector() && "Should only be called for vectors.");
229 SDValue X
= Node
->getOperand(0);
230 SDValue C
= Node
->getOperand(1);
232 auto *BVN
= dyn_cast
<BuildVectorSDNode
>(C
);
236 APInt SplatValue
, SplatUndef
;
237 unsigned SplatBitSize
;
240 if (!BVN
->isConstantSplat(SplatValue
, SplatUndef
, SplatBitSize
, HasAnyUndefs
,
241 8, !Subtarget
->isLittle()))
244 auto IsInlineConstant
= [](const APInt
&Imm
) { return Imm
.isIntN(5); };
246 if (IsInlineConstant(SplatValue
))
247 return false; // Can already be encoded as an immediate.
249 APInt NegSplatValue
= 0 - SplatValue
;
250 if (!IsInlineConstant(NegSplatValue
))
251 return false; // Even if we negate it it won't help.
255 SDValue NegC
= CurDAG
->FoldConstantArithmetic(
256 ISD::SUB
, DL
, VT
, CurDAG
->getConstant(0, DL
, VT
).getNode(), C
.getNode());
257 assert(NegC
&& "Constant-folding failed!");
258 SDValue NewNode
= CurDAG
->getNode(ISD::SUB
, DL
, VT
, X
, NegC
);
260 ReplaceNode(Node
, NewNode
.getNode());
261 SelectCode(NewNode
.getNode());
265 /// Select instructions not customized! Used for
266 /// expanded, promoted and normal instructions
267 void MipsDAGToDAGISel::Select(SDNode
*Node
) {
268 unsigned Opcode
= Node
->getOpcode();
270 // If we have a custom node, we already have selected!
271 if (Node
->isMachineOpcode()) {
272 LLVM_DEBUG(errs() << "== "; Node
->dump(CurDAG
); errs() << "\n");
277 // See if subclasses can handle this node.
285 if (Node
->getSimpleValueType(0).isVector() &&
286 selectVecAddAsVecSubIfProfitable(Node
))
290 // Get target GOT address.
291 case ISD::GLOBAL_OFFSET_TABLE
:
292 ReplaceNode(Node
, getGlobalBaseReg());
298 assert((Subtarget
->systemSupportsUnalignedAccess() ||
299 cast
<MemSDNode
>(Node
)->getMemoryVT().getSizeInBits() / 8 <=
300 cast
<MemSDNode
>(Node
)->getAlignment()) &&
301 "Unexpected unaligned loads/stores.");
306 // Select the default instruction
310 bool MipsDAGToDAGISel::
311 SelectInlineAsmMemoryOperand(const SDValue
&Op
, unsigned ConstraintID
,
312 std::vector
<SDValue
> &OutOps
) {
313 // All memory constraints can at least accept raw pointers.
314 switch(ConstraintID
) {
316 llvm_unreachable("Unexpected asm memory constraint");
317 case InlineAsm::Constraint_i
:
318 case InlineAsm::Constraint_m
:
319 case InlineAsm::Constraint_R
:
320 case InlineAsm::Constraint_ZC
:
321 OutOps
.push_back(Op
);