Jia Liu | c570711 | 2012-02-17 08:55: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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/MipsBaseInfo.h" |
Akira Hatanaka | 57fa382 | 2012-01-25 03:01:35 +0000 | [diff] [blame] | 17 | #include "MipsAnalyzeImmediate.h" |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 18 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "MipsRegisterInfo.h" |
| 20 | #include "MipsSubtarget.h" |
| 21 | #include "MipsTargetMachine.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Akira Hatanaka | 44b6c71 | 2012-02-28 02:55:02 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GlobalValue.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/Type.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CFG.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 41 | // Instruction Selector Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 43 | |
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 | // MipsDAGToDAGISel - MIPS specific code to select MIPS machine |
| 46 | // instructions for SelectionDAG operations. |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 50 | class MipsDAGToDAGISel : public SelectionDAGISel { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 51 | |
| 52 | /// TM - Keep a reference to MipsTargetMachine. |
| 53 | MipsTargetMachine &TM; |
| 54 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 55 | /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can |
| 56 | /// make the right decision when generating code for different targets. |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 57 | const MipsSubtarget &Subtarget; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 58 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 59 | public: |
Dan Gohman | 1002c02 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 60 | explicit MipsDAGToDAGISel(MipsTargetMachine &tm) : |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 61 | SelectionDAGISel(tm), |
Dan Gohman | da8ac5f | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 62 | TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {} |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 63 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 64 | // Pass Name |
| 65 | virtual const char *getPassName() const { |
| 66 | return "MIPS DAG->DAG Pattern Instruction Selection"; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 67 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 68 | |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 69 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 70 | |
| 71 | private: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 72 | // Include the pieces autogenerated from the target description. |
| 73 | #include "MipsGenDAGISel.inc" |
| 74 | |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 75 | /// getTargetMachine - Return a reference to the TargetMachine, casted |
| 76 | /// to the target-specific type. |
| 77 | const MipsTargetMachine &getTargetMachine() { |
| 78 | return static_cast<const MipsTargetMachine &>(TM); |
| 79 | } |
| 80 | |
| 81 | /// getInstrInfo - Return a reference to the TargetInstrInfo, casted |
| 82 | /// to the target-specific type. |
| 83 | const MipsInstrInfo *getInstrInfo() { |
| 84 | return getTargetMachine().getInstrInfo(); |
| 85 | } |
| 86 | |
| 87 | SDNode *getGlobalBaseReg(); |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 88 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 89 | SDValue getMips16SPAliasReg(); |
| 90 | |
| 91 | void getMips16SPRefReg(SDNode *parent, SDValue &AliasReg); |
| 92 | |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 93 | std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, |
| 94 | EVT Ty, bool HasLo, bool HasHi); |
| 95 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 96 | SDNode *Select(SDNode *N); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 97 | |
| 98 | // Complex Pattern. |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 99 | /// (reg + imm). |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 100 | bool selectAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset) const; |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 101 | |
| 102 | /// Fall back on this function if all else fails. |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 103 | bool selectAddrDefault(SDValue Addr, SDValue &Base, SDValue &Offset) const; |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 104 | |
| 105 | /// Match integer address pattern. |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 106 | bool selectIntAddr(SDValue Addr, SDValue &Base, SDValue &Offset) const; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 107 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 108 | bool SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset, |
| 109 | SDValue &Alias); |
| 110 | |
Akira Hatanaka | bd15090 | 2011-12-07 20:15:01 +0000 | [diff] [blame] | 111 | // getImm - Return a target constant with the specified value. |
Akira Hatanaka | ee767fe | 2013-03-01 21:52:08 +0000 | [diff] [blame] | 112 | inline SDValue getImm(const SDNode *Node, uint64_t Imm) { |
Akira Hatanaka | 4d0eb63 | 2011-12-07 20:10:24 +0000 | [diff] [blame] | 113 | return CurDAG->getTargetConstant(Imm, Node->getValueType(0)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 114 | } |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 115 | |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 116 | void ProcessFunctionAfterISel(MachineFunction &MF); |
| 117 | bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 118 | void InitGlobalBaseReg(MachineFunction &MF); |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 119 | void InitMips16SPAliasReg(MachineFunction &MF); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 120 | |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 121 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 122 | char ConstraintCode, |
| 123 | std::vector<SDValue> &OutOps); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } |
| 127 | |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 128 | // Insert instructions to initialize the global base register in the |
| 129 | // first MBB of the function. When the ABI is O32 and the relocation model is |
| 130 | // PIC, the necessary instructions are emitted later to prevent optimization |
| 131 | // passes from moving them. |
| 132 | void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) { |
| 133 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 134 | |
Akira Hatanaka | de4a127 | 2012-07-25 03:16:47 +0000 | [diff] [blame] | 135 | if (!MipsFI->globalBaseRegSet()) |
Akira Hatanaka | 4782a6e | 2012-06-27 00:20:39 +0000 | [diff] [blame] | 136 | return; |
| 137 | |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 138 | MachineBasicBlock &MBB = MF.front(); |
| 139 | MachineBasicBlock::iterator I = MBB.begin(); |
| 140 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 141 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
| 142 | DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc(); |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 143 | unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg(); |
Akira Hatanaka | de4a127 | 2012-07-25 03:16:47 +0000 | [diff] [blame] | 144 | const TargetRegisterClass *RC; |
Akira Hatanaka | 54c5bc8 | 2012-06-21 20:39:10 +0000 | [diff] [blame] | 145 | |
Akira Hatanaka | de4a127 | 2012-07-25 03:16:47 +0000 | [diff] [blame] | 146 | if (Subtarget.isABI_N64()) |
| 147 | RC = (const TargetRegisterClass*)&Mips::CPU64RegsRegClass; |
| 148 | else if (Subtarget.inMips16Mode()) |
| 149 | RC = (const TargetRegisterClass*)&Mips::CPU16RegsRegClass; |
| 150 | else |
| 151 | RC = (const TargetRegisterClass*)&Mips::CPURegsRegClass; |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 152 | |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 153 | V0 = RegInfo.createVirtualRegister(RC); |
| 154 | V1 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 155 | V2 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 156 | |
| 157 | if (Subtarget.isABI_N64()) { |
| 158 | MF.getRegInfo().addLiveIn(Mips::T9_64); |
Akira Hatanaka | 56e1ed5 | 2012-03-27 02:46:25 +0000 | [diff] [blame] | 159 | MBB.addLiveIn(Mips::T9_64); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 160 | |
| 161 | // lui $v0, %hi(%neg(%gp_rel(fname))) |
| 162 | // daddu $v1, $v0, $t9 |
| 163 | // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) |
| 164 | const GlobalValue *FName = MF.getFunction(); |
| 165 | BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0) |
| 166 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 167 | BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0) |
| 168 | .addReg(Mips::T9_64); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 169 | BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1) |
| 170 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 174 | if (Subtarget.inMips16Mode()) { |
| 175 | BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0) |
Akira Hatanaka | de4a127 | 2012-07-25 03:16:47 +0000 | [diff] [blame] | 176 | .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI); |
| 177 | BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1) |
| 178 | .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO); |
| 179 | BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16); |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 180 | BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg) |
| 181 | .addReg(V1).addReg(V2); |
Akira Hatanaka | 3ee306c | 2012-07-23 23:45:54 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 185 | if (MF.getTarget().getRelocationModel() == Reloc::Static) { |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 186 | // Set global register to __gnu_local_gp. |
| 187 | // |
| 188 | // lui $v0, %hi(__gnu_local_gp) |
| 189 | // addiu $globalbasereg, $v0, %lo(__gnu_local_gp) |
| 190 | BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) |
| 191 | .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI); |
| 192 | BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0) |
| 193 | .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO); |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 194 | return; |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 195 | } |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 196 | |
| 197 | MF.getRegInfo().addLiveIn(Mips::T9); |
| 198 | MBB.addLiveIn(Mips::T9); |
| 199 | |
| 200 | if (Subtarget.isABI_N32()) { |
| 201 | // lui $v0, %hi(%neg(%gp_rel(fname))) |
| 202 | // addu $v1, $v0, $t9 |
| 203 | // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) |
| 204 | const GlobalValue *FName = MF.getFunction(); |
| 205 | BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) |
| 206 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); |
| 207 | BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9); |
| 208 | BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1) |
| 209 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | assert(Subtarget.isABI_O32()); |
| 214 | |
| 215 | // For O32 ABI, the following instruction sequence is emitted to initialize |
| 216 | // the global base register: |
| 217 | // |
| 218 | // 0. lui $2, %hi(_gp_disp) |
| 219 | // 1. addiu $2, $2, %lo(_gp_disp) |
| 220 | // 2. addu $globalbasereg, $2, $t9 |
| 221 | // |
| 222 | // We emit only the last instruction here. |
| 223 | // |
| 224 | // GNU linker requires that the first two instructions appear at the beginning |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 225 | // of a function and no instructions be inserted before or between them. |
Akira Hatanaka | 27ba61d | 2012-05-12 00:17:17 +0000 | [diff] [blame] | 226 | // The two instructions are emitted during lowering to MC layer in order to |
| 227 | // avoid any reordering. |
| 228 | // |
| 229 | // Register $2 (Mips::V0) is added to the list of live-in registers to ensure |
| 230 | // the value instruction 1 (addiu) defines is valid when instruction 2 (addu) |
| 231 | // reads it. |
| 232 | MF.getRegInfo().addLiveIn(Mips::V0); |
| 233 | MBB.addLiveIn(Mips::V0); |
| 234 | BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg) |
| 235 | .addReg(Mips::V0).addReg(Mips::T9); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 238 | // Insert instructions to initialize the Mips16 SP Alias register in the |
| 239 | // first MBB of the function. |
| 240 | // |
| 241 | void MipsDAGToDAGISel::InitMips16SPAliasReg(MachineFunction &MF) { |
| 242 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 243 | |
| 244 | if (!MipsFI->mips16SPAliasRegSet()) |
| 245 | return; |
| 246 | |
| 247 | MachineBasicBlock &MBB = MF.front(); |
| 248 | MachineBasicBlock::iterator I = MBB.begin(); |
| 249 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
| 250 | DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc(); |
| 251 | unsigned Mips16SPAliasReg = MipsFI->getMips16SPAliasReg(); |
| 252 | |
| 253 | BuildMI(MBB, I, DL, TII.get(Mips::MoveR3216), Mips16SPAliasReg) |
| 254 | .addReg(Mips::SP); |
| 255 | } |
| 256 | |
| 257 | |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 258 | bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, |
| 259 | const MachineInstr& MI) { |
| 260 | unsigned DstReg = 0, ZeroReg = 0; |
| 261 | |
| 262 | // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0". |
| 263 | if ((MI.getOpcode() == Mips::ADDiu) && |
| 264 | (MI.getOperand(1).getReg() == Mips::ZERO) && |
| 265 | (MI.getOperand(2).getImm() == 0)) { |
| 266 | DstReg = MI.getOperand(0).getReg(); |
| 267 | ZeroReg = Mips::ZERO; |
| 268 | } else if ((MI.getOpcode() == Mips::DADDiu) && |
| 269 | (MI.getOperand(1).getReg() == Mips::ZERO_64) && |
| 270 | (MI.getOperand(2).getImm() == 0)) { |
| 271 | DstReg = MI.getOperand(0).getReg(); |
| 272 | ZeroReg = Mips::ZERO_64; |
| 273 | } |
| 274 | |
| 275 | if (!DstReg) |
| 276 | return false; |
| 277 | |
| 278 | // Replace uses with ZeroReg. |
| 279 | for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg), |
Jakob Stoklund Olesen | 69a0aa8 | 2012-08-09 22:08:24 +0000 | [diff] [blame] | 280 | E = MRI->use_end(); U != E;) { |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 281 | MachineOperand &MO = U.getOperand(); |
Jakob Stoklund Olesen | 69a0aa8 | 2012-08-09 22:08:24 +0000 | [diff] [blame] | 282 | unsigned OpNo = U.getOperandNo(); |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 283 | MachineInstr *MI = MO.getParent(); |
Jakob Stoklund Olesen | 69a0aa8 | 2012-08-09 22:08:24 +0000 | [diff] [blame] | 284 | ++U; |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 285 | |
| 286 | // Do not replace if it is a phi's operand or is tied to def operand. |
Jakob Stoklund Olesen | 69a0aa8 | 2012-08-09 22:08:24 +0000 | [diff] [blame] | 287 | if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo()) |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 288 | continue; |
| 289 | |
| 290 | MO.setReg(ZeroReg); |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) { |
| 297 | InitGlobalBaseReg(MF); |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 298 | InitMips16SPAliasReg(MF); |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 299 | |
| 300 | MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 301 | |
| 302 | for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE; |
| 303 | ++MFI) |
| 304 | for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) |
| 305 | ReplaceUsesWithZeroReg(MRI, *I); |
| 306 | } |
| 307 | |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 308 | bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { |
| 309 | bool Ret = SelectionDAGISel::runOnMachineFunction(MF); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 310 | |
Akira Hatanaka | 7065b7b | 2012-03-08 01:51:59 +0000 | [diff] [blame] | 311 | ProcessFunctionAfterISel(MF); |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 312 | |
| 313 | return Ret; |
| 314 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 315 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 316 | /// getGlobalBaseReg - Output the instructions required to put the |
| 317 | /// GOT address into a register. |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 318 | SDNode *MipsDAGToDAGISel::getGlobalBaseReg() { |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 319 | unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg(); |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 320 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 323 | /// getMips16SPAliasReg - Output the instructions required to put the |
| 324 | /// SP into a Mips16 accessible aliased register. |
| 325 | SDValue MipsDAGToDAGISel::getMips16SPAliasReg() { |
| 326 | unsigned Mips16SPAliasReg = |
| 327 | MF->getInfo<MipsFunctionInfo>()->getMips16SPAliasReg(); |
| 328 | return CurDAG->getRegister(Mips16SPAliasReg, TLI.getPointerTy()); |
| 329 | } |
| 330 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 331 | /// ComplexPattern used on MipsInstrInfo |
| 332 | /// Used on Mips Load/Store instructions |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 333 | bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base, |
| 334 | SDValue &Offset) const { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 335 | EVT ValTy = Addr.getValueType(); |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 336 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 337 | // if Address is FI, get the TargetFrameIndex. |
| 338 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 339 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
| 340 | Offset = CurDAG->getTargetConstant(0, ValTy); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 341 | return true; |
| 342 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 343 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 344 | // on PIC code Load GA |
Akira Hatanaka | 6df7e23 | 2011-12-09 01:53:17 +0000 | [diff] [blame] | 345 | if (Addr.getOpcode() == MipsISD::Wrapper) { |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 346 | Base = Addr.getOperand(0); |
| 347 | Offset = Addr.getOperand(1); |
Akira Hatanaka | ca07479 | 2011-12-08 20:34:32 +0000 | [diff] [blame] | 348 | return true; |
| 349 | } |
| 350 | |
| 351 | if (TM.getRelocationModel() != Reloc::PIC_) { |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 352 | if ((Addr.getOpcode() == ISD::TargetExternalSymbol || |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 353 | Addr.getOpcode() == ISD::TargetGlobalAddress)) |
| 354 | return false; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 357 | // Addresses of the form FI+const or FI|const |
| 358 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 359 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)); |
| 360 | if (isInt<16>(CN->getSExtValue())) { |
| 361 | |
| 362 | // If the first operand is a FI, get the TargetFI Node |
| 363 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode> |
| 364 | (Addr.getOperand(0))) |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 365 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 366 | else |
| 367 | Base = Addr.getOperand(0); |
| 368 | |
Akira Hatanaka | 381e97d | 2011-10-11 00:44:20 +0000 | [diff] [blame] | 369 | Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy); |
Akira Hatanaka | 5e06903 | 2011-06-02 01:03:14 +0000 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | } |
| 373 | |
Bruno Cardoso Lopes | 7ff6fa2 | 2007-08-18 02:16:30 +0000 | [diff] [blame] | 374 | // Operand is a result from an ADD. |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 375 | if (Addr.getOpcode() == ISD::ADD) { |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 376 | // When loading from constant pools, load the lower address part in |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 377 | // the instruction itself. Example, instead of: |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 378 | // lui $2, %hi($CPI1_0) |
| 379 | // addiu $2, $2, %lo($CPI1_0) |
| 380 | // lwc1 $f0, 0($2) |
| 381 | // Generate: |
| 382 | // lui $2, %hi($CPI1_0) |
| 383 | // lwc1 $f0, %lo($CPI1_0)($2) |
Akira Hatanaka | 45d8dbc | 2012-08-24 20:21:49 +0000 | [diff] [blame] | 384 | if (Addr.getOperand(1).getOpcode() == MipsISD::Lo || |
| 385 | Addr.getOperand(1).getOpcode() == MipsISD::GPRel) { |
| 386 | SDValue Opnd0 = Addr.getOperand(1).getOperand(0); |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 387 | if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) || |
| 388 | isa<JumpTableSDNode>(Opnd0)) { |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 389 | Base = Addr.getOperand(0); |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 390 | Offset = Opnd0; |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 391 | return true; |
Bruno Cardoso Lopes | 6e0b658 | 2009-11-16 04:33:42 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 399 | bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base, |
| 400 | SDValue &Offset) const { |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 401 | Base = Addr; |
| 402 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 403 | return true; |
| 404 | } |
| 405 | |
Akira Hatanaka | abbf9df | 2013-02-16 00:14:37 +0000 | [diff] [blame] | 406 | bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base, |
| 407 | SDValue &Offset) const { |
| 408 | return selectAddrRegImm(Addr, Base, Offset) || |
| 409 | selectAddrDefault(Addr, Base, Offset); |
Akira Hatanaka | dc2f792 | 2013-02-15 21:20:45 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 412 | void MipsDAGToDAGISel::getMips16SPRefReg(SDNode *Parent, SDValue &AliasReg) { |
| 413 | SDValue AliasFPReg = CurDAG->getRegister(Mips::S0, TLI.getPointerTy()); |
| 414 | if (Parent) { |
| 415 | switch (Parent->getOpcode()) { |
| 416 | case ISD::LOAD: { |
| 417 | LoadSDNode *SD = dyn_cast<LoadSDNode>(Parent); |
| 418 | switch (SD->getMemoryVT().getSizeInBits()) { |
| 419 | case 8: |
| 420 | case 16: |
| 421 | AliasReg = TM.getFrameLowering()->hasFP(*MF)? |
| 422 | AliasFPReg: getMips16SPAliasReg(); |
| 423 | return; |
| 424 | } |
| 425 | break; |
| 426 | } |
| 427 | case ISD::STORE: { |
| 428 | StoreSDNode *SD = dyn_cast<StoreSDNode>(Parent); |
| 429 | switch (SD->getMemoryVT().getSizeInBits()) { |
| 430 | case 8: |
| 431 | case 16: |
| 432 | AliasReg = TM.getFrameLowering()->hasFP(*MF)? |
| 433 | AliasFPReg: getMips16SPAliasReg(); |
| 434 | return; |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | AliasReg = CurDAG->getRegister(Mips::SP, TLI.getPointerTy()); |
| 441 | return; |
| 442 | |
| 443 | } |
| 444 | bool MipsDAGToDAGISel::SelectAddr16( |
| 445 | SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset, |
| 446 | SDValue &Alias) { |
| 447 | EVT ValTy = Addr.getValueType(); |
| 448 | |
| 449 | Alias = CurDAG->getTargetConstant(0, ValTy); |
| 450 | |
| 451 | // if Address is FI, get the TargetFrameIndex. |
| 452 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 453 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
| 454 | Offset = CurDAG->getTargetConstant(0, ValTy); |
| 455 | getMips16SPRefReg(Parent, Alias); |
| 456 | return true; |
| 457 | } |
| 458 | // on PIC code Load GA |
| 459 | if (Addr.getOpcode() == MipsISD::Wrapper) { |
| 460 | Base = Addr.getOperand(0); |
| 461 | Offset = Addr.getOperand(1); |
| 462 | return true; |
| 463 | } |
| 464 | if (TM.getRelocationModel() != Reloc::PIC_) { |
| 465 | if ((Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 466 | Addr.getOpcode() == ISD::TargetGlobalAddress)) |
| 467 | return false; |
| 468 | } |
| 469 | // Addresses of the form FI+const or FI|const |
| 470 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 471 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)); |
| 472 | if (isInt<16>(CN->getSExtValue())) { |
| 473 | |
| 474 | // If the first operand is a FI, get the TargetFI Node |
| 475 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode> |
| 476 | (Addr.getOperand(0))) { |
| 477 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
| 478 | getMips16SPRefReg(Parent, Alias); |
| 479 | } |
| 480 | else |
| 481 | Base = Addr.getOperand(0); |
| 482 | |
| 483 | Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy); |
| 484 | return true; |
| 485 | } |
| 486 | } |
| 487 | // Operand is a result from an ADD. |
| 488 | if (Addr.getOpcode() == ISD::ADD) { |
| 489 | // When loading from constant pools, load the lower address part in |
| 490 | // the instruction itself. Example, instead of: |
| 491 | // lui $2, %hi($CPI1_0) |
| 492 | // addiu $2, $2, %lo($CPI1_0) |
| 493 | // lwc1 $f0, 0($2) |
| 494 | // Generate: |
| 495 | // lui $2, %hi($CPI1_0) |
| 496 | // lwc1 $f0, %lo($CPI1_0)($2) |
| 497 | if (Addr.getOperand(1).getOpcode() == MipsISD::Lo || |
| 498 | Addr.getOperand(1).getOpcode() == MipsISD::GPRel) { |
| 499 | SDValue Opnd0 = Addr.getOperand(1).getOperand(0); |
| 500 | if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) || |
| 501 | isa<JumpTableSDNode>(Opnd0)) { |
| 502 | Base = Addr.getOperand(0); |
| 503 | Offset = Opnd0; |
| 504 | return true; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // If an indexed floating point load/store can be emitted, return false. |
| 509 | const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent); |
| 510 | |
| 511 | if (LS && |
| 512 | (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) && |
Akira Hatanaka | 0301bc5 | 2012-11-15 21:17:13 +0000 | [diff] [blame] | 513 | Subtarget.hasFPIdx()) |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 514 | return false; |
| 515 | } |
| 516 | Base = Addr; |
| 517 | Offset = CurDAG->getTargetConstant(0, ValTy); |
| 518 | return true; |
| 519 | } |
| 520 | |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 521 | /// Select multiply instructions. |
| 522 | std::pair<SDNode*, SDNode*> |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 523 | MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty, |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 524 | bool HasLo, bool HasHi) { |
Chad Rosier | a32a08c | 2012-01-06 20:02:49 +0000 | [diff] [blame] | 525 | SDNode *Lo = 0, *Hi = 0; |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 526 | SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0), |
| 527 | N->getOperand(1)); |
| 528 | SDValue InFlag = SDValue(Mul, 0); |
| 529 | |
| 530 | if (HasLo) { |
Reed Kotler | dfb8dbb | 2012-10-05 18:27:54 +0000 | [diff] [blame] | 531 | unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mflo16 : |
| 532 | (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64); |
| 533 | Lo = CurDAG->getMachineNode(Opcode, dl, Ty, MVT::Glue, InFlag); |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 534 | InFlag = SDValue(Lo, 1); |
| 535 | } |
Reed Kotler | dfb8dbb | 2012-10-05 18:27:54 +0000 | [diff] [blame] | 536 | if (HasHi) { |
| 537 | unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mfhi16 : |
| 538 | (Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64); |
| 539 | Hi = CurDAG->getMachineNode(Opcode, dl, Ty, InFlag); |
| 540 | } |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 541 | return std::make_pair(Lo, Hi); |
| 542 | } |
| 543 | |
| 544 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 545 | /// Select instructions not customized! Used for |
| 546 | /// expanded, promoted and normal instructions |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 547 | SDNode* MipsDAGToDAGISel::Select(SDNode *Node) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 548 | unsigned Opcode = Node->getOpcode(); |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 549 | DebugLoc dl = Node->getDebugLoc(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 550 | |
| 551 | // Dump information about the Node being selected |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 552 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 553 | |
| 554 | // If we have a custom node, we already have selected! |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 555 | if (Node->isMachineOpcode()) { |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 556 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | /// |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 561 | // Instruction Selection not handled by the auto-generated |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 562 | // tablegen selection should be handled here. |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 563 | /// |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 564 | EVT NodeTy = Node->getValueType(0); |
| 565 | unsigned MultOpc; |
| 566 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 567 | switch(Opcode) { |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 568 | default: break; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 569 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 570 | case ISD::SUBE: |
| 571 | case ISD::ADDE: { |
Reed Kotler | a81be80 | 2012-10-26 04:46:26 +0000 | [diff] [blame] | 572 | bool inMips16Mode = Subtarget.inMips16Mode(); |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 573 | SDValue InFlag = Node->getOperand(2), CmpLHS; |
| 574 | unsigned Opc = InFlag.getOpcode(); (void)Opc; |
| 575 | assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || |
| 576 | (Opc == ISD::SUBC || Opc == ISD::SUBE)) && |
| 577 | "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn"); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 578 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 579 | unsigned MOp; |
| 580 | if (Opcode == ISD::ADDE) { |
| 581 | CmpLHS = InFlag.getValue(0); |
Reed Kotler | a81be80 | 2012-10-26 04:46:26 +0000 | [diff] [blame] | 582 | if (inMips16Mode) |
| 583 | MOp = Mips::AdduRxRyRz16; |
| 584 | else |
| 585 | MOp = Mips::ADDu; |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 586 | } else { |
| 587 | CmpLHS = InFlag.getOperand(0); |
Reed Kotler | a81be80 | 2012-10-26 04:46:26 +0000 | [diff] [blame] | 588 | if (inMips16Mode) |
| 589 | MOp = Mips::SubuRxRyRz16; |
| 590 | else |
| 591 | MOp = Mips::SUBu; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 594 | SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) }; |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 595 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 596 | SDValue LHS = Node->getOperand(0); |
| 597 | SDValue RHS = Node->getOperand(1); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 598 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 599 | EVT VT = LHS.getValueType(); |
Reed Kotler | a81be80 | 2012-10-26 04:46:26 +0000 | [diff] [blame] | 600 | |
| 601 | unsigned Sltu_op = inMips16Mode? Mips::SltuRxRyRz16: Mips::SLTu; |
| 602 | SDNode *Carry = CurDAG->getMachineNode(Sltu_op, dl, VT, Ops, 2); |
| 603 | unsigned Addu_op = inMips16Mode? Mips::AdduRxRyRz16 : Mips::ADDu; |
| 604 | SDNode *AddCarry = CurDAG->getMachineNode(Addu_op, dl, VT, |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 605 | SDValue(Carry,0), RHS); |
Bruno Cardoso Lopes | 07cec75 | 2008-06-06 00:58:26 +0000 | [diff] [blame] | 606 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 607 | return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, |
| 608 | LHS, SDValue(AddCarry,0)); |
| 609 | } |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 610 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 611 | /// Mul with two results |
| 612 | case ISD::SMUL_LOHI: |
| 613 | case ISD::UMUL_LOHI: { |
Reed Kotler | dfb8dbb | 2012-10-05 18:27:54 +0000 | [diff] [blame] | 614 | if (NodeTy == MVT::i32) { |
| 615 | if (Subtarget.inMips16Mode()) |
| 616 | MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 : |
| 617 | Mips::MultRxRy16); |
| 618 | else |
| 619 | MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT); |
| 620 | } |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 621 | else |
| 622 | MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 623 | |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 624 | std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy, |
| 625 | true, true); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 626 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 627 | if (!SDValue(Node, 0).use_empty()) |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 628 | ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 629 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 630 | if (!SDValue(Node, 1).use_empty()) |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 631 | ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0)); |
Bruno Cardoso Lopes | 0af5e09 | 2008-06-06 06:37:31 +0000 | [diff] [blame] | 632 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 635 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 636 | /// Special Muls |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 637 | case ISD::MUL: { |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 638 | // Mips32 has a 32-bit three operand mul instruction. |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 639 | if (Subtarget.hasMips32() && NodeTy == MVT::i32) |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 640 | break; |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 641 | return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT, |
| 642 | dl, NodeTy, true, false).first; |
| 643 | } |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 644 | case ISD::MULHS: |
| 645 | case ISD::MULHU: { |
Reed Kotler | dfb8dbb | 2012-10-05 18:27:54 +0000 | [diff] [blame] | 646 | if (NodeTy == MVT::i32) { |
| 647 | if (Subtarget.inMips16Mode()) |
| 648 | MultOpc = (Opcode == ISD::MULHU ? |
| 649 | Mips::MultuRxRy16 : Mips::MultRxRy16); |
| 650 | else |
| 651 | MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT); |
| 652 | } |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 653 | else |
Akira Hatanaka | 2fd0475 | 2011-12-20 23:10:57 +0000 | [diff] [blame] | 654 | MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT); |
| 655 | |
| 656 | return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second; |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 657 | } |
Bruno Cardoso Lopes | a8173b9 | 2009-11-13 18:49:59 +0000 | [diff] [blame] | 658 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 659 | // Get target GOT address. |
| 660 | case ISD::GLOBAL_OFFSET_TABLE: |
| 661 | return getGlobalBaseReg(); |
Akira Hatanaka | ca07479 | 2011-12-08 20:34:32 +0000 | [diff] [blame] | 662 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 663 | case ISD::ConstantFP: { |
| 664 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node); |
| 665 | if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) { |
| 666 | if (Subtarget.hasMips64()) { |
| 667 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 668 | Mips::ZERO_64, MVT::i64); |
| 669 | return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero); |
Akira Hatanaka | ca07479 | 2011-12-08 20:34:32 +0000 | [diff] [blame] | 670 | } |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 671 | |
| 672 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 673 | Mips::ZERO, MVT::i32); |
| 674 | return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero, |
| 675 | Zero); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 676 | } |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 677 | break; |
| 678 | } |
| 679 | |
Akira Hatanaka | 57fa382 | 2012-01-25 03:01:35 +0000 | [diff] [blame] | 680 | case ISD::Constant: { |
| 681 | const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node); |
| 682 | unsigned Size = CN->getValueSizeInBits(0); |
| 683 | |
| 684 | if (Size == 32) |
| 685 | break; |
| 686 | |
| 687 | MipsAnalyzeImmediate AnalyzeImm; |
| 688 | int64_t Imm = CN->getSExtValue(); |
| 689 | |
| 690 | const MipsAnalyzeImmediate::InstSeq &Seq = |
| 691 | AnalyzeImm.Analyze(Imm, Size, false); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 692 | |
Akira Hatanaka | 57fa382 | 2012-01-25 03:01:35 +0000 | [diff] [blame] | 693 | MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin(); |
| 694 | DebugLoc DL = CN->getDebugLoc(); |
| 695 | SDNode *RegOpnd; |
| 696 | SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), |
| 697 | MVT::i64); |
| 698 | |
| 699 | // The first instruction can be a LUi which is different from other |
| 700 | // instructions (ADDiu, ORI and SLL) in that it does not have a register |
| 701 | // operand. |
| 702 | if (Inst->Opc == Mips::LUi64) |
| 703 | RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd); |
| 704 | else |
| 705 | RegOpnd = |
| 706 | CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, |
| 707 | CurDAG->getRegister(Mips::ZERO_64, MVT::i64), |
| 708 | ImmOpnd); |
| 709 | |
| 710 | // The remaining instructions in the sequence are handled here. |
| 711 | for (++Inst; Inst != Seq.end(); ++Inst) { |
| 712 | ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), |
| 713 | MVT::i64); |
| 714 | RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, |
| 715 | SDValue(RegOpnd, 0), ImmOpnd); |
| 716 | } |
| 717 | |
| 718 | return RegOpnd; |
| 719 | } |
| 720 | |
Akira Hatanaka | 5a7dd43 | 2012-09-15 01:52:08 +0000 | [diff] [blame] | 721 | #ifndef NDEBUG |
| 722 | case ISD::LOAD: |
| 723 | case ISD::STORE: |
| 724 | assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <= |
| 725 | cast<MemSDNode>(Node)->getAlignment() && |
| 726 | "Unexpected unaligned loads/stores."); |
| 727 | break; |
| 728 | #endif |
| 729 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 730 | case MipsISD::ThreadPointer: { |
| 731 | EVT PtrVT = TLI.getPointerTy(); |
| 732 | unsigned RdhwrOpc, SrcReg, DestReg; |
| 733 | |
| 734 | if (PtrVT == MVT::i32) { |
| 735 | RdhwrOpc = Mips::RDHWR; |
| 736 | SrcReg = Mips::HWR29; |
| 737 | DestReg = Mips::V1; |
| 738 | } else { |
| 739 | RdhwrOpc = Mips::RDHWR64; |
| 740 | SrcReg = Mips::HWR29_64; |
| 741 | DestReg = Mips::V1_64; |
| 742 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 743 | |
Akira Hatanaka | 49d534b | 2011-12-20 22:58:01 +0000 | [diff] [blame] | 744 | SDNode *Rdhwr = |
| 745 | CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(), |
| 746 | Node->getValueType(0), |
| 747 | CurDAG->getRegister(SrcReg, PtrVT)); |
| 748 | SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg, |
| 749 | SDValue(Rdhwr, 0)); |
| 750 | SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT); |
| 751 | ReplaceUses(SDValue(Node, 0), ResNode); |
| 752 | return ResNode.getNode(); |
| 753 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Select the default instruction |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 757 | SDNode *ResNode = SelectCode(Node); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 759 | DEBUG(errs() << "=> "); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 760 | if (ResNode == NULL || ResNode == Node) |
| 761 | DEBUG(Node->dump(CurDAG)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 762 | else |
| 763 | DEBUG(ResNode->dump(CurDAG)); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 764 | DEBUG(errs() << "\n"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 765 | return ResNode; |
| 766 | } |
| 767 | |
Akira Hatanaka | 21afc63 | 2011-06-21 00:40:49 +0000 | [diff] [blame] | 768 | bool MipsDAGToDAGISel:: |
| 769 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
| 770 | std::vector<SDValue> &OutOps) { |
| 771 | assert(ConstraintCode == 'm' && "unexpected asm memory constraint"); |
| 772 | OutOps.push_back(Op); |
| 773 | return false; |
| 774 | } |
| 775 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 776 | /// createMipsISelDag - This pass converts a legalized DAG into a |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 777 | /// MIPS-specific DAG, ready for instruction scheduling. |
| 778 | FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) { |
| 779 | return new MipsDAGToDAGISel(TM); |
| 780 | } |