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