Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 1 | //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===// |
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 the interfaces that Mips uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "mips-lower" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "MipsISelLowering.h" |
Craig Topper | 79aa341 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 16 | #include "InstPrinter/MipsInstPrinter.h" |
| 17 | #include "MCTargetDesc/MipsBaseInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "MipsMachineFunction.h" |
| 19 | #include "MipsSubtarget.h" |
| 20 | #include "MipsTargetMachine.h" |
| 21 | #include "MipsTargetObjectFile.h" |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/CallingConvLower.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/ValueTypes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/CallingConv.h" |
| 31 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalVariable.h" |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
NAKAMURA Takumi | 8959393 | 2012-04-21 15:31:45 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Akira Hatanaka | bfb07b1 | 2013-08-14 00:21:25 +0000 | [diff] [blame] | 37 | #include <cctype> |
NAKAMURA Takumi | 8959393 | 2012-04-21 15:31:45 +0000 | [diff] [blame] | 38 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 41 | STATISTIC(NumTailCalls, "Number of tail calls"); |
| 42 | |
| 43 | static cl::opt<bool> |
Akira Hatanaka | 81784cb | 2012-11-21 20:21:11 +0000 | [diff] [blame] | 44 | LargeGOT("mxgot", cl::Hidden, |
| 45 | cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false)); |
| 46 | |
Akira Hatanaka | f894199 | 2013-05-20 18:07:43 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
Akira Hatanaka | 2591b5c | 2013-05-21 17:17:59 +0000 | [diff] [blame] | 48 | NoZeroDivCheck("mno-check-zero-division", cl::Hidden, |
Akira Hatanaka | f894199 | 2013-05-20 18:07:43 +0000 | [diff] [blame] | 49 | cl::desc("MIPS: Don't trap on integer division by zero."), |
| 50 | cl::init(false)); |
| 51 | |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 52 | static const uint16_t O32IntRegs[4] = { |
| 53 | Mips::A0, Mips::A1, Mips::A2, Mips::A3 |
| 54 | }; |
| 55 | |
| 56 | static const uint16_t Mips64IntRegs[8] = { |
| 57 | Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64, |
| 58 | Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64 |
| 59 | }; |
| 60 | |
| 61 | static const uint16_t Mips64DPRegs[8] = { |
| 62 | Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64, |
| 63 | Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64 |
| 64 | }; |
| 65 | |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 66 | // If I is a shifted mask, set the size (Size) and the first bit of the |
Akira Hatanaka | dbe9a31 | 2011-08-18 20:07:42 +0000 | [diff] [blame] | 67 | // mask (Pos), and return true. |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 68 | // For example, if I is 0x003ff800, (Pos, Size) = (11, 11). |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 69 | static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) { |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 70 | if (!isShiftedMask_64(I)) |
Akira Hatanaka | 854a7db | 2011-08-19 22:59:00 +0000 | [diff] [blame] | 71 | return false; |
Akira Hatanaka | bb15e11 | 2011-08-17 02:05:42 +0000 | [diff] [blame] | 72 | |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 73 | Size = CountPopulation_64(I); |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 74 | Pos = countTrailingZeros(I); |
Akira Hatanaka | dbe9a31 | 2011-08-18 20:07:42 +0000 | [diff] [blame] | 75 | return true; |
Akira Hatanaka | bb15e11 | 2011-08-17 02:05:42 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 78 | SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const { |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 79 | MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>(); |
| 80 | return DAG.getRegister(FI->getGlobalBaseReg(), Ty); |
| 81 | } |
| 82 | |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 83 | static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) { |
| 84 | EVT Ty = Op.getValueType(); |
| 85 | |
| 86 | if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 87 | return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(Op), Ty, 0, |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 88 | Flag); |
| 89 | if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op)) |
| 90 | return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag); |
| 91 | if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op)) |
| 92 | return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag); |
| 93 | if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op)) |
| 94 | return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag); |
| 95 | if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op)) |
| 96 | return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(), |
| 97 | N->getOffset(), Flag); |
| 98 | |
| 99 | llvm_unreachable("Unexpected node type."); |
| 100 | return SDValue(); |
| 101 | } |
| 102 | |
| 103 | static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 104 | SDLoc DL(Op); |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 105 | EVT Ty = Op.getValueType(); |
| 106 | SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI); |
| 107 | SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO); |
| 108 | return DAG.getNode(ISD::ADD, DL, Ty, |
| 109 | DAG.getNode(MipsISD::Hi, DL, Ty, Hi), |
| 110 | DAG.getNode(MipsISD::Lo, DL, Ty, Lo)); |
| 111 | } |
| 112 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 113 | SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG, |
| 114 | bool HasMips64) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 115 | SDLoc DL(Op); |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 116 | EVT Ty = Op.getValueType(); |
| 117 | unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 118 | SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty), |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 119 | getTargetNode(Op, DAG, GOTFlag)); |
| 120 | SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT, |
| 121 | MachinePointerInfo::getGOT(), false, false, false, |
| 122 | 0); |
| 123 | unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO; |
| 124 | SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag)); |
| 125 | return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo); |
| 126 | } |
| 127 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 128 | SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG, |
| 129 | unsigned Flag) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 130 | SDLoc DL(Op); |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 131 | EVT Ty = Op.getValueType(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 132 | SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty), |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 133 | getTargetNode(Op, DAG, Flag)); |
| 134 | return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt, |
| 135 | MachinePointerInfo::getGOT(), false, false, false, 0); |
| 136 | } |
| 137 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 138 | SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG, |
| 139 | unsigned HiFlag, |
| 140 | unsigned LoFlag) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 141 | SDLoc DL(Op); |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 142 | EVT Ty = Op.getValueType(); |
| 143 | SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag)); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 144 | Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty)); |
Akira Hatanaka | 6b28b80 | 2012-11-21 20:26:38 +0000 | [diff] [blame] | 145 | SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi, |
| 146 | getTargetNode(Op, DAG, LoFlag)); |
| 147 | return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper, |
| 148 | MachinePointerInfo::getGOT(), false, false, false, 0); |
| 149 | } |
| 150 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 151 | const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 152 | switch (Opcode) { |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 153 | case MipsISD::JmpLink: return "MipsISD::JmpLink"; |
Akira Hatanaka | 58d1e3f | 2012-10-19 20:59:39 +0000 | [diff] [blame] | 154 | case MipsISD::TailCall: return "MipsISD::TailCall"; |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 155 | case MipsISD::Hi: return "MipsISD::Hi"; |
| 156 | case MipsISD::Lo: return "MipsISD::Lo"; |
| 157 | case MipsISD::GPRel: return "MipsISD::GPRel"; |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 158 | case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer"; |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 159 | case MipsISD::Ret: return "MipsISD::Ret"; |
Akira Hatanaka | 544cc21 | 2013-01-30 00:26:49 +0000 | [diff] [blame] | 160 | case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN"; |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 161 | case MipsISD::FPBrcond: return "MipsISD::FPBrcond"; |
| 162 | case MipsISD::FPCmp: return "MipsISD::FPCmp"; |
| 163 | case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T"; |
| 164 | case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F"; |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 165 | case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP"; |
Akira Hatanaka | dd95892 | 2013-03-30 01:14:04 +0000 | [diff] [blame] | 166 | case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI"; |
| 167 | case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI"; |
| 168 | case MipsISD::Mult: return "MipsISD::Mult"; |
| 169 | case MipsISD::Multu: return "MipsISD::Multu"; |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 170 | case MipsISD::MAdd: return "MipsISD::MAdd"; |
| 171 | case MipsISD::MAddu: return "MipsISD::MAddu"; |
| 172 | case MipsISD::MSub: return "MipsISD::MSub"; |
| 173 | case MipsISD::MSubu: return "MipsISD::MSubu"; |
| 174 | case MipsISD::DivRem: return "MipsISD::DivRem"; |
| 175 | case MipsISD::DivRemU: return "MipsISD::DivRemU"; |
Akira Hatanaka | dd95892 | 2013-03-30 01:14:04 +0000 | [diff] [blame] | 176 | case MipsISD::DivRem16: return "MipsISD::DivRem16"; |
| 177 | case MipsISD::DivRemU16: return "MipsISD::DivRemU16"; |
Akira Hatanaka | bdd2ce9 | 2011-05-23 21:13:59 +0000 | [diff] [blame] | 178 | case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64"; |
| 179 | case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64"; |
Akira Hatanaka | bfcb83f | 2011-12-12 22:38:19 +0000 | [diff] [blame] | 180 | case MipsISD::Wrapper: return "MipsISD::Wrapper"; |
Akira Hatanaka | db54826 | 2011-07-19 23:30:50 +0000 | [diff] [blame] | 181 | case MipsISD::Sync: return "MipsISD::Sync"; |
Akira Hatanaka | bb15e11 | 2011-08-17 02:05:42 +0000 | [diff] [blame] | 182 | case MipsISD::Ext: return "MipsISD::Ext"; |
| 183 | case MipsISD::Ins: return "MipsISD::Ins"; |
Akira Hatanaka | b6f1dc2 | 2012-06-02 00:03:12 +0000 | [diff] [blame] | 184 | case MipsISD::LWL: return "MipsISD::LWL"; |
| 185 | case MipsISD::LWR: return "MipsISD::LWR"; |
| 186 | case MipsISD::SWL: return "MipsISD::SWL"; |
| 187 | case MipsISD::SWR: return "MipsISD::SWR"; |
| 188 | case MipsISD::LDL: return "MipsISD::LDL"; |
| 189 | case MipsISD::LDR: return "MipsISD::LDR"; |
| 190 | case MipsISD::SDL: return "MipsISD::SDL"; |
| 191 | case MipsISD::SDR: return "MipsISD::SDR"; |
Akira Hatanaka | 6fad5e7 | 2012-09-21 23:52:47 +0000 | [diff] [blame] | 192 | case MipsISD::EXTP: return "MipsISD::EXTP"; |
| 193 | case MipsISD::EXTPDP: return "MipsISD::EXTPDP"; |
| 194 | case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H"; |
| 195 | case MipsISD::EXTR_W: return "MipsISD::EXTR_W"; |
| 196 | case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W"; |
| 197 | case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W"; |
| 198 | case MipsISD::SHILO: return "MipsISD::SHILO"; |
| 199 | case MipsISD::MTHLIP: return "MipsISD::MTHLIP"; |
| 200 | case MipsISD::MULT: return "MipsISD::MULT"; |
| 201 | case MipsISD::MULTU: return "MipsISD::MULTU"; |
Jia Liu | b3ea880 | 2013-03-04 01:06:54 +0000 | [diff] [blame] | 202 | case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP"; |
Akira Hatanaka | 6fad5e7 | 2012-09-21 23:52:47 +0000 | [diff] [blame] | 203 | case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP"; |
| 204 | case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP"; |
| 205 | case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP"; |
Akira Hatanaka | 97a62bf | 2013-04-19 23:21:32 +0000 | [diff] [blame] | 206 | case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP"; |
| 207 | case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP"; |
| 208 | case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP"; |
Akira Hatanaka | cd6c579 | 2013-04-30 22:37:26 +0000 | [diff] [blame] | 209 | case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP"; |
| 210 | case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP"; |
Daniel Sanders | 3c380d5 | 2013-08-28 12:14:50 +0000 | [diff] [blame] | 211 | case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO"; |
| 212 | case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO"; |
| 213 | case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO"; |
| 214 | case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO"; |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 215 | default: return NULL; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | MipsTargetLowering:: |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 220 | MipsTargetLowering(MipsTargetMachine &TM) |
Akira Hatanaka | 8b4198d | 2011-09-26 21:47:02 +0000 | [diff] [blame] | 221 | : TargetLowering(TM, new MipsTargetObjectFile()), |
| 222 | Subtarget(&TM.getSubtarget<MipsSubtarget>()), |
Akira Hatanaka | 2ec69fa | 2011-10-28 18:47:24 +0000 | [diff] [blame] | 223 | HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()), |
| 224 | IsO32(Subtarget->isABI_O32()) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 225 | // Mips does not have i1 type, so use i32 for |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 226 | // setcc operations results (slt, sgt, ...). |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 227 | setBooleanContents(ZeroOrOneBooleanContent); |
Akira Hatanaka | cd6c579 | 2013-04-30 22:37:26 +0000 | [diff] [blame] | 228 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 229 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 230 | // Load extented operations for i1 types must be promoted |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 231 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
| 232 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 233 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 234 | |
Eli Friedman | 6055a6a | 2009-07-17 04:07:24 +0000 | [diff] [blame] | 235 | // MIPS doesn't have extending float->double load/store |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 236 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 237 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Eli Friedman | 10a3659 | 2009-07-17 02:28:12 +0000 | [diff] [blame] | 238 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 239 | // Used by legalize types to correctly generate the setcc result. |
| 240 | // Without this, every float setcc comes with a AND/OR with the result, |
| 241 | // we don't want this, since the fpcmp result goes to a flag register, |
Bruno Cardoso Lopes | 7728377 | 2008-07-31 18:31:28 +0000 | [diff] [blame] | 242 | // which is used implicitly by brcond and select operations. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 243 | AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); |
Bruno Cardoso Lopes | 7728377 | 2008-07-31 18:31:28 +0000 | [diff] [blame] | 244 | |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 245 | // Mips Custom Operations |
Akira Hatanaka | b7656a9 | 2013-03-06 21:32:03 +0000 | [diff] [blame] | 246 | setOperationAction(ISD::BR_JT, MVT::Other, Custom); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 247 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Bruno Cardoso Lopes | ca8a2aa | 2011-03-04 20:01:52 +0000 | [diff] [blame] | 248 | setOperationAction(ISD::BlockAddress, MVT::i32, Custom); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 249 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
| 250 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
| 251 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 252 | setOperationAction(ISD::SELECT, MVT::f32, Custom); |
| 253 | setOperationAction(ISD::SELECT, MVT::f64, Custom); |
| 254 | setOperationAction(ISD::SELECT, MVT::i32, Custom); |
Akira Hatanaka | 3fef29d | 2012-07-11 19:32:27 +0000 | [diff] [blame] | 255 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 256 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Akira Hatanaka | 0a40c23 | 2012-03-09 23:46:03 +0000 | [diff] [blame] | 257 | setOperationAction(ISD::SETCC, MVT::f32, Custom); |
| 258 | setOperationAction(ISD::SETCC, MVT::f64, Custom); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 259 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Bruno Cardoso Lopes | 6059b85 | 2010-02-06 21:00:02 +0000 | [diff] [blame] | 260 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
Akira Hatanaka | d229b7b | 2012-03-10 00:03:50 +0000 | [diff] [blame] | 261 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 262 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 263 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
Akira Hatanaka | d229b7b | 2012-03-10 00:03:50 +0000 | [diff] [blame] | 264 | |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 265 | if (!TM.Options.NoNaNsFPMath) { |
| 266 | setOperationAction(ISD::FABS, MVT::f32, Custom); |
| 267 | setOperationAction(ISD::FABS, MVT::f64, Custom); |
| 268 | } |
| 269 | |
Akira Hatanaka | d229b7b | 2012-03-10 00:03:50 +0000 | [diff] [blame] | 270 | if (HasMips64) { |
| 271 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
| 272 | setOperationAction(ISD::BlockAddress, MVT::i64, Custom); |
| 273 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
| 274 | setOperationAction(ISD::JumpTable, MVT::i64, Custom); |
| 275 | setOperationAction(ISD::ConstantPool, MVT::i64, Custom); |
| 276 | setOperationAction(ISD::SELECT, MVT::i64, Custom); |
Akira Hatanaka | 7664f05 | 2012-06-02 00:04:42 +0000 | [diff] [blame] | 277 | setOperationAction(ISD::LOAD, MVT::i64, Custom); |
| 278 | setOperationAction(ISD::STORE, MVT::i64, Custom); |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 279 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
Akira Hatanaka | d229b7b | 2012-03-10 00:03:50 +0000 | [diff] [blame] | 280 | } |
Bruno Cardoso Lopes | 6059b85 | 2010-02-06 21:00:02 +0000 | [diff] [blame] | 281 | |
Akira Hatanaka | a284acb | 2012-05-09 00:55:21 +0000 | [diff] [blame] | 282 | if (!HasMips64) { |
| 283 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); |
| 284 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); |
| 285 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); |
| 286 | } |
| 287 | |
Akira Hatanaka | e90a3bc | 2012-11-07 19:10:58 +0000 | [diff] [blame] | 288 | setOperationAction(ISD::ADD, MVT::i32, Custom); |
| 289 | if (HasMips64) |
| 290 | setOperationAction(ISD::ADD, MVT::i64, Custom); |
| 291 | |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 292 | setOperationAction(ISD::SDIV, MVT::i32, Expand); |
| 293 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 294 | setOperationAction(ISD::UDIV, MVT::i32, Expand); |
| 295 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Akira Hatanaka | dda4a07 | 2011-10-03 21:06:13 +0000 | [diff] [blame] | 296 | setOperationAction(ISD::SDIV, MVT::i64, Expand); |
| 297 | setOperationAction(ISD::SREM, MVT::i64, Expand); |
| 298 | setOperationAction(ISD::UDIV, MVT::i64, Expand); |
| 299 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 300 | |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 301 | // Operations not directly supported by Mips. |
Tom Stellard | 3ef5383 | 2013-03-08 15:36:57 +0000 | [diff] [blame] | 302 | setOperationAction(ISD::BR_CC, MVT::f32, Expand); |
| 303 | setOperationAction(ISD::BR_CC, MVT::f64, Expand); |
| 304 | setOperationAction(ISD::BR_CC, MVT::i32, Expand); |
| 305 | setOperationAction(ISD::BR_CC, MVT::i64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 306 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 307 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
Akira Hatanaka | e1bcd6b | 2011-12-20 23:40:56 +0000 | [diff] [blame] | 308 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 309 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
Akira Hatanaka | e1bcd6b | 2011-12-20 23:40:56 +0000 | [diff] [blame] | 310 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 311 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 312 | setOperationAction(ISD::CTPOP, MVT::i32, Expand); |
Akira Hatanaka | 7f16274 | 2011-12-21 00:14:05 +0000 | [diff] [blame] | 313 | setOperationAction(ISD::CTPOP, MVT::i64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 314 | setOperationAction(ISD::CTTZ, MVT::i32, Expand); |
Akira Hatanaka | 7f16274 | 2011-12-21 00:14:05 +0000 | [diff] [blame] | 315 | setOperationAction(ISD::CTTZ, MVT::i64, Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 316 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); |
| 317 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); |
| 318 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); |
| 319 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 320 | setOperationAction(ISD::ROTL, MVT::i32, Expand); |
Akira Hatanaka | c7bafe9 | 2011-09-30 18:51:46 +0000 | [diff] [blame] | 321 | setOperationAction(ISD::ROTL, MVT::i64, Expand); |
Akira Hatanaka | 1d165f1 | 2012-07-31 20:54:48 +0000 | [diff] [blame] | 322 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
| 323 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Bruno Cardoso Lopes | 908b6dd | 2010-12-09 17:32:30 +0000 | [diff] [blame] | 324 | |
Akira Hatanaka | 5663344 | 2011-09-20 23:53:09 +0000 | [diff] [blame] | 325 | if (!Subtarget->hasMips32r2()) |
Bruno Cardoso Lopes | 908b6dd | 2010-12-09 17:32:30 +0000 | [diff] [blame] | 326 | setOperationAction(ISD::ROTR, MVT::i32, Expand); |
| 327 | |
Akira Hatanaka | c7bafe9 | 2011-09-30 18:51:46 +0000 | [diff] [blame] | 328 | if (!Subtarget->hasMips64r2()) |
| 329 | setOperationAction(ISD::ROTR, MVT::i64, Expand); |
| 330 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 331 | setOperationAction(ISD::FSIN, MVT::f32, Expand); |
Bruno Cardoso Lopes | 5d6fb5d | 2011-03-04 18:54:14 +0000 | [diff] [blame] | 332 | setOperationAction(ISD::FSIN, MVT::f64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 333 | setOperationAction(ISD::FCOS, MVT::f32, Expand); |
Bruno Cardoso Lopes | 5d6fb5d | 2011-03-04 18:54:14 +0000 | [diff] [blame] | 334 | setOperationAction(ISD::FCOS, MVT::f64, Expand); |
Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 335 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
| 336 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 337 | setOperationAction(ISD::FPOWI, MVT::f32, Expand); |
| 338 | setOperationAction(ISD::FPOW, MVT::f32, Expand); |
Akira Hatanaka | 46da136 | 2011-05-23 22:23:58 +0000 | [diff] [blame] | 339 | setOperationAction(ISD::FPOW, MVT::f64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 340 | setOperationAction(ISD::FLOG, MVT::f32, Expand); |
| 341 | setOperationAction(ISD::FLOG2, MVT::f32, Expand); |
| 342 | setOperationAction(ISD::FLOG10, MVT::f32, Expand); |
| 343 | setOperationAction(ISD::FEXP, MVT::f32, Expand); |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 344 | setOperationAction(ISD::FMA, MVT::f32, Expand); |
| 345 | setOperationAction(ISD::FMA, MVT::f64, Expand); |
Akira Hatanaka | 21ecc2f | 2012-03-29 18:43:11 +0000 | [diff] [blame] | 346 | setOperationAction(ISD::FREM, MVT::f32, Expand); |
| 347 | setOperationAction(ISD::FREM, MVT::f64, Expand); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 348 | |
Akira Hatanaka | 1cc6333 | 2012-04-11 22:59:08 +0000 | [diff] [blame] | 349 | if (!TM.Options.NoNaNsFPMath) { |
| 350 | setOperationAction(ISD::FNEG, MVT::f32, Expand); |
| 351 | setOperationAction(ISD::FNEG, MVT::f64, Expand); |
| 352 | } |
| 353 | |
Akira Hatanaka | 544cc21 | 2013-01-30 00:26:49 +0000 | [diff] [blame] | 354 | setOperationAction(ISD::EH_RETURN, MVT::Other, Custom); |
| 355 | |
Bruno Cardoso Lopes | 954dac0 | 2011-03-09 19:22:22 +0000 | [diff] [blame] | 356 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 357 | setOperationAction(ISD::VACOPY, MVT::Other, Expand); |
| 358 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
| 359 | |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 360 | // Use the default for now |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 361 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 362 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 363 | |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 364 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand); |
| 365 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); |
| 366 | setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand); |
| 367 | setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); |
Eli Friedman | 4db5aca | 2011-08-29 18:23:02 +0000 | [diff] [blame] | 368 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 369 | setInsertFencesForAtomic(true); |
| 370 | |
Bruno Cardoso Lopes | 7728f7e | 2008-07-09 05:32:22 +0000 | [diff] [blame] | 371 | if (!Subtarget->hasSEInReg()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 372 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); |
| 373 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Akira Hatanaka | c79507a | 2011-12-21 00:20:27 +0000 | [diff] [blame] | 376 | if (!Subtarget->hasBitCount()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 377 | setOperationAction(ISD::CTLZ, MVT::i32, Expand); |
Akira Hatanaka | c79507a | 2011-12-21 00:20:27 +0000 | [diff] [blame] | 378 | setOperationAction(ISD::CTLZ, MVT::i64, Expand); |
| 379 | } |
Bruno Cardoso Lopes | 65ad452 | 2008-08-08 06:16:31 +0000 | [diff] [blame] | 380 | |
Akira Hatanaka | c0ea043 | 2011-12-20 23:56:43 +0000 | [diff] [blame] | 381 | if (!Subtarget->hasSwap()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 382 | setOperationAction(ISD::BSWAP, MVT::i32, Expand); |
Akira Hatanaka | c0ea043 | 2011-12-20 23:56:43 +0000 | [diff] [blame] | 383 | setOperationAction(ISD::BSWAP, MVT::i64, Expand); |
| 384 | } |
Bruno Cardoso Lopes | 739e441 | 2008-08-13 07:13:40 +0000 | [diff] [blame] | 385 | |
Akira Hatanaka | 7664f05 | 2012-06-02 00:04:42 +0000 | [diff] [blame] | 386 | if (HasMips64) { |
| 387 | setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom); |
| 388 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom); |
| 389 | setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom); |
| 390 | setTruncStoreAction(MVT::i64, MVT::i32, Custom); |
| 391 | } |
| 392 | |
Akira Hatanaka | 9758562 | 2013-07-26 20:58:55 +0000 | [diff] [blame] | 393 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
| 394 | |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 395 | setTargetDAGCombine(ISD::SDIVREM); |
| 396 | setTargetDAGCombine(ISD::UDIVREM); |
Akira Hatanaka | ee8c3b0 | 2012-03-08 03:26:37 +0000 | [diff] [blame] | 397 | setTargetDAGCombine(ISD::SELECT); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 398 | setTargetDAGCombine(ISD::AND); |
| 399 | setTargetDAGCombine(ISD::OR); |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 400 | setTargetDAGCombine(ISD::ADD); |
Bruno Cardoso Lopes | 8be7611 | 2011-01-18 19:29:17 +0000 | [diff] [blame] | 401 | |
Akira Hatanaka | 5fdf500 | 2012-03-08 01:59:33 +0000 | [diff] [blame] | 402 | setMinFunctionAlignment(HasMips64 ? 3 : 2); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 403 | |
Akira Hatanaka | 3f5b107 | 2012-02-02 03:17:04 +0000 | [diff] [blame] | 404 | setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP); |
Akira Hatanaka | cf0cd80 | 2011-05-26 18:59:03 +0000 | [diff] [blame] | 405 | |
Akira Hatanaka | 590baca | 2012-02-02 03:13:40 +0000 | [diff] [blame] | 406 | setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0); |
| 407 | setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1); |
Akira Hatanaka | e193b32 | 2012-06-13 19:33:32 +0000 | [diff] [blame] | 408 | |
Jim Grosbach | 3450f80 | 2013-02-20 21:13:59 +0000 | [diff] [blame] | 409 | MaxStoresPerMemcpy = 16; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 412 | const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) { |
| 413 | if (TM.getSubtargetImpl()->inMips16Mode()) |
| 414 | return llvm::createMips16TargetLowering(TM); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 415 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 416 | return llvm::createMipsSETargetLowering(TM); |
Akira Hatanaka | 5c21c9e | 2011-08-12 21:30:06 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 419 | EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
Akira Hatanaka | e13f441 | 2013-01-04 20:06:01 +0000 | [diff] [blame] | 420 | if (!VT.isVector()) |
| 421 | return MVT::i32; |
| 422 | return VT.changeVectorElementTypeToInteger(); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 425 | static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG, |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 426 | TargetLowering::DAGCombinerInfo &DCI, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 427 | const MipsSubtarget *Subtarget) { |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 428 | if (DCI.isBeforeLegalizeOps()) |
| 429 | return SDValue(); |
| 430 | |
Akira Hatanaka | dda4a07 | 2011-10-03 21:06:13 +0000 | [diff] [blame] | 431 | EVT Ty = N->getValueType(0); |
Akira Hatanaka | cbaf6d0 | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 432 | unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64; |
| 433 | unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64; |
Akira Hatanaka | f5926fd | 2013-03-30 01:36:35 +0000 | [diff] [blame] | 434 | unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 : |
| 435 | MipsISD::DivRemU16; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 436 | SDLoc DL(N); |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 437 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 438 | SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue, |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 439 | N->getOperand(0), N->getOperand(1)); |
| 440 | SDValue InChain = DAG.getEntryNode(); |
| 441 | SDValue InGlue = DivRem; |
| 442 | |
| 443 | // insert MFLO |
| 444 | if (N->hasAnyUseOfValue(0)) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 445 | SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty, |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 446 | InGlue); |
| 447 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo); |
| 448 | InChain = CopyFromLo.getValue(1); |
| 449 | InGlue = CopyFromLo.getValue(2); |
| 450 | } |
| 451 | |
| 452 | // insert MFHI |
| 453 | if (N->hasAnyUseOfValue(1)) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 454 | SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL, |
Akira Hatanaka | dda4a07 | 2011-10-03 21:06:13 +0000 | [diff] [blame] | 455 | HI, Ty, InGlue); |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 456 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi); |
| 457 | } |
| 458 | |
| 459 | return SDValue(); |
| 460 | } |
| 461 | |
Akira Hatanaka | 2fbe90c | 2013-04-18 01:00:46 +0000 | [diff] [blame] | 462 | static Mips::CondCode condCodeToFCC(ISD::CondCode CC) { |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 463 | switch (CC) { |
| 464 | default: llvm_unreachable("Unknown fp condition code!"); |
| 465 | case ISD::SETEQ: |
| 466 | case ISD::SETOEQ: return Mips::FCOND_OEQ; |
| 467 | case ISD::SETUNE: return Mips::FCOND_UNE; |
| 468 | case ISD::SETLT: |
| 469 | case ISD::SETOLT: return Mips::FCOND_OLT; |
| 470 | case ISD::SETGT: |
| 471 | case ISD::SETOGT: return Mips::FCOND_OGT; |
| 472 | case ISD::SETLE: |
| 473 | case ISD::SETOLE: return Mips::FCOND_OLE; |
| 474 | case ISD::SETGE: |
| 475 | case ISD::SETOGE: return Mips::FCOND_OGE; |
| 476 | case ISD::SETULT: return Mips::FCOND_ULT; |
| 477 | case ISD::SETULE: return Mips::FCOND_ULE; |
| 478 | case ISD::SETUGT: return Mips::FCOND_UGT; |
| 479 | case ISD::SETUGE: return Mips::FCOND_UGE; |
| 480 | case ISD::SETUO: return Mips::FCOND_UN; |
| 481 | case ISD::SETO: return Mips::FCOND_OR; |
| 482 | case ISD::SETNE: |
| 483 | case ISD::SETONE: return Mips::FCOND_ONE; |
| 484 | case ISD::SETUEQ: return Mips::FCOND_UEQ; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
Akira Hatanaka | 9cf0724 | 2013-03-30 01:16:38 +0000 | [diff] [blame] | 489 | /// This function returns true if the floating point conditional branches and |
| 490 | /// conditional moves which use condition code CC should be inverted. |
| 491 | static bool invertFPCondCodeUser(Mips::CondCode CC) { |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 492 | if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT) |
| 493 | return false; |
| 494 | |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 495 | assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) && |
| 496 | "Illegal Condition Code"); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 497 | |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 498 | return true; |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Creates and returns an FPCmp node from a setcc node. |
| 502 | // Returns Op if setcc is not a floating point comparison. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 503 | static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) { |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 504 | // must be a SETCC node |
| 505 | if (Op.getOpcode() != ISD::SETCC) |
| 506 | return Op; |
| 507 | |
| 508 | SDValue LHS = Op.getOperand(0); |
| 509 | |
| 510 | if (!LHS.getValueType().isFloatingPoint()) |
| 511 | return Op; |
| 512 | |
| 513 | SDValue RHS = Op.getOperand(1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 514 | SDLoc DL(Op); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 515 | |
Akira Hatanaka | 0bf3dfb | 2011-04-15 21:00:26 +0000 | [diff] [blame] | 516 | // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of |
| 517 | // node if necessary. |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 518 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| 519 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 520 | return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS, |
Akira Hatanaka | 2fbe90c | 2013-04-18 01:00:46 +0000 | [diff] [blame] | 521 | DAG.getConstant(condCodeToFCC(CC), MVT::i32)); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | // Creates and returns a CMovFPT/F node. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 525 | static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 526 | SDValue False, SDLoc DL) { |
Akira Hatanaka | 9cf0724 | 2013-03-30 01:16:38 +0000 | [diff] [blame] | 527 | ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2)); |
| 528 | bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue()); |
Akira Hatanaka | 407883b | 2013-07-26 20:51:20 +0000 | [diff] [blame] | 529 | SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 530 | |
| 531 | return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL, |
Akira Hatanaka | 407883b | 2013-07-26 20:51:20 +0000 | [diff] [blame] | 532 | True.getValueType(), True, FCC0, False, Cond); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 535 | static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG, |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 536 | TargetLowering::DAGCombinerInfo &DCI, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 537 | const MipsSubtarget *Subtarget) { |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 538 | if (DCI.isBeforeLegalizeOps()) |
| 539 | return SDValue(); |
| 540 | |
| 541 | SDValue SetCC = N->getOperand(0); |
| 542 | |
| 543 | if ((SetCC.getOpcode() != ISD::SETCC) || |
| 544 | !SetCC.getOperand(0).getValueType().isInteger()) |
| 545 | return SDValue(); |
| 546 | |
| 547 | SDValue False = N->getOperand(2); |
| 548 | EVT FalseTy = False.getValueType(); |
| 549 | |
| 550 | if (!FalseTy.isInteger()) |
| 551 | return SDValue(); |
| 552 | |
| 553 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False); |
| 554 | |
| 555 | if (!CN || CN->getZExtValue()) |
| 556 | return SDValue(); |
| 557 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 558 | const SDLoc DL(N); |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 559 | ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get(); |
| 560 | SDValue True = N->getOperand(1); |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 561 | |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 562 | SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0), |
| 563 | SetCC.getOperand(1), ISD::getSetCCInverse(CC, true)); |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 564 | |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 565 | return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True); |
| 566 | } |
| 567 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 568 | static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG, |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 569 | TargetLowering::DAGCombinerInfo &DCI, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 570 | const MipsSubtarget *Subtarget) { |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 571 | // Pattern match EXT. |
| 572 | // $dst = and ((sra or srl) $src , pos), (2**size - 1) |
| 573 | // => ext $dst, $src, size, pos |
Akira Hatanaka | 5663344 | 2011-09-20 23:53:09 +0000 | [diff] [blame] | 574 | if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2()) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 575 | return SDValue(); |
| 576 | |
| 577 | SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1); |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 578 | unsigned ShiftRightOpc = ShiftRight.getOpcode(); |
| 579 | |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 580 | // Op's first operand must be a shift right. |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 581 | if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 582 | return SDValue(); |
| 583 | |
| 584 | // The second operand of the shift must be an immediate. |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 585 | ConstantSDNode *CN; |
| 586 | if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1)))) |
| 587 | return SDValue(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 588 | |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 589 | uint64_t Pos = CN->getZExtValue(); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 590 | uint64_t SMPos, SMSize; |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 591 | |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 592 | // Op's second operand must be a shifted mask. |
| 593 | if (!(CN = dyn_cast<ConstantSDNode>(Mask)) || |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 594 | !isShiftedMask(CN->getZExtValue(), SMPos, SMSize)) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 595 | return SDValue(); |
| 596 | |
| 597 | // Return if the shifted mask does not start at bit 0 or the sum of its size |
| 598 | // and Pos exceeds the word's size. |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 599 | EVT ValTy = N->getValueType(0); |
| 600 | if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits()) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 601 | return SDValue(); |
| 602 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 603 | return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 604 | ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32), |
Akira Hatanaka | 667645f | 2011-08-17 22:59:46 +0000 | [diff] [blame] | 605 | DAG.getConstant(SMSize, MVT::i32)); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 606 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 607 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 608 | static SDValue performORCombine(SDNode *N, SelectionDAG &DAG, |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 609 | TargetLowering::DAGCombinerInfo &DCI, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 610 | const MipsSubtarget *Subtarget) { |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 611 | // Pattern match INS. |
| 612 | // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1), |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 613 | // where mask1 = (2**size - 1) << pos, mask0 = ~mask1 |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 614 | // => ins $dst, $src, size, pos, $src1 |
Akira Hatanaka | 5663344 | 2011-09-20 23:53:09 +0000 | [diff] [blame] | 615 | if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2()) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 616 | return SDValue(); |
| 617 | |
| 618 | SDValue And0 = N->getOperand(0), And1 = N->getOperand(1); |
| 619 | uint64_t SMPos0, SMSize0, SMPos1, SMSize1; |
| 620 | ConstantSDNode *CN; |
| 621 | |
| 622 | // See if Op's first operand matches (and $src1 , mask0). |
| 623 | if (And0.getOpcode() != ISD::AND) |
| 624 | return SDValue(); |
| 625 | |
| 626 | if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) || |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 627 | !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0)) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 628 | return SDValue(); |
| 629 | |
| 630 | // See if Op's second operand matches (and (shl $src, pos), mask1). |
| 631 | if (And1.getOpcode() != ISD::AND) |
| 632 | return SDValue(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 633 | |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 634 | if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) || |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 635 | !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1)) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 636 | return SDValue(); |
| 637 | |
| 638 | // The shift masks must have the same position and size. |
| 639 | if (SMPos0 != SMPos1 || SMSize0 != SMSize1) |
| 640 | return SDValue(); |
| 641 | |
| 642 | SDValue Shl = And1.getOperand(0); |
| 643 | if (Shl.getOpcode() != ISD::SHL) |
| 644 | return SDValue(); |
| 645 | |
| 646 | if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1)))) |
| 647 | return SDValue(); |
| 648 | |
| 649 | unsigned Shamt = CN->getZExtValue(); |
| 650 | |
| 651 | // Return if the shift amount and the first bit position of mask are not the |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 652 | // same. |
Akira Hatanaka | d6bc523 | 2011-12-05 21:26:34 +0000 | [diff] [blame] | 653 | EVT ValTy = N->getValueType(0); |
| 654 | if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits())) |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 655 | return SDValue(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 656 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 657 | return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0), |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 658 | DAG.getConstant(SMPos0, MVT::i32), |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 659 | DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0)); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 660 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 661 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 662 | static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 663 | TargetLowering::DAGCombinerInfo &DCI, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 664 | const MipsSubtarget *Subtarget) { |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 665 | // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt)) |
| 666 | |
| 667 | if (DCI.isBeforeLegalizeOps()) |
| 668 | return SDValue(); |
| 669 | |
| 670 | SDValue Add = N->getOperand(1); |
| 671 | |
| 672 | if (Add.getOpcode() != ISD::ADD) |
| 673 | return SDValue(); |
| 674 | |
| 675 | SDValue Lo = Add.getOperand(1); |
| 676 | |
| 677 | if ((Lo.getOpcode() != MipsISD::Lo) || |
| 678 | (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable)) |
| 679 | return SDValue(); |
| 680 | |
| 681 | EVT ValTy = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 682 | SDLoc DL(N); |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 683 | |
| 684 | SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0), |
| 685 | Add.getOperand(0)); |
| 686 | return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo); |
| 687 | } |
| 688 | |
Bruno Cardoso Lopes | 8e826e6 | 2011-02-10 18:05:10 +0000 | [diff] [blame] | 689 | SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) |
Bruno Cardoso Lopes | 8be7611 | 2011-01-18 19:29:17 +0000 | [diff] [blame] | 690 | const { |
| 691 | SelectionDAG &DAG = DCI.DAG; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 692 | unsigned Opc = N->getOpcode(); |
Bruno Cardoso Lopes | 8be7611 | 2011-01-18 19:29:17 +0000 | [diff] [blame] | 693 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 694 | switch (Opc) { |
Bruno Cardoso Lopes | 8be7611 | 2011-01-18 19:29:17 +0000 | [diff] [blame] | 695 | default: break; |
Bruno Cardoso Lopes | 38b5e86 | 2011-03-04 21:03:24 +0000 | [diff] [blame] | 696 | case ISD::SDIVREM: |
| 697 | case ISD::UDIVREM: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 698 | return performDivRemCombine(N, DAG, DCI, Subtarget); |
Akira Hatanaka | e2bdf7f | 2012-03-08 02:14:24 +0000 | [diff] [blame] | 699 | case ISD::SELECT: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 700 | return performSELECTCombine(N, DAG, DCI, Subtarget); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 701 | case ISD::AND: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 702 | return performANDCombine(N, DAG, DCI, Subtarget); |
Akira Hatanaka | 77b85b6 | 2011-08-17 17:45:08 +0000 | [diff] [blame] | 703 | case ISD::OR: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 704 | return performORCombine(N, DAG, DCI, Subtarget); |
Akira Hatanaka | 8782707 | 2012-06-13 20:33:18 +0000 | [diff] [blame] | 705 | case ISD::ADD: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 706 | return performADDCombine(N, DAG, DCI, Subtarget); |
Bruno Cardoso Lopes | 8be7611 | 2011-01-18 19:29:17 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return SDValue(); |
| 710 | } |
| 711 | |
Akira Hatanaka | b430cec | 2012-09-21 23:58:31 +0000 | [diff] [blame] | 712 | void |
| 713 | MipsTargetLowering::LowerOperationWrapper(SDNode *N, |
| 714 | SmallVectorImpl<SDValue> &Results, |
| 715 | SelectionDAG &DAG) const { |
| 716 | SDValue Res = LowerOperation(SDValue(N, 0), DAG); |
| 717 | |
| 718 | for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I) |
| 719 | Results.push_back(Res.getValue(I)); |
| 720 | } |
| 721 | |
| 722 | void |
| 723 | MipsTargetLowering::ReplaceNodeResults(SDNode *N, |
| 724 | SmallVectorImpl<SDValue> &Results, |
| 725 | SelectionDAG &DAG) const { |
Akira Hatanaka | 13ec481 | 2013-04-30 21:17:07 +0000 | [diff] [blame] | 726 | return LowerOperationWrapper(N, Results, DAG); |
Akira Hatanaka | b430cec | 2012-09-21 23:58:31 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 729 | SDValue MipsTargetLowering:: |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 730 | LowerOperation(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 731 | { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 732 | switch (Op.getOpcode()) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 733 | { |
Akira Hatanaka | 2459afe | 2013-03-30 01:15:17 +0000 | [diff] [blame] | 734 | case ISD::BR_JT: return lowerBR_JT(Op, DAG); |
| 735 | case ISD::BRCOND: return lowerBRCOND(Op, DAG); |
| 736 | case ISD::ConstantPool: return lowerConstantPool(Op, DAG); |
| 737 | case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG); |
| 738 | case ISD::BlockAddress: return lowerBlockAddress(Op, DAG); |
| 739 | case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG); |
| 740 | case ISD::JumpTable: return lowerJumpTable(Op, DAG); |
| 741 | case ISD::SELECT: return lowerSELECT(Op, DAG); |
| 742 | case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG); |
| 743 | case ISD::SETCC: return lowerSETCC(Op, DAG); |
| 744 | case ISD::VASTART: return lowerVASTART(Op, DAG); |
| 745 | case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG); |
| 746 | case ISD::FABS: return lowerFABS(Op, DAG); |
| 747 | case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG); |
| 748 | case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG); |
| 749 | case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG); |
Akira Hatanaka | 2459afe | 2013-03-30 01:15:17 +0000 | [diff] [blame] | 750 | case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG); |
| 751 | case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG); |
| 752 | case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true); |
| 753 | case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false); |
| 754 | case ISD::LOAD: return lowerLOAD(Op, DAG); |
| 755 | case ISD::STORE: return lowerSTORE(Op, DAG); |
Akira Hatanaka | 2459afe | 2013-03-30 01:15:17 +0000 | [diff] [blame] | 756 | case ISD::ADD: return lowerADD(Op, DAG); |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 757 | case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 758 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 759 | return SDValue(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 762 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 763 | // Lower helper functions |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 764 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 765 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 766 | // addLiveIn - This helper function adds the specified physical register to the |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 767 | // MachineFunction as a live in value. It also creates a corresponding |
| 768 | // virtual register for it. |
| 769 | static unsigned |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 770 | addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 771 | { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 772 | unsigned VReg = MF.getRegInfo().createVirtualRegister(RC); |
| 773 | MF.getRegInfo().addLiveIn(PReg, VReg); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 774 | return VReg; |
| 775 | } |
| 776 | |
Akira Hatanaka | f894199 | 2013-05-20 18:07:43 +0000 | [diff] [blame] | 777 | static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI, |
| 778 | MachineBasicBlock &MBB, |
| 779 | const TargetInstrInfo &TII, |
| 780 | bool Is64Bit) { |
| 781 | if (NoZeroDivCheck) |
| 782 | return &MBB; |
| 783 | |
| 784 | // Insert instruction "teq $divisor_reg, $zero, 7". |
| 785 | MachineBasicBlock::iterator I(MI); |
| 786 | MachineInstrBuilder MIB; |
| 787 | MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ)) |
| 788 | .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7); |
| 789 | |
| 790 | // Use the 32-bit sub-register if this is a 64-bit division. |
| 791 | if (Is64Bit) |
| 792 | MIB->getOperand(0).setSubReg(Mips::sub_32); |
| 793 | |
| 794 | return &MBB; |
| 795 | } |
| 796 | |
Akira Hatanaka | 01f7089 | 2012-09-27 02:15:57 +0000 | [diff] [blame] | 797 | MachineBasicBlock * |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 798 | MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 799 | MachineBasicBlock *BB) const { |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 800 | switch (MI->getOpcode()) { |
Reed Kotler | ffbe432 | 2013-02-21 04:22:38 +0000 | [diff] [blame] | 801 | default: |
| 802 | llvm_unreachable("Unexpected instr type to insert"); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 803 | case Mips::ATOMIC_LOAD_ADD_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 804 | return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 805 | case Mips::ATOMIC_LOAD_ADD_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 806 | return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 807 | case Mips::ATOMIC_LOAD_ADD_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 808 | return emitAtomicBinary(MI, BB, 4, Mips::ADDu); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 809 | case Mips::ATOMIC_LOAD_ADD_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 810 | return emitAtomicBinary(MI, BB, 8, Mips::DADDu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 811 | |
| 812 | case Mips::ATOMIC_LOAD_AND_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 813 | return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 814 | case Mips::ATOMIC_LOAD_AND_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 815 | return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 816 | case Mips::ATOMIC_LOAD_AND_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 817 | return emitAtomicBinary(MI, BB, 4, Mips::AND); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 818 | case Mips::ATOMIC_LOAD_AND_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 819 | return emitAtomicBinary(MI, BB, 8, Mips::AND64); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 820 | |
| 821 | case Mips::ATOMIC_LOAD_OR_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 822 | return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 823 | case Mips::ATOMIC_LOAD_OR_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 824 | return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 825 | case Mips::ATOMIC_LOAD_OR_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 826 | return emitAtomicBinary(MI, BB, 4, Mips::OR); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 827 | case Mips::ATOMIC_LOAD_OR_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 828 | return emitAtomicBinary(MI, BB, 8, Mips::OR64); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 829 | |
| 830 | case Mips::ATOMIC_LOAD_XOR_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 831 | return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 832 | case Mips::ATOMIC_LOAD_XOR_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 833 | return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 834 | case Mips::ATOMIC_LOAD_XOR_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 835 | return emitAtomicBinary(MI, BB, 4, Mips::XOR); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 836 | case Mips::ATOMIC_LOAD_XOR_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 837 | return emitAtomicBinary(MI, BB, 8, Mips::XOR64); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 838 | |
| 839 | case Mips::ATOMIC_LOAD_NAND_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 840 | return emitAtomicBinaryPartword(MI, BB, 1, 0, true); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 841 | case Mips::ATOMIC_LOAD_NAND_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 842 | return emitAtomicBinaryPartword(MI, BB, 2, 0, true); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 843 | case Mips::ATOMIC_LOAD_NAND_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 844 | return emitAtomicBinary(MI, BB, 4, 0, true); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 845 | case Mips::ATOMIC_LOAD_NAND_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 846 | return emitAtomicBinary(MI, BB, 8, 0, true); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 847 | |
| 848 | case Mips::ATOMIC_LOAD_SUB_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 849 | return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 850 | case Mips::ATOMIC_LOAD_SUB_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 851 | return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 852 | case Mips::ATOMIC_LOAD_SUB_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 853 | return emitAtomicBinary(MI, BB, 4, Mips::SUBu); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 854 | case Mips::ATOMIC_LOAD_SUB_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 855 | return emitAtomicBinary(MI, BB, 8, Mips::DSUBu); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 856 | |
| 857 | case Mips::ATOMIC_SWAP_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 858 | return emitAtomicBinaryPartword(MI, BB, 1, 0); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 859 | case Mips::ATOMIC_SWAP_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 860 | return emitAtomicBinaryPartword(MI, BB, 2, 0); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 861 | case Mips::ATOMIC_SWAP_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 862 | return emitAtomicBinary(MI, BB, 4, 0); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 863 | case Mips::ATOMIC_SWAP_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 864 | return emitAtomicBinary(MI, BB, 8, 0); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 865 | |
| 866 | case Mips::ATOMIC_CMP_SWAP_I8: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 867 | return emitAtomicCmpSwapPartword(MI, BB, 1); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 868 | case Mips::ATOMIC_CMP_SWAP_I16: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 869 | return emitAtomicCmpSwapPartword(MI, BB, 2); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 870 | case Mips::ATOMIC_CMP_SWAP_I32: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 871 | return emitAtomicCmpSwap(MI, BB, 4); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 872 | case Mips::ATOMIC_CMP_SWAP_I64: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 873 | return emitAtomicCmpSwap(MI, BB, 8); |
Akira Hatanaka | f894199 | 2013-05-20 18:07:43 +0000 | [diff] [blame] | 874 | case Mips::PseudoSDIV: |
| 875 | case Mips::PseudoUDIV: |
| 876 | return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false); |
| 877 | case Mips::PseudoDSDIV: |
| 878 | case Mips::PseudoDUDIV: |
| 879 | return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 880 | } |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 883 | // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and |
| 884 | // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true) |
| 885 | MachineBasicBlock * |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 886 | MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 887 | unsigned Size, unsigned BinOpcode, |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 888 | bool Nand) const { |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 889 | assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary."); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 890 | |
| 891 | MachineFunction *MF = BB->getParent(); |
| 892 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 893 | const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8)); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 894 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 895 | DebugLoc DL = MI->getDebugLoc(); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 896 | unsigned LL, SC, AND, NOR, ZERO, BEQ; |
| 897 | |
| 898 | if (Size == 4) { |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 899 | LL = Mips::LL; |
| 900 | SC = Mips::SC; |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 901 | AND = Mips::AND; |
| 902 | NOR = Mips::NOR; |
| 903 | ZERO = Mips::ZERO; |
| 904 | BEQ = Mips::BEQ; |
| 905 | } |
| 906 | else { |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 907 | LL = Mips::LLD; |
| 908 | SC = Mips::SCD; |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 909 | AND = Mips::AND64; |
| 910 | NOR = Mips::NOR64; |
| 911 | ZERO = Mips::ZERO_64; |
| 912 | BEQ = Mips::BEQ64; |
| 913 | } |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 914 | |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 915 | unsigned OldVal = MI->getOperand(0).getReg(); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 916 | unsigned Ptr = MI->getOperand(1).getReg(); |
| 917 | unsigned Incr = MI->getOperand(2).getReg(); |
| 918 | |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 919 | unsigned StoreVal = RegInfo.createVirtualRegister(RC); |
| 920 | unsigned AndRes = RegInfo.createVirtualRegister(RC); |
| 921 | unsigned Success = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 922 | |
| 923 | // insert new blocks after the current block |
| 924 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 925 | MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 926 | MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 927 | MachineFunction::iterator It = BB; |
| 928 | ++It; |
| 929 | MF->insert(It, loopMBB); |
| 930 | MF->insert(It, exitMBB); |
| 931 | |
| 932 | // Transfer the remainder of BB and its successor edges to exitMBB. |
| 933 | exitMBB->splice(exitMBB->begin(), BB, |
| 934 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 935 | BB->end()); |
| 936 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 937 | |
| 938 | // thisMBB: |
| 939 | // ... |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 940 | // fallthrough --> loopMBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 941 | BB->addSuccessor(loopMBB); |
Akira Hatanaka | 81b4411 | 2011-07-19 17:09:53 +0000 | [diff] [blame] | 942 | loopMBB->addSuccessor(loopMBB); |
| 943 | loopMBB->addSuccessor(exitMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 944 | |
| 945 | // loopMBB: |
| 946 | // ll oldval, 0(ptr) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 947 | // <binop> storeval, oldval, incr |
| 948 | // sc success, storeval, 0(ptr) |
| 949 | // beq success, $0, loopMBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 950 | BB = loopMBB; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 951 | BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 952 | if (Nand) { |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 953 | // and andres, oldval, incr |
| 954 | // nor storeval, $0, andres |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 955 | BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr); |
| 956 | BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 957 | } else if (BinOpcode) { |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 958 | // <binop> storeval, oldval, incr |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 959 | BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 960 | } else { |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 961 | StoreVal = Incr; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 962 | } |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 963 | BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0); |
| 964 | BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 965 | |
| 966 | MI->eraseFromParent(); // The instruction is gone now. |
| 967 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 968 | return exitMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | MachineBasicBlock * |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 972 | MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI, |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 973 | MachineBasicBlock *BB, |
| 974 | unsigned Size, unsigned BinOpcode, |
| 975 | bool Nand) const { |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 976 | assert((Size == 1 || Size == 2) && |
| 977 | "Unsupported size for EmitAtomicBinaryPartial."); |
| 978 | |
| 979 | MachineFunction *MF = BB->getParent(); |
| 980 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
| 981 | const TargetRegisterClass *RC = getRegClassFor(MVT::i32); |
| 982 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 983 | DebugLoc DL = MI->getDebugLoc(); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 984 | |
| 985 | unsigned Dest = MI->getOperand(0).getReg(); |
| 986 | unsigned Ptr = MI->getOperand(1).getReg(); |
| 987 | unsigned Incr = MI->getOperand(2).getReg(); |
| 988 | |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 989 | unsigned AlignedAddr = RegInfo.createVirtualRegister(RC); |
| 990 | unsigned ShiftAmt = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 991 | unsigned Mask = RegInfo.createVirtualRegister(RC); |
| 992 | unsigned Mask2 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 993 | unsigned NewVal = RegInfo.createVirtualRegister(RC); |
| 994 | unsigned OldVal = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 995 | unsigned Incr2 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 996 | unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC); |
| 997 | unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC); |
| 998 | unsigned MaskUpper = RegInfo.createVirtualRegister(RC); |
| 999 | unsigned AndRes = RegInfo.createVirtualRegister(RC); |
| 1000 | unsigned BinOpRes = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | bdd83fe | 2011-07-19 20:56:53 +0000 | [diff] [blame] | 1001 | unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1002 | unsigned StoreVal = RegInfo.createVirtualRegister(RC); |
| 1003 | unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC); |
| 1004 | unsigned SrlRes = RegInfo.createVirtualRegister(RC); |
| 1005 | unsigned SllRes = RegInfo.createVirtualRegister(RC); |
| 1006 | unsigned Success = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1007 | |
| 1008 | // insert new blocks after the current block |
| 1009 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1010 | MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1011 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1012 | MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1013 | MachineFunction::iterator It = BB; |
| 1014 | ++It; |
| 1015 | MF->insert(It, loopMBB); |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1016 | MF->insert(It, sinkMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1017 | MF->insert(It, exitMBB); |
| 1018 | |
| 1019 | // Transfer the remainder of BB and its successor edges to exitMBB. |
| 1020 | exitMBB->splice(exitMBB->begin(), BB, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 1021 | llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1022 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 1023 | |
Akira Hatanaka | 81b4411 | 2011-07-19 17:09:53 +0000 | [diff] [blame] | 1024 | BB->addSuccessor(loopMBB); |
| 1025 | loopMBB->addSuccessor(loopMBB); |
| 1026 | loopMBB->addSuccessor(sinkMBB); |
| 1027 | sinkMBB->addSuccessor(exitMBB); |
| 1028 | |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1029 | // thisMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1030 | // addiu masklsb2,$0,-4 # 0xfffffffc |
| 1031 | // and alignedaddr,ptr,masklsb2 |
| 1032 | // andi ptrlsb2,ptr,3 |
| 1033 | // sll shiftamt,ptrlsb2,3 |
| 1034 | // ori maskupper,$0,255 # 0xff |
| 1035 | // sll mask,maskupper,shiftamt |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1036 | // nor mask2,$0,mask |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1037 | // sll incr2,incr,shiftamt |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1038 | |
| 1039 | int64_t MaskImm = (Size == 1) ? 255 : 65535; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1040 | BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1041 | .addReg(Mips::ZERO).addImm(-4); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1042 | BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1043 | .addReg(Ptr).addReg(MaskLSB2); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1044 | BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3); |
Akira Hatanaka | affed7e | 2013-05-31 03:25:44 +0000 | [diff] [blame] | 1045 | if (Subtarget->isLittle()) { |
| 1046 | BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); |
| 1047 | } else { |
| 1048 | unsigned Off = RegInfo.createVirtualRegister(RC); |
| 1049 | BuildMI(BB, DL, TII->get(Mips::XORi), Off) |
| 1050 | .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); |
| 1051 | BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); |
| 1052 | } |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1053 | BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1054 | .addReg(Mips::ZERO).addImm(MaskImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1055 | BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1056 | .addReg(MaskUpper).addReg(ShiftAmt); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1057 | BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1058 | BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt); |
Bruno Cardoso Lopes | cada2d0 | 2011-05-31 20:25:26 +0000 | [diff] [blame] | 1059 | |
Akira Hatanaka | 0d7d0b5 | 2011-07-18 18:52:12 +0000 | [diff] [blame] | 1060 | // atomic.load.binop |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1061 | // loopMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1062 | // ll oldval,0(alignedaddr) |
| 1063 | // binop binopres,oldval,incr2 |
| 1064 | // and newval,binopres,mask |
| 1065 | // and maskedoldval0,oldval,mask2 |
| 1066 | // or storeval,maskedoldval0,newval |
| 1067 | // sc success,storeval,0(alignedaddr) |
| 1068 | // beq success,$0,loopMBB |
| 1069 | |
Akira Hatanaka | 0d7d0b5 | 2011-07-18 18:52:12 +0000 | [diff] [blame] | 1070 | // atomic.swap |
| 1071 | // loopMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1072 | // ll oldval,0(alignedaddr) |
Akira Hatanaka | 70564a9 | 2011-07-19 18:14:26 +0000 | [diff] [blame] | 1073 | // and newval,incr2,mask |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1074 | // and maskedoldval0,oldval,mask2 |
| 1075 | // or storeval,maskedoldval0,newval |
| 1076 | // sc success,storeval,0(alignedaddr) |
| 1077 | // beq success,$0,loopMBB |
Akira Hatanaka | 0d7d0b5 | 2011-07-18 18:52:12 +0000 | [diff] [blame] | 1078 | |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1079 | BB = loopMBB; |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1080 | BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1081 | if (Nand) { |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1082 | // and andres, oldval, incr2 |
| 1083 | // nor binopres, $0, andres |
| 1084 | // and newval, binopres, mask |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1085 | BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2); |
| 1086 | BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1087 | .addReg(Mips::ZERO).addReg(AndRes); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1088 | BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1089 | } else if (BinOpcode) { |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1090 | // <binop> binopres, oldval, incr2 |
| 1091 | // and newval, binopres, mask |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1092 | BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2); |
| 1093 | BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask); |
Akira Hatanaka | 70564a9 | 2011-07-19 18:14:26 +0000 | [diff] [blame] | 1094 | } else {// atomic.swap |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1095 | // and newval, incr2, mask |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1096 | BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask); |
Akira Hatanaka | 70564a9 | 2011-07-19 18:14:26 +0000 | [diff] [blame] | 1097 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 1098 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1099 | BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1100 | .addReg(OldVal).addReg(Mask2); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1101 | BuildMI(BB, DL, TII->get(Mips::OR), StoreVal) |
Akira Hatanaka | bdd83fe | 2011-07-19 20:56:53 +0000 | [diff] [blame] | 1102 | .addReg(MaskedOldVal0).addReg(NewVal); |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1103 | BuildMI(BB, DL, TII->get(Mips::SC), Success) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1104 | .addReg(StoreVal).addReg(AlignedAddr).addImm(0); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1105 | BuildMI(BB, DL, TII->get(Mips::BEQ)) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1106 | .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1107 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1108 | // sinkMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1109 | // and maskedoldval1,oldval,mask |
| 1110 | // srl srlres,maskedoldval1,shiftamt |
| 1111 | // sll sllres,srlres,24 |
| 1112 | // sra dest,sllres,24 |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1113 | BB = sinkMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1114 | int64_t ShiftImm = (Size == 1) ? 24 : 16; |
Akira Hatanaka | a308c67 | 2011-07-19 03:14:58 +0000 | [diff] [blame] | 1115 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1116 | BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1117 | .addReg(OldVal).addReg(Mask); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1118 | BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1119 | .addReg(MaskedOldVal1).addReg(ShiftAmt); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1120 | BuildMI(BB, DL, TII->get(Mips::SLL), SllRes) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1121 | .addReg(SrlRes).addImm(ShiftImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1122 | BuildMI(BB, DL, TII->get(Mips::SRA), Dest) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1123 | .addReg(SllRes).addImm(ShiftImm); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1124 | |
| 1125 | MI->eraseFromParent(); // The instruction is gone now. |
| 1126 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1127 | return exitMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | MachineBasicBlock * |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1131 | MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI, |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 1132 | MachineBasicBlock *BB, |
| 1133 | unsigned Size) const { |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1134 | assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap."); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1135 | |
| 1136 | MachineFunction *MF = BB->getParent(); |
| 1137 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1138 | const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8)); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1139 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1140 | DebugLoc DL = MI->getDebugLoc(); |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1141 | unsigned LL, SC, ZERO, BNE, BEQ; |
| 1142 | |
| 1143 | if (Size == 4) { |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1144 | LL = Mips::LL; |
| 1145 | SC = Mips::SC; |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1146 | ZERO = Mips::ZERO; |
| 1147 | BNE = Mips::BNE; |
| 1148 | BEQ = Mips::BEQ; |
| 1149 | } |
| 1150 | else { |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1151 | LL = Mips::LLD; |
| 1152 | SC = Mips::SCD; |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1153 | ZERO = Mips::ZERO_64; |
| 1154 | BNE = Mips::BNE64; |
| 1155 | BEQ = Mips::BEQ64; |
| 1156 | } |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1157 | |
| 1158 | unsigned Dest = MI->getOperand(0).getReg(); |
| 1159 | unsigned Ptr = MI->getOperand(1).getReg(); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1160 | unsigned OldVal = MI->getOperand(2).getReg(); |
| 1161 | unsigned NewVal = MI->getOperand(3).getReg(); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1162 | |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1163 | unsigned Success = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1164 | |
| 1165 | // insert new blocks after the current block |
| 1166 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1167 | MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1168 | MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1169 | MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1170 | MachineFunction::iterator It = BB; |
| 1171 | ++It; |
| 1172 | MF->insert(It, loop1MBB); |
| 1173 | MF->insert(It, loop2MBB); |
| 1174 | MF->insert(It, exitMBB); |
| 1175 | |
| 1176 | // Transfer the remainder of BB and its successor edges to exitMBB. |
| 1177 | exitMBB->splice(exitMBB->begin(), BB, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 1178 | llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1179 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 1180 | |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1181 | // thisMBB: |
| 1182 | // ... |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1183 | // fallthrough --> loop1MBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1184 | BB->addSuccessor(loop1MBB); |
Akira Hatanaka | 81b4411 | 2011-07-19 17:09:53 +0000 | [diff] [blame] | 1185 | loop1MBB->addSuccessor(exitMBB); |
| 1186 | loop1MBB->addSuccessor(loop2MBB); |
| 1187 | loop2MBB->addSuccessor(loop1MBB); |
| 1188 | loop2MBB->addSuccessor(exitMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1189 | |
| 1190 | // loop1MBB: |
| 1191 | // ll dest, 0(ptr) |
| 1192 | // bne dest, oldval, exitMBB |
| 1193 | BB = loop1MBB; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1194 | BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0); |
| 1195 | BuildMI(BB, DL, TII->get(BNE)) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1196 | .addReg(Dest).addReg(OldVal).addMBB(exitMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1197 | |
| 1198 | // loop2MBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1199 | // sc success, newval, 0(ptr) |
| 1200 | // beq success, $0, loop1MBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1201 | BB = loop2MBB; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1202 | BuildMI(BB, DL, TII->get(SC), Success) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1203 | .addReg(NewVal).addReg(Ptr).addImm(0); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1204 | BuildMI(BB, DL, TII->get(BEQ)) |
Akira Hatanaka | 5906806 | 2011-11-11 04:14:30 +0000 | [diff] [blame] | 1205 | .addReg(Success).addReg(ZERO).addMBB(loop1MBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1206 | |
| 1207 | MI->eraseFromParent(); // The instruction is gone now. |
| 1208 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1209 | return exitMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | MachineBasicBlock * |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1213 | MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI, |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 1214 | MachineBasicBlock *BB, |
| 1215 | unsigned Size) const { |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1216 | assert((Size == 1 || Size == 2) && |
| 1217 | "Unsupported size for EmitAtomicCmpSwapPartial."); |
| 1218 | |
| 1219 | MachineFunction *MF = BB->getParent(); |
| 1220 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
| 1221 | const TargetRegisterClass *RC = getRegClassFor(MVT::i32); |
| 1222 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1223 | DebugLoc DL = MI->getDebugLoc(); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1224 | |
| 1225 | unsigned Dest = MI->getOperand(0).getReg(); |
| 1226 | unsigned Ptr = MI->getOperand(1).getReg(); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1227 | unsigned CmpVal = MI->getOperand(2).getReg(); |
| 1228 | unsigned NewVal = MI->getOperand(3).getReg(); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1229 | |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1230 | unsigned AlignedAddr = RegInfo.createVirtualRegister(RC); |
| 1231 | unsigned ShiftAmt = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1232 | unsigned Mask = RegInfo.createVirtualRegister(RC); |
| 1233 | unsigned Mask2 = RegInfo.createVirtualRegister(RC); |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1234 | unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC); |
| 1235 | unsigned OldVal = RegInfo.createVirtualRegister(RC); |
| 1236 | unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC); |
| 1237 | unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC); |
| 1238 | unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC); |
| 1239 | unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC); |
| 1240 | unsigned MaskUpper = RegInfo.createVirtualRegister(RC); |
| 1241 | unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC); |
| 1242 | unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC); |
| 1243 | unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC); |
| 1244 | unsigned StoreVal = RegInfo.createVirtualRegister(RC); |
| 1245 | unsigned SrlRes = RegInfo.createVirtualRegister(RC); |
| 1246 | unsigned SllRes = RegInfo.createVirtualRegister(RC); |
| 1247 | unsigned Success = RegInfo.createVirtualRegister(RC); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1248 | |
| 1249 | // insert new blocks after the current block |
| 1250 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1251 | MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1252 | MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB); |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1253 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1254 | MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 1255 | MachineFunction::iterator It = BB; |
| 1256 | ++It; |
| 1257 | MF->insert(It, loop1MBB); |
| 1258 | MF->insert(It, loop2MBB); |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1259 | MF->insert(It, sinkMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1260 | MF->insert(It, exitMBB); |
| 1261 | |
| 1262 | // Transfer the remainder of BB and its successor edges to exitMBB. |
| 1263 | exitMBB->splice(exitMBB->begin(), BB, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 1264 | llvm::next(MachineBasicBlock::iterator(MI)), BB->end()); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1265 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 1266 | |
Akira Hatanaka | 81b4411 | 2011-07-19 17:09:53 +0000 | [diff] [blame] | 1267 | BB->addSuccessor(loop1MBB); |
| 1268 | loop1MBB->addSuccessor(sinkMBB); |
| 1269 | loop1MBB->addSuccessor(loop2MBB); |
| 1270 | loop2MBB->addSuccessor(loop1MBB); |
| 1271 | loop2MBB->addSuccessor(sinkMBB); |
| 1272 | sinkMBB->addSuccessor(exitMBB); |
| 1273 | |
Akira Hatanaka | 70564a9 | 2011-07-19 18:14:26 +0000 | [diff] [blame] | 1274 | // FIXME: computation of newval2 can be moved to loop2MBB. |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1275 | // thisMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1276 | // addiu masklsb2,$0,-4 # 0xfffffffc |
| 1277 | // and alignedaddr,ptr,masklsb2 |
| 1278 | // andi ptrlsb2,ptr,3 |
| 1279 | // sll shiftamt,ptrlsb2,3 |
| 1280 | // ori maskupper,$0,255 # 0xff |
| 1281 | // sll mask,maskupper,shiftamt |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1282 | // nor mask2,$0,mask |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1283 | // andi maskedcmpval,cmpval,255 |
| 1284 | // sll shiftedcmpval,maskedcmpval,shiftamt |
| 1285 | // andi maskednewval,newval,255 |
| 1286 | // sll shiftednewval,maskednewval,shiftamt |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1287 | int64_t MaskImm = (Size == 1) ? 255 : 65535; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1288 | BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1289 | .addReg(Mips::ZERO).addImm(-4); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1290 | BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1291 | .addReg(Ptr).addReg(MaskLSB2); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1292 | BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3); |
Akira Hatanaka | affed7e | 2013-05-31 03:25:44 +0000 | [diff] [blame] | 1293 | if (Subtarget->isLittle()) { |
| 1294 | BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3); |
| 1295 | } else { |
| 1296 | unsigned Off = RegInfo.createVirtualRegister(RC); |
| 1297 | BuildMI(BB, DL, TII->get(Mips::XORi), Off) |
| 1298 | .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2); |
| 1299 | BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3); |
| 1300 | } |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1301 | BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1302 | .addReg(Mips::ZERO).addImm(MaskImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1303 | BuildMI(BB, DL, TII->get(Mips::SLLV), Mask) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1304 | .addReg(MaskUpper).addReg(ShiftAmt); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1305 | BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask); |
| 1306 | BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1307 | .addReg(CmpVal).addImm(MaskImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1308 | BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1309 | .addReg(MaskedCmpVal).addReg(ShiftAmt); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1310 | BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1311 | .addReg(NewVal).addImm(MaskImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1312 | BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1313 | .addReg(MaskedNewVal).addReg(ShiftAmt); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1314 | |
| 1315 | // loop1MBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1316 | // ll oldval,0(alginedaddr) |
| 1317 | // and maskedoldval0,oldval,mask |
| 1318 | // bne maskedoldval0,shiftedcmpval,sinkMBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1319 | BB = loop1MBB; |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1320 | BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1321 | BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1322 | .addReg(OldVal).addReg(Mask); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1323 | BuildMI(BB, DL, TII->get(Mips::BNE)) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1324 | .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1325 | |
| 1326 | // loop2MBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1327 | // and maskedoldval1,oldval,mask2 |
| 1328 | // or storeval,maskedoldval1,shiftednewval |
| 1329 | // sc success,storeval,0(alignedaddr) |
| 1330 | // beq success,$0,loop1MBB |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1331 | BB = loop2MBB; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1332 | BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1333 | .addReg(OldVal).addReg(Mask2); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1334 | BuildMI(BB, DL, TII->get(Mips::OR), StoreVal) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1335 | .addReg(MaskedOldVal1).addReg(ShiftedNewVal); |
Akira Hatanaka | a98a486 | 2013-08-20 21:08:22 +0000 | [diff] [blame] | 1336 | BuildMI(BB, DL, TII->get(Mips::SC), Success) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1337 | .addReg(StoreVal).addReg(AlignedAddr).addImm(0); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1338 | BuildMI(BB, DL, TII->get(Mips::BEQ)) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1339 | .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1340 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1341 | // sinkMBB: |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1342 | // srl srlres,maskedoldval0,shiftamt |
| 1343 | // sll sllres,srlres,24 |
| 1344 | // sra dest,sllres,24 |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1345 | BB = sinkMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1346 | int64_t ShiftImm = (Size == 1) ? 24 : 16; |
Akira Hatanaka | a308c67 | 2011-07-19 03:14:58 +0000 | [diff] [blame] | 1347 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1348 | BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes) |
Akira Hatanaka | 5112243 | 2013-07-01 20:39:53 +0000 | [diff] [blame] | 1349 | .addReg(MaskedOldVal0).addReg(ShiftAmt); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1350 | BuildMI(BB, DL, TII->get(Mips::SLL), SllRes) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1351 | .addReg(SrlRes).addImm(ShiftImm); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1352 | BuildMI(BB, DL, TII->get(Mips::SRA), Dest) |
Akira Hatanaka | 4061da1 | 2011-07-19 20:11:17 +0000 | [diff] [blame] | 1353 | .addReg(SllRes).addImm(ShiftImm); |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1354 | |
| 1355 | MI->eraseFromParent(); // The instruction is gone now. |
| 1356 | |
Akira Hatanaka | 939ece1 | 2011-07-19 03:42:13 +0000 | [diff] [blame] | 1357 | return exitMBB; |
Bruno Cardoso Lopes | 4e694c9 | 2011-05-31 02:54:07 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 1360 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 1361 | // Misc Lower Operation implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 1362 | //===----------------------------------------------------------------------===// |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1363 | SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const { |
Akira Hatanaka | b7656a9 | 2013-03-06 21:32:03 +0000 | [diff] [blame] | 1364 | SDValue Chain = Op.getOperand(0); |
| 1365 | SDValue Table = Op.getOperand(1); |
| 1366 | SDValue Index = Op.getOperand(2); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1367 | SDLoc DL(Op); |
Akira Hatanaka | b7656a9 | 2013-03-06 21:32:03 +0000 | [diff] [blame] | 1368 | EVT PTy = getPointerTy(); |
| 1369 | unsigned EntrySize = |
| 1370 | DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout()); |
| 1371 | |
| 1372 | Index = DAG.getNode(ISD::MUL, DL, PTy, Index, |
| 1373 | DAG.getConstant(EntrySize, PTy)); |
| 1374 | SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table); |
| 1375 | |
| 1376 | EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); |
| 1377 | Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr, |
| 1378 | MachinePointerInfo::getJumpTable(), MemVT, false, false, |
| 1379 | 0); |
| 1380 | Chain = Addr.getValue(1); |
| 1381 | |
| 1382 | if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) { |
| 1383 | // For PIC, the sequence is: |
| 1384 | // BRIND(load(Jumptable + index) + RelocBase) |
| 1385 | // RelocBase can be JumpTable, GOT or some sort of global base. |
| 1386 | Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr, |
| 1387 | getPICJumpTableRelocBase(Table, DAG)); |
| 1388 | } |
| 1389 | |
| 1390 | return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr); |
| 1391 | } |
| 1392 | |
Bruno Cardoso Lopes | d3bdf19 | 2009-05-27 17:23:44 +0000 | [diff] [blame] | 1393 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1394 | lowerBRCOND(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 1395 | { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1396 | // The first operand is the chain, the second is the condition, the third is |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 1397 | // the block to branch to if the condition is true. |
| 1398 | SDValue Chain = Op.getOperand(0); |
| 1399 | SDValue Dest = Op.getOperand(2); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1400 | SDLoc DL(Op); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 1401 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1402 | SDValue CondRes = createFPCmp(DAG, Op.getOperand(1)); |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1404 | // Return if flag is not set by a floating point comparison. |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 1405 | if (CondRes.getOpcode() != MipsISD::FPCmp) |
Bruno Cardoso Lopes | 4b877ca | 2008-07-30 17:06:13 +0000 | [diff] [blame] | 1406 | return Op; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1407 | |
Bruno Cardoso Lopes | 7728377 | 2008-07-31 18:31:28 +0000 | [diff] [blame] | 1408 | SDValue CCNode = CondRes.getOperand(2); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1409 | Mips::CondCode CC = |
| 1410 | (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue(); |
Akira Hatanaka | 9cf0724 | 2013-03-30 01:16:38 +0000 | [diff] [blame] | 1411 | unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T; |
| 1412 | SDValue BrCode = DAG.getConstant(Opc, MVT::i32); |
Akira Hatanaka | 83d8ef1 | 2013-07-26 20:13:47 +0000 | [diff] [blame] | 1413 | SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1414 | return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode, |
Akira Hatanaka | 83d8ef1 | 2013-07-26 20:13:47 +0000 | [diff] [blame] | 1415 | FCC0, Dest, CondRes); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1419 | lowerSELECT(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 1420 | { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1421 | SDValue Cond = createFPCmp(DAG, Op.getOperand(0)); |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1423 | // Return if flag is not set by a floating point comparison. |
Akira Hatanaka | 1d6b38d | 2011-03-31 18:26:17 +0000 | [diff] [blame] | 1424 | if (Cond.getOpcode() != MipsISD::FPCmp) |
| 1425 | return Op; |
Bruno Cardoso Lopes | 739e441 | 2008-08-13 07:13:40 +0000 | [diff] [blame] | 1426 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1427 | return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1428 | SDLoc(Op)); |
Bruno Cardoso Lopes | 6d399bd | 2008-07-29 19:05:28 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Akira Hatanaka | 3fef29d | 2012-07-11 19:32:27 +0000 | [diff] [blame] | 1431 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1432 | lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const |
Akira Hatanaka | 3fef29d | 2012-07-11 19:32:27 +0000 | [diff] [blame] | 1433 | { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1434 | SDLoc DL(Op); |
Akira Hatanaka | 3fef29d | 2012-07-11 19:32:27 +0000 | [diff] [blame] | 1435 | EVT Ty = Op.getOperand(0).getValueType(); |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1436 | SDValue Cond = DAG.getNode(ISD::SETCC, DL, |
| 1437 | getSetCCResultType(*DAG.getContext(), Ty), |
Akira Hatanaka | 3fef29d | 2012-07-11 19:32:27 +0000 | [diff] [blame] | 1438 | Op.getOperand(0), Op.getOperand(1), |
| 1439 | Op.getOperand(4)); |
| 1440 | |
| 1441 | return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2), |
| 1442 | Op.getOperand(3)); |
| 1443 | } |
| 1444 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1445 | SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| 1446 | SDValue Cond = createFPCmp(DAG, Op); |
Akira Hatanaka | 0a40c23 | 2012-03-09 23:46:03 +0000 | [diff] [blame] | 1447 | |
| 1448 | assert(Cond.getOpcode() == MipsISD::FPCmp && |
| 1449 | "Floating point operand expected."); |
| 1450 | |
| 1451 | SDValue True = DAG.getConstant(1, MVT::i32); |
| 1452 | SDValue False = DAG.getConstant(0, MVT::i32); |
| 1453 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1454 | return createCMovFP(DAG, Cond, True, False, SDLoc(Op)); |
Akira Hatanaka | 0a40c23 | 2012-03-09 23:46:03 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1457 | SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1458 | SelectionDAG &DAG) const { |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1459 | // FIXME there isn't actually debug info here |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1460 | SDLoc DL(Op); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 1461 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1462 | |
Akira Hatanaka | a5903ac | 2011-10-11 00:55:05 +0000 | [diff] [blame] | 1463 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) { |
Akira Hatanaka | afc945b | 2012-09-12 23:27:55 +0000 | [diff] [blame] | 1464 | const MipsTargetObjectFile &TLOF = |
| 1465 | (const MipsTargetObjectFile&)getObjFileLowering(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | e3736f8 | 2009-08-13 05:41:27 +0000 | [diff] [blame] | 1467 | // %gp_rel relocation |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1468 | if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1469 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0, |
Bruno Cardoso Lopes | c517cb0 | 2009-09-01 17:27:58 +0000 | [diff] [blame] | 1470 | MipsII::MO_GPREL); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1471 | SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL, |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1472 | DAG.getVTList(MVT::i32), &GA, 1); |
Akira Hatanaka | e7338cd | 2012-08-22 03:18:13 +0000 | [diff] [blame] | 1473 | SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1474 | return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode); |
Chris Lattner | e3736f8 | 2009-08-13 05:41:27 +0000 | [diff] [blame] | 1475 | } |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1476 | |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1477 | // %hi/%lo relocation |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1478 | return getAddrNonPIC(Op, DAG); |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1481 | if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV))) |
| 1482 | return getAddrLocal(Op, DAG, HasMips64); |
| 1483 | |
Akira Hatanaka | f09a037 | 2012-11-21 20:40:38 +0000 | [diff] [blame] | 1484 | if (LargeGOT) |
| 1485 | return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16, |
| 1486 | MipsII::MO_GOT_LO16); |
| 1487 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1488 | return getAddrGlobal(Op, DAG, |
| 1489 | HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16); |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1492 | SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op, |
Bruno Cardoso Lopes | ca8a2aa | 2011-03-04 20:01:52 +0000 | [diff] [blame] | 1493 | SelectionDAG &DAG) const { |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1494 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) |
| 1495 | return getAddrNonPIC(Op, DAG); |
Akira Hatanaka | f48eb53 | 2011-04-25 17:10:45 +0000 | [diff] [blame] | 1496 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1497 | return getAddrLocal(Op, DAG, HasMips64); |
Bruno Cardoso Lopes | ca8a2aa | 2011-03-04 20:01:52 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1500 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1501 | lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1502 | { |
Akira Hatanaka | 3faac0a | 2011-12-14 18:26:41 +0000 | [diff] [blame] | 1503 | // If the relocation model is PIC, use the General Dynamic TLS Model or |
| 1504 | // Local Dynamic TLS model, otherwise use the Initial Exec or |
| 1505 | // Local Exec TLS Model. |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1506 | |
| 1507 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1508 | SDLoc DL(GA); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1509 | const GlobalValue *GV = GA->getGlobal(); |
| 1510 | EVT PtrVT = getPointerTy(); |
| 1511 | |
Hans Wennborg | fd5abd5 | 2012-05-04 09:40:39 +0000 | [diff] [blame] | 1512 | TLSModel::Model model = getTargetMachine().getTLSModel(GV); |
| 1513 | |
| 1514 | if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) { |
Hans Wennborg | 70a07c7 | 2012-06-04 14:02:08 +0000 | [diff] [blame] | 1515 | // General Dynamic and Local Dynamic TLS Model. |
| 1516 | unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM |
| 1517 | : MipsII::MO_TLSGD; |
| 1518 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1519 | SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag); |
| 1520 | SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, |
| 1521 | getGlobalReg(DAG, PtrVT), TGA); |
Akira Hatanaka | 7a7194b | 2011-12-08 21:05:38 +0000 | [diff] [blame] | 1522 | unsigned PtrSize = PtrVT.getSizeInBits(); |
| 1523 | IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize); |
| 1524 | |
Benjamin Kramer | 5eccf67 | 2011-12-11 12:21:34 +0000 | [diff] [blame] | 1525 | SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1526 | |
| 1527 | ArgListTy Args; |
| 1528 | ArgListEntry Entry; |
| 1529 | Entry.Node = Argument; |
Akira Hatanaka | ca07479 | 2011-12-08 20:34:32 +0000 | [diff] [blame] | 1530 | Entry.Ty = PtrTy; |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1531 | Args.push_back(Entry); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 1532 | |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 1533 | TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 1534 | false, false, false, false, 0, CallingConv::C, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1535 | /*IsTailCall=*/false, /*doesNotRet=*/false, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 1536 | /*isReturnValueUsed=*/true, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1537 | TlsGetAddr, Args, DAG, DL); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 1538 | std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1539 | |
Akira Hatanaka | 3faac0a | 2011-12-14 18:26:41 +0000 | [diff] [blame] | 1540 | SDValue Ret = CallResult.first; |
| 1541 | |
Hans Wennborg | fd5abd5 | 2012-05-04 09:40:39 +0000 | [diff] [blame] | 1542 | if (model != TLSModel::LocalDynamic) |
Akira Hatanaka | 3faac0a | 2011-12-14 18:26:41 +0000 | [diff] [blame] | 1543 | return Ret; |
| 1544 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1545 | SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
Akira Hatanaka | 3faac0a | 2011-12-14 18:26:41 +0000 | [diff] [blame] | 1546 | MipsII::MO_DTPREL_HI); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1547 | SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi); |
| 1548 | SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
Akira Hatanaka | 3faac0a | 2011-12-14 18:26:41 +0000 | [diff] [blame] | 1549 | MipsII::MO_DTPREL_LO); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1550 | SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); |
| 1551 | SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret); |
| 1552 | return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo); |
Bruno Cardoso Lopes | d979686 | 2011-05-31 02:53:58 +0000 | [diff] [blame] | 1553 | } |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1554 | |
| 1555 | SDValue Offset; |
Hans Wennborg | fd5abd5 | 2012-05-04 09:40:39 +0000 | [diff] [blame] | 1556 | if (model == TLSModel::InitialExec) { |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1557 | // Initial Exec TLS Model |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1558 | SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1559 | MipsII::MO_GOTTPREL); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1560 | TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT), |
Akira Hatanaka | 648f00c | 2012-02-24 22:34:47 +0000 | [diff] [blame] | 1561 | TGA); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1562 | Offset = DAG.getLoad(PtrVT, DL, |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1563 | DAG.getEntryNode(), TGA, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1564 | false, false, false, 0); |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1565 | } else { |
| 1566 | // Local Exec TLS Model |
Hans Wennborg | fd5abd5 | 2012-05-04 09:40:39 +0000 | [diff] [blame] | 1567 | assert(model == TLSModel::LocalExec); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1568 | SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1569 | MipsII::MO_TPREL_HI); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1570 | SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1571 | MipsII::MO_TPREL_LO); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1572 | SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi); |
| 1573 | SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo); |
| 1574 | Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); |
Akira Hatanaka | 5f7451f | 2011-06-21 01:02:03 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1577 | SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT); |
| 1578 | return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset); |
Bruno Cardoso Lopes | 97843cd | 2008-07-29 19:29:50 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1582 | lowerJumpTable(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 1583 | { |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1584 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) |
| 1585 | return getAddrNonPIC(Op, DAG); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 1586 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1587 | return getAddrLocal(Op, DAG, HasMips64); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1590 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1591 | lowerConstantPool(SDValue Op, SelectionDAG &DAG) const |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 1592 | { |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 1593 | // gp_rel relocation |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1594 | // FIXME: we should reference the constant pool using small data sections, |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1595 | // but the asm printer currently doesn't support this feature without |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1596 | // hacking it. This feature should come soon so we can uncomment the |
Bruno Cardoso Lopes | f33bc43 | 2008-07-28 19:26:25 +0000 | [diff] [blame] | 1597 | // stuff below. |
Eli Friedman | e2c7408 | 2009-08-03 02:22:28 +0000 | [diff] [blame] | 1598 | //if (IsInSmallSection(C->getType())) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1599 | // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP); |
| 1600 | // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1601 | // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); |
Bruno Cardoso Lopes | d71cebf | 2009-11-25 12:17:58 +0000 | [diff] [blame] | 1602 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1603 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) |
| 1604 | return getAddrNonPIC(Op, DAG); |
Bruno Cardoso Lopes | 92e87f2 | 2008-07-23 16:01:50 +0000 | [diff] [blame] | 1605 | |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 1606 | return getAddrLocal(Op, DAG, HasMips64); |
Bruno Cardoso Lopes | 97c2537 | 2008-07-09 04:15:08 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1609 | SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const { |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1610 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1611 | MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>(); |
| 1612 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1613 | SDLoc DL(Op); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1614 | SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 1615 | getPointerTy()); |
Bruno Cardoso Lopes | 6059b85 | 2010-02-06 21:00:02 +0000 | [diff] [blame] | 1616 | |
| 1617 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 1618 | // memory location argument. |
| 1619 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1620 | return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1), |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 1621 | MachinePointerInfo(SV), false, false, 0); |
Bruno Cardoso Lopes | 6059b85 | 2010-02-06 21:00:02 +0000 | [diff] [blame] | 1622 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 1623 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1624 | static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) { |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1625 | EVT TyX = Op.getOperand(0).getValueType(); |
| 1626 | EVT TyY = Op.getOperand(1).getValueType(); |
| 1627 | SDValue Const1 = DAG.getConstant(1, MVT::i32); |
| 1628 | SDValue Const31 = DAG.getConstant(31, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1629 | SDLoc DL(Op); |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1630 | SDValue Res; |
| 1631 | |
| 1632 | // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it |
| 1633 | // to i32. |
| 1634 | SDValue X = (TyX == MVT::f32) ? |
| 1635 | DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) : |
| 1636 | DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0), |
| 1637 | Const1); |
| 1638 | SDValue Y = (TyY == MVT::f32) ? |
| 1639 | DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) : |
| 1640 | DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1), |
| 1641 | Const1); |
| 1642 | |
| 1643 | if (HasR2) { |
| 1644 | // ext E, Y, 31, 1 ; extract bit31 of Y |
| 1645 | // ins X, E, 31, 1 ; insert extracted bit at bit31 of X |
| 1646 | SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1); |
| 1647 | Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X); |
| 1648 | } else { |
| 1649 | // sll SllX, X, 1 |
| 1650 | // srl SrlX, SllX, 1 |
| 1651 | // srl SrlY, Y, 31 |
| 1652 | // sll SllY, SrlX, 31 |
| 1653 | // or Or, SrlX, SllY |
| 1654 | SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1); |
| 1655 | SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1); |
| 1656 | SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31); |
| 1657 | SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31); |
| 1658 | Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY); |
| 1659 | } |
| 1660 | |
| 1661 | if (TyX == MVT::f32) |
| 1662 | return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res); |
| 1663 | |
| 1664 | SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, |
| 1665 | Op.getOperand(0), DAG.getConstant(0, MVT::i32)); |
| 1666 | return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1669 | static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) { |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1670 | unsigned WidthX = Op.getOperand(0).getValueSizeInBits(); |
| 1671 | unsigned WidthY = Op.getOperand(1).getValueSizeInBits(); |
| 1672 | EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY); |
| 1673 | SDValue Const1 = DAG.getConstant(1, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1674 | SDLoc DL(Op); |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1675 | |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1676 | // Bitcast to integer nodes. |
| 1677 | SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0)); |
| 1678 | SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1)); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1679 | |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1680 | if (HasR2) { |
| 1681 | // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y |
| 1682 | // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X |
| 1683 | SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y, |
| 1684 | DAG.getConstant(WidthY - 1, MVT::i32), Const1); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1685 | |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1686 | if (WidthX > WidthY) |
| 1687 | E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E); |
| 1688 | else if (WidthY > WidthX) |
| 1689 | E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1690 | |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1691 | SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E, |
| 1692 | DAG.getConstant(WidthX - 1, MVT::i32), Const1, X); |
| 1693 | return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I); |
| 1694 | } |
| 1695 | |
| 1696 | // (d)sll SllX, X, 1 |
| 1697 | // (d)srl SrlX, SllX, 1 |
| 1698 | // (d)srl SrlY, Y, width(Y)-1 |
| 1699 | // (d)sll SllY, SrlX, width(Y)-1 |
| 1700 | // or Or, SrlX, SllY |
| 1701 | SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1); |
| 1702 | SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1); |
| 1703 | SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y, |
| 1704 | DAG.getConstant(WidthY - 1, MVT::i32)); |
| 1705 | |
| 1706 | if (WidthX > WidthY) |
| 1707 | SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY); |
| 1708 | else if (WidthY > WidthX) |
| 1709 | SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY); |
| 1710 | |
| 1711 | SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY, |
| 1712 | DAG.getConstant(WidthX - 1, MVT::i32)); |
| 1713 | SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY); |
| 1714 | return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 1717 | SDValue |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1718 | MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { |
Akira Hatanaka | 056c51e | 2012-04-11 22:13:04 +0000 | [diff] [blame] | 1719 | if (Subtarget->hasMips64()) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1720 | return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2()); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1721 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1722 | return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2()); |
Akira Hatanaka | 9c3d57c | 2011-05-25 19:32:07 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1725 | static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) { |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1726 | SDValue Res, Const1 = DAG.getConstant(1, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1727 | SDLoc DL(Op); |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1728 | |
| 1729 | // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it |
| 1730 | // to i32. |
| 1731 | SDValue X = (Op.getValueType() == MVT::f32) ? |
| 1732 | DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) : |
| 1733 | DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0), |
| 1734 | Const1); |
| 1735 | |
| 1736 | // Clear MSB. |
| 1737 | if (HasR2) |
| 1738 | Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, |
| 1739 | DAG.getRegister(Mips::ZERO, MVT::i32), |
| 1740 | DAG.getConstant(31, MVT::i32), Const1, X); |
| 1741 | else { |
| 1742 | SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1); |
| 1743 | Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1); |
| 1744 | } |
| 1745 | |
| 1746 | if (Op.getValueType() == MVT::f32) |
| 1747 | return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res); |
| 1748 | |
| 1749 | SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, |
| 1750 | Op.getOperand(0), DAG.getConstant(0, MVT::i32)); |
| 1751 | return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res); |
| 1752 | } |
| 1753 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1754 | static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) { |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1755 | SDValue Res, Const1 = DAG.getConstant(1, MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1756 | SDLoc DL(Op); |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1757 | |
| 1758 | // Bitcast to integer node. |
| 1759 | SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0)); |
| 1760 | |
| 1761 | // Clear MSB. |
| 1762 | if (HasR2) |
| 1763 | Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64, |
| 1764 | DAG.getRegister(Mips::ZERO_64, MVT::i64), |
| 1765 | DAG.getConstant(63, MVT::i32), Const1, X); |
| 1766 | else { |
| 1767 | SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1); |
| 1768 | Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1); |
| 1769 | } |
| 1770 | |
| 1771 | return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res); |
| 1772 | } |
| 1773 | |
| 1774 | SDValue |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1775 | MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const { |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1776 | if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64)) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1777 | return lowerFABS64(Op, DAG, Subtarget->hasMips32r2()); |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1778 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1779 | return lowerFABS32(Op, DAG, Subtarget->hasMips32r2()); |
Akira Hatanaka | c12a6e6 | 2012-04-11 22:49:04 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Akira Hatanaka | 2e59147 | 2011-06-02 00:24:44 +0000 | [diff] [blame] | 1782 | SDValue MipsTargetLowering:: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1783 | lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { |
Bruno Cardoso Lopes | e0b5cfc | 2011-06-16 00:40:02 +0000 | [diff] [blame] | 1784 | // check the depth |
| 1785 | assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) && |
Akira Hatanaka | 0f84382 | 2011-06-07 18:58:42 +0000 | [diff] [blame] | 1786 | "Frame address can only be determined for current frame."); |
Akira Hatanaka | 2e59147 | 2011-06-02 00:24:44 +0000 | [diff] [blame] | 1787 | |
| 1788 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 1789 | MFI->setFrameAddressIsTaken(true); |
| 1790 | EVT VT = Op.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1791 | SDLoc DL(Op); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1792 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, |
Akira Hatanaka | 46ac439 | 2011-11-11 04:11:56 +0000 | [diff] [blame] | 1793 | IsN64 ? Mips::FP_64 : Mips::FP, VT); |
Akira Hatanaka | 2e59147 | 2011-06-02 00:24:44 +0000 | [diff] [blame] | 1794 | return FrameAddr; |
| 1795 | } |
| 1796 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1797 | SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op, |
Akira Hatanaka | ba584fe | 2012-07-11 00:53:32 +0000 | [diff] [blame] | 1798 | SelectionDAG &DAG) const { |
| 1799 | // check the depth |
| 1800 | assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) && |
| 1801 | "Return address can be determined only for current frame."); |
| 1802 | |
| 1803 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1804 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1805 | MVT VT = Op.getSimpleValueType(); |
Akira Hatanaka | ba584fe | 2012-07-11 00:53:32 +0000 | [diff] [blame] | 1806 | unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA; |
| 1807 | MFI->setReturnAddressIsTaken(true); |
| 1808 | |
| 1809 | // Return RA, which contains the return address. Mark it an implicit live-in. |
| 1810 | unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1811 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT); |
Akira Hatanaka | ba584fe | 2012-07-11 00:53:32 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Akira Hatanaka | 544cc21 | 2013-01-30 00:26:49 +0000 | [diff] [blame] | 1814 | // An EH_RETURN is the result of lowering llvm.eh.return which in turn is |
| 1815 | // generated from __builtin_eh_return (offset, handler) |
| 1816 | // The effect of this is to adjust the stack pointer by "offset" |
| 1817 | // and then branch to "handler". |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1818 | SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG) |
Akira Hatanaka | 544cc21 | 2013-01-30 00:26:49 +0000 | [diff] [blame] | 1819 | const { |
| 1820 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1821 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 1822 | |
| 1823 | MipsFI->setCallsEhReturn(); |
| 1824 | SDValue Chain = Op.getOperand(0); |
| 1825 | SDValue Offset = Op.getOperand(1); |
| 1826 | SDValue Handler = Op.getOperand(2); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1827 | SDLoc DL(Op); |
Akira Hatanaka | 544cc21 | 2013-01-30 00:26:49 +0000 | [diff] [blame] | 1828 | EVT Ty = IsN64 ? MVT::i64 : MVT::i32; |
| 1829 | |
| 1830 | // Store stack offset in V1, store jump target in V0. Glue CopyToReg and |
| 1831 | // EH_RETURN nodes, so that instructions are emitted back-to-back. |
| 1832 | unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1; |
| 1833 | unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0; |
| 1834 | Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue()); |
| 1835 | Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1)); |
| 1836 | return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain, |
| 1837 | DAG.getRegister(OffsetReg, Ty), |
| 1838 | DAG.getRegister(AddrReg, getPointerTy()), |
| 1839 | Chain.getValue(1)); |
| 1840 | } |
| 1841 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1842 | SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 1843 | SelectionDAG &DAG) const { |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 1844 | // FIXME: Need pseudo-fence for 'singlethread' fences |
| 1845 | // FIXME: Set SType for weaker fences where supported/appropriate. |
| 1846 | unsigned SType = 0; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1847 | SDLoc DL(Op); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1848 | return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0), |
Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 1849 | DAG.getConstant(SType, MVT::i32)); |
| 1850 | } |
| 1851 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1852 | SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op, |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 1853 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1854 | SDLoc DL(Op); |
Akira Hatanaka | a284acb | 2012-05-09 00:55:21 +0000 | [diff] [blame] | 1855 | SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); |
| 1856 | SDValue Shamt = Op.getOperand(2); |
| 1857 | |
| 1858 | // if shamt < 32: |
| 1859 | // lo = (shl lo, shamt) |
| 1860 | // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt)) |
| 1861 | // else: |
| 1862 | // lo = 0 |
| 1863 | // hi = (shl lo, shamt[4:0]) |
| 1864 | SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, |
| 1865 | DAG.getConstant(-1, MVT::i32)); |
| 1866 | SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, |
| 1867 | DAG.getConstant(1, MVT::i32)); |
| 1868 | SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo, |
| 1869 | Not); |
| 1870 | SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt); |
| 1871 | SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo); |
| 1872 | SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt); |
| 1873 | SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, |
| 1874 | DAG.getConstant(0x20, MVT::i32)); |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 1875 | Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, |
| 1876 | DAG.getConstant(0, MVT::i32), ShiftLeftLo); |
Akira Hatanaka | a284acb | 2012-05-09 00:55:21 +0000 | [diff] [blame] | 1877 | Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or); |
| 1878 | |
| 1879 | SDValue Ops[2] = {Lo, Hi}; |
| 1880 | return DAG.getMergeValues(Ops, 2, DL); |
| 1881 | } |
| 1882 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1883 | SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, |
Akira Hatanaka | a284acb | 2012-05-09 00:55:21 +0000 | [diff] [blame] | 1884 | bool IsSRA) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1885 | SDLoc DL(Op); |
Akira Hatanaka | a284acb | 2012-05-09 00:55:21 +0000 | [diff] [blame] | 1886 | SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1); |
| 1887 | SDValue Shamt = Op.getOperand(2); |
| 1888 | |
| 1889 | // if shamt < 32: |
| 1890 | // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt)) |
| 1891 | // if isSRA: |
| 1892 | // hi = (sra hi, shamt) |
| 1893 | // else: |
| 1894 | // hi = (srl hi, shamt) |
| 1895 | // else: |
| 1896 | // if isSRA: |
| 1897 | // lo = (sra hi, shamt[4:0]) |
| 1898 | // hi = (sra hi, 31) |
| 1899 | // else: |
| 1900 | // lo = (srl hi, shamt[4:0]) |
| 1901 | // hi = 0 |
| 1902 | SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt, |
| 1903 | DAG.getConstant(-1, MVT::i32)); |
| 1904 | SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, |
| 1905 | DAG.getConstant(1, MVT::i32)); |
| 1906 | SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not); |
| 1907 | SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt); |
| 1908 | SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo); |
| 1909 | SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32, |
| 1910 | Hi, Shamt); |
| 1911 | SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt, |
| 1912 | DAG.getConstant(0x20, MVT::i32)); |
| 1913 | SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi, |
| 1914 | DAG.getConstant(31, MVT::i32)); |
| 1915 | Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or); |
| 1916 | Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, |
| 1917 | IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32), |
| 1918 | ShiftRightHi); |
| 1919 | |
| 1920 | SDValue Ops[2] = {Lo, Hi}; |
| 1921 | return DAG.getMergeValues(Ops, 2, DL); |
| 1922 | } |
| 1923 | |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 1924 | static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1925 | SDValue Chain, SDValue Src, unsigned Offset) { |
Akira Hatanaka | 2bd7e53 | 2012-06-13 19:06:08 +0000 | [diff] [blame] | 1926 | SDValue Ptr = LD->getBasePtr(); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1927 | EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT(); |
Akira Hatanaka | 2bd7e53 | 2012-06-13 19:06:08 +0000 | [diff] [blame] | 1928 | EVT BasePtrVT = Ptr.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1929 | SDLoc DL(LD); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1930 | SDVTList VTList = DAG.getVTList(VT, MVT::Other); |
| 1931 | |
| 1932 | if (Offset) |
Akira Hatanaka | 2bd7e53 | 2012-06-13 19:06:08 +0000 | [diff] [blame] | 1933 | Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1934 | DAG.getConstant(Offset, BasePtrVT)); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1935 | |
| 1936 | SDValue Ops[] = { Chain, Ptr, Src }; |
| 1937 | return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT, |
| 1938 | LD->getMemOperand()); |
| 1939 | } |
| 1940 | |
| 1941 | // Expand an unaligned 32 or 64-bit integer load node. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 1942 | SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1943 | LoadSDNode *LD = cast<LoadSDNode>(Op); |
| 1944 | EVT MemVT = LD->getMemoryVT(); |
| 1945 | |
| 1946 | // Return if load is aligned or if MemVT is neither i32 nor i64. |
| 1947 | if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) || |
| 1948 | ((MemVT != MVT::i32) && (MemVT != MVT::i64))) |
| 1949 | return SDValue(); |
| 1950 | |
| 1951 | bool IsLittle = Subtarget->isLittle(); |
| 1952 | EVT VT = Op.getValueType(); |
| 1953 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1954 | SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT); |
| 1955 | |
| 1956 | assert((VT == MVT::i32) || (VT == MVT::i64)); |
| 1957 | |
| 1958 | // Expand |
| 1959 | // (set dst, (i64 (load baseptr))) |
| 1960 | // to |
| 1961 | // (set tmp, (ldl (add baseptr, 7), undef)) |
| 1962 | // (set dst, (ldr baseptr, tmp)) |
| 1963 | if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) { |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 1964 | SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1965 | IsLittle ? 7 : 0); |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 1966 | return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1967 | IsLittle ? 0 : 7); |
| 1968 | } |
| 1969 | |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 1970 | SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1971 | IsLittle ? 3 : 0); |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 1972 | SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1973 | IsLittle ? 0 : 3); |
| 1974 | |
| 1975 | // Expand |
| 1976 | // (set dst, (i32 (load baseptr))) or |
| 1977 | // (set dst, (i64 (sextload baseptr))) or |
| 1978 | // (set dst, (i64 (extload baseptr))) |
| 1979 | // to |
| 1980 | // (set tmp, (lwl (add baseptr, 3), undef)) |
| 1981 | // (set dst, (lwr baseptr, tmp)) |
| 1982 | if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) || |
| 1983 | (ExtType == ISD::EXTLOAD)) |
| 1984 | return LWR; |
| 1985 | |
| 1986 | assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD)); |
| 1987 | |
| 1988 | // Expand |
| 1989 | // (set dst, (i64 (zextload baseptr))) |
| 1990 | // to |
| 1991 | // (set tmp0, (lwl (add baseptr, 3), undef)) |
| 1992 | // (set tmp1, (lwr baseptr, tmp0)) |
| 1993 | // (set tmp2, (shl tmp1, 32)) |
| 1994 | // (set dst, (srl tmp2, 32)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1995 | SDLoc DL(LD); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 1996 | SDValue Const32 = DAG.getConstant(32, MVT::i32); |
| 1997 | SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32); |
Akira Hatanaka | 94ccee2 | 2012-06-04 17:46:29 +0000 | [diff] [blame] | 1998 | SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32); |
| 1999 | SDValue Ops[] = { SRL, LWR.getValue(1) }; |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2000 | return DAG.getMergeValues(Ops, 2, DL); |
| 2001 | } |
| 2002 | |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 2003 | static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2004 | SDValue Chain, unsigned Offset) { |
Akira Hatanaka | 2bd7e53 | 2012-06-13 19:06:08 +0000 | [diff] [blame] | 2005 | SDValue Ptr = SD->getBasePtr(), Value = SD->getValue(); |
| 2006 | EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2007 | SDLoc DL(SD); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2008 | SDVTList VTList = DAG.getVTList(MVT::Other); |
| 2009 | |
| 2010 | if (Offset) |
Akira Hatanaka | 2bd7e53 | 2012-06-13 19:06:08 +0000 | [diff] [blame] | 2011 | Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2012 | DAG.getConstant(Offset, BasePtrVT)); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2013 | |
| 2014 | SDValue Ops[] = { Chain, Value, Ptr }; |
| 2015 | return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT, |
| 2016 | SD->getMemOperand()); |
| 2017 | } |
| 2018 | |
| 2019 | // Expand an unaligned 32 or 64-bit integer store node. |
Akira Hatanaka | 6345143 | 2013-05-16 20:45:17 +0000 | [diff] [blame] | 2020 | static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG, |
| 2021 | bool IsLittle) { |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2022 | SDValue Value = SD->getValue(), Chain = SD->getChain(); |
| 2023 | EVT VT = Value.getValueType(); |
| 2024 | |
| 2025 | // Expand |
| 2026 | // (store val, baseptr) or |
| 2027 | // (truncstore val, baseptr) |
| 2028 | // to |
| 2029 | // (swl val, (add baseptr, 3)) |
| 2030 | // (swr val, baseptr) |
| 2031 | if ((VT == MVT::i32) || SD->isTruncatingStore()) { |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 2032 | SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain, |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2033 | IsLittle ? 3 : 0); |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 2034 | return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | assert(VT == MVT::i64); |
| 2038 | |
| 2039 | // Expand |
| 2040 | // (store val, baseptr) |
| 2041 | // to |
| 2042 | // (sdl val, (add baseptr, 7)) |
| 2043 | // (sdr val, baseptr) |
Akira Hatanaka | fee62c1 | 2013-04-11 19:07:14 +0000 | [diff] [blame] | 2044 | SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0); |
| 2045 | return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7); |
Akira Hatanaka | 1cd0ec0 | 2012-06-02 00:03:49 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2048 | // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr). |
| 2049 | static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) { |
| 2050 | SDValue Val = SD->getValue(); |
| 2051 | |
| 2052 | if (Val.getOpcode() != ISD::FP_TO_SINT) |
| 2053 | return SDValue(); |
| 2054 | |
| 2055 | EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2056 | SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy, |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2057 | Val.getOperand(0)); |
| 2058 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2059 | return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(), |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2060 | SD->getPointerInfo(), SD->isVolatile(), |
| 2061 | SD->isNonTemporal(), SD->getAlignment()); |
| 2062 | } |
| 2063 | |
Akira Hatanaka | 6345143 | 2013-05-16 20:45:17 +0000 | [diff] [blame] | 2064 | SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| 2065 | StoreSDNode *SD = cast<StoreSDNode>(Op); |
| 2066 | EVT MemVT = SD->getMemoryVT(); |
| 2067 | |
| 2068 | // Lower unaligned integer stores. |
| 2069 | if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) && |
| 2070 | ((MemVT == MVT::i32) || (MemVT == MVT::i64))) |
| 2071 | return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle()); |
| 2072 | |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2073 | return lowerFP_TO_SINT_STORE(SD, DAG); |
Akira Hatanaka | 6345143 | 2013-05-16 20:45:17 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2076 | SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const { |
Akira Hatanaka | e90a3bc | 2012-11-07 19:10:58 +0000 | [diff] [blame] | 2077 | if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR |
| 2078 | || cast<ConstantSDNode> |
| 2079 | (Op->getOperand(0).getOperand(0))->getZExtValue() != 0 |
| 2080 | || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET) |
| 2081 | return SDValue(); |
| 2082 | |
| 2083 | // The pattern |
| 2084 | // (add (frameaddr 0), (frame_to_args_offset)) |
| 2085 | // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to |
| 2086 | // (add FrameObject, 0) |
| 2087 | // where FrameObject is a fixed StackObject with offset 0 which points to |
| 2088 | // the old stack pointer. |
| 2089 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 2090 | EVT ValTy = Op->getValueType(0); |
| 2091 | int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false); |
| 2092 | SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2093 | return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr, |
Akira Hatanaka | e90a3bc | 2012-11-07 19:10:58 +0000 | [diff] [blame] | 2094 | DAG.getConstant(0, ValTy)); |
| 2095 | } |
| 2096 | |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2097 | SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op, |
| 2098 | SelectionDAG &DAG) const { |
| 2099 | EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2100 | SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy, |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2101 | Op.getOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2102 | return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc); |
Akira Hatanaka | ae7e7cb | 2013-05-16 21:17:15 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2105 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2106 | // Calling Convention Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2107 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2108 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2109 | //===----------------------------------------------------------------------===// |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2110 | // TODO: Implement a generic logic using tblgen that can support this. |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2111 | // Mips O32 ABI rules: |
| 2112 | // --- |
| 2113 | // i32 - Passed in A0, A1, A2, A3 and stack |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2114 | // f32 - Only passed in f32 registers if no int reg has been used yet to hold |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2115 | // an argument. Otherwise, passed in A1, A2, A3 and stack. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2116 | // f64 - Only passed in two aliased f32 registers if no int reg has been used |
| 2117 | // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2118 | // not used, it must be shadowed. If only A3 is avaiable, shadow it and |
| 2119 | // go to stack. |
Akira Hatanaka | 95b8ae1 | 2011-05-19 18:06:05 +0000 | [diff] [blame] | 2120 | // |
| 2121 | // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack. |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2122 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2123 | |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 2124 | static bool CC_MipsO32(unsigned ValNo, MVT ValVT, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2125 | MVT LocVT, CCValAssign::LocInfo LocInfo, |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2126 | ISD::ArgFlagsTy ArgFlags, CCState &State, |
| 2127 | const uint16_t *F64Regs) { |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2128 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2129 | static const unsigned IntRegsSize=4, FloatRegsSize=2; |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2130 | |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2131 | static const uint16_t IntRegs[] = { |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2132 | Mips::A0, Mips::A1, Mips::A2, Mips::A3 |
| 2133 | }; |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2134 | static const uint16_t F32Regs[] = { |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2135 | Mips::F12, Mips::F14 |
| 2136 | }; |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2137 | |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2138 | // Do not process byval args here. |
| 2139 | if (ArgFlags.isByVal()) |
| 2140 | return true; |
Akira Hatanaka | 4231c7e | 2011-05-24 19:18:33 +0000 | [diff] [blame] | 2141 | |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2142 | // Promote i8 and i16 |
| 2143 | if (LocVT == MVT::i8 || LocVT == MVT::i16) { |
| 2144 | LocVT = MVT::i32; |
| 2145 | if (ArgFlags.isSExt()) |
| 2146 | LocInfo = CCValAssign::SExt; |
| 2147 | else if (ArgFlags.isZExt()) |
| 2148 | LocInfo = CCValAssign::ZExt; |
| 2149 | else |
| 2150 | LocInfo = CCValAssign::AExt; |
| 2151 | } |
| 2152 | |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2153 | unsigned Reg; |
| 2154 | |
Akira Hatanaka | 95b8ae1 | 2011-05-19 18:06:05 +0000 | [diff] [blame] | 2155 | // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following |
| 2156 | // is true: function is vararg, argument is 3rd or higher, there is previous |
| 2157 | // argument which is not f32 or f64. |
| 2158 | bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 |
| 2159 | || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo; |
Akira Hatanaka | a1a7ba8 | 2011-05-19 20:29:48 +0000 | [diff] [blame] | 2160 | unsigned OrigAlign = ArgFlags.getOrigAlign(); |
| 2161 | bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8); |
Akira Hatanaka | 95b8ae1 | 2011-05-19 18:06:05 +0000 | [diff] [blame] | 2162 | |
| 2163 | if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) { |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2164 | Reg = State.AllocateReg(IntRegs, IntRegsSize); |
Akira Hatanaka | a1a7ba8 | 2011-05-19 20:29:48 +0000 | [diff] [blame] | 2165 | // If this is the first part of an i64 arg, |
| 2166 | // the allocated register must be either A0 or A2. |
| 2167 | if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3)) |
| 2168 | Reg = State.AllocateReg(IntRegs, IntRegsSize); |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2169 | LocVT = MVT::i32; |
Akira Hatanaka | 95b8ae1 | 2011-05-19 18:06:05 +0000 | [diff] [blame] | 2170 | } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) { |
| 2171 | // Allocate int register and shadow next int register. If first |
| 2172 | // available register is Mips::A1 or Mips::A3, shadow it too. |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2173 | Reg = State.AllocateReg(IntRegs, IntRegsSize); |
| 2174 | if (Reg == Mips::A1 || Reg == Mips::A3) |
| 2175 | Reg = State.AllocateReg(IntRegs, IntRegsSize); |
| 2176 | State.AllocateReg(IntRegs, IntRegsSize); |
| 2177 | LocVT = MVT::i32; |
Akira Hatanaka | 95b8ae1 | 2011-05-19 18:06:05 +0000 | [diff] [blame] | 2178 | } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) { |
| 2179 | // we are guaranteed to find an available float register |
| 2180 | if (ValVT == MVT::f32) { |
| 2181 | Reg = State.AllocateReg(F32Regs, FloatRegsSize); |
| 2182 | // Shadow int register |
| 2183 | State.AllocateReg(IntRegs, IntRegsSize); |
| 2184 | } else { |
| 2185 | Reg = State.AllocateReg(F64Regs, FloatRegsSize); |
| 2186 | // Shadow int registers |
| 2187 | unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize); |
| 2188 | if (Reg2 == Mips::A1 || Reg2 == Mips::A3) |
| 2189 | State.AllocateReg(IntRegs, IntRegsSize); |
| 2190 | State.AllocateReg(IntRegs, IntRegsSize); |
| 2191 | } |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2192 | } else |
| 2193 | llvm_unreachable("Cannot handle this ValVT."); |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2194 | |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2195 | if (!Reg) { |
| 2196 | unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3, |
| 2197 | OrigAlign); |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2198 | State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2199 | } else |
Bruno Cardoso Lopes | c42fb5f | 2011-03-04 20:27:44 +0000 | [diff] [blame] | 2200 | State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2201 | |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2202 | return false; |
Akira Hatanaka | 2c5d652 | 2011-11-12 02:20:46 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2205 | static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, |
| 2206 | MVT LocVT, CCValAssign::LocInfo LocInfo, |
| 2207 | ISD::ArgFlagsTy ArgFlags, CCState &State) { |
| 2208 | static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 }; |
| 2209 | |
| 2210 | return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); |
| 2211 | } |
| 2212 | |
| 2213 | static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, |
| 2214 | MVT LocVT, CCValAssign::LocInfo LocInfo, |
| 2215 | ISD::ArgFlagsTy ArgFlags, CCState &State) { |
| 2216 | static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 }; |
| 2217 | |
| 2218 | return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs); |
| 2219 | } |
| 2220 | |
Akira Hatanaka | 2c5d652 | 2011-11-12 02:20:46 +0000 | [diff] [blame] | 2221 | #include "MipsGenCallingConv.inc" |
| 2222 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2223 | //===----------------------------------------------------------------------===// |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2224 | // Call Calling Convention Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2225 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2226 | |
Akira Hatanaka | 4231c7e | 2011-05-24 19:18:33 +0000 | [diff] [blame] | 2227 | static const unsigned O32IntRegsSize = 4; |
| 2228 | |
Akira Hatanaka | 373e3a4 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 2229 | // Return next O32 integer argument register. |
| 2230 | static unsigned getNextIntArgReg(unsigned Reg) { |
| 2231 | assert((Reg == Mips::A0) || (Reg == Mips::A2)); |
| 2232 | return (Reg == Mips::A0) ? Mips::A1 : Mips::A3; |
| 2233 | } |
| 2234 | |
Akira Hatanaka | 7d71209 | 2012-10-30 19:23:25 +0000 | [diff] [blame] | 2235 | SDValue |
| 2236 | MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2237 | SDValue Chain, SDValue Arg, SDLoc DL, |
Akira Hatanaka | 7d71209 | 2012-10-30 19:23:25 +0000 | [diff] [blame] | 2238 | bool IsTailCall, SelectionDAG &DAG) const { |
| 2239 | if (!IsTailCall) { |
| 2240 | SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, |
| 2241 | DAG.getIntPtrConstant(Offset)); |
| 2242 | return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false, |
| 2243 | false, 0); |
| 2244 | } |
| 2245 | |
| 2246 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 2247 | int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false); |
| 2248 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 2249 | return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(), |
| 2250 | /*isVolatile=*/ true, false, 0); |
| 2251 | } |
| 2252 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2253 | void MipsTargetLowering:: |
| 2254 | getOpndList(SmallVectorImpl<SDValue> &Ops, |
| 2255 | std::deque< std::pair<unsigned, SDValue> > &RegsToPass, |
| 2256 | bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage, |
| 2257 | CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const { |
| 2258 | // Insert node "GP copy globalreg" before call to function. |
| 2259 | // |
| 2260 | // R_MIPS_CALL* operators (emitted when non-internal functions are called |
| 2261 | // in PIC mode) allow symbols to be resolved via lazy binding. |
| 2262 | // The lazy binding stub requires GP to point to the GOT. |
| 2263 | if (IsPICCall && !InternalLinkage) { |
| 2264 | unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP; |
| 2265 | EVT Ty = IsN64 ? MVT::i64 : MVT::i32; |
| 2266 | RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty))); |
| 2267 | } |
Reed Kotler | 8453b3f | 2013-01-24 04:24:02 +0000 | [diff] [blame] | 2268 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2269 | // Build a sequence of copy-to-reg nodes chained together with token |
| 2270 | // chain and flag operands which copy the outgoing args into registers. |
| 2271 | // The InFlag in necessary since all emitted instructions must be |
| 2272 | // stuck together. |
| 2273 | SDValue InFlag; |
Reed Kotler | 8453b3f | 2013-01-24 04:24:02 +0000 | [diff] [blame] | 2274 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2275 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 2276 | Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first, |
| 2277 | RegsToPass[i].second, InFlag); |
| 2278 | InFlag = Chain.getValue(1); |
| 2279 | } |
Reed Kotler | 8453b3f | 2013-01-24 04:24:02 +0000 | [diff] [blame] | 2280 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2281 | // Add argument registers to the end of the list so that they are |
| 2282 | // known live into the call. |
| 2283 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 2284 | Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first, |
| 2285 | RegsToPass[i].second.getValueType())); |
Reed Kotler | 8453b3f | 2013-01-24 04:24:02 +0000 | [diff] [blame] | 2286 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2287 | // Add a register mask operand representing the call-preserved registers. |
| 2288 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 2289 | const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv); |
| 2290 | assert(Mask && "Missing call preserved mask for calling convention"); |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 2291 | if (Subtarget->inMips16HardFloat()) { |
| 2292 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) { |
| 2293 | llvm::StringRef Sym = G->getGlobal()->getName(); |
| 2294 | Function *F = G->getGlobal()->getParent()->getFunction(Sym); |
| 2295 | if (F->hasFnAttribute("__Mips16RetHelper")) { |
| 2296 | Mask = MipsRegisterInfo::getMips16RetHelperMask(); |
| 2297 | } |
| 2298 | } |
| 2299 | } |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2300 | Ops.push_back(CLI.DAG.getRegisterMask(Mask)); |
| 2301 | |
| 2302 | if (InFlag.getNode()) |
| 2303 | Ops.push_back(InFlag); |
Reed Kotler | 8453b3f | 2013-01-24 04:24:02 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2306 | /// LowerCall - functions arguments are copied from virtual regs to |
Nate Begeman | 5bf4b75 | 2009-01-26 03:15:54 +0000 | [diff] [blame] | 2307 | /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2308 | SDValue |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2309 | MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2310 | SmallVectorImpl<SDValue> &InVals) const { |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2311 | SelectionDAG &DAG = CLI.DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2312 | SDLoc DL = CLI.DL; |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2313 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 2314 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 2315 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
Akira Hatanaka | e2d529a | 2012-07-31 18:46:41 +0000 | [diff] [blame] | 2316 | SDValue Chain = CLI.Chain; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2317 | SDValue Callee = CLI.Callee; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2318 | bool &IsTailCall = CLI.IsTailCall; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2319 | CallingConv::ID CallConv = CLI.CallConv; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2320 | bool IsVarArg = CLI.IsVarArg; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2321 | |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2322 | MachineFunction &MF = DAG.getMachineFunction(); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2323 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Akira Hatanaka | d37776d | 2011-05-20 21:39:54 +0000 | [diff] [blame] | 2324 | const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering(); |
Bruno Cardoso Lopes | c517cb0 | 2009-09-01 17:27:58 +0000 | [diff] [blame] | 2325 | bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2326 | |
| 2327 | // Analyze operands of the call, assigning locations to each operand. |
| 2328 | SmallVector<CCValAssign, 16> ArgLocs; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2329 | CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 2330 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 2331 | MipsCC::SpecialCallingConvType SpecialCallingConv = |
| 2332 | getSpecialCallingConv(Callee); |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2333 | MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo, |
| 2334 | SpecialCallingConv); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2335 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2336 | MipsCCInfo.analyzeCallOperands(Outs, IsVarArg, |
Reed Kotler | c673f9c | 2013-08-30 19:40:56 +0000 | [diff] [blame^] | 2337 | Subtarget->mipsSEUsesSoftFloat(), |
Akira Hatanaka | cb2eafd | 2013-03-05 22:20:28 +0000 | [diff] [blame] | 2338 | Callee.getNode(), CLI.Args); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2339 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2340 | // Get a count of how many bytes are to be pushed on the stack. |
Akira Hatanaka | 3d21c24 | 2011-06-08 17:39:33 +0000 | [diff] [blame] | 2341 | unsigned NextStackOffset = CCInfo.getNextStackOffset(); |
Akira Hatanaka | 480eeb5 | 2012-07-26 23:27:01 +0000 | [diff] [blame] | 2342 | |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 2343 | // Check if it's really possible to do a tail call. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2344 | if (IsTailCall) |
| 2345 | IsTailCall = |
| 2346 | isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset, |
Akira Hatanaka | 2f34d75 | 2012-10-30 20:16:31 +0000 | [diff] [blame] | 2347 | *MF.getInfo<MipsFunctionInfo>()); |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 2348 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2349 | if (IsTailCall) |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 2350 | ++NumTailCalls; |
| 2351 | |
Akira Hatanaka | da7f5f1 | 2011-09-19 20:26:02 +0000 | [diff] [blame] | 2352 | // Chain is the output chain of the last Load/Store or CopyToReg node. |
| 2353 | // ByValChain is the output chain of the last Memcpy node created for copying |
| 2354 | // byval arguments to the stack. |
Akira Hatanaka | 2f34d75 | 2012-10-30 20:16:31 +0000 | [diff] [blame] | 2355 | unsigned StackAlignment = TFL->getStackAlignment(); |
| 2356 | NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment); |
Akira Hatanaka | da7f5f1 | 2011-09-19 20:26:02 +0000 | [diff] [blame] | 2357 | SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true); |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 2358 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2359 | if (!IsTailCall) |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 2360 | Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL); |
Akira Hatanaka | e2d529a | 2012-07-31 18:46:41 +0000 | [diff] [blame] | 2361 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2362 | SDValue StackPtr = DAG.getCopyFromReg(Chain, DL, |
Akira Hatanaka | e2d529a | 2012-07-31 18:46:41 +0000 | [diff] [blame] | 2363 | IsN64 ? Mips::SP_64 : Mips::SP, |
| 2364 | getPointerTy()); |
Akira Hatanaka | 3d21c24 | 2011-06-08 17:39:33 +0000 | [diff] [blame] | 2365 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2366 | // With EABI is it possible to have 16 args on registers. |
Akira Hatanaka | bf6a77b | 2013-01-22 20:05:56 +0000 | [diff] [blame] | 2367 | std::deque< std::pair<unsigned, SDValue> > RegsToPass; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2368 | SmallVector<SDValue, 8> MemOpChains; |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2369 | MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2370 | |
| 2371 | // Walk the register/memloc assignments, inserting copies/loads. |
| 2372 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2373 | SDValue Arg = OutVals[i]; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2374 | CCValAssign &VA = ArgLocs[i]; |
Akira Hatanaka | e42f33b | 2011-10-28 19:49:00 +0000 | [diff] [blame] | 2375 | MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT(); |
Akira Hatanaka | 6df3e7b | 2011-11-12 02:34:50 +0000 | [diff] [blame] | 2376 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| 2377 | |
| 2378 | // ByVal Arg. |
| 2379 | if (Flags.isByVal()) { |
| 2380 | assert(Flags.getByValSize() && |
| 2381 | "ByVal args of size 0 should have been ignored by front-end."); |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2382 | assert(ByValArg != MipsCCInfo.byval_end()); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2383 | assert(!IsTailCall && |
Akira Hatanaka | 2f34d75 | 2012-10-30 20:16:31 +0000 | [diff] [blame] | 2384 | "Do not tail-call optimize if there is a byval argument."); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2385 | passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg, |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2386 | MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle()); |
| 2387 | ++ByValArg; |
Akira Hatanaka | 6df3e7b | 2011-11-12 02:34:50 +0000 | [diff] [blame] | 2388 | continue; |
| 2389 | } |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 2390 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2391 | // Promote the value if needed. |
| 2392 | switch (VA.getLocInfo()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2393 | default: llvm_unreachable("Unknown loc info!"); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2394 | case CCValAssign::Full: |
Akira Hatanaka | e42f33b | 2011-10-28 19:49:00 +0000 | [diff] [blame] | 2395 | if (VA.isRegLoc()) { |
| 2396 | if ((ValVT == MVT::f32 && LocVT == MVT::i32) || |
Akira Hatanaka | cb2eafd | 2013-03-05 22:20:28 +0000 | [diff] [blame] | 2397 | (ValVT == MVT::f64 && LocVT == MVT::i64) || |
| 2398 | (ValVT == MVT::i64 && LocVT == MVT::f64)) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2399 | Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg); |
Akira Hatanaka | e42f33b | 2011-10-28 19:49:00 +0000 | [diff] [blame] | 2400 | else if (ValVT == MVT::f64 && LocVT == MVT::i32) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2401 | SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2402 | Arg, DAG.getConstant(0, MVT::i32)); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2403 | SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, |
Akira Hatanaka | 0bf3dfb | 2011-04-15 21:00:26 +0000 | [diff] [blame] | 2404 | Arg, DAG.getConstant(1, MVT::i32)); |
Akira Hatanaka | 99a2e98 | 2011-04-15 19:52:08 +0000 | [diff] [blame] | 2405 | if (!Subtarget->isLittle()) |
| 2406 | std::swap(Lo, Hi); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 2407 | unsigned LocRegLo = VA.getLocReg(); |
Akira Hatanaka | 373e3a4 | 2011-09-23 00:58:33 +0000 | [diff] [blame] | 2408 | unsigned LocRegHigh = getNextIntArgReg(LocRegLo); |
| 2409 | RegsToPass.push_back(std::make_pair(LocRegLo, Lo)); |
| 2410 | RegsToPass.push_back(std::make_pair(LocRegHigh, Hi)); |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2411 | continue; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2412 | } |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2413 | } |
| 2414 | break; |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2415 | case CCValAssign::SExt: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2416 | Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2417 | break; |
| 2418 | case CCValAssign::ZExt: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2419 | Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | case CCValAssign::AExt: |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2422 | Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2423 | break; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2424 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Arguments that can be passed on register must be kept at |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 2427 | // RegsToPass vector |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2428 | if (VA.isRegLoc()) { |
| 2429 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2430 | continue; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2431 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2432 | |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2433 | // Register can't get to this point... |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2434 | assert(VA.isMemLoc()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2435 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2436 | // emit ISD::STORE whichs stores the |
Chris Lattner | e0b1215 | 2008-03-17 06:57:02 +0000 | [diff] [blame] | 2437 | // parameter value to a stack Location |
Akira Hatanaka | 2f34d75 | 2012-10-30 20:16:31 +0000 | [diff] [blame] | 2438 | MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(), |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2439 | Chain, Arg, DL, IsTailCall, DAG)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2442 | // Transform all store nodes into one single node because all store |
| 2443 | // nodes are independent of each other. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2444 | if (!MemOpChains.empty()) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2445 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2446 | &MemOpChains[0], MemOpChains.size()); |
| 2447 | |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 2448 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2449 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 2450 | // node so that legalize doesn't hack it. |
Akira Hatanaka | e42f33b | 2011-10-28 19:49:00 +0000 | [diff] [blame] | 2451 | bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25 |
Akira Hatanaka | ed185da | 2012-12-13 03:17:29 +0000 | [diff] [blame] | 2452 | bool GlobalOrExternal = false, InternalLinkage = false; |
Akira Hatanaka | 9777e7a | 2011-04-07 19:51:44 +0000 | [diff] [blame] | 2453 | SDValue CalleeLo; |
Akira Hatanaka | f49fde2 | 2011-04-04 17:11:07 +0000 | [diff] [blame] | 2454 | |
| 2455 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2456 | if (IsPICCall) { |
Akira Hatanaka | ed185da | 2012-12-13 03:17:29 +0000 | [diff] [blame] | 2457 | InternalLinkage = G->getGlobal()->hasInternalLinkage(); |
| 2458 | |
| 2459 | if (InternalLinkage) |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2460 | Callee = getAddrLocal(Callee, DAG, HasMips64); |
Akira Hatanaka | f09a037 | 2012-11-21 20:40:38 +0000 | [diff] [blame] | 2461 | else if (LargeGOT) |
| 2462 | Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16, |
| 2463 | MipsII::MO_CALL_LO16); |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2464 | else |
| 2465 | Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL); |
| 2466 | } else |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2467 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0, |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2468 | MipsII::MO_NO_FLAG); |
Akira Hatanaka | 0dca945 | 2011-12-09 01:45:12 +0000 | [diff] [blame] | 2469 | GlobalOrExternal = true; |
Akira Hatanaka | f49fde2 | 2011-04-04 17:11:07 +0000 | [diff] [blame] | 2470 | } |
| 2471 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Akira Hatanaka | f09a037 | 2012-11-21 20:40:38 +0000 | [diff] [blame] | 2472 | if (!IsN64 && !IsPIC) // !N64 && static |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2473 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(), |
| 2474 | MipsII::MO_NO_FLAG); |
Akira Hatanaka | f09a037 | 2012-11-21 20:40:38 +0000 | [diff] [blame] | 2475 | else if (LargeGOT) |
| 2476 | Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16, |
| 2477 | MipsII::MO_CALL_LO16); |
Akira Hatanaka | 6068932 | 2013-02-22 21:10:03 +0000 | [diff] [blame] | 2478 | else // N64 || PIC |
Akira Hatanaka | d43e06d | 2012-11-21 20:30:40 +0000 | [diff] [blame] | 2479 | Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL); |
| 2480 | |
Akira Hatanaka | 0dca945 | 2011-12-09 01:45:12 +0000 | [diff] [blame] | 2481 | GlobalOrExternal = true; |
Akira Hatanaka | f49fde2 | 2011-04-04 17:11:07 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Akira Hatanaka | bf6a77b | 2013-01-22 20:05:56 +0000 | [diff] [blame] | 2484 | SmallVector<SDValue, 8> Ops(1, Chain); |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2485 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Akira Hatanaka | bf6a77b | 2013-01-22 20:05:56 +0000 | [diff] [blame] | 2486 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2487 | getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage, |
| 2488 | CLI, Callee, Chain); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2489 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2490 | if (IsTailCall) |
| 2491 | return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size()); |
Akira Hatanaka | 2b861be | 2012-10-19 21:47:33 +0000 | [diff] [blame] | 2492 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2493 | Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size()); |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 2494 | SDValue InFlag = Chain.getValue(1); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2495 | |
Bruno Cardoso Lopes | 3ed6f87 | 2010-01-30 18:32:07 +0000 | [diff] [blame] | 2496 | // Create the CALLSEQ_END node. |
Akira Hatanaka | 480eeb5 | 2012-07-26 23:27:01 +0000 | [diff] [blame] | 2497 | Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal, |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 2498 | DAG.getIntPtrConstant(0, true), InFlag, DL); |
Bruno Cardoso Lopes | 3ed6f87 | 2010-01-30 18:32:07 +0000 | [diff] [blame] | 2499 | InFlag = Chain.getValue(1); |
| 2500 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2501 | // Handle result values, copying them out of physregs into vregs that we |
| 2502 | // return. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2503 | return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, |
| 2504 | Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2507 | /// LowerCallResult - Lower the result values of a call into the |
| 2508 | /// appropriate copies out of appropriate physical registers. |
| 2509 | SDValue |
| 2510 | MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2511 | CallingConv::ID CallConv, bool IsVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2512 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2513 | SDLoc DL, SelectionDAG &DAG, |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2514 | SmallVectorImpl<SDValue> &InVals, |
| 2515 | const SDNode *CallNode, |
| 2516 | const Type *RetTy) const { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2517 | // Assign locations to each value returned by this call. |
| 2518 | SmallVector<CCValAssign, 16> RVLocs; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2519 | CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), |
Akira Hatanaka | 864f660 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 2520 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2521 | MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2522 | |
Reed Kotler | c673f9c | 2013-08-30 19:40:56 +0000 | [diff] [blame^] | 2523 | MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(), |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2524 | CallNode, RetTy); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2525 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2526 | // Copy all of the result registers out of their specified physreg. |
| 2527 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2528 | SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(), |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2529 | RVLocs[i].getLocVT(), InFlag); |
| 2530 | Chain = Val.getValue(1); |
| 2531 | InFlag = Val.getValue(2); |
| 2532 | |
| 2533 | if (RVLocs[i].getValVT() != RVLocs[i].getLocVT()) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2534 | Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val); |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2535 | |
| 2536 | InVals.push_back(Val); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2537 | } |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 2538 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2539 | return Chain; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2542 | //===----------------------------------------------------------------------===// |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2543 | // Formal Arguments Calling Convention Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2544 | //===----------------------------------------------------------------------===// |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2545 | /// LowerFormalArguments - transform physical registers into virtual registers |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2546 | /// and generate load operations for arguments places on the stack. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2547 | SDValue |
| 2548 | MipsTargetLowering::LowerFormalArguments(SDValue Chain, |
Akira Hatanaka | 0bf3dfb | 2011-04-15 21:00:26 +0000 | [diff] [blame] | 2549 | CallingConv::ID CallConv, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2550 | bool IsVarArg, |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 2551 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2552 | SDLoc DL, SelectionDAG &DAG, |
Akira Hatanaka | 0bf3dfb | 2011-04-15 21:00:26 +0000 | [diff] [blame] | 2553 | SmallVectorImpl<SDValue> &InVals) |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2554 | const { |
Bruno Cardoso Lopes | f7f3b50 | 2008-08-04 07:12:52 +0000 | [diff] [blame] | 2555 | MachineFunction &MF = DAG.getMachineFunction(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2556 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bruno Cardoso Lopes | a2b1bb5 | 2007-08-28 05:08:16 +0000 | [diff] [blame] | 2557 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2558 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2559 | MipsFI->setVarArgsFrameIndex(0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2560 | |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2561 | // Used with vargs to acumulate store chains. |
| 2562 | std::vector<SDValue> OutChains; |
| 2563 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2564 | // Assign locations to all of the incoming arguments. |
| 2565 | SmallVector<CCValAssign, 16> ArgLocs; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2566 | CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), |
Akira Hatanaka | 8209968 | 2011-12-19 19:52:25 +0000 | [diff] [blame] | 2567 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2568 | MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo); |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 2569 | Function::const_arg_iterator FuncArg = |
| 2570 | DAG.getMachineFunction().getFunction()->arg_begin(); |
Reed Kotler | c673f9c | 2013-08-30 19:40:56 +0000 | [diff] [blame^] | 2571 | bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat(); |
Bruno Cardoso Lopes | 2ab22d1 | 2007-07-11 23:16:16 +0000 | [diff] [blame] | 2572 | |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 2573 | MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg); |
Akira Hatanaka | b33b34a | 2012-10-30 19:37:25 +0000 | [diff] [blame] | 2574 | MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(), |
| 2575 | MipsCCInfo.hasByValArg()); |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2576 | |
Akira Hatanaka | 4618e0b | 2012-10-27 00:44:39 +0000 | [diff] [blame] | 2577 | unsigned CurArgIdx = 0; |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2578 | MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin(); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2579 | |
Akira Hatanaka | 4618e0b | 2012-10-27 00:44:39 +0000 | [diff] [blame] | 2580 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2581 | CCValAssign &VA = ArgLocs[i]; |
Akira Hatanaka | 4618e0b | 2012-10-27 00:44:39 +0000 | [diff] [blame] | 2582 | std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx); |
| 2583 | CurArgIdx = Ins[i].OrigArgIndex; |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2584 | EVT ValVT = VA.getValVT(); |
Akira Hatanaka | 3a5257d | 2011-11-12 02:29:58 +0000 | [diff] [blame] | 2585 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
| 2586 | bool IsRegLoc = VA.isRegLoc(); |
| 2587 | |
| 2588 | if (Flags.isByVal()) { |
| 2589 | assert(Flags.getByValSize() && |
| 2590 | "ByVal args of size 0 should have been ignored by front-end."); |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2591 | assert(ByValArg != MipsCCInfo.byval_end()); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2592 | copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg, |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2593 | MipsCCInfo, *ByValArg); |
| 2594 | ++ByValArg; |
Akira Hatanaka | 3a5257d | 2011-11-12 02:29:58 +0000 | [diff] [blame] | 2595 | continue; |
| 2596 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2597 | |
| 2598 | // Arguments stored on registers |
Akira Hatanaka | 3a5257d | 2011-11-12 02:29:58 +0000 | [diff] [blame] | 2599 | if (IsRegLoc) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2600 | EVT RegVT = VA.getLocVT(); |
Akira Hatanaka | b4d8d31 | 2011-05-24 00:23:52 +0000 | [diff] [blame] | 2601 | unsigned ArgReg = VA.getLocReg(); |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 2602 | const TargetRegisterClass *RC; |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2603 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2604 | if (RegVT == MVT::i32) |
Reed Kotler | bacbf1c | 2012-12-20 06:06:35 +0000 | [diff] [blame] | 2605 | RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass : |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2606 | &Mips::GPR32RegClass; |
Akira Hatanaka | 9593484 | 2011-09-24 01:34:44 +0000 | [diff] [blame] | 2607 | else if (RegVT == MVT::i64) |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2608 | RC = &Mips::GPR64RegClass; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2609 | else if (RegVT == MVT::f32) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 2610 | RC = &Mips::FGR32RegClass; |
Akira Hatanaka | 09dd60f | 2011-09-26 21:37:50 +0000 | [diff] [blame] | 2611 | else if (RegVT == MVT::f64) |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2612 | RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass : |
| 2613 | &Mips::AFGR64RegClass; |
Akira Hatanaka | 09dd60f | 2011-09-26 21:37:50 +0000 | [diff] [blame] | 2614 | else |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2615 | llvm_unreachable("RegVT not supported by FormalArguments Lowering"); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2616 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2617 | // Transform the arguments stored on |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2618 | // physical registers into virtual ones |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2619 | unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC); |
| 2620 | SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2621 | |
| 2622 | // If this is an 8 or 16-bit value, it has been passed promoted |
| 2623 | // to 32 bits. Insert an assert[sz]ext to capture this, then |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2624 | // truncate to the right size. |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2625 | if (VA.getLocInfo() != CCValAssign::Full) { |
Chris Lattner | d401507 | 2009-03-26 05:28:14 +0000 | [diff] [blame] | 2626 | unsigned Opcode = 0; |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2627 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 2628 | Opcode = ISD::AssertSext; |
| 2629 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 2630 | Opcode = ISD::AssertZext; |
Chris Lattner | d401507 | 2009-03-26 05:28:14 +0000 | [diff] [blame] | 2631 | if (Opcode) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2632 | ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue, |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2633 | DAG.getValueType(ValVT)); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2634 | ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue); |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 2637 | // Handle floating point arguments passed in integer registers and |
| 2638 | // long double arguments passed in floating point registers. |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2639 | if ((RegVT == MVT::i32 && ValVT == MVT::f32) || |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 2640 | (RegVT == MVT::i64 && ValVT == MVT::f64) || |
| 2641 | (RegVT == MVT::f64 && ValVT == MVT::i64)) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2642 | ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue); |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2643 | else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2644 | unsigned Reg2 = addLiveIn(DAG.getMachineFunction(), |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2645 | getNextIntArgReg(ArgReg), RC); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2646 | SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT); |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2647 | if (!Subtarget->isLittle()) |
| 2648 | std::swap(ArgValue, ArgValue2); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2649 | ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, |
Akira Hatanaka | feaa4c3 | 2011-10-28 19:55:48 +0000 | [diff] [blame] | 2650 | ArgValue, ArgValue2); |
Bruno Cardoso Lopes | b53db4f | 2009-03-19 02:12:28 +0000 | [diff] [blame] | 2651 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2652 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2653 | InVals.push_back(ArgValue); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2654 | } else { // VA.isRegLoc() |
| 2655 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2656 | // sanity check |
| 2657 | assert(VA.isMemLoc()); |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2658 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2659 | // The stack pointer offset is relative to the caller stack frame. |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2660 | int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8, |
Akira Hatanaka | b4d8d31 | 2011-05-24 00:23:52 +0000 | [diff] [blame] | 2661 | VA.getLocMemOffset(), true); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2662 | |
| 2663 | // Create load nodes to retrieve arguments from the stack |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2664 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2665 | InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN, |
Akira Hatanaka | fe30a9b | 2012-10-27 00:29:43 +0000 | [diff] [blame] | 2666 | MachinePointerInfo::getFixedStack(FI), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2667 | false, false, false, 0)); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2668 | } |
| 2669 | } |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2670 | |
| 2671 | // The mips ABIs for returning structs by value requires that we copy |
| 2672 | // the sret argument into $v0 for the return. Save the argument into |
| 2673 | // a virtual register so that we can access it from the return points. |
| 2674 | if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 2675 | unsigned Reg = MipsFI->getSRetReturnReg(); |
| 2676 | if (!Reg) { |
Akira Hatanaka | 30580ce | 2012-10-19 22:11:40 +0000 | [diff] [blame] | 2677 | Reg = MF.getRegInfo(). |
| 2678 | createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32)); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2679 | MipsFI->setSRetReturnReg(Reg); |
| 2680 | } |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2681 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]); |
| 2682 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2685 | if (IsVarArg) |
| 2686 | writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG); |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2687 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2688 | // All stores are grouped in one node to allow the matching between |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2689 | // the size of Ins and InVals. This only happens when on varg functions |
| 2690 | if (!OutChains.empty()) { |
| 2691 | OutChains.push_back(Chain); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2692 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
Bruno Cardoso Lopes | b37a742 | 2010-02-06 19:20:49 +0000 | [diff] [blame] | 2693 | &OutChains[0], OutChains.size()); |
| 2694 | } |
| 2695 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2696 | return Chain; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2699 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2700 | // Return Value Calling Convention Implementation |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2701 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2702 | |
Akira Hatanaka | 97d9f08 | 2012-10-10 01:27:09 +0000 | [diff] [blame] | 2703 | bool |
| 2704 | MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2705 | MachineFunction &MF, bool IsVarArg, |
Akira Hatanaka | 97d9f08 | 2012-10-10 01:27:09 +0000 | [diff] [blame] | 2706 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 2707 | LLVMContext &Context) const { |
| 2708 | SmallVector<CCValAssign, 16> RVLocs; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2709 | CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), |
Akira Hatanaka | 97d9f08 | 2012-10-10 01:27:09 +0000 | [diff] [blame] | 2710 | RVLocs, Context); |
| 2711 | return CCInfo.CheckReturn(Outs, RetCC_Mips); |
| 2712 | } |
| 2713 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2714 | SDValue |
| 2715 | MipsTargetLowering::LowerReturn(SDValue Chain, |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2716 | CallingConv::ID CallConv, bool IsVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2717 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2718 | const SmallVectorImpl<SDValue> &OutVals, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2719 | SDLoc DL, SelectionDAG &DAG) const { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2720 | // CCValAssign - represent the assignment of |
| 2721 | // the return value to a location |
| 2722 | SmallVector<CCValAssign, 16> RVLocs; |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2723 | MachineFunction &MF = DAG.getMachineFunction(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2724 | |
| 2725 | // CCState - Info about the registers and stack slot. |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2726 | CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs, |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2727 | *DAG.getContext()); |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 2728 | MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2729 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2730 | // Analyze return values. |
Reed Kotler | c673f9c | 2013-08-30 19:40:56 +0000 | [diff] [blame^] | 2731 | MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(), |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2732 | MF.getFunction()->getReturnType()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2733 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2734 | SDValue Flag; |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2735 | SmallVector<SDValue, 4> RetOps(1, Chain); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Copy the result values into the output registers. |
| 2738 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2739 | SDValue Val = OutVals[i]; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2740 | CCValAssign &VA = RVLocs[i]; |
| 2741 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 2742 | |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2743 | if (RVLocs[i].getValVT() != RVLocs[i].getLocVT()) |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2744 | Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val); |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2745 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2746 | Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2747 | |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2748 | // Guarantee that all emitted copies are stuck together with flags. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2749 | Flag = Chain.getValue(1); |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2750 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2753 | // The mips ABIs for returning structs by value requires that we copy |
| 2754 | // the sret argument into $v0 for the return. We saved the argument into |
| 2755 | // a virtual register in the entry block, so now we copy the value out |
| 2756 | // and into $v0. |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 2757 | if (MF.getFunction()->hasStructRetAttr()) { |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2758 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 2759 | unsigned Reg = MipsFI->getSRetReturnReg(); |
| 2760 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2761 | if (!Reg) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2762 | llvm_unreachable("sret virtual register not created in the entry block"); |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2763 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy()); |
Akira Hatanaka | 2ef5bd3 | 2012-10-24 02:10:54 +0000 | [diff] [blame] | 2764 | unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0; |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2765 | |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2766 | Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2767 | Flag = Chain.getValue(1); |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2768 | RetOps.push_back(DAG.getRegister(V0, getPointerTy())); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2771 | RetOps[0] = Chain; // Update chain. |
Akira Hatanaka | 182ef6f | 2012-07-10 00:19:06 +0000 | [diff] [blame] | 2772 | |
Jakob Stoklund Olesen | d073596 | 2013-02-05 18:12:03 +0000 | [diff] [blame] | 2773 | // Add the flag if we have it. |
| 2774 | if (Flag.getNode()) |
| 2775 | RetOps.push_back(Flag); |
| 2776 | |
| 2777 | // Return on Mips is always a "jr $ra" |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 2778 | return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2779 | } |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2780 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2781 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2782 | // Mips Inline Assembly Support |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 2783 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2784 | |
| 2785 | /// getConstraintType - Given a constraint letter, return the type of |
| 2786 | /// constraint it is for this target. |
| 2787 | MipsTargetLowering::ConstraintType MipsTargetLowering:: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2788 | getConstraintType(const std::string &Constraint) const |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2789 | { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2790 | // Mips specific constrainy |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2791 | // GCC config/mips/constraints.md |
| 2792 | // |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2793 | // 'd' : An address register. Equivalent to r |
| 2794 | // unless generating MIPS16 code. |
| 2795 | // 'y' : Equivalent to r; retained for |
| 2796 | // backwards compatibility. |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2797 | // 'c' : A register suitable for use in an indirect |
| 2798 | // jump. This will always be $25 for -mabicalls. |
Eric Christopher | af97f73 | 2012-05-07 06:25:19 +0000 | [diff] [blame] | 2799 | // 'l' : The lo register. 1 word storage. |
| 2800 | // 'x' : The hilo register pair. Double word storage. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2801 | if (Constraint.size() == 1) { |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2802 | switch (Constraint[0]) { |
| 2803 | default : break; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2804 | case 'd': |
| 2805 | case 'y': |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2806 | case 'f': |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2807 | case 'c': |
Eric Christopher | 4adbefe | 2012-05-07 06:25:15 +0000 | [diff] [blame] | 2808 | case 'l': |
Eric Christopher | af97f73 | 2012-05-07 06:25:19 +0000 | [diff] [blame] | 2809 | case 'x': |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2810 | return C_RegisterClass; |
Jack Carter | 0b9675d | 2013-03-04 21:33:15 +0000 | [diff] [blame] | 2811 | case 'R': |
| 2812 | return C_Memory; |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | return TargetLowering::getConstraintType(Constraint); |
| 2816 | } |
| 2817 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 2818 | /// Examine constraint type and operand type and determine a weight value. |
| 2819 | /// This object must already have been set up with the operand type |
| 2820 | /// and the current alternative constraint selected. |
| 2821 | TargetLowering::ConstraintWeight |
| 2822 | MipsTargetLowering::getSingleConstraintMatchWeight( |
| 2823 | AsmOperandInfo &info, const char *constraint) const { |
| 2824 | ConstraintWeight weight = CW_Invalid; |
| 2825 | Value *CallOperandVal = info.CallOperandVal; |
| 2826 | // If we don't have a value, we can't do a match, |
| 2827 | // but allow it at the lowest weight. |
| 2828 | if (CallOperandVal == NULL) |
| 2829 | return CW_Default; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2830 | Type *type = CallOperandVal->getType(); |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 2831 | // Look at the constraint type. |
| 2832 | switch (*constraint) { |
| 2833 | default: |
| 2834 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 2835 | break; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2836 | case 'd': |
| 2837 | case 'y': |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 2838 | if (type->isIntegerTy()) |
| 2839 | weight = CW_Register; |
| 2840 | break; |
| 2841 | case 'f': |
| 2842 | if (type->isFloatTy()) |
| 2843 | weight = CW_Register; |
| 2844 | break; |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2845 | case 'c': // $25 for indirect jumps |
Eric Christopher | 4adbefe | 2012-05-07 06:25:15 +0000 | [diff] [blame] | 2846 | case 'l': // lo register |
Eric Christopher | af97f73 | 2012-05-07 06:25:19 +0000 | [diff] [blame] | 2847 | case 'x': // hilo register pair |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2848 | if (type->isIntegerTy()) |
| 2849 | weight = CW_SpecificReg; |
| 2850 | break; |
Eric Christopher | 50ab039 | 2012-05-07 03:13:32 +0000 | [diff] [blame] | 2851 | case 'I': // signed 16 bit immediate |
Eric Christopher | e5076d4 | 2012-05-07 03:13:42 +0000 | [diff] [blame] | 2852 | case 'J': // integer zero |
Eric Christopher | f49f846 | 2012-05-07 05:46:29 +0000 | [diff] [blame] | 2853 | case 'K': // unsigned 16 bit immediate |
Eric Christopher | 5ac47bb | 2012-05-07 05:46:37 +0000 | [diff] [blame] | 2854 | case 'L': // signed 32 bit immediate where lower 16 bits are 0 |
Eric Christopher | 60cfc79 | 2012-05-07 05:46:43 +0000 | [diff] [blame] | 2855 | case 'N': // immediate in the range of -65535 to -1 (inclusive) |
Eric Christopher | 1ce2034 | 2012-05-07 05:46:48 +0000 | [diff] [blame] | 2856 | case 'O': // signed 15 bit immediate (+- 16383) |
Eric Christopher | 54412a7 | 2012-05-07 06:25:02 +0000 | [diff] [blame] | 2857 | case 'P': // immediate in the range of 65535 to 1 (inclusive) |
Eric Christopher | 50ab039 | 2012-05-07 03:13:32 +0000 | [diff] [blame] | 2858 | if (isa<ConstantInt>(CallOperandVal)) |
| 2859 | weight = CW_Constant; |
| 2860 | break; |
Jack Carter | 0b9675d | 2013-03-04 21:33:15 +0000 | [diff] [blame] | 2861 | case 'R': |
| 2862 | weight = CW_Memory; |
| 2863 | break; |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 2864 | } |
| 2865 | return weight; |
| 2866 | } |
| 2867 | |
Akira Hatanaka | bfb07b1 | 2013-08-14 00:21:25 +0000 | [diff] [blame] | 2868 | /// This is a helper function to parse a physical register string and split it |
| 2869 | /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag |
| 2870 | /// that is returned indicates whether parsing was successful. The second flag |
| 2871 | /// is true if the numeric part exists. |
| 2872 | static std::pair<bool, bool> |
| 2873 | parsePhysicalReg(const StringRef &C, std::string &Prefix, |
| 2874 | unsigned long long &Reg) { |
| 2875 | if (C.front() != '{' || C.back() != '}') |
| 2876 | return std::make_pair(false, false); |
| 2877 | |
| 2878 | // Search for the first numeric character. |
| 2879 | StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1; |
| 2880 | I = std::find_if(B, E, std::ptr_fun(isdigit)); |
| 2881 | |
| 2882 | Prefix.assign(B, I - B); |
| 2883 | |
| 2884 | // The second flag is set to false if no numeric characters were found. |
| 2885 | if (I == E) |
| 2886 | return std::make_pair(true, false); |
| 2887 | |
| 2888 | // Parse the numeric characters. |
| 2889 | return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg), |
| 2890 | true); |
| 2891 | } |
| 2892 | |
| 2893 | std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering:: |
| 2894 | parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const { |
| 2895 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 2896 | const TargetRegisterClass *RC; |
| 2897 | std::string Prefix; |
| 2898 | unsigned long long Reg; |
| 2899 | |
| 2900 | std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg); |
| 2901 | |
| 2902 | if (!R.first) |
| 2903 | return std::make_pair((unsigned)0, (const TargetRegisterClass*)0); |
| 2904 | |
| 2905 | if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo. |
| 2906 | // No numeric characters follow "hi" or "lo". |
| 2907 | if (R.second) |
| 2908 | return std::make_pair((unsigned)0, (const TargetRegisterClass*)0); |
| 2909 | |
| 2910 | RC = TRI->getRegClass(Prefix == "hi" ? |
Akira Hatanaka | cbaf6d0 | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 2911 | Mips::HI32RegClassID : Mips::LO32RegClassID); |
Akira Hatanaka | bfb07b1 | 2013-08-14 00:21:25 +0000 | [diff] [blame] | 2912 | return std::make_pair(*(RC->begin()), RC); |
| 2913 | } |
| 2914 | |
| 2915 | if (!R.second) |
| 2916 | return std::make_pair((unsigned)0, (const TargetRegisterClass*)0); |
| 2917 | |
| 2918 | if (Prefix == "$f") { // Parse $f0-$f31. |
| 2919 | // If the size of FP registers is 64-bit or Reg is an even number, select |
| 2920 | // the 64-bit register class. Otherwise, select the 32-bit register class. |
| 2921 | if (VT == MVT::Other) |
| 2922 | VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32; |
| 2923 | |
| 2924 | RC= getRegClassFor(VT); |
| 2925 | |
| 2926 | if (RC == &Mips::AFGR64RegClass) { |
| 2927 | assert(Reg % 2 == 0); |
| 2928 | Reg >>= 1; |
| 2929 | } |
| 2930 | } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7. |
| 2931 | RC = TRI->getRegClass(Mips::FCCRegClassID); |
| 2932 | } else { // Parse $0-$31. |
| 2933 | assert(Prefix == "$"); |
| 2934 | RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT); |
| 2935 | } |
| 2936 | |
| 2937 | assert(Reg < RC->getNumRegs()); |
| 2938 | return std::make_pair(*(RC->begin() + Reg), RC); |
| 2939 | } |
| 2940 | |
Eric Christopher | 38d6426 | 2011-06-29 19:33:04 +0000 | [diff] [blame] | 2941 | /// Given a register class constraint, like 'r', if this corresponds directly |
| 2942 | /// to an LLVM register class, return a register of 0 and the register class |
| 2943 | /// pointer. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2944 | std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering:: |
Chad Rosier | 5b3fca5 | 2013-06-22 18:37:38 +0000 | [diff] [blame] | 2945 | getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2946 | { |
| 2947 | if (Constraint.size() == 1) { |
| 2948 | switch (Constraint[0]) { |
Eric Christopher | 314aff1 | 2011-06-29 19:04:31 +0000 | [diff] [blame] | 2949 | case 'd': // Address register. Same as 'r' unless generating MIPS16 code. |
| 2950 | case 'y': // Same as 'r'. Exists for compatibility. |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2951 | case 'r': |
Akira Hatanaka | afc945b | 2012-09-12 23:27:55 +0000 | [diff] [blame] | 2952 | if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) { |
| 2953 | if (Subtarget->inMips16Mode()) |
| 2954 | return std::make_pair(0U, &Mips::CPU16RegsRegClass); |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2955 | return std::make_pair(0U, &Mips::GPR32RegClass); |
Akira Hatanaka | afc945b | 2012-09-12 23:27:55 +0000 | [diff] [blame] | 2956 | } |
Jack Carter | 10de025 | 2012-07-02 23:35:23 +0000 | [diff] [blame] | 2957 | if (VT == MVT::i64 && !HasMips64) |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2958 | return std::make_pair(0U, &Mips::GPR32RegClass); |
Eric Christopher | 0ed1f76 | 2012-05-07 03:13:22 +0000 | [diff] [blame] | 2959 | if (VT == MVT::i64 && HasMips64) |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2960 | return std::make_pair(0U, &Mips::GPR64RegClass); |
Eric Christopher | 0ed1f76 | 2012-05-07 03:13:22 +0000 | [diff] [blame] | 2961 | // This will generate an error message |
| 2962 | return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0)); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 2963 | case 'f': |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2964 | if (VT == MVT::f32) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 2965 | return std::make_pair(0U, &Mips::FGR32RegClass); |
Akira Hatanaka | cb9dd72 | 2012-01-04 02:45:01 +0000 | [diff] [blame] | 2966 | if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) { |
| 2967 | if (Subtarget->isFP64bit()) |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 2968 | return std::make_pair(0U, &Mips::FGR64RegClass); |
| 2969 | return std::make_pair(0U, &Mips::AFGR64RegClass); |
Akira Hatanaka | cb9dd72 | 2012-01-04 02:45:01 +0000 | [diff] [blame] | 2970 | } |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2971 | break; |
| 2972 | case 'c': // register suitable for indirect jump |
| 2973 | if (VT == MVT::i32) |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2974 | return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass); |
Eric Christopher | 1d5a392 | 2012-05-07 06:25:10 +0000 | [diff] [blame] | 2975 | assert(VT == MVT::i64 && "Unexpected type."); |
Akira Hatanaka | 1858786 | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 2976 | return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass); |
Eric Christopher | 4adbefe | 2012-05-07 06:25:15 +0000 | [diff] [blame] | 2977 | case 'l': // register suitable for indirect jump |
| 2978 | if (VT == MVT::i32) |
Akira Hatanaka | cbaf6d0 | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 2979 | return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass); |
| 2980 | return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass); |
Eric Christopher | af97f73 | 2012-05-07 06:25:19 +0000 | [diff] [blame] | 2981 | case 'x': // register suitable for indirect jump |
| 2982 | // Fixme: Not triggering the use of both hi and low |
| 2983 | // This will generate an error message |
| 2984 | return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0)); |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2985 | } |
| 2986 | } |
Akira Hatanaka | bfb07b1 | 2013-08-14 00:21:25 +0000 | [diff] [blame] | 2987 | |
| 2988 | std::pair<unsigned, const TargetRegisterClass *> R; |
| 2989 | R = parseRegForInlineAsmConstraint(Constraint, VT); |
| 2990 | |
| 2991 | if (R.second) |
| 2992 | return R; |
| 2993 | |
Bruno Cardoso Lopes | 84f47c5 | 2007-08-21 16:09:25 +0000 | [diff] [blame] | 2994 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 2995 | } |
| 2996 | |
Eric Christopher | 50ab039 | 2012-05-07 03:13:32 +0000 | [diff] [blame] | 2997 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 2998 | /// vector. If it is invalid, don't add anything to Ops. |
| 2999 | void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
| 3000 | std::string &Constraint, |
| 3001 | std::vector<SDValue>&Ops, |
| 3002 | SelectionDAG &DAG) const { |
| 3003 | SDValue Result(0, 0); |
| 3004 | |
| 3005 | // Only support length 1 constraints for now. |
| 3006 | if (Constraint.length() > 1) return; |
| 3007 | |
| 3008 | char ConstraintLetter = Constraint[0]; |
| 3009 | switch (ConstraintLetter) { |
| 3010 | default: break; // This will fall through to the generic implementation |
| 3011 | case 'I': // Signed 16 bit constant |
| 3012 | // If this fails, the parent routine will give an error |
| 3013 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3014 | EVT Type = Op.getValueType(); |
| 3015 | int64_t Val = C->getSExtValue(); |
| 3016 | if (isInt<16>(Val)) { |
| 3017 | Result = DAG.getTargetConstant(Val, Type); |
| 3018 | break; |
| 3019 | } |
| 3020 | } |
| 3021 | return; |
Eric Christopher | e5076d4 | 2012-05-07 03:13:42 +0000 | [diff] [blame] | 3022 | case 'J': // integer zero |
| 3023 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3024 | EVT Type = Op.getValueType(); |
| 3025 | int64_t Val = C->getZExtValue(); |
| 3026 | if (Val == 0) { |
| 3027 | Result = DAG.getTargetConstant(0, Type); |
| 3028 | break; |
| 3029 | } |
| 3030 | } |
| 3031 | return; |
Eric Christopher | f49f846 | 2012-05-07 05:46:29 +0000 | [diff] [blame] | 3032 | case 'K': // unsigned 16 bit immediate |
| 3033 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3034 | EVT Type = Op.getValueType(); |
| 3035 | uint64_t Val = (uint64_t)C->getZExtValue(); |
| 3036 | if (isUInt<16>(Val)) { |
| 3037 | Result = DAG.getTargetConstant(Val, Type); |
| 3038 | break; |
| 3039 | } |
| 3040 | } |
| 3041 | return; |
Eric Christopher | 5ac47bb | 2012-05-07 05:46:37 +0000 | [diff] [blame] | 3042 | case 'L': // signed 32 bit immediate where lower 16 bits are 0 |
| 3043 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3044 | EVT Type = Op.getValueType(); |
| 3045 | int64_t Val = C->getSExtValue(); |
| 3046 | if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){ |
| 3047 | Result = DAG.getTargetConstant(Val, Type); |
| 3048 | break; |
| 3049 | } |
| 3050 | } |
| 3051 | return; |
Eric Christopher | 60cfc79 | 2012-05-07 05:46:43 +0000 | [diff] [blame] | 3052 | case 'N': // immediate in the range of -65535 to -1 (inclusive) |
| 3053 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3054 | EVT Type = Op.getValueType(); |
| 3055 | int64_t Val = C->getSExtValue(); |
| 3056 | if ((Val >= -65535) && (Val <= -1)) { |
| 3057 | Result = DAG.getTargetConstant(Val, Type); |
| 3058 | break; |
| 3059 | } |
| 3060 | } |
| 3061 | return; |
Eric Christopher | 1ce2034 | 2012-05-07 05:46:48 +0000 | [diff] [blame] | 3062 | case 'O': // signed 15 bit immediate |
| 3063 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3064 | EVT Type = Op.getValueType(); |
| 3065 | int64_t Val = C->getSExtValue(); |
| 3066 | if ((isInt<15>(Val))) { |
| 3067 | Result = DAG.getTargetConstant(Val, Type); |
| 3068 | break; |
| 3069 | } |
| 3070 | } |
| 3071 | return; |
Eric Christopher | 54412a7 | 2012-05-07 06:25:02 +0000 | [diff] [blame] | 3072 | case 'P': // immediate in the range of 1 to 65535 (inclusive) |
| 3073 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 3074 | EVT Type = Op.getValueType(); |
| 3075 | int64_t Val = C->getSExtValue(); |
| 3076 | if ((Val <= 65535) && (Val >= 1)) { |
| 3077 | Result = DAG.getTargetConstant(Val, Type); |
| 3078 | break; |
| 3079 | } |
| 3080 | } |
| 3081 | return; |
Eric Christopher | 50ab039 | 2012-05-07 03:13:32 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | if (Result.getNode()) { |
| 3085 | Ops.push_back(Result); |
| 3086 | return; |
| 3087 | } |
| 3088 | |
| 3089 | TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
| 3090 | } |
| 3091 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 3092 | bool |
Akira Hatanaka | 94e4728 | 2012-11-17 00:25:41 +0000 | [diff] [blame] | 3093 | MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const { |
| 3094 | // No global is ever allowed as a base. |
| 3095 | if (AM.BaseGV) |
| 3096 | return false; |
| 3097 | |
| 3098 | switch (AM.Scale) { |
| 3099 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 3100 | break; |
| 3101 | case 1: |
| 3102 | if (!AM.HasBaseReg) // allow "r+i". |
| 3103 | break; |
| 3104 | return false; // disallow "r+r" or "r+r+i". |
| 3105 | default: |
| 3106 | return false; |
| 3107 | } |
| 3108 | |
| 3109 | return true; |
| 3110 | } |
| 3111 | |
| 3112 | bool |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 3113 | MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 3114 | // The Mips target isn't yet aware of offsets. |
| 3115 | return false; |
| 3116 | } |
Evan Cheng | eb2f969 | 2009-10-27 19:56:55 +0000 | [diff] [blame] | 3117 | |
Akira Hatanaka | e193b32 | 2012-06-13 19:33:32 +0000 | [diff] [blame] | 3118 | EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3119 | unsigned SrcAlign, |
| 3120 | bool IsMemset, bool ZeroMemset, |
Akira Hatanaka | e193b32 | 2012-06-13 19:33:32 +0000 | [diff] [blame] | 3121 | bool MemcpyStrSrc, |
| 3122 | MachineFunction &MF) const { |
| 3123 | if (Subtarget->hasMips64()) |
| 3124 | return MVT::i64; |
| 3125 | |
| 3126 | return MVT::i32; |
| 3127 | } |
| 3128 | |
Evan Cheng | a1eaa3c | 2009-10-28 01:43:28 +0000 | [diff] [blame] | 3129 | bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 3130 | if (VT != MVT::f32 && VT != MVT::f64) |
| 3131 | return false; |
Bruno Cardoso Lopes | 6b90282 | 2011-01-18 19:41:41 +0000 | [diff] [blame] | 3132 | if (Imm.isNegZero()) |
| 3133 | return false; |
Evan Cheng | eb2f969 | 2009-10-27 19:56:55 +0000 | [diff] [blame] | 3134 | return Imm.isZero(); |
| 3135 | } |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 3136 | |
| 3137 | unsigned MipsTargetLowering::getJumpTableEncoding() const { |
| 3138 | if (IsN64) |
| 3139 | return MachineJumpTableInfo::EK_GPRel64BlockAddress; |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 3140 | |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 3141 | return TargetLowering::getJumpTableEncoding(); |
| 3142 | } |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3143 | |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3144 | /// This function returns true if CallSym is a long double emulation routine. |
| 3145 | static bool isF128SoftLibCall(const char *CallSym) { |
| 3146 | const char *const LibCalls[] = |
| 3147 | {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2", |
| 3148 | "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi", |
| 3149 | "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf", |
| 3150 | "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2", |
| 3151 | "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3", |
| 3152 | "__trunctfdf2", "__trunctfsf2", "__unordtf2", |
| 3153 | "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl", |
| 3154 | "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl", |
| 3155 | "truncl"}; |
| 3156 | |
| 3157 | const char * const *End = LibCalls + array_lengthof(LibCalls); |
| 3158 | |
| 3159 | // Check that LibCalls is sorted alphabetically. |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 3160 | MipsTargetLowering::LTStr Comp; |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3161 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 3162 | #ifndef NDEBUG |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3163 | for (const char * const *I = LibCalls; I < End - 1; ++I) |
| 3164 | assert(Comp(*I, *(I + 1))); |
| 3165 | #endif |
| 3166 | |
Akira Hatanaka | 5ac065a | 2013-03-13 00:54:29 +0000 | [diff] [blame] | 3167 | return std::binary_search(LibCalls, End, CallSym, Comp); |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | /// This function returns true if Ty is fp128 or i128 which was originally a |
| 3171 | /// fp128. |
| 3172 | static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) { |
| 3173 | if (Ty->isFP128Ty()) |
| 3174 | return true; |
| 3175 | |
| 3176 | const ExternalSymbolSDNode *ES = |
| 3177 | dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode); |
| 3178 | |
| 3179 | // If the Ty is i128 and the function being called is a long double emulation |
| 3180 | // routine, then the original type is f128. |
| 3181 | return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol())); |
| 3182 | } |
| 3183 | |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 3184 | MipsTargetLowering::MipsCC::SpecialCallingConvType |
| 3185 | MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const { |
| 3186 | MipsCC::SpecialCallingConvType SpecialCallingConv = |
| 3187 | MipsCC::NoSpecialCallingConv;; |
| 3188 | if (Subtarget->inMips16HardFloat()) { |
| 3189 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 3190 | llvm::StringRef Sym = G->getGlobal()->getName(); |
| 3191 | Function *F = G->getGlobal()->getParent()->getFunction(Sym); |
| 3192 | if (F->hasFnAttribute("__Mips16RetHelper")) { |
| 3193 | SpecialCallingConv = MipsCC::Mips16RetHelperConv; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | return SpecialCallingConv; |
| 3198 | } |
| 3199 | |
| 3200 | MipsTargetLowering::MipsCC::MipsCC( |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 3201 | CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info, |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 3202 | MipsCC::SpecialCallingConvType SpecialCallingConv_) |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 3203 | : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_), |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 3204 | SpecialCallingConv(SpecialCallingConv_){ |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3205 | // Pre-allocate reserved argument area. |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3206 | CCInfo.AllocateStack(reservedArgArea(), 1); |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 3209 | |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3210 | void MipsTargetLowering::MipsCC:: |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3211 | analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args, |
Akira Hatanaka | cb2eafd | 2013-03-05 22:20:28 +0000 | [diff] [blame] | 3212 | bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode, |
| 3213 | std::vector<ArgListEntry> &FuncArgs) { |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3214 | assert((CallConv != CallingConv::Fast || !IsVarArg) && |
| 3215 | "CallingConv::Fast shouldn't be used for vararg functions."); |
| 3216 | |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3217 | unsigned NumOpnds = Args.size(); |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3218 | llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn(); |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3219 | |
| 3220 | for (unsigned I = 0; I != NumOpnds; ++I) { |
| 3221 | MVT ArgVT = Args[I].VT; |
| 3222 | ISD::ArgFlagsTy ArgFlags = Args[I].Flags; |
| 3223 | bool R; |
| 3224 | |
| 3225 | if (ArgFlags.isByVal()) { |
| 3226 | handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags); |
| 3227 | continue; |
| 3228 | } |
| 3229 | |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3230 | if (IsVarArg && !Args[I].IsFixed) |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3231 | R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo); |
Akira Hatanaka | cb2eafd | 2013-03-05 22:20:28 +0000 | [diff] [blame] | 3232 | else { |
| 3233 | MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode, |
| 3234 | IsSoftFloat); |
| 3235 | R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo); |
| 3236 | } |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3237 | |
| 3238 | if (R) { |
| 3239 | #ifndef NDEBUG |
| 3240 | dbgs() << "Call operand #" << I << " has unhandled type " |
| 3241 | << EVT(ArgVT).getEVTString(); |
| 3242 | #endif |
| 3243 | llvm_unreachable(0); |
| 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | void MipsTargetLowering::MipsCC:: |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3249 | analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args, |
| 3250 | bool IsSoftFloat, Function::const_arg_iterator FuncArg) { |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3251 | unsigned NumArgs = Args.size(); |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3252 | llvm::CCAssignFn *FixedFn = fixedArgFn(); |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3253 | unsigned CurArgIdx = 0; |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3254 | |
| 3255 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3256 | MVT ArgVT = Args[I].VT; |
| 3257 | ISD::ArgFlagsTy ArgFlags = Args[I].Flags; |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3258 | std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx); |
| 3259 | CurArgIdx = Args[I].OrigArgIndex; |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3260 | |
| 3261 | if (ArgFlags.isByVal()) { |
| 3262 | handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags); |
| 3263 | continue; |
| 3264 | } |
| 3265 | |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3266 | MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat); |
| 3267 | |
| 3268 | if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo)) |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3269 | continue; |
| 3270 | |
| 3271 | #ifndef NDEBUG |
| 3272 | dbgs() << "Formal Arg #" << I << " has unhandled type " |
| 3273 | << EVT(ArgVT).getEVTString(); |
| 3274 | #endif |
| 3275 | llvm_unreachable(0); |
| 3276 | } |
| 3277 | } |
| 3278 | |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 3279 | template<typename Ty> |
| 3280 | void MipsTargetLowering::MipsCC:: |
| 3281 | analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat, |
| 3282 | const SDNode *CallNode, const Type *RetTy) const { |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3283 | CCAssignFn *Fn; |
| 3284 | |
| 3285 | if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode)) |
| 3286 | Fn = RetCC_F128Soft; |
| 3287 | else |
| 3288 | Fn = RetCC_Mips; |
| 3289 | |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 3290 | for (unsigned I = 0, E = RetVals.size(); I < E; ++I) { |
| 3291 | MVT VT = RetVals[I].VT; |
| 3292 | ISD::ArgFlagsTy Flags = RetVals[I].Flags; |
| 3293 | MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat); |
| 3294 | |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3295 | if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) { |
Akira Hatanaka | 7433b2e | 2013-03-05 22:41:55 +0000 | [diff] [blame] | 3296 | #ifndef NDEBUG |
| 3297 | dbgs() << "Call result #" << I << " has unhandled type " |
| 3298 | << EVT(VT).getEVTString() << '\n'; |
| 3299 | #endif |
| 3300 | llvm_unreachable(0); |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | void MipsTargetLowering::MipsCC:: |
| 3306 | analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat, |
| 3307 | const SDNode *CallNode, const Type *RetTy) const { |
| 3308 | analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy); |
| 3309 | } |
| 3310 | |
| 3311 | void MipsTargetLowering::MipsCC:: |
| 3312 | analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat, |
| 3313 | const Type *RetTy) const { |
| 3314 | analyzeReturn(Outs, IsSoftFloat, 0, RetTy); |
| 3315 | } |
| 3316 | |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3317 | void |
| 3318 | MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT, |
| 3319 | MVT LocVT, |
| 3320 | CCValAssign::LocInfo LocInfo, |
| 3321 | ISD::ArgFlagsTy ArgFlags) { |
| 3322 | assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0."); |
| 3323 | |
| 3324 | struct ByValArgInfo ByVal; |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3325 | unsigned RegSize = regSize(); |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3326 | unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize); |
| 3327 | unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize), |
| 3328 | RegSize * 2); |
| 3329 | |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3330 | if (useRegsForByval()) |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3331 | allocateRegs(ByVal, ByValSize, Align); |
| 3332 | |
| 3333 | // Allocate space on caller's stack. |
| 3334 | ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs, |
| 3335 | Align); |
| 3336 | CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT, |
| 3337 | LocInfo)); |
| 3338 | ByValArgs.push_back(ByVal); |
| 3339 | } |
| 3340 | |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3341 | unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const { |
| 3342 | return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs); |
| 3343 | } |
| 3344 | |
| 3345 | unsigned MipsTargetLowering::MipsCC::reservedArgArea() const { |
| 3346 | return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0; |
| 3347 | } |
| 3348 | |
| 3349 | const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const { |
| 3350 | return IsO32 ? O32IntRegs : Mips64IntRegs; |
| 3351 | } |
| 3352 | |
| 3353 | llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const { |
| 3354 | if (CallConv == CallingConv::Fast) |
| 3355 | return CC_Mips_FastCC; |
| 3356 | |
Reed Kotler | 4609091 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 3357 | if (SpecialCallingConv == Mips16RetHelperConv) |
| 3358 | return CC_Mips16RetHelper; |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 3359 | return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN; |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const { |
Akira Hatanaka | ad341d4 | 2013-08-20 23:38:40 +0000 | [diff] [blame] | 3363 | return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg; |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const { |
| 3367 | return IsO32 ? O32IntRegs : Mips64DPRegs; |
| 3368 | } |
| 3369 | |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3370 | void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal, |
| 3371 | unsigned ByValSize, |
| 3372 | unsigned Align) { |
Akira Hatanaka | ffd28a4 | 2013-02-15 21:45:11 +0000 | [diff] [blame] | 3373 | unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs(); |
| 3374 | const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs(); |
Akira Hatanaka | 7887c90 | 2012-10-26 23:56:38 +0000 | [diff] [blame] | 3375 | assert(!(ByValSize % RegSize) && !(Align % RegSize) && |
| 3376 | "Byval argument's size and alignment should be a multiple of" |
| 3377 | "RegSize."); |
| 3378 | |
| 3379 | ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs); |
| 3380 | |
| 3381 | // If Align > RegSize, the first arg register must be even. |
| 3382 | if ((Align > RegSize) && (ByVal.FirstIdx % 2)) { |
| 3383 | CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]); |
| 3384 | ++ByVal.FirstIdx; |
| 3385 | } |
| 3386 | |
| 3387 | // Mark the registers allocated. |
| 3388 | for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs); |
| 3389 | ByValSize -= RegSize, ++I, ++ByVal.NumRegs) |
| 3390 | CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]); |
| 3391 | } |
Akira Hatanaka | eb98ae4 | 2012-10-27 00:10:18 +0000 | [diff] [blame] | 3392 | |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3393 | MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy, |
| 3394 | const SDNode *CallNode, |
| 3395 | bool IsSoftFloat) const { |
| 3396 | if (IsSoftFloat || IsO32) |
| 3397 | return VT; |
| 3398 | |
| 3399 | // Check if the original type was fp128. |
Akira Hatanaka | 1e3e869 | 2013-03-05 22:54:59 +0000 | [diff] [blame] | 3400 | if (originalTypeIsF128(OrigTy, CallNode)) { |
Akira Hatanaka | 5fdee6d | 2013-03-05 22:13:04 +0000 | [diff] [blame] | 3401 | assert(VT == MVT::i64); |
| 3402 | return MVT::f64; |
| 3403 | } |
| 3404 | |
| 3405 | return VT; |
| 3406 | } |
| 3407 | |
Akira Hatanaka | eb98ae4 | 2012-10-27 00:10:18 +0000 | [diff] [blame] | 3408 | void MipsTargetLowering:: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3409 | copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, |
Akira Hatanaka | eb98ae4 | 2012-10-27 00:10:18 +0000 | [diff] [blame] | 3410 | SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags, |
| 3411 | SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg, |
| 3412 | const MipsCC &CC, const ByValArgInfo &ByVal) const { |
| 3413 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3414 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 3415 | unsigned RegAreaSize = ByVal.NumRegs * CC.regSize(); |
| 3416 | unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize); |
| 3417 | int FrameObjOffset; |
| 3418 | |
| 3419 | if (RegAreaSize) |
| 3420 | FrameObjOffset = (int)CC.reservedArgArea() - |
| 3421 | (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize()); |
| 3422 | else |
| 3423 | FrameObjOffset = ByVal.Address; |
| 3424 | |
| 3425 | // Create frame object. |
| 3426 | EVT PtrTy = getPointerTy(); |
| 3427 | int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true); |
| 3428 | SDValue FIN = DAG.getFrameIndex(FI, PtrTy); |
| 3429 | InVals.push_back(FIN); |
| 3430 | |
| 3431 | if (!ByVal.NumRegs) |
| 3432 | return; |
| 3433 | |
| 3434 | // Copy arg registers. |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 3435 | MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8); |
Akira Hatanaka | eb98ae4 | 2012-10-27 00:10:18 +0000 | [diff] [blame] | 3436 | const TargetRegisterClass *RC = getRegClassFor(RegTy); |
| 3437 | |
| 3438 | for (unsigned I = 0; I < ByVal.NumRegs; ++I) { |
| 3439 | unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I]; |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 3440 | unsigned VReg = addLiveIn(MF, ArgReg, RC); |
Akira Hatanaka | eb98ae4 | 2012-10-27 00:10:18 +0000 | [diff] [blame] | 3441 | unsigned Offset = I * CC.regSize(); |
| 3442 | SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN, |
| 3443 | DAG.getConstant(Offset, PtrTy)); |
| 3444 | SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy), |
| 3445 | StorePtr, MachinePointerInfo(FuncArg, Offset), |
| 3446 | false, false, 0); |
| 3447 | OutChains.push_back(Store); |
| 3448 | } |
| 3449 | } |
Akira Hatanaka | db40ede | 2012-10-27 00:16:36 +0000 | [diff] [blame] | 3450 | |
| 3451 | // Copy byVal arg to registers and stack. |
| 3452 | void MipsTargetLowering:: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3453 | passByValArg(SDValue Chain, SDLoc DL, |
Akira Hatanaka | bf6a77b | 2013-01-22 20:05:56 +0000 | [diff] [blame] | 3454 | std::deque< std::pair<unsigned, SDValue> > &RegsToPass, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3455 | SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr, |
Akira Hatanaka | db40ede | 2012-10-27 00:16:36 +0000 | [diff] [blame] | 3456 | MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, |
| 3457 | const MipsCC &CC, const ByValArgInfo &ByVal, |
| 3458 | const ISD::ArgFlagsTy &Flags, bool isLittle) const { |
| 3459 | unsigned ByValSize = Flags.getByValSize(); |
| 3460 | unsigned Offset = 0; // Offset in # of bytes from the beginning of struct. |
| 3461 | unsigned RegSize = CC.regSize(); |
| 3462 | unsigned Alignment = std::min(Flags.getByValAlign(), RegSize); |
| 3463 | EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8); |
| 3464 | |
| 3465 | if (ByVal.NumRegs) { |
| 3466 | const uint16_t *ArgRegs = CC.intArgRegs(); |
| 3467 | bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize); |
| 3468 | unsigned I = 0; |
| 3469 | |
| 3470 | // Copy words to registers. |
| 3471 | for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) { |
| 3472 | SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, |
| 3473 | DAG.getConstant(Offset, PtrTy)); |
| 3474 | SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr, |
| 3475 | MachinePointerInfo(), false, false, false, |
| 3476 | Alignment); |
| 3477 | MemOpChains.push_back(LoadVal.getValue(1)); |
| 3478 | unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I]; |
| 3479 | RegsToPass.push_back(std::make_pair(ArgReg, LoadVal)); |
| 3480 | } |
| 3481 | |
| 3482 | // Return if the struct has been fully copied. |
| 3483 | if (ByValSize == Offset) |
| 3484 | return; |
| 3485 | |
| 3486 | // Copy the remainder of the byval argument with sub-word loads and shifts. |
| 3487 | if (LeftoverBytes) { |
| 3488 | assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) && |
| 3489 | "Size of the remainder should be smaller than RegSize."); |
| 3490 | SDValue Val; |
| 3491 | |
| 3492 | for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0; |
| 3493 | Offset < ByValSize; LoadSize /= 2) { |
| 3494 | unsigned RemSize = ByValSize - Offset; |
| 3495 | |
| 3496 | if (RemSize < LoadSize) |
| 3497 | continue; |
| 3498 | |
| 3499 | // Load subword. |
| 3500 | SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, |
| 3501 | DAG.getConstant(Offset, PtrTy)); |
| 3502 | SDValue LoadVal = |
| 3503 | DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, |
| 3504 | MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8), |
| 3505 | false, false, Alignment); |
| 3506 | MemOpChains.push_back(LoadVal.getValue(1)); |
| 3507 | |
| 3508 | // Shift the loaded value. |
| 3509 | unsigned Shamt; |
| 3510 | |
| 3511 | if (isLittle) |
| 3512 | Shamt = TotalSizeLoaded; |
| 3513 | else |
| 3514 | Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8; |
| 3515 | |
| 3516 | SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal, |
| 3517 | DAG.getConstant(Shamt, MVT::i32)); |
| 3518 | |
| 3519 | if (Val.getNode()) |
| 3520 | Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift); |
| 3521 | else |
| 3522 | Val = Shift; |
| 3523 | |
| 3524 | Offset += LoadSize; |
| 3525 | TotalSizeLoaded += LoadSize; |
| 3526 | Alignment = std::min(Alignment, LoadSize); |
| 3527 | } |
| 3528 | |
| 3529 | unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I]; |
| 3530 | RegsToPass.push_back(std::make_pair(ArgReg, Val)); |
| 3531 | return; |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | // Copy remainder of byval arg to it with memcpy. |
| 3536 | unsigned MemCpySize = ByValSize - Offset; |
| 3537 | SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg, |
| 3538 | DAG.getConstant(Offset, PtrTy)); |
| 3539 | SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr, |
| 3540 | DAG.getIntPtrConstant(ByVal.Address)); |
| 3541 | Chain = DAG.getMemcpy(Chain, DL, Dst, Src, |
| 3542 | DAG.getConstant(MemCpySize, PtrTy), Alignment, |
| 3543 | /*isVolatile=*/false, /*AlwaysInline=*/false, |
| 3544 | MachinePointerInfo(0), MachinePointerInfo(0)); |
| 3545 | MemOpChains.push_back(Chain); |
| 3546 | } |
Akira Hatanaka | f084847 | 2012-10-27 00:21:13 +0000 | [diff] [blame] | 3547 | |
| 3548 | void |
| 3549 | MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains, |
| 3550 | const MipsCC &CC, SDValue Chain, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3551 | SDLoc DL, SelectionDAG &DAG) const { |
Akira Hatanaka | f084847 | 2012-10-27 00:21:13 +0000 | [diff] [blame] | 3552 | unsigned NumRegs = CC.numIntArgRegs(); |
| 3553 | const uint16_t *ArgRegs = CC.intArgRegs(); |
| 3554 | const CCState &CCInfo = CC.getCCInfo(); |
| 3555 | unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs); |
| 3556 | unsigned RegSize = CC.regSize(); |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 3557 | MVT RegTy = MVT::getIntegerVT(RegSize * 8); |
Akira Hatanaka | f084847 | 2012-10-27 00:21:13 +0000 | [diff] [blame] | 3558 | const TargetRegisterClass *RC = getRegClassFor(RegTy); |
| 3559 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3560 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 3561 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 3562 | |
| 3563 | // Offset of the first variable argument from stack pointer. |
| 3564 | int VaArgOffset; |
| 3565 | |
| 3566 | if (NumRegs == Idx) |
| 3567 | VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize); |
| 3568 | else |
| 3569 | VaArgOffset = |
| 3570 | (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx)); |
| 3571 | |
| 3572 | // Record the frame index of the first variable argument |
| 3573 | // which is a value necessary to VASTART. |
| 3574 | int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true); |
| 3575 | MipsFI->setVarArgsFrameIndex(FI); |
| 3576 | |
| 3577 | // Copy the integer registers that have not been used for argument passing |
| 3578 | // to the argument register save area. For O32, the save area is allocated |
| 3579 | // in the caller's stack frame, while for N32/64, it is allocated in the |
| 3580 | // callee's stack frame. |
| 3581 | for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) { |
Akira Hatanaka | f635ef4 | 2013-03-12 00:16:36 +0000 | [diff] [blame] | 3582 | unsigned Reg = addLiveIn(MF, ArgRegs[I], RC); |
Akira Hatanaka | f084847 | 2012-10-27 00:21:13 +0000 | [diff] [blame] | 3583 | SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy); |
| 3584 | FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true); |
| 3585 | SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy()); |
| 3586 | SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff, |
| 3587 | MachinePointerInfo(), false, false, 0); |
| 3588 | cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0); |
| 3589 | OutChains.push_back(Store); |
| 3590 | } |
| 3591 | } |