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