Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 1 | //===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the MIPS target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "mips-isel" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "Mips.h" |
| 16 | #include "MipsISelLowering.h" |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 17 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 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" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
| 33 | #include "llvm/Support/Compiler.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include <queue> |
| 36 | #include <set> |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Instruction Selector Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // MipsDAGToDAGISel - MIPS specific code to select MIPS machine |
| 46 | // instructions for SelectionDAG operations. |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | namespace { |
| 49 | |
| 50 | class VISIBILITY_HIDDEN MipsDAGToDAGISel : public SelectionDAGISel { |
| 51 | |
| 52 | /// TM - Keep a reference to MipsTargetMachine. |
| 53 | MipsTargetMachine &TM; |
| 54 | |
| 55 | /// MipsLowering - This object fully describes how to lower LLVM code to an |
| 56 | /// Mips-specific SelectionDAG. |
| 57 | MipsTargetLowering MipsLowering; |
| 58 | |
| 59 | /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can |
| 60 | /// make the right decision when generating code for different targets. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame^] | 61 | const MipsSubtarget &Subtarget; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 62 | |
| 63 | public: |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame^] | 64 | MipsDAGToDAGISel(MipsTargetMachine &tm) : SelectionDAGISel(MipsLowering), |
| 65 | TM(tm), MipsLowering(*TM.getTargetLowering()), |
| 66 | Subtarget(tm.getSubtarget<MipsSubtarget>()) {} |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 67 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 68 | virtual void InstructionSelect(SelectionDAG &SD); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 69 | |
| 70 | // Pass Name |
| 71 | virtual const char *getPassName() const { |
| 72 | return "MIPS DAG->DAG Pattern Instruction Selection"; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | private: |
| 77 | // Include the pieces autogenerated from the target description. |
| 78 | #include "MipsGenDAGISel.inc" |
| 79 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 80 | SDOperand getGlobalBaseReg(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 81 | SDNode *Select(SDOperand N); |
| 82 | |
| 83 | // Complex Pattern. |
| 84 | bool SelectAddr(SDOperand Op, SDOperand N, |
| 85 | SDOperand &Base, SDOperand &Offset); |
| 86 | |
| 87 | |
| 88 | // getI32Imm - Return a target constant with the specified |
| 89 | // value, of type i32. |
| 90 | inline SDOperand getI32Imm(unsigned Imm) { |
| 91 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | #ifndef NDEBUG |
| 96 | unsigned Indent; |
| 97 | #endif |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 102 | /// InstructionSelect - This callback is invoked by |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 103 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 104 | void MipsDAGToDAGISel:: |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 105 | InstructionSelect(SelectionDAG &SD) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 106 | { |
| 107 | DEBUG(BB->dump()); |
| 108 | // Codegen the basic block. |
| 109 | #ifndef NDEBUG |
| 110 | DOUT << "===== Instruction selection begins:\n"; |
| 111 | Indent = 0; |
| 112 | #endif |
| 113 | |
| 114 | // Select target instructions for the DAG. |
| 115 | SD.setRoot(SelectRoot(SD.getRoot())); |
| 116 | |
| 117 | #ifndef NDEBUG |
| 118 | DOUT << "===== Instruction selection ends:\n"; |
| 119 | #endif |
| 120 | |
| 121 | SD.RemoveDeadNodes(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 124 | /// getGlobalBaseReg - Output the instructions required to put the |
| 125 | /// GOT address into a register. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 126 | SDOperand MipsDAGToDAGISel::getGlobalBaseReg() { |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 127 | MachineFunction* MF = BB->getParent(); |
| 128 | unsigned GP = 0; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 129 | for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(), |
| 130 | ee = MF->getRegInfo().livein_end(); ii != ee; ++ii) |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 131 | if (ii->first == Mips::GP) { |
| 132 | GP = ii->second; |
| 133 | break; |
| 134 | } |
| 135 | assert(GP && "GOT PTR not in liveins"); |
| 136 | return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 137 | GP, MVT::i32); |
| 138 | } |
| 139 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 140 | /// ComplexPattern used on MipsInstrInfo |
| 141 | /// Used on Mips Load/Store instructions |
| 142 | bool MipsDAGToDAGISel:: |
| 143 | SelectAddr(SDOperand Op, SDOperand Addr, SDOperand &Offset, SDOperand &Base) |
| 144 | { |
| 145 | // if Address is FI, get the TargetFrameIndex. |
| 146 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 147 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 148 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 149 | return true; |
| 150 | } |
| 151 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 152 | // on PIC code Load GA |
| 153 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 154 | if ((Addr.getOpcode() == ISD::TargetGlobalAddress) || |
| 155 | (Addr.getOpcode() == ISD::TargetJumpTable)){ |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 156 | Base = CurDAG->getRegister(Mips::GP, MVT::i32); |
| 157 | Offset = Addr; |
| 158 | return true; |
| 159 | } |
| 160 | } else { |
| 161 | if ((Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 162 | Addr.getOpcode() == ISD::TargetGlobalAddress)) |
| 163 | return false; |
| 164 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 165 | |
Bruno Cardoso Lopes | 7ff6fa2 | 2007-08-18 02:16:30 +0000 | [diff] [blame] | 166 | // Operand is a result from an ADD. |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 167 | if (Addr.getOpcode() == ISD::ADD) { |
| 168 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) { |
| 169 | if (Predicate_immSExt16(CN)) { |
| 170 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 171 | // If the first operand is a FI, get the TargetFI Node |
| 172 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode> |
| 173 | (Addr.getOperand(0))) { |
| 174 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 175 | } else { |
| 176 | Base = Addr.getOperand(0); |
| 177 | } |
| 178 | |
| 179 | Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32); |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 185 | Base = Addr; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 186 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | /// Select instructions not customized! Used for |
| 191 | /// expanded, promoted and normal instructions |
| 192 | SDNode* MipsDAGToDAGISel:: |
| 193 | Select(SDOperand N) |
| 194 | { |
| 195 | SDNode *Node = N.Val; |
| 196 | unsigned Opcode = Node->getOpcode(); |
| 197 | |
| 198 | // Dump information about the Node being selected |
| 199 | #ifndef NDEBUG |
| 200 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
| 201 | DEBUG(Node->dump(CurDAG)); |
| 202 | DOUT << "\n"; |
| 203 | Indent += 2; |
| 204 | #endif |
| 205 | |
| 206 | // If we have a custom node, we already have selected! |
| 207 | if (Opcode >= ISD::BUILTIN_OP_END && Opcode < MipsISD::FIRST_NUMBER) { |
| 208 | #ifndef NDEBUG |
| 209 | DOUT << std::string(Indent-2, ' ') << "== "; |
| 210 | DEBUG(Node->dump(CurDAG)); |
| 211 | DOUT << "\n"; |
| 212 | Indent -= 2; |
| 213 | #endif |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /// |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 218 | // Instruction Selection not handled by the auto-generated |
| 219 | // tablegen selection should be handled here. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 220 | /// |
| 221 | switch(Opcode) { |
| 222 | |
| 223 | default: break; |
| 224 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 225 | case ISD::SUBE: |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 226 | case ISD::ADDE: { |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 227 | SDOperand InFlag = Node->getOperand(2), CmpLHS; |
| 228 | unsigned Opc = InFlag.getOpcode(), MOp; |
| 229 | |
| 230 | assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || |
| 231 | (Opc == ISD::SUBC || Opc == ISD::SUBE)) && |
| 232 | "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn"); |
| 233 | |
| 234 | if (Opcode == ISD::ADDE) { |
| 235 | CmpLHS = InFlag.getValue(0); |
| 236 | MOp = Mips::ADDu; |
| 237 | } else { |
| 238 | CmpLHS = InFlag.getOperand(0); |
| 239 | MOp = Mips::SUBu; |
| 240 | } |
| 241 | |
| 242 | SDOperand Ops[] = { CmpLHS, InFlag.getOperand(1) }; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 243 | |
| 244 | SDOperand LHS = Node->getOperand(0); |
| 245 | SDOperand RHS = Node->getOperand(1); |
| 246 | AddToISelQueue(LHS); |
| 247 | AddToISelQueue(RHS); |
| 248 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 249 | MVT VT = LHS.getValueType(); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 250 | SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2); |
| 251 | SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT, |
| 252 | SDOperand(Carry,0), RHS); |
| 253 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 254 | return CurDAG->SelectNodeTo(N.Val, MOp, VT, MVT::Flag, |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 255 | LHS, SDOperand(AddCarry,0)); |
| 256 | } |
| 257 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 258 | /// Mul/Div with two results |
| 259 | case ISD::SDIVREM: |
| 260 | case ISD::UDIVREM: |
| 261 | case ISD::SMUL_LOHI: |
| 262 | case ISD::UMUL_LOHI: { |
| 263 | SDOperand Op1 = Node->getOperand(0); |
| 264 | SDOperand Op2 = Node->getOperand(1); |
| 265 | AddToISelQueue(Op1); |
| 266 | AddToISelQueue(Op2); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 267 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 268 | unsigned Op; |
| 269 | if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI) |
| 270 | Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT); |
| 271 | else |
| 272 | Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 273 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 274 | SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 275 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 276 | SDOperand InFlag = SDOperand(Node, 0); |
| 277 | SDNode *Lo = CurDAG->getTargetNode(Mips::MFLO, MVT::i32, MVT::Flag, InFlag); |
| 278 | |
| 279 | InFlag = SDOperand(Lo,1); |
| 280 | SDNode *Hi = CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag); |
| 281 | |
| 282 | if (!N.getValue(0).use_empty()) |
| 283 | ReplaceUses(N.getValue(0), SDOperand(Lo,0)); |
| 284 | |
| 285 | if (!N.getValue(1).use_empty()) |
| 286 | ReplaceUses(N.getValue(1), SDOperand(Hi,0)); |
| 287 | |
| 288 | return NULL; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 291 | /// Special Muls |
| 292 | case ISD::MUL: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 293 | case ISD::MULHS: |
| 294 | case ISD::MULHU: { |
| 295 | SDOperand MulOp1 = Node->getOperand(0); |
| 296 | SDOperand MulOp2 = Node->getOperand(1); |
| 297 | AddToISelQueue(MulOp1); |
| 298 | AddToISelQueue(MulOp2); |
| 299 | |
| 300 | unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT); |
| 301 | SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2); |
| 302 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 303 | SDOperand InFlag = SDOperand(MulNode, 0); |
| 304 | |
| 305 | if (MulOp == ISD::MUL) |
| 306 | return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, InFlag); |
| 307 | else |
| 308 | return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 311 | /// Div/Rem operations |
| 312 | case ISD::SREM: |
| 313 | case ISD::UREM: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 314 | case ISD::SDIV: |
| 315 | case ISD::UDIV: { |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 316 | SDOperand Op1 = Node->getOperand(0); |
| 317 | SDOperand Op2 = Node->getOperand(1); |
| 318 | AddToISelQueue(Op1); |
| 319 | AddToISelQueue(Op2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 320 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 321 | unsigned Op, MOp; |
| 322 | if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) { |
| 323 | Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu); |
| 324 | MOp = Mips::MFLO; |
| 325 | } else { |
| 326 | Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu); |
| 327 | MOp = Mips::MFHI; |
| 328 | } |
| 329 | SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 330 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 331 | SDOperand InFlag = SDOperand(Node, 0); |
| 332 | return CurDAG->getTargetNode(MOp, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 333 | } |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 334 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 335 | // Get target GOT address. |
| 336 | case ISD::GLOBAL_OFFSET_TABLE: { |
| 337 | SDOperand Result = getGlobalBaseReg(); |
| 338 | ReplaceUses(N, Result); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 342 | /// Handle direct and indirect calls when using PIC. On PIC, when |
| 343 | /// GOT is smaller than about 64k (small code) the GA target is |
| 344 | /// loaded with only one instruction. Otherwise GA's target must |
| 345 | /// be loaded with 3 instructions. |
| 346 | case MipsISD::JmpLink: { |
| 347 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 348 | //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large); |
| 349 | SDOperand Chain = Node->getOperand(0); |
| 350 | SDOperand Callee = Node->getOperand(1); |
| 351 | AddToISelQueue(Chain); |
| 352 | SDOperand T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32); |
| 353 | SDOperand InFlag(0, 0); |
| 354 | |
| 355 | if ( (isa<GlobalAddressSDNode>(Callee)) || |
| 356 | (isa<ExternalSymbolSDNode>(Callee)) ) |
| 357 | { |
| 358 | /// Direct call for global addresses and external symbols |
| 359 | SDOperand GPReg = CurDAG->getRegister(Mips::GP, MVT::i32); |
| 360 | |
| 361 | // Use load to get GOT target |
| 362 | SDOperand Ops[] = { Callee, GPReg, Chain }; |
| 363 | SDOperand Load = SDOperand(CurDAG->getTargetNode(Mips::LW, MVT::i32, |
| 364 | MVT::Other, Ops, 3), 0); |
| 365 | Chain = Load.getValue(1); |
| 366 | AddToISelQueue(Chain); |
| 367 | |
| 368 | // Call target must be on T9 |
| 369 | Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag); |
| 370 | } else |
| 371 | /// Indirect call |
| 372 | Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag); |
| 373 | |
| 374 | AddToISelQueue(Chain); |
| 375 | |
| 376 | // Emit Jump and Link Register |
| 377 | SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other, |
| 378 | MVT::Flag, T9Reg, Chain); |
| 379 | Chain = SDOperand(ResNode, 0); |
| 380 | InFlag = SDOperand(ResNode, 1); |
| 381 | ReplaceUses(SDOperand(Node, 0), Chain); |
| 382 | ReplaceUses(SDOperand(Node, 1), InFlag); |
| 383 | return ResNode; |
| 384 | } |
| 385 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Select the default instruction |
| 389 | SDNode *ResNode = SelectCode(N); |
| 390 | |
| 391 | #ifndef NDEBUG |
| 392 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 393 | if (ResNode == NULL || ResNode == N.Val) |
| 394 | DEBUG(N.Val->dump(CurDAG)); |
| 395 | else |
| 396 | DEBUG(ResNode->dump(CurDAG)); |
| 397 | DOUT << "\n"; |
| 398 | Indent -= 2; |
| 399 | #endif |
| 400 | |
| 401 | return ResNode; |
| 402 | } |
| 403 | |
| 404 | /// createMipsISelDag - This pass converts a legalized DAG into a |
| 405 | /// MIPS-specific DAG, ready for instruction scheduling. |
| 406 | FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) { |
| 407 | return new MipsDAGToDAGISel(TM); |
| 408 | } |