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"
37 //===----------------------------------------------------------------------===//
38 // Instruction Selector Implementation
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
42 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
43 // instructions for SelectionDAG operations.
44 //===----------------------------------------------------------------------===//
47 class VISIBILITY_HIDDEN MipsDAGToDAGISel : public SelectionDAGISel {
49 /// TM - Keep a reference to MipsTargetMachine.
50 MipsTargetMachine &TM;
52 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const MipsSubtarget &Subtarget;
57 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
58 SelectionDAGISel(*tm.getTargetLowering()),
59 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
61 virtual void InstructionSelect();
64 virtual const char *getPassName() const {
65 return "MIPS DAG->DAG Pattern Instruction Selection";
70 // Include the pieces autogenerated from the target description.
71 #include "MipsGenDAGISel.inc"
73 SDValue getGlobalBaseReg();
74 SDNode *Select(SDValue N);
77 bool SelectAddr(SDValue Op, SDValue N,
78 SDValue &Base, SDValue &Offset);
81 // getI32Imm - Return a target constant with the specified
82 // value, of type i32.
83 inline SDValue getI32Imm(unsigned Imm) {
84 return CurDAG->getTargetConstant(Imm, MVT::i32);
95 /// InstructionSelect - This callback is invoked by
96 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
97 void MipsDAGToDAGISel::
101 // Codegen the basic block.
103 DOUT << "===== Instruction selection begins:\n";
107 // Select target instructions for the DAG.
111 DOUT << "===== Instruction selection ends:\n";
114 CurDAG->RemoveDeadNodes();
117 /// getGlobalBaseReg - Output the instructions required to put the
118 /// GOT address into a register.
119 SDValue MipsDAGToDAGISel::getGlobalBaseReg() {
120 MachineFunction* MF = BB->getParent();
122 for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(),
123 ee = MF->getRegInfo().livein_end(); ii != ee; ++ii)
124 if (ii->first == Mips::GP) {
128 assert(GP && "GOT PTR not in liveins");
129 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
133 /// ComplexPattern used on MipsInstrInfo
134 /// Used on Mips Load/Store instructions
135 bool MipsDAGToDAGISel::
136 SelectAddr(SDValue Op, SDValue Addr, SDValue &Offset, SDValue &Base)
138 // if Address is FI, get the TargetFrameIndex.
139 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
140 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
141 Offset = CurDAG->getTargetConstant(0, MVT::i32);
145 // on PIC code Load GA
146 if (TM.getRelocationModel() == Reloc::PIC_) {
147 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
148 (Addr.getOpcode() == ISD::TargetJumpTable)){
149 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
154 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
155 Addr.getOpcode() == ISD::TargetGlobalAddress))
159 // Operand is a result from an ADD.
160 if (Addr.getOpcode() == ISD::ADD) {
161 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
162 if (Predicate_immSExt16(CN)) {
164 // If the first operand is a FI, get the TargetFI Node
165 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
166 (Addr.getOperand(0))) {
167 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
169 Base = Addr.getOperand(0);
172 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
179 Offset = CurDAG->getTargetConstant(0, MVT::i32);
183 /// Select instructions not customized! Used for
184 /// expanded, promoted and normal instructions
185 SDNode* MipsDAGToDAGISel::
188 SDNode *Node = N.getNode();
189 unsigned Opcode = Node->getOpcode();
191 // Dump information about the Node being selected
193 DOUT << std::string(Indent, ' ') << "Selecting: ";
194 DEBUG(Node->dump(CurDAG));
199 // If we have a custom node, we already have selected!
200 if (Node->isMachineOpcode()) {
202 DOUT << std::string(Indent-2, ' ') << "== ";
203 DEBUG(Node->dump(CurDAG));
211 // Instruction Selection not handled by the auto-generated
212 // tablegen selection should be handled here.
220 SDValue InFlag = Node->getOperand(2), CmpLHS;
221 unsigned Opc = InFlag.getOpcode(), MOp;
223 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
224 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
225 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
227 if (Opcode == ISD::ADDE) {
228 CmpLHS = InFlag.getValue(0);
231 CmpLHS = InFlag.getOperand(0);
235 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
237 SDValue LHS = Node->getOperand(0);
238 SDValue RHS = Node->getOperand(1);
242 MVT VT = LHS.getValueType();
243 SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
244 SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT,
245 SDValue(Carry,0), RHS);
247 return CurDAG->SelectNodeTo(N.getNode(), MOp, VT, MVT::Flag,
248 LHS, SDValue(AddCarry,0));
251 /// Mul/Div with two results
255 case ISD::UMUL_LOHI: {
256 SDValue Op1 = Node->getOperand(0);
257 SDValue Op2 = Node->getOperand(1);
262 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
263 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
265 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
267 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
269 SDValue InFlag = SDValue(Node, 0);
270 SDNode *Lo = CurDAG->getTargetNode(Mips::MFLO, MVT::i32,
272 InFlag = SDValue(Lo,1);
273 SDNode *Hi = CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
275 if (!N.getValue(0).use_empty())
276 ReplaceUses(N.getValue(0), SDValue(Lo,0));
278 if (!N.getValue(1).use_empty())
279 ReplaceUses(N.getValue(1), SDValue(Hi,0));
288 SDValue MulOp1 = Node->getOperand(0);
289 SDValue MulOp2 = Node->getOperand(1);
290 AddToISelQueue(MulOp1);
291 AddToISelQueue(MulOp2);
293 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
294 SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2);
296 SDValue InFlag = SDValue(MulNode, 0);
298 if (MulOp == ISD::MUL)
299 return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, InFlag);
301 return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
304 /// Div/Rem operations
309 SDValue Op1 = Node->getOperand(0);
310 SDValue Op2 = Node->getOperand(1);
315 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
316 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
319 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
322 SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
324 SDValue InFlag = SDValue(Node, 0);
325 return CurDAG->getTargetNode(MOp, MVT::i32, InFlag);
328 // Get target GOT address.
329 case ISD::GLOBAL_OFFSET_TABLE: {
330 SDValue Result = getGlobalBaseReg();
331 ReplaceUses(N, Result);
335 /// Handle direct and indirect calls when using PIC. On PIC, when
336 /// GOT is smaller than about 64k (small code) the GA target is
337 /// loaded with only one instruction. Otherwise GA's target must
338 /// be loaded with 3 instructions.
339 case MipsISD::JmpLink: {
340 if (TM.getRelocationModel() == Reloc::PIC_) {
341 //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
342 SDValue Chain = Node->getOperand(0);
343 SDValue Callee = Node->getOperand(1);
344 AddToISelQueue(Chain);
345 SDValue T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
346 SDValue InFlag(0, 0);
348 if ( (isa<GlobalAddressSDNode>(Callee)) ||
349 (isa<ExternalSymbolSDNode>(Callee)) )
351 /// Direct call for global addresses and external symbols
352 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
354 // Use load to get GOT target
355 SDValue Ops[] = { Callee, GPReg, Chain };
356 SDValue Load = SDValue(CurDAG->getTargetNode(Mips::LW, MVT::i32,
357 MVT::Other, Ops, 3), 0);
358 Chain = Load.getValue(1);
359 AddToISelQueue(Chain);
361 // Call target must be on T9
362 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag);
365 Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag);
367 AddToISelQueue(Chain);
369 // Emit Jump and Link Register
370 SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other,
371 MVT::Flag, T9Reg, Chain);
372 Chain = SDValue(ResNode, 0);
373 InFlag = SDValue(ResNode, 1);
374 ReplaceUses(SDValue(Node, 0), Chain);
375 ReplaceUses(SDValue(Node, 1), InFlag);
381 // Select the default instruction
382 SDNode *ResNode = SelectCode(N);
385 DOUT << std::string(Indent-2, ' ') << "=> ";
386 if (ResNode == NULL || ResNode == N.getNode())
387 DEBUG(N.getNode()->dump(CurDAG));
389 DEBUG(ResNode->dump(CurDAG));
397 /// createMipsISelDag - This pass converts a legalized DAG into a
398 /// MIPS-specific DAG, ready for instruction scheduling.
399 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
400 return new MipsDAGToDAGISel(TM);