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