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/Compiler.h"
34 #include "llvm/Support/Debug.h"
40 //===----------------------------------------------------------------------===//
41 // Instruction Selector Implementation
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
45 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46 // instructions for SelectionDAG operations.
47 //===----------------------------------------------------------------------===//
50 class VISIBILITY_HIDDEN MipsDAGToDAGISel : public SelectionDAGISel {
52 /// TM - Keep a reference to MipsTargetMachine.
53 MipsTargetMachine &TM;
55 /// MipsLowering - This object fully describes how to lower LLVM code to an
56 /// Mips-specific SelectionDAG.
57 MipsTargetLowering MipsLowering;
59 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const MipsSubtarget &Subtarget;
64 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
65 SelectionDAGISel(MipsLowering),
66 TM(tm), MipsLowering(*TM.getTargetLowering()),
67 Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
69 virtual void InstructionSelect(SelectionDAG &SD);
72 virtual const char *getPassName() const {
73 return "MIPS DAG->DAG Pattern Instruction Selection";
78 // Include the pieces autogenerated from the target description.
79 #include "MipsGenDAGISel.inc"
81 SDValue getGlobalBaseReg();
82 SDNode *Select(SDValue N);
85 bool SelectAddr(SDValue Op, SDValue N,
86 SDValue &Base, SDValue &Offset);
89 // getI32Imm - Return a target constant with the specified
90 // value, of type i32.
91 inline SDValue getI32Imm(unsigned Imm) {
92 return CurDAG->getTargetConstant(Imm, MVT::i32);
103 /// InstructionSelect - This callback is invoked by
104 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
105 void MipsDAGToDAGISel::
106 InstructionSelect(SelectionDAG &SD)
109 // Codegen the basic block.
111 DOUT << "===== Instruction selection begins:\n";
115 // Select target instructions for the DAG.
116 SD.setRoot(SelectRoot(SD.getRoot()));
119 DOUT << "===== Instruction selection ends:\n";
122 SD.RemoveDeadNodes();
125 /// getGlobalBaseReg - Output the instructions required to put the
126 /// GOT address into a register.
127 SDValue MipsDAGToDAGISel::getGlobalBaseReg() {
128 MachineFunction* MF = BB->getParent();
130 for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(),
131 ee = MF->getRegInfo().livein_end(); ii != ee; ++ii)
132 if (ii->first == Mips::GP) {
136 assert(GP && "GOT PTR not in liveins");
137 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
141 /// ComplexPattern used on MipsInstrInfo
142 /// Used on Mips Load/Store instructions
143 bool MipsDAGToDAGISel::
144 SelectAddr(SDValue Op, SDValue Addr, SDValue &Offset, SDValue &Base)
146 // if Address is FI, get the TargetFrameIndex.
147 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
148 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
149 Offset = CurDAG->getTargetConstant(0, MVT::i32);
153 // on PIC code Load GA
154 if (TM.getRelocationModel() == Reloc::PIC_) {
155 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
156 (Addr.getOpcode() == ISD::TargetJumpTable)){
157 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
162 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
163 Addr.getOpcode() == ISD::TargetGlobalAddress))
167 // Operand is a result from an ADD.
168 if (Addr.getOpcode() == ISD::ADD) {
169 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
170 if (Predicate_immSExt16(CN)) {
172 // If the first operand is a FI, get the TargetFI Node
173 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
174 (Addr.getOperand(0))) {
175 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
177 Base = Addr.getOperand(0);
180 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
187 Offset = CurDAG->getTargetConstant(0, MVT::i32);
191 /// Select instructions not customized! Used for
192 /// expanded, promoted and normal instructions
193 SDNode* MipsDAGToDAGISel::
196 SDNode *Node = N.Val;
197 unsigned Opcode = Node->getOpcode();
199 // Dump information about the Node being selected
201 DOUT << std::string(Indent, ' ') << "Selecting: ";
202 DEBUG(Node->dump(CurDAG));
207 // If we have a custom node, we already have selected!
208 if (Node->isMachineOpcode()) {
210 DOUT << std::string(Indent-2, ' ') << "== ";
211 DEBUG(Node->dump(CurDAG));
219 // Instruction Selection not handled by the auto-generated
220 // tablegen selection should be handled here.
228 SDValue InFlag = Node->getOperand(2), CmpLHS;
229 unsigned Opc = InFlag.getOpcode(), MOp;
231 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
232 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
233 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
235 if (Opcode == ISD::ADDE) {
236 CmpLHS = InFlag.getValue(0);
239 CmpLHS = InFlag.getOperand(0);
243 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
245 SDValue LHS = Node->getOperand(0);
246 SDValue RHS = Node->getOperand(1);
250 MVT VT = LHS.getValueType();
251 SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
252 SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT,
253 SDValue(Carry,0), RHS);
255 return CurDAG->SelectNodeTo(N.Val, MOp, VT, MVT::Flag,
256 LHS, SDValue(AddCarry,0));
259 /// Mul/Div with two results
263 case ISD::UMUL_LOHI: {
264 SDValue Op1 = Node->getOperand(0);
265 SDValue Op2 = Node->getOperand(1);
270 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
271 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
273 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
275 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
277 SDValue InFlag = SDValue(Node, 0);
278 SDNode *Lo = CurDAG->getTargetNode(Mips::MFLO, MVT::i32,
280 InFlag = SDValue(Lo,1);
281 SDNode *Hi = CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
283 if (!N.getValue(0).use_empty())
284 ReplaceUses(N.getValue(0), SDValue(Lo,0));
286 if (!N.getValue(1).use_empty())
287 ReplaceUses(N.getValue(1), SDValue(Hi,0));
296 SDValue MulOp1 = Node->getOperand(0);
297 SDValue MulOp2 = Node->getOperand(1);
298 AddToISelQueue(MulOp1);
299 AddToISelQueue(MulOp2);
301 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
302 SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2);
304 SDValue InFlag = SDValue(MulNode, 0);
306 if (MulOp == ISD::MUL)
307 return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, InFlag);
309 return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
312 /// Div/Rem operations
317 SDValue Op1 = Node->getOperand(0);
318 SDValue Op2 = Node->getOperand(1);
323 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
324 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
327 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
330 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
332 SDValue InFlag = SDValue(Node, 0);
333 return CurDAG->getTargetNode(MOp, MVT::i32, InFlag);
336 // Get target GOT address.
337 case ISD::GLOBAL_OFFSET_TABLE: {
338 SDValue Result = getGlobalBaseReg();
339 ReplaceUses(N, Result);
343 /// Handle direct and indirect calls when using PIC. On PIC, when
344 /// GOT is smaller than about 64k (small code) the GA target is
345 /// loaded with only one instruction. Otherwise GA's target must
346 /// be loaded with 3 instructions.
347 case MipsISD::JmpLink: {
348 if (TM.getRelocationModel() == Reloc::PIC_) {
349 //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
350 SDValue Chain = Node->getOperand(0);
351 SDValue Callee = Node->getOperand(1);
352 AddToISelQueue(Chain);
353 SDValue T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
354 SDValue InFlag(0, 0);
356 if ( (isa<GlobalAddressSDNode>(Callee)) ||
357 (isa<ExternalSymbolSDNode>(Callee)) )
359 /// Direct call for global addresses and external symbols
360 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
362 // Use load to get GOT target
363 SDValue Ops[] = { Callee, GPReg, Chain };
364 SDValue Load = SDValue(CurDAG->getTargetNode(Mips::LW, MVT::i32,
365 MVT::Other, Ops, 3), 0);
366 Chain = Load.getValue(1);
367 AddToISelQueue(Chain);
369 // Call target must be on T9
370 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag);
373 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag);
375 AddToISelQueue(Chain);
377 // Emit Jump and Link Register
378 SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other,
379 MVT::Flag, T9Reg, Chain);
380 Chain = SDValue(ResNode, 0);
381 InFlag = SDValue(ResNode, 1);
382 ReplaceUses(SDValue(Node, 0), Chain);
383 ReplaceUses(SDValue(Node, 1), InFlag);
389 // Select the default instruction
390 SDNode *ResNode = SelectCode(N);
393 DOUT << std::string(Indent-2, ' ') << "=> ";
394 if (ResNode == NULL || ResNode == N.Val)
395 DEBUG(N.Val->dump(CurDAG));
397 DEBUG(ResNode->dump(CurDAG));
405 /// createMipsISelDag - This pass converts a legalized DAG into a
406 /// MIPS-specific DAG, ready for instruction scheduling.
407 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
408 return new MipsDAGToDAGISel(TM);