1 //===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the MIPS target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mips-isel"
16 #include "MipsISelLowering.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAGISel.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
38 //===----------------------------------------------------------------------===//
39 // Instruction Selector Implementation
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
43 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
44 // instructions for SelectionDAG operations.
45 //===----------------------------------------------------------------------===//
48 class MipsDAGToDAGISel
: public SelectionDAGISel
{
50 /// TM - Keep a reference to MipsTargetMachine.
51 MipsTargetMachine
&TM
;
53 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
54 /// make the right decision when generating code for different targets.
55 const MipsSubtarget
&Subtarget
;
58 explicit MipsDAGToDAGISel(MipsTargetMachine
&tm
) :
60 TM(tm
), Subtarget(tm
.getSubtarget
<MipsSubtarget
>()) {}
62 virtual void InstructionSelect();
65 virtual const char *getPassName() const {
66 return "MIPS DAG->DAG Pattern Instruction Selection";
71 // Include the pieces autogenerated from the target description.
72 #include "MipsGenDAGISel.inc"
74 /// getTargetMachine - Return a reference to the TargetMachine, casted
75 /// to the target-specific type.
76 const MipsTargetMachine
&getTargetMachine() {
77 return static_cast<const MipsTargetMachine
&>(TM
);
80 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
81 /// to the target-specific type.
82 const MipsInstrInfo
*getInstrInfo() {
83 return getTargetMachine().getInstrInfo();
86 SDNode
*getGlobalBaseReg();
87 SDNode
*Select(SDValue N
);
90 bool SelectAddr(SDValue Op
, SDValue N
,
91 SDValue
&Base
, SDValue
&Offset
);
94 // getI32Imm - Return a target constant with the specified
95 // value, of type i32.
96 inline SDValue
getI32Imm(unsigned Imm
) {
97 return CurDAG
->getTargetConstant(Imm
, MVT::i32
);
108 /// InstructionSelect - This callback is invoked by
109 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
110 void MipsDAGToDAGISel::InstructionSelect() {
112 // Codegen the basic block.
113 DEBUG(errs() << "===== Instruction selection begins:\n");
116 // Select target instructions for the DAG.
119 DEBUG(errs() << "===== Instruction selection ends:\n");
121 CurDAG
->RemoveDeadNodes();
124 /// getGlobalBaseReg - Output the instructions required to put the
125 /// GOT address into a register.
126 SDNode
*MipsDAGToDAGISel::getGlobalBaseReg() {
127 unsigned GlobalBaseReg
= getInstrInfo()->getGlobalBaseReg(MF
);
128 return CurDAG
->getRegister(GlobalBaseReg
, TLI
.getPointerTy()).getNode();
131 /// ComplexPattern used on MipsInstrInfo
132 /// Used on Mips Load/Store instructions
133 bool MipsDAGToDAGISel::
134 SelectAddr(SDValue Op
, SDValue Addr
, SDValue
&Offset
, SDValue
&Base
)
136 // if Address is FI, get the TargetFrameIndex.
137 if (FrameIndexSDNode
*FIN
= dyn_cast
<FrameIndexSDNode
>(Addr
)) {
138 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), MVT::i32
);
139 Offset
= CurDAG
->getTargetConstant(0, MVT::i32
);
143 // on PIC code Load GA
144 if (TM
.getRelocationModel() == Reloc::PIC_
) {
145 if ((Addr
.getOpcode() == ISD::TargetGlobalAddress
) ||
146 (Addr
.getOpcode() == ISD::TargetJumpTable
)){
147 Base
= CurDAG
->getRegister(Mips::GP
, MVT::i32
);
152 if ((Addr
.getOpcode() == ISD::TargetExternalSymbol
||
153 Addr
.getOpcode() == ISD::TargetGlobalAddress
))
157 // Operand is a result from an ADD.
158 if (Addr
.getOpcode() == ISD::ADD
) {
159 if (ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(Addr
.getOperand(1))) {
160 if (Predicate_immSExt16(CN
)) {
162 // If the first operand is a FI, get the TargetFI Node
163 if (FrameIndexSDNode
*FIN
= dyn_cast
<FrameIndexSDNode
>
164 (Addr
.getOperand(0))) {
165 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), MVT::i32
);
167 Base
= Addr
.getOperand(0);
170 Offset
= CurDAG
->getTargetConstant(CN
->getZExtValue(), MVT::i32
);
177 Offset
= CurDAG
->getTargetConstant(0, MVT::i32
);
181 /// Select instructions not customized! Used for
182 /// expanded, promoted and normal instructions
183 SDNode
* MipsDAGToDAGISel::Select(SDValue N
) {
184 SDNode
*Node
= N
.getNode();
185 unsigned Opcode
= Node
->getOpcode();
186 DebugLoc dl
= Node
->getDebugLoc();
188 // Dump information about the Node being selected
189 DEBUG(errs().indent(Indent
) << "Selecting: ";
194 // If we have a custom node, we already have selected!
195 if (Node
->isMachineOpcode()) {
196 DEBUG(errs().indent(Indent
-2) << "== ";
204 // Instruction Selection not handled by the auto-generated
205 // tablegen selection should be handled here.
213 SDValue InFlag
= Node
->getOperand(2), CmpLHS
;
214 unsigned Opc
= InFlag
.getOpcode(); Opc
=Opc
;
215 assert(((Opc
== ISD::ADDC
|| Opc
== ISD::ADDE
) ||
216 (Opc
== ISD::SUBC
|| Opc
== ISD::SUBE
)) &&
217 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
220 if (Opcode
== ISD::ADDE
) {
221 CmpLHS
= InFlag
.getValue(0);
224 CmpLHS
= InFlag
.getOperand(0);
228 SDValue Ops
[] = { CmpLHS
, InFlag
.getOperand(1) };
230 SDValue LHS
= Node
->getOperand(0);
231 SDValue RHS
= Node
->getOperand(1);
233 EVT VT
= LHS
.getValueType();
234 SDNode
*Carry
= CurDAG
->getMachineNode(Mips::SLTu
, dl
, VT
, Ops
, 2);
235 SDNode
*AddCarry
= CurDAG
->getMachineNode(Mips::ADDu
, dl
, VT
,
236 SDValue(Carry
,0), RHS
);
238 return CurDAG
->SelectNodeTo(N
.getNode(), MOp
, VT
, MVT::Flag
,
239 LHS
, SDValue(AddCarry
,0));
242 /// Mul/Div with two results
246 case ISD::UMUL_LOHI
: {
247 SDValue Op1
= Node
->getOperand(0);
248 SDValue Op2
= Node
->getOperand(1);
251 if (Opcode
== ISD::UMUL_LOHI
|| Opcode
== ISD::SMUL_LOHI
)
252 Op
= (Opcode
== ISD::UMUL_LOHI
? Mips::MULTu
: Mips::MULT
);
254 Op
= (Opcode
== ISD::UDIVREM
? Mips::DIVu
: Mips::DIV
);
256 SDNode
*Node
= CurDAG
->getMachineNode(Op
, dl
, MVT::Flag
, Op1
, Op2
);
258 SDValue InFlag
= SDValue(Node
, 0);
259 SDNode
*Lo
= CurDAG
->getMachineNode(Mips::MFLO
, dl
, MVT::i32
,
261 InFlag
= SDValue(Lo
,1);
262 SDNode
*Hi
= CurDAG
->getMachineNode(Mips::MFHI
, dl
, MVT::i32
, InFlag
);
264 if (!N
.getValue(0).use_empty())
265 ReplaceUses(N
.getValue(0), SDValue(Lo
,0));
267 if (!N
.getValue(1).use_empty())
268 ReplaceUses(N
.getValue(1), SDValue(Hi
,0));
277 SDValue MulOp1
= Node
->getOperand(0);
278 SDValue MulOp2
= Node
->getOperand(1);
280 unsigned MulOp
= (Opcode
== ISD::MULHU
? Mips::MULTu
: Mips::MULT
);
281 SDNode
*MulNode
= CurDAG
->getMachineNode(MulOp
, dl
,
282 MVT::Flag
, MulOp1
, MulOp2
);
284 SDValue InFlag
= SDValue(MulNode
, 0);
286 if (MulOp
== ISD::MUL
)
287 return CurDAG
->getMachineNode(Mips::MFLO
, dl
, MVT::i32
, InFlag
);
289 return CurDAG
->getMachineNode(Mips::MFHI
, dl
, MVT::i32
, InFlag
);
292 /// Div/Rem operations
297 SDValue Op1
= Node
->getOperand(0);
298 SDValue Op2
= Node
->getOperand(1);
301 if (Opcode
== ISD::SDIV
|| Opcode
== ISD::UDIV
) {
302 Op
= (Opcode
== ISD::SDIV
? Mips::DIV
: Mips::DIVu
);
305 Op
= (Opcode
== ISD::SREM
? Mips::DIV
: Mips::DIVu
);
308 SDNode
*Node
= CurDAG
->getMachineNode(Op
, dl
, MVT::Flag
, Op1
, Op2
);
310 SDValue InFlag
= SDValue(Node
, 0);
311 return CurDAG
->getMachineNode(MOp
, dl
, MVT::i32
, InFlag
);
314 // Get target GOT address.
315 case ISD::GLOBAL_OFFSET_TABLE
:
316 return getGlobalBaseReg();
318 /// Handle direct and indirect calls when using PIC. On PIC, when
319 /// GOT is smaller than about 64k (small code) the GA target is
320 /// loaded with only one instruction. Otherwise GA's target must
321 /// be loaded with 3 instructions.
322 case MipsISD::JmpLink
: {
323 if (TM
.getRelocationModel() == Reloc::PIC_
) {
324 SDValue Chain
= Node
->getOperand(0);
325 SDValue Callee
= Node
->getOperand(1);
326 SDValue T9Reg
= CurDAG
->getRegister(Mips::T9
, MVT::i32
);
327 SDValue
InFlag(0, 0);
329 if ( (isa
<GlobalAddressSDNode
>(Callee
)) ||
330 (isa
<ExternalSymbolSDNode
>(Callee
)) )
332 /// Direct call for global addresses and external symbols
333 SDValue GPReg
= CurDAG
->getRegister(Mips::GP
, MVT::i32
);
335 // Use load to get GOT target
336 SDValue Ops
[] = { Callee
, GPReg
, Chain
};
337 SDValue Load
= SDValue(CurDAG
->getMachineNode(Mips::LW
, dl
, MVT::i32
,
338 MVT::Other
, Ops
, 3), 0);
339 Chain
= Load
.getValue(1);
341 // Call target must be on T9
342 Chain
= CurDAG
->getCopyToReg(Chain
, dl
, T9Reg
, Load
, InFlag
);
345 Chain
= CurDAG
->getCopyToReg(Chain
, dl
, T9Reg
, Callee
, InFlag
);
347 // Emit Jump and Link Register
348 SDNode
*ResNode
= CurDAG
->getMachineNode(Mips::JALR
, dl
, MVT::Other
,
349 MVT::Flag
, T9Reg
, Chain
);
350 Chain
= SDValue(ResNode
, 0);
351 InFlag
= SDValue(ResNode
, 1);
352 ReplaceUses(SDValue(Node
, 0), Chain
);
353 ReplaceUses(SDValue(Node
, 1), InFlag
);
359 // Select the default instruction
360 SDNode
*ResNode
= SelectCode(N
);
362 DEBUG(errs().indent(Indent
-2) << "=> ");
363 if (ResNode
== NULL
|| ResNode
== N
.getNode())
364 DEBUG(N
.getNode()->dump(CurDAG
));
366 DEBUG(ResNode
->dump(CurDAG
));
367 DEBUG(errs() << "\n");
373 /// createMipsISelDag - This pass converts a legalized DAG into a
374 /// MIPS-specific DAG, ready for instruction scheduling.
375 FunctionPass
*llvm::createMipsISelDag(MipsTargetMachine
&TM
) {
376 return new MipsDAGToDAGISel(TM
);