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