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 | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 87 | SDNode *Select(SDNode *N); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 88 | |
| 89 | // Complex Pattern. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 90 | bool SelectAddr(SDNode *Op, SDValue N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 91 | SDValue &Base, SDValue &Offset); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 92 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 93 | SDNode *SelectLoadFp64(SDNode *N); |
| 94 | SDNode *SelectStoreFp64(SDNode *N); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 95 | |
| 96 | // getI32Imm - Return a target constant with the specified |
| 97 | // value, of type i32. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 98 | inline SDValue getI32Imm(unsigned Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 99 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | #ifndef NDEBUG |
| 104 | unsigned Indent; |
| 105 | #endif |
| 106 | }; |
| 107 | |
| 108 | } |
| 109 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 110 | /// InstructionSelect - This callback is invoked by |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 111 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 112 | void MipsDAGToDAGISel::InstructionSelect() { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 113 | // Codegen the basic block. |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 114 | DEBUG(errs() << "===== Instruction selection begins:\n"); |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 115 | DEBUG(Indent = 0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 116 | |
| 117 | // Select target instructions for the DAG. |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 118 | SelectRoot(*CurDAG); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 120 | DEBUG(errs() << "===== Instruction selection ends:\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 121 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 122 | CurDAG->RemoveDeadNodes(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 125 | /// getGlobalBaseReg - Output the instructions required to put the |
| 126 | /// GOT address into a register. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 127 | SDNode *MipsDAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 128 | unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
| 129 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 132 | /// ComplexPattern used on MipsInstrInfo |
| 133 | /// Used on Mips Load/Store instructions |
| 134 | bool MipsDAGToDAGISel:: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 135 | SelectAddr(SDNode *Op, SDValue Addr, SDValue &Offset, SDValue &Base) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 136 | { |
| 137 | // if Address is FI, get the TargetFrameIndex. |
| 138 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 139 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 140 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 144 | // on PIC code Load GA |
| 145 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 146 | if ((Addr.getOpcode() == ISD::TargetGlobalAddress) || |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 147 | (Addr.getOpcode() == ISD::TargetConstantPool) || |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 148 | (Addr.getOpcode() == ISD::TargetJumpTable)){ |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 149 | Base = CurDAG->getRegister(Mips::GP, MVT::i32); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 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))) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 167 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 168 | } else { |
| 169 | Base = Addr.getOperand(0); |
| 170 | } |
| 171 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +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 | } |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 176 | |
| 177 | // When loading from constant pools, load the lower address part in |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 178 | // the instruction itself. Example, instead of: |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 179 | // lui $2, %hi($CPI1_0) |
| 180 | // addiu $2, $2, %lo($CPI1_0) |
| 181 | // lwc1 $f0, 0($2) |
| 182 | // Generate: |
| 183 | // lui $2, %hi($CPI1_0) |
| 184 | // lwc1 $f0, %lo($CPI1_0)($2) |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 185 | if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi || |
| 186 | Addr.getOperand(0).getOpcode() == ISD::LOAD) && |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 187 | Addr.getOperand(1).getOpcode() == MipsISD::Lo) { |
| 188 | SDValue LoVal = Addr.getOperand(1); |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 189 | if (dyn_cast<ConstantPoolSDNode>(LoVal.getOperand(0))) { |
| 190 | Base = Addr.getOperand(0); |
| 191 | Offset = LoVal.getOperand(0); |
| 192 | return true; |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 197 | Base = Addr; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 198 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 202 | SDNode *MipsDAGToDAGISel::SelectLoadFp64(SDNode *N) { |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 203 | MVT::SimpleValueType NVT = |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 204 | N->getValueType(0).getSimpleVT().SimpleTy; |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 205 | |
| 206 | if (!Subtarget.isMips1() || NVT != MVT::f64) |
| 207 | return NULL; |
| 208 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 209 | if (!Predicate_unindexedload(N) || |
| 210 | !Predicate_load(N)) |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 211 | return NULL; |
| 212 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 213 | SDValue Chain = N->getOperand(0); |
| 214 | SDValue N1 = N->getOperand(1); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 215 | SDValue Offset0, Offset1, Base; |
| 216 | |
| 217 | if (!SelectAddr(N, N1, Offset0, Base) || |
| 218 | N1.getValueType() != MVT::i32) |
| 219 | return NULL; |
| 220 | |
| 221 | MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1); |
| 222 | MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand(); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 223 | DebugLoc dl = N->getDebugLoc(); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 224 | |
| 225 | // The second load should start after for 4 bytes. |
| 226 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0)) |
| 227 | Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32); |
| 228 | else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0)) |
| 229 | Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(), |
| 230 | MVT::i32, |
| 231 | CP->getAlignment(), |
| 232 | CP->getOffset()+4, |
| 233 | CP->getTargetFlags()); |
| 234 | else |
| 235 | return NULL; |
| 236 | |
Bruno Cardoso Lopes | 37fd537 | 2009-11-25 01:05:25 +0000 | [diff] [blame] | 237 | // Choose the offsets depending on the endianess |
| 238 | if (TM.getTargetData()->isBigEndian()) |
| 239 | std::swap(Offset0, Offset1); |
| 240 | |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 241 | // Instead of: |
| 242 | // ldc $f0, X($3) |
| 243 | // Generate: |
| 244 | // lwc $f0, X($3) |
| 245 | // lwc $f1, X+4($3) |
| 246 | SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32, |
| 247 | MVT::Other, Offset0, Base, Chain); |
| 248 | SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF, |
| 249 | dl, NVT), 0); |
| 250 | SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl, |
| 251 | MVT::f64, Undef, SDValue(LD0, 0)); |
| 252 | |
| 253 | SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32, |
| 254 | MVT::Other, Offset1, Base, SDValue(LD0, 1)); |
| 255 | SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl, |
| 256 | MVT::f64, I0, SDValue(LD1, 0)); |
| 257 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 258 | ReplaceUses(SDValue(N, 0), I1); |
| 259 | ReplaceUses(SDValue(N, 1), Chain); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 260 | cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1); |
| 261 | cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1); |
| 262 | return I1.getNode(); |
| 263 | } |
| 264 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 265 | SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDNode *N) { |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 266 | |
| 267 | if (!Subtarget.isMips1() || |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 268 | N->getOperand(1).getValueType() != MVT::f64) |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 269 | return NULL; |
| 270 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 271 | SDValue Chain = N->getOperand(0); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 272 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 273 | if (!Predicate_unindexedstore(N) || |
| 274 | !Predicate_store(N)) |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 277 | SDValue N1 = N->getOperand(1); |
| 278 | SDValue N2 = N->getOperand(2); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 279 | SDValue Offset0, Offset1, Base; |
| 280 | |
| 281 | if (!SelectAddr(N, N2, Offset0, Base) || |
| 282 | N1.getValueType() != MVT::f64 || |
| 283 | N2.getValueType() != MVT::i32) |
| 284 | return NULL; |
| 285 | |
| 286 | MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1); |
| 287 | MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand(); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 288 | DebugLoc dl = N->getDebugLoc(); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 289 | |
| 290 | // Get the even and odd part from the f64 register |
| 291 | SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPODD, |
| 292 | dl, MVT::f32, N1); |
| 293 | SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPEVEN, |
| 294 | dl, MVT::f32, N1); |
| 295 | |
| 296 | // The second store should start after for 4 bytes. |
| 297 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0)) |
| 298 | Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32); |
| 299 | else |
| 300 | return NULL; |
| 301 | |
Bruno Cardoso Lopes | 37fd537 | 2009-11-25 01:05:25 +0000 | [diff] [blame] | 302 | // Choose the offsets depending on the endianess |
| 303 | if (TM.getTargetData()->isBigEndian()) |
| 304 | std::swap(Offset0, Offset1); |
| 305 | |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 306 | // Instead of: |
| 307 | // sdc $f0, X($3) |
| 308 | // Generate: |
| 309 | // swc $f0, X($3) |
| 310 | // swc $f1, X+4($3) |
| 311 | SDValue Ops0[] = { FPEven, Offset0, Base, Chain }; |
| 312 | Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl, |
| 313 | MVT::Other, Ops0, 4), 0); |
| 314 | cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1); |
| 315 | |
| 316 | SDValue Ops1[] = { FPOdd, Offset1, Base, Chain }; |
| 317 | Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl, |
| 318 | MVT::Other, Ops1, 4), 0); |
| 319 | cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1); |
| 320 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 321 | ReplaceUses(SDValue(N, 0), Chain); |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 322 | return Chain.getNode(); |
| 323 | } |
| 324 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 325 | /// Select instructions not customized! Used for |
| 326 | /// expanded, promoted and normal instructions |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 327 | SDNode* MipsDAGToDAGISel::Select(SDNode *Node) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 328 | unsigned Opcode = Node->getOpcode(); |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 329 | DebugLoc dl = Node->getDebugLoc(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 330 | |
| 331 | // Dump information about the Node being selected |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 332 | DEBUG(errs().indent(Indent) << "Selecting: "; |
| 333 | Node->dump(CurDAG); |
| 334 | errs() << "\n"); |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 335 | DEBUG(Indent += 2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 336 | |
| 337 | // If we have a custom node, we already have selected! |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 338 | if (Node->isMachineOpcode()) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 339 | DEBUG(errs().indent(Indent-2) << "== "; |
| 340 | Node->dump(CurDAG); |
| 341 | errs() << "\n"); |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 342 | DEBUG(Indent -= 2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | /// |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 347 | // Instruction Selection not handled by the auto-generated |
| 348 | // tablegen selection should be handled here. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 349 | /// |
| 350 | switch(Opcode) { |
| 351 | |
| 352 | default: break; |
| 353 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 354 | case ISD::SUBE: |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 355 | case ISD::ADDE: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 356 | SDValue InFlag = Node->getOperand(2), CmpLHS; |
Chris Lattner | 30baa95 | 2008-12-14 21:38:24 +0000 | [diff] [blame] | 357 | unsigned Opc = InFlag.getOpcode(); Opc=Opc; |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 358 | assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || |
| 359 | (Opc == ISD::SUBC || Opc == ISD::SUBE)) && |
| 360 | "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn"); |
| 361 | |
Chris Lattner | 30baa95 | 2008-12-14 21:38:24 +0000 | [diff] [blame] | 362 | unsigned MOp; |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 363 | if (Opcode == ISD::ADDE) { |
| 364 | CmpLHS = InFlag.getValue(0); |
| 365 | MOp = Mips::ADDu; |
| 366 | } else { |
| 367 | CmpLHS = InFlag.getOperand(0); |
| 368 | MOp = Mips::SUBu; |
| 369 | } |
| 370 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 371 | SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) }; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 372 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 373 | SDValue LHS = Node->getOperand(0); |
| 374 | SDValue RHS = Node->getOperand(1); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 375 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 376 | EVT VT = LHS.getValueType(); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 377 | SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2); |
| 378 | SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT, |
| 379 | SDValue(Carry,0), RHS); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 380 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 381 | return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Flag, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 382 | LHS, SDValue(AddCarry,0)); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 385 | /// Mul/Div with two results |
| 386 | case ISD::SDIVREM: |
| 387 | case ISD::UDIVREM: |
| 388 | case ISD::SMUL_LOHI: |
| 389 | case ISD::UMUL_LOHI: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 390 | SDValue Op1 = Node->getOperand(0); |
| 391 | SDValue Op2 = Node->getOperand(1); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 392 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 393 | unsigned Op; |
| 394 | if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI) |
| 395 | Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT); |
| 396 | else |
| 397 | Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 398 | |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 399 | SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 400 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 401 | SDValue InFlag = SDValue(Node, 0); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 402 | SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, |
| 403 | MVT::Flag, InFlag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 404 | InFlag = SDValue(Lo,1); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 405 | SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 406 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 407 | if (!SDValue(Node, 0).use_empty()) |
| 408 | ReplaceUses(SDValue(Node, 0), SDValue(Lo,0)); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 409 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 410 | if (!SDValue(Node, 1).use_empty()) |
| 411 | ReplaceUses(SDValue(Node, 1), SDValue(Hi,0)); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 412 | |
| 413 | return NULL; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 416 | /// Special Muls |
| 417 | case ISD::MUL: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 418 | case ISD::MULHS: |
| 419 | case ISD::MULHU: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 420 | SDValue MulOp1 = Node->getOperand(0); |
| 421 | SDValue MulOp2 = Node->getOperand(1); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 422 | |
| 423 | unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 424 | SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl, |
| 425 | MVT::Flag, MulOp1, MulOp2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 426 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 427 | SDValue InFlag = SDValue(MulNode, 0); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 428 | |
| 429 | if (MulOp == ISD::MUL) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 430 | return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 431 | else |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 432 | return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 435 | /// Div/Rem operations |
| 436 | case ISD::SREM: |
| 437 | case ISD::UREM: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 438 | case ISD::SDIV: |
| 439 | case ISD::UDIV: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 440 | SDValue Op1 = Node->getOperand(0); |
| 441 | SDValue Op2 = Node->getOperand(1); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 442 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 443 | unsigned Op, MOp; |
| 444 | if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) { |
| 445 | Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu); |
| 446 | MOp = Mips::MFLO; |
| 447 | } else { |
| 448 | Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu); |
| 449 | MOp = Mips::MFHI; |
| 450 | } |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 451 | SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 452 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 453 | SDValue InFlag = SDValue(Node, 0); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 454 | return CurDAG->getMachineNode(MOp, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 455 | } |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 456 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 457 | // Get target GOT address. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 458 | case ISD::GLOBAL_OFFSET_TABLE: |
| 459 | return getGlobalBaseReg(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 460 | |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 461 | case ISD::ConstantFP: { |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 462 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node); |
| 463 | if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) { |
Bruno Cardoso Lopes | ea98278 | 2010-01-19 12:53:04 +0000 | [diff] [blame] | 464 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 465 | Mips::ZERO, MVT::i32); |
| 466 | SDValue Undef = SDValue( |
| 467 | CurDAG->getMachineNode( |
| 468 | TargetInstrInfo::IMPLICIT_DEF, dl, MVT::f64), 0); |
| 469 | SDNode *MTC = CurDAG->getMachineNode(Mips::MTC1, dl, MVT::f32, Zero); |
| 470 | SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl, |
| 471 | MVT::f64, Undef, SDValue(MTC, 0)); |
| 472 | SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl, |
| 473 | MVT::f64, I0, SDValue(MTC, 0)); |
| 474 | ReplaceUses(SDValue(Node, 0), I1); |
| 475 | return I1.getNode(); |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 476 | } |
| 477 | break; |
| 478 | } |
| 479 | |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 480 | case ISD::LOAD: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 481 | if (SDNode *ResNode = SelectLoadFp64(Node)) |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 482 | return ResNode; |
| 483 | // Other cases are autogenerated. |
| 484 | break; |
| 485 | |
| 486 | case ISD::STORE: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 487 | if (SDNode *ResNode = SelectStoreFp64(Node)) |
Bruno Cardoso Lopes | 2045c47 | 2009-11-19 06:06:13 +0000 | [diff] [blame] | 488 | return ResNode; |
| 489 | // Other cases are autogenerated. |
| 490 | break; |
| 491 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 492 | /// Handle direct and indirect calls when using PIC. On PIC, when |
| 493 | /// GOT is smaller than about 64k (small code) the GA target is |
| 494 | /// loaded with only one instruction. Otherwise GA's target must |
| 495 | /// be loaded with 3 instructions. |
| 496 | case MipsISD::JmpLink: { |
| 497 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Bruno Cardoso Lopes | 9201b10 | 2010-01-19 17:00:43 +0000 | [diff] [blame] | 498 | unsigned LastOpNum = Node->getNumOperands()-1; |
| 499 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 500 | SDValue Chain = Node->getOperand(0); |
| 501 | SDValue Callee = Node->getOperand(1); |
Bruno Cardoso Lopes | 9201b10 | 2010-01-19 17:00:43 +0000 | [diff] [blame] | 502 | SDValue InFlag; |
| 503 | |
| 504 | // Skip the incomming flag if present |
| 505 | if (Node->getOperand(LastOpNum).getValueType() == MVT::Flag) |
| 506 | LastOpNum--; |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 507 | |
| 508 | if ( (isa<GlobalAddressSDNode>(Callee)) || |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 509 | (isa<ExternalSymbolSDNode>(Callee)) ) |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 510 | { |
| 511 | /// Direct call for global addresses and external symbols |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 512 | SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 513 | |
| 514 | // Use load to get GOT target |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 515 | SDValue Ops[] = { Callee, GPReg, Chain }; |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 516 | SDValue Load = SDValue(CurDAG->getMachineNode(Mips::LW, dl, MVT::i32, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 517 | MVT::Other, Ops, 3), 0); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 518 | Chain = Load.getValue(1); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 519 | |
| 520 | // Call target must be on T9 |
Bruno Cardoso Lopes | 9201b10 | 2010-01-19 17:00:43 +0000 | [diff] [blame] | 521 | Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Load, InFlag); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 522 | } else |
| 523 | /// Indirect call |
Bruno Cardoso Lopes | 9201b10 | 2010-01-19 17:00:43 +0000 | [diff] [blame] | 524 | Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Callee, InFlag); |
| 525 | |
| 526 | // Map the JmpLink operands to JALR |
| 527 | SDVTList NodeTys = CurDAG->getVTList(MVT::Other, MVT::Flag); |
| 528 | SmallVector<SDValue, 8> Ops; |
| 529 | Ops.push_back(CurDAG->getRegister(Mips::T9, MVT::i32)); |
| 530 | |
| 531 | for (unsigned i = 2, e = LastOpNum+1; i != e; ++i) |
| 532 | Ops.push_back(Node->getOperand(i)); |
| 533 | Ops.push_back(Chain); |
| 534 | Ops.push_back(Chain.getValue(1)); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 535 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 536 | // Emit Jump and Link Register |
Bruno Cardoso Lopes | 9201b10 | 2010-01-19 17:00:43 +0000 | [diff] [blame] | 537 | SDNode *ResNode = CurDAG->getMachineNode(Mips::JALR, dl, NodeTys, |
| 538 | &Ops[0], Ops.size()); |
| 539 | |
| 540 | // Replace Chain and InFlag |
| 541 | ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0)); |
| 542 | ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 1)); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 543 | return ResNode; |
| 544 | } |
| 545 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // Select the default instruction |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 549 | SDNode *ResNode = SelectCode(Node); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 551 | DEBUG(errs().indent(Indent-2) << "=> "); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 552 | if (ResNode == NULL || ResNode == Node) |
| 553 | DEBUG(Node->dump(CurDAG)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 554 | else |
| 555 | DEBUG(ResNode->dump(CurDAG)); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 556 | DEBUG(errs() << "\n"); |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 557 | DEBUG(Indent -= 2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 558 | |
| 559 | return ResNode; |
| 560 | } |
| 561 | |
| 562 | /// createMipsISelDag - This pass converts a legalized DAG into a |
| 563 | /// MIPS-specific DAG, ready for instruction scheduling. |
| 564 | FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) { |
| 565 | return new MipsDAGToDAGISel(TM); |
| 566 | } |