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