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