Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 1 | //===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 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 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines an instruction selector for the MIPS target. |
| 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
| 14 | #define DEBUG_TYPE "mips-isel" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "Mips.h" |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 16 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 17 | #include "MipsRegisterInfo.h" |
| 18 | #include "MipsSubtarget.h" |
| 19 | #include "MipsTargetMachine.h" |
| 20 | #include "llvm/GlobalValue.h" |
| 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Intrinsics.h" |
| 23 | #include "llvm/Support/CFG.h" |
| 24 | #include "llvm/Type.h" |
| 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 38 | // Instruction Selector Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 40 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 42 | // MipsDAGToDAGISel - MIPS specific code to select MIPS machine |
| 43 | // instructions for SelectionDAG operations. |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 47 | class MipsDAGToDAGISel : public SelectionDAGISel { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 48 | |
| 49 | /// TM - Keep a reference to MipsTargetMachine. |
| 50 | MipsTargetMachine &TM; |
| 51 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 52 | /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can |
| 53 | /// make the right decision when generating code for different targets. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 54 | const MipsSubtarget &Subtarget; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 55 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 56 | public: |
Dan Gohman | 1002c02 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 57 | explicit MipsDAGToDAGISel(MipsTargetMachine &tm) : |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 58 | SelectionDAGISel(tm), |
Dan Gohman | da8ac5f | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 59 | TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {} |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 60 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 61 | // Pass Name |
| 62 | virtual const char *getPassName() const { |
| 63 | return "MIPS DAG->DAG Pattern Instruction Selection"; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 64 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 65 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 68 | // Include the pieces autogenerated from the target description. |
| 69 | #include "MipsGenDAGISel.inc" |
| 70 | |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 71 | /// getTargetMachine - Return a reference to the TargetMachine, casted |
| 72 | /// to the target-specific type. |
| 73 | const MipsTargetMachine &getTargetMachine() { |
| 74 | return static_cast<const MipsTargetMachine &>(TM); |
| 75 | } |
| 76 | |
| 77 | /// getInstrInfo - Return a reference to the TargetInstrInfo, casted |
| 78 | /// to the target-specific type. |
| 79 | const MipsInstrInfo *getInstrInfo() { |
| 80 | return getTargetMachine().getInstrInfo(); |
| 81 | } |
| 82 | |
| 83 | SDNode *getGlobalBaseReg(); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 84 | SDNode *Select(SDNode *N); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 85 | |
| 86 | // Complex Pattern. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 87 | bool SelectAddr(SDValue N, SDValue &Base, SDValue &Offset); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 88 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 89 | // getI32Imm - Return a target constant with the specified |
Akira Hatanaka | 3bdc03a | 2011-12-07 20:13:53 +0000 | [diff] [blame^] | 90 | // value. |
Akira Hatanaka | 4d0eb63 | 2011-12-07 20:10:24 +0000 | [diff] [blame] | 91 | inline SDValue getImm(const SDNode *Node, unsigned Imm) { |
| 92 | return CurDAG->getTargetConstant(Imm, Node->getValueType(0)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 93 | } |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 94 | |
| 95 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 96 | char ConstraintCode, |
| 97 | std::vector<SDValue> &OutOps); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 102 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 103 | /// getGlobalBaseReg - Output the instructions required to put the |
| 104 | /// GOT address into a register. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 105 | SDNode *MipsDAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 106 | unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
| 107 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 110 | /// ComplexPattern used on MipsInstrInfo |
| 111 | /// Used on Mips Load/Store instructions |
| 112 | bool MipsDAGToDAGISel:: |
Akira Hatanaka | d3ac47f | 2011-07-07 18:57:00 +0000 | [diff] [blame] | 113 | SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 114 | EVT ValTy = Addr.getValueType(); |
| 115 | unsigned GPReg = ValTy == MVT::i32 ? Mips::GP : Mips::GP_64; |
| 116 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 117 | // if Address is FI, get the TargetFrameIndex. |
| 118 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 119 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
| 120 | Offset = CurDAG->getTargetConstant(0, ValTy); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 123 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 124 | // on PIC code Load GA |
| 125 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Akira Hatanaka | 342837d | 2011-05-28 01:07:07 +0000 | [diff] [blame] | 126 | if (Addr.getOpcode() == MipsISD::WrapperPIC) { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 127 | Base = CurDAG->getRegister(GPReg, ValTy); |
Akira Hatanaka | 342837d | 2011-05-28 01:07:07 +0000 | [diff] [blame] | 128 | Offset = Addr.getOperand(0); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | } else { |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 132 | if ((Addr.getOpcode() == ISD::TargetExternalSymbol || |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 133 | Addr.getOpcode() == ISD::TargetGlobalAddress)) |
| 134 | return false; |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 135 | else if (Addr.getOpcode() == ISD::TargetGlobalTLSAddress) { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 136 | Base = CurDAG->getRegister(GPReg, ValTy); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 137 | Offset = Addr; |
| 138 | return true; |
| 139 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 142 | // Addresses of the form FI+const or FI|const |
| 143 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 144 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)); |
| 145 | if (isInt<16>(CN->getSExtValue())) { |
| 146 | |
| 147 | // If the first operand is a FI, get the TargetFI Node |
| 148 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode> |
| 149 | (Addr.getOperand(0))) |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 150 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 151 | else |
| 152 | Base = Addr.getOperand(0); |
| 153 | |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 154 | Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy); |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | } |
| 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) { |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 161 | // When loading from constant pools, load the lower address part in |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 162 | // the instruction itself. Example, instead of: |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 163 | // lui $2, %hi($CPI1_0) |
| 164 | // addiu $2, $2, %lo($CPI1_0) |
| 165 | // lwc1 $f0, 0($2) |
| 166 | // Generate: |
| 167 | // lui $2, %hi($CPI1_0) |
| 168 | // lwc1 $f0, %lo($CPI1_0)($2) |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 169 | if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi || |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 170 | Addr.getOperand(0).getOpcode() == ISD::LOAD) && |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 171 | Addr.getOperand(1).getOpcode() == MipsISD::Lo) { |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 172 | SDValue LoVal = Addr.getOperand(1); |
Akira Hatanaka | 8b2b713 | 2011-06-24 17:55:19 +0000 | [diff] [blame] | 173 | if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) || |
| 174 | isa<GlobalAddressSDNode>(LoVal.getOperand(0))) { |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 175 | Base = Addr.getOperand(0); |
| 176 | Offset = LoVal.getOperand(0); |
| 177 | return true; |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 182 | Base = Addr; |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 183 | Offset = CurDAG->getTargetConstant(0, ValTy); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
| 187 | /// Select instructions not customized! Used for |
| 188 | /// expanded, promoted and normal instructions |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 189 | SDNode* MipsDAGToDAGISel::Select(SDNode *Node) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 190 | unsigned Opcode = Node->getOpcode(); |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 191 | DebugLoc dl = Node->getDebugLoc(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 192 | |
| 193 | // Dump information about the Node being selected |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 194 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 195 | |
| 196 | // If we have a custom node, we already have selected! |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 197 | if (Node->isMachineOpcode()) { |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 198 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | /// |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 203 | // Instruction Selection not handled by the auto-generated |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 204 | // tablegen selection should be handled here. |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 205 | /// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 206 | switch(Opcode) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 207 | default: break; |
| 208 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 209 | case ISD::SUBE: |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 210 | case ISD::ADDE: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | SDValue InFlag = Node->getOperand(2), CmpLHS; |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 212 | unsigned Opc = InFlag.getOpcode(); (void)Opc; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 213 | assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || |
| 214 | (Opc == ISD::SUBC || Opc == ISD::SUBE)) && |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 215 | "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn"); |
| 216 | |
Chris Lattner | 30baa95 | 2008-12-14 21:38:24 +0000 | [diff] [blame] | 217 | unsigned MOp; |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 218 | if (Opcode == ISD::ADDE) { |
| 219 | CmpLHS = InFlag.getValue(0); |
| 220 | MOp = Mips::ADDu; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 221 | } else { |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 222 | CmpLHS = InFlag.getOperand(0); |
| 223 | MOp = Mips::SUBu; |
| 224 | } |
| 225 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 226 | SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) }; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 227 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 228 | SDValue LHS = Node->getOperand(0); |
| 229 | SDValue RHS = Node->getOperand(1); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 230 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 231 | EVT VT = LHS.getValueType(); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 232 | SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 233 | SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 234 | SDValue(Carry,0), RHS); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 235 | |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 236 | return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 237 | LHS, SDValue(AddCarry,0)); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Akira Hatanaka | c079ad0 | 2011-06-07 19:03:14 +0000 | [diff] [blame] | 240 | /// Mul with two results |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 241 | case ISD::SMUL_LOHI: |
| 242 | case ISD::UMUL_LOHI: { |
Akira Hatanaka | 04d3762 | 2011-10-03 20:01:11 +0000 | [diff] [blame] | 243 | assert(Node->getValueType(0) != MVT::i64 && |
| 244 | "64-bit multiplication with two results not handled."); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 245 | SDValue Op1 = Node->getOperand(0); |
| 246 | SDValue Op2 = Node->getOperand(1); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 247 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 248 | unsigned Op; |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 249 | Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 250 | |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 251 | SDNode *Mul = CurDAG->getMachineNode(Op, dl, MVT::Glue, Op1, Op2); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 252 | |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 253 | SDValue InFlag = SDValue(Mul, 0); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 254 | SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 255 | MVT::Glue, InFlag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 256 | InFlag = SDValue(Lo,1); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 257 | SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 258 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 259 | if (!SDValue(Node, 0).use_empty()) |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 260 | ReplaceUses(SDValue(Node, 0), SDValue(Lo,0)); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 261 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 262 | if (!SDValue(Node, 1).use_empty()) |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 263 | ReplaceUses(SDValue(Node, 1), SDValue(Hi,0)); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 264 | |
| 265 | return NULL; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 268 | /// Special Muls |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 269 | case ISD::MUL: |
Akira Hatanaka | 04d3762 | 2011-10-03 20:01:11 +0000 | [diff] [blame] | 270 | // Mips32 has a 32-bit three operand mul instruction. |
| 271 | if (Subtarget.hasMips32() && Node->getValueType(0) == MVT::i32) |
Bruno Cardoso Lopes | 7d5652d | 2010-11-12 00:38:32 +0000 | [diff] [blame] | 272 | break; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 273 | case ISD::MULHS: |
| 274 | case ISD::MULHU: { |
Akira Hatanaka | 04d3762 | 2011-10-03 20:01:11 +0000 | [diff] [blame] | 275 | assert((Opcode == ISD::MUL || Node->getValueType(0) != MVT::i64) && |
| 276 | "64-bit MULH* not handled."); |
| 277 | EVT Ty = Node->getValueType(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 278 | SDValue MulOp1 = Node->getOperand(0); |
| 279 | SDValue MulOp2 = Node->getOperand(1); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 280 | |
Akira Hatanaka | 04d3762 | 2011-10-03 20:01:11 +0000 | [diff] [blame] | 281 | unsigned MulOp = (Opcode == ISD::MULHU ? |
| 282 | Mips::MULTu : |
| 283 | (Ty == MVT::i32 ? Mips::MULT : Mips::DMULT)); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 284 | SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl, |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 285 | MVT::Glue, MulOp1, MulOp2); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 286 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 287 | SDValue InFlag = SDValue(MulNode, 0); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 288 | |
Akira Hatanaka | 04d3762 | 2011-10-03 20:01:11 +0000 | [diff] [blame] | 289 | if (Opcode == ISD::MUL) { |
| 290 | unsigned Opc = (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64); |
| 291 | return CurDAG->getMachineNode(Opc, dl, Ty, InFlag); |
| 292 | } |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 293 | else |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 294 | return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 297 | // Get target GOT address. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 298 | case ISD::GLOBAL_OFFSET_TABLE: |
| 299 | return getGlobalBaseReg(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 300 | |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 301 | case ISD::ConstantFP: { |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 302 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 303 | if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) { |
| 304 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Bruno Cardoso Lopes | ea98278 | 2010-01-19 12:53:04 +0000 | [diff] [blame] | 305 | Mips::ZERO, MVT::i32); |
Akira Hatanaka | 0285e7d | 2011-08-12 18:09:59 +0000 | [diff] [blame] | 306 | return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero, |
| 307 | Zero); |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 308 | } |
| 309 | break; |
| 310 | } |
| 311 | |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 312 | case MipsISD::ThreadPointer: { |
| 313 | unsigned SrcReg = Mips::HWR29; |
| 314 | unsigned DestReg = Mips::V1; |
| 315 | SDNode *Rdhwr = CurDAG->getMachineNode(Mips::RDHWR, Node->getDebugLoc(), |
| 316 | Node->getValueType(0), CurDAG->getRegister(SrcReg, MVT::i32)); |
| 317 | SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg, |
| 318 | SDValue(Rdhwr, 0)); |
| 319 | SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, MVT::i32); |
| 320 | ReplaceUses(SDValue(Node, 0), ResNode); |
| 321 | return ResNode.getNode(); |
| 322 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Select the default instruction |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 326 | SDNode *ResNode = SelectCode(Node); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 328 | DEBUG(errs() << "=> "); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 329 | if (ResNode == NULL || ResNode == Node) |
| 330 | DEBUG(Node->dump(CurDAG)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 331 | else |
| 332 | DEBUG(ResNode->dump(CurDAG)); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 333 | DEBUG(errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 334 | return ResNode; |
| 335 | } |
| 336 | |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 337 | bool MipsDAGToDAGISel:: |
| 338 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
| 339 | std::vector<SDValue> &OutOps) { |
| 340 | assert(ConstraintCode == 'm' && "unexpected asm memory constraint"); |
| 341 | OutOps.push_back(Op); |
| 342 | return false; |
| 343 | } |
| 344 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 345 | /// createMipsISelDag - This pass converts a legalized DAG into a |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 346 | /// MIPS-specific DAG, ready for instruction scheduling. |
| 347 | FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) { |
| 348 | return new MipsDAGToDAGISel(TM); |
| 349 | } |