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